update for HEAD-2003021201
[reactos.git] / lib / msvcrt / stdio / fopen.c
1 /* $Id$
2  *
3  *  ReactOS msvcrt library
4  *
5  *  fopen.c
6  *
7  *  Copyright (C) 2002  Robert Dickenson <robd@reactos.org>
8  *
9  *  Based on original work Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
10  *                         28/12/1998: Appropriated for Reactos
11  *
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.
16  *
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.
21  *
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
25  */
26 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
27
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>
34
35 //might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
36
37 #undef _fmode
38 extern unsigned int _fmode;
39
40 FILE* __alloc_file(void);
41
42 //extern int _fmode;
43
44
45 FILE* fopen(const char *file, const char *mode)
46 {
47   FILE *f;
48   int fd, rw, oflags = 0;
49    
50   if (file == 0)
51     return 0;
52   if (mode == 0)
53     return 0;
54
55   f = __alloc_file();
56   if (f == NULL)
57     return NULL;
58
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'))
67     oflags |= O_TEXT;
68   else if (strchr(mode, 'b'))
69     oflags |= O_BINARY;
70   else
71     oflags |= (_fmode & (O_TEXT|O_BINARY));
72
73   fd = _open(file, oflags, 0);
74   if (fd < 0)
75     return NULL;
76
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
79
80   if (strchr(mode, 'a'))
81     lseek(fd, 0, SEEK_END);
82
83   f->_cnt = 0;
84   f->_file = fd;
85   f->_bufsiz = 0;
86   if (rw)
87     f->_flag = _IOREAD | _IOWRT;
88   else if (strchr(mode, 'r'))
89     f->_flag = _IOREAD;
90   else
91     f->_flag = _IOWRT;
92
93   if (strchr(mode, 't'))
94     f->_flag |= _IOTEXT;
95   else if (strchr(mode, 'b'))
96     f->_flag |= _IOBINARY;
97   else if (_fmode & O_BINARY)
98     f->_flag |= _IOBINARY;
99
100   f->_base = f->_ptr = NULL;
101   return f;
102 }
103
104 FILE* _wfopen(const wchar_t *file, const wchar_t *mode)
105 {
106   FILE *f;
107   int fd, rw, oflags = 0;
108    
109   if (file == 0)
110     return 0;
111   if (mode == 0)
112     return 0;
113
114   f = __alloc_file();
115   if (f == NULL)
116     return NULL;
117
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'))
126     oflags |= O_TEXT;
127   else if (wcschr(mode, L'b'))
128     oflags |= O_BINARY;
129   else
130     oflags |= (_fmode & (O_TEXT|O_BINARY));
131
132   fd = _wopen(file, oflags, 0);
133   if (fd < 0)
134     return NULL;
135
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);
140
141   f->_cnt = 0;
142   f->_file = fd;
143   f->_bufsiz = 0;
144   if (rw)
145     f->_flag = _IOREAD | _IOWRT;
146   else if (wcschr(mode, L'r'))
147     f->_flag = _IOREAD;
148   else
149     f->_flag = _IOWRT;
150
151   if (wcschr(mode, L't'))
152     f->_flag |= _IOTEXT;
153   else if (wcschr(mode, L'b'))
154     f->_flag |= _IOBINARY;
155   else if (_fmode & O_BINARY)
156     f->_flag |= _IOBINARY;
157
158   f->_base = f->_ptr = NULL;
159   return f;
160 }