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);
11 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, 0666);
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;