1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
3 #include <msvcrt/sys/types.h>
4 #include <msvcrt/stdio.h>
5 #include <msvcrt/fcntl.h>
7 #include <msvcrt/internal/file.h>
13 FILE *freopen(const char *file, const char *mode, FILE *f)
18 if (file == 0 || mode == 0 || f == 0)
21 rw = (mode[1] == '+');
27 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
30 oflags = rw ? O_RDWR : O_RDONLY;
33 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
44 else if (tbchar == 'b')
47 oflags |= (_fmode & (O_TEXT|O_BINARY));
49 fd = _open(file, oflags, 0666);
54 lseek(fd, 0, SEEK_END);
60 f->_flag = _IOREAD | _IOWRT;
61 else if (*mode == 'r')
66 f->_base = f->_ptr = NULL;
73 FILE *_wfreopen(const wchar_t *file, const wchar_t *mode, FILE *f)
78 if (file == 0 || mode == 0 || f == 0)
81 rw = (mode[1] == L'+');
87 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
90 oflags = rw ? O_RDWR : O_RDONLY;
93 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
104 else if (tbchar == L'b')
107 oflags |= (_fmode & (O_TEXT|O_BINARY));
109 fd = _wopen(file, oflags, 0666);
114 lseek(fd, 0, SEEK_END);
120 f->_flag = _IOREAD | _IOWRT;
121 else if (*mode == L'r')
126 f->_base = f->_ptr = NULL;