1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
3 #include <msvcrt/sys/types.h>
4 #include <msvcrt/stdio.h>
6 #include <msvcrt/fcntl.h>
7 //#include <msvcrt/internal/file.h>
9 //might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
12 extern unsigned int _fmode
;
14 FILE * __alloc_file(void);
17 FILE* fopen(const char *file
, const char *mode
)
20 int fd
, rw
, oflags
= 0;
32 rw
= (mode
[1] == '+') || (mode
[1] && (mode
[2] == '+'));
37 oflags
= O_CREAT
| (rw
? O_RDWR
: O_WRONLY
);
40 oflags
= rw
? O_RDWR
: O_RDONLY
;
43 oflags
= O_TRUNC
| O_CREAT
| (rw
? O_RDWR
: O_WRONLY
);
54 else if (tbchar
== 'b')
57 oflags
|= (_fmode
& (O_TEXT
|O_BINARY
));
59 fd
= _open(file
, oflags
, 0);
63 // ms crtdll ensures that writes will end up at the end of file in append mode
64 // we just move the file pointer to the end of file initially
66 lseek(fd
, 0, SEEK_END
);
72 f
->_flag
= _IOREAD
| _IOWRT
;
73 else if (*mode
== 'r')
78 f
->_base
= f
->_ptr
= NULL
;
82 FILE* _wfopen(const wchar_t *file
, const wchar_t *mode
)
85 int fd
, rw
, oflags
= 0;
97 rw
= (mode
[1] == L
'+') || (mode
[1] && (mode
[2] == L
'+'));
102 oflags
= O_CREAT
| (rw
? O_RDWR
: O_WRONLY
);
105 oflags
= rw
? O_RDWR
: O_RDONLY
;
108 oflags
= O_TRUNC
| O_CREAT
| (rw
? O_RDWR
: O_WRONLY
);
119 else if (tbchar
== L
'b')
122 oflags
|= (_fmode
& (O_TEXT
|O_BINARY
));
124 fd
= _wopen(file
, oflags
, 0);
128 // ms crtdll ensures that writes will end up at the end of file in append mode
129 // we just move the file pointer to the end of file initially
131 lseek(fd
, 0, SEEK_END
);
137 f
->_flag
= _IOREAD
| _IOWRT
;
138 else if (*mode
== L
'r')
143 f
->_base
= f
->_ptr
= NULL
;