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);
14 FILE* fopen(const char *file, const char *mode)
17 int fd, rw, oflags = 0;
29 rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
34 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
37 oflags = rw ? O_RDWR : O_RDONLY;
40 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
51 else if (tbchar == 'b')
54 oflags |= (_fmode & (O_TEXT|O_BINARY));
56 fd = _open(file, oflags, 0666);
60 // ms crtdll ensures that writes will end up at the end of file in append mode
61 // we just move the file pointer to the end of file initially
63 lseek(fd, 0, SEEK_END);
69 f->_flag = _IOREAD | _IOWRT;
70 else if (*mode == 'r')
75 f->_base = f->_ptr = NULL;