]> git.sur5r.net Git - cc65/blob - libsrc/common/freopen.c
no TGI_ERR_NO_MEM or TGI_ERR_NO_IOCB anymore: replaced by TGI_ERR_NO_RES
[cc65] / libsrc / common / freopen.c
1 /*
2  * freopen.c
3  *
4  * Ullrich von Bassewitz, 17.06.1998
5  */
6
7
8
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include "_file.h"
13
14
15
16 /*****************************************************************************/
17 /*                                   Code                                    */
18 /*****************************************************************************/
19
20
21
22 FILE* __fastcall__ freopen (const char* name, const char* mode, FILE* f)
23 {
24     /* Check if the file is open, if so, close it */
25     if ((f->f_flags & _FOPEN) == 0) {
26         /* File is not open */
27         _errno = EINVAL;                /* File not input */
28         return 0;
29     }
30
31     /* Close the file. Don't bother setting the flag, it will get
32      * overwritten by _fopen.
33      */
34     if (close (f->f_fd) < 0) {
35         /* An error occured, _oserror is set */
36         return 0;
37     }
38
39     /* Open the file and return the descriptor */
40     return _fopen (name, mode, f);
41 }
42
43
44