3 * ReactOS msvcrt library
7 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
9 * Based on original work Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
10 * 28/12/1998: Appropriated for Reactos
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
28 #include <msvcrt/sys/types.h>
29 #include <msvcrt/stdio.h>
30 #include <msvcrt/string.h>
31 #include <msvcrt/io.h>
32 #include <msvcrt/fcntl.h>
33 //#include <msvcrt/internal/file.h>
35 //might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
38 extern unsigned int _fmode;
40 FILE* __alloc_file(void);
45 FILE* fopen(const char *file, const char *mode)
48 int fd, rw, oflags = 0;
59 rw = (strchr(mode, '+') == NULL) ? 0 : 1;
60 if (strchr(mode, 'a'))
61 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
62 if (strchr(mode, 'r'))
63 oflags = rw ? O_RDWR : O_RDONLY;
64 if (strchr(mode, 'w'))
65 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
66 if (strchr(mode, 't'))
68 else if (strchr(mode, 'b'))
71 oflags |= (_fmode & (O_TEXT|O_BINARY));
73 fd = _open(file, oflags, 0);
77 // msvcrt ensures that writes will end up at the end of file in append mode
78 // we just move the file pointer to the end of file initially
80 if (strchr(mode, 'a'))
81 lseek(fd, 0, SEEK_END);
87 f->_flag = _IOREAD | _IOWRT;
88 else if (strchr(mode, 'r'))
93 if (strchr(mode, 't'))
95 else if (strchr(mode, 'b'))
96 f->_flag |= _IOBINARY;
97 else if (_fmode & O_BINARY)
98 f->_flag |= _IOBINARY;
100 f->_base = f->_ptr = NULL;
104 FILE* _wfopen(const wchar_t *file, const wchar_t *mode)
107 int fd, rw, oflags = 0;
118 rw = (wcschr(mode, L'+') == NULL) ? 0 : 1;
119 if (wcschr(mode, L'a'))
120 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
121 if (wcschr(mode, L'r'))
122 oflags = rw ? O_RDWR : O_RDONLY;
123 if (wcschr(mode, L'w'))
124 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
125 if (wcschr(mode, L't'))
127 else if (wcschr(mode, L'b'))
130 oflags |= (_fmode & (O_TEXT|O_BINARY));
132 fd = _wopen(file, oflags, 0);
136 // msvcrt ensures that writes will end up at the end of file in append mode
137 // we just move the file pointer to the end of file initially
138 if (wcschr(mode, 'a'))
139 lseek(fd, 0, SEEK_END);
145 f->_flag = _IOREAD | _IOWRT;
146 else if (wcschr(mode, L'r'))
151 if (wcschr(mode, L't'))
153 else if (wcschr(mode, L'b'))
154 f->_flag |= _IOBINARY;
155 else if (_fmode & O_BINARY)
156 f->_flag |= _IOBINARY;
158 f->_base = f->_ptr = NULL;