update for HEAD-2003091401
[reactos.git] / lib / msvcrt / stdio / fsopen.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/msvcrt/stdio/fsopen.c
5  * PURPOSE:     Checks for keyboard hits
6  * PROGRAMER:   Boudewijn Dekker
7  * UPDATE HISTORY:
8  *              28/12/98: Created
9  */
10 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
11
12 #include <msvcrt/sys/types.h>
13 #include <msvcrt/stdio.h>
14 #include <msvcrt/io.h>
15 #include <msvcrt/fcntl.h>
16 #include <msvcrt/share.h>
17 #include <msvcrt/internal/file.h>
18
19
20 FILE *  __alloc_file(void);
21
22
23 /*
24  * @implemented
25  */
26 FILE* _fsopen(const char *file, const char *mode, int shflag)
27 {
28   FILE *f;
29   int fd, rw, oflags = 0;
30   char tbchar;
31    
32   int shf;
33
34   if (file == 0)
35     return 0;
36   if (mode == 0)
37     return 0;
38
39   f = __alloc_file();
40   if (f == NULL)
41     return NULL;
42
43   rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
44
45   switch (*mode)
46   {
47   case 'a':
48     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
49     break;
50   case 'r':
51     oflags = rw ? O_RDWR : O_RDONLY;
52     break;
53   case 'w':
54     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
55     break;
56   default:
57     return (NULL);
58   }
59   if (mode[1] == '+')
60     tbchar = mode[2];
61   else
62     tbchar = mode[1];
63   if (tbchar == 't')
64     oflags |= O_TEXT;
65   else if (tbchar == 'b')
66     oflags |= O_BINARY;
67   else
68     oflags |= (_fmode & (O_TEXT|O_BINARY));
69
70   if ( shflag == _SH_DENYNO )
71     shf = _S_IREAD | _S_IWRITE;
72   else if( shflag == _SH_DENYRD )
73     shf =  _S_IWRITE;
74   else if( shflag == _SH_DENYRW )
75     shf =  0;
76   else if( shflag == _SH_DENYWR )
77     shf =  _S_IREAD;
78   else
79     shf = _S_IREAD | _S_IWRITE;
80
81   fd = _open(file, oflags, shf);
82   if (fd < 0)
83     return NULL;
84
85 // msvcrt ensures that writes will end up at the end of file in append mode
86 // we just move the file pointer to the end of file initially
87   if (*mode == 'a')
88     lseek(fd, 0, SEEK_END);
89
90   f->_cnt = 0;
91   f->_file = fd;
92   f->_bufsiz = 0;
93   if (rw)
94     f->_flag = _IOREAD | _IOWRT;
95   else if (*mode == 'r')
96     f->_flag = _IOREAD;
97   else
98     f->_flag = _IOWRT;
99
100   f->_base = f->_ptr = NULL;
101   return f;
102 }
103
104 /*
105  * @implemented
106  */
107 FILE* _wfsopen(const wchar_t *file, const wchar_t *mode, int shflag)
108 {
109   FILE *f;
110   int fd, rw, oflags = 0;
111   wchar_t tbchar;
112    
113   int shf;
114
115   if (file == 0)
116     return 0;
117   if (mode == 0)
118     return 0;
119
120   f = __alloc_file();
121   if (f == NULL)
122     return NULL;
123
124   rw = (mode[1] == L'+') || (mode[1] && (mode[2] == L'+'));
125
126   switch (*mode)
127   {
128   case L'a':
129     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
130     break;
131   case L'r':
132     oflags = rw ? O_RDWR : O_RDONLY;
133     break;
134   case L'w':
135     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
136     break;
137   default:
138     return (NULL);
139   }
140   if (mode[1] == L'+')
141     tbchar = mode[2];
142   else
143     tbchar = mode[1];
144   if (tbchar == L't')
145     oflags |= O_TEXT;
146   else if (tbchar == L'b')
147     oflags |= O_BINARY;
148   else
149     oflags |= (_fmode & (O_TEXT|O_BINARY));
150
151   if ( shflag == _SH_DENYNO )
152     shf = _S_IREAD | _S_IWRITE;
153   else if( shflag == _SH_DENYRD )
154     shf =  _S_IWRITE;
155   else if( shflag == _SH_DENYRW )
156     shf =  0;
157   else if( shflag == _SH_DENYWR )
158     shf =  _S_IREAD;
159   else
160     shf = _S_IREAD | _S_IWRITE;
161
162   fd = _wopen(file, oflags, shf);
163   if (fd < 0)
164     return NULL;
165
166 // msvcrt ensures that writes will end up at the end of file in append mode
167 // we just move the file pointer to the end of file initially
168   if (*mode == L'a')
169     lseek(fd, 0, SEEK_END);
170
171   f->_cnt = 0;
172   f->_file = fd;
173   f->_bufsiz = 0;
174   if (rw)
175     f->_flag = _IOREAD | _IOWRT;
176   else if (*mode == L'r')
177     f->_flag = _IOREAD;
178   else
179     f->_flag = _IOWRT;
180
181   f->_base = f->_ptr = NULL;
182   return f;
183 }