:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / msvcrt / stdio / fopen.c
1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
2
3 #include <msvcrt/sys/types.h>
4 #include <msvcrt/stdio.h>
5 #include <msvcrt/io.h>
6 #include <msvcrt/fcntl.h>
7 //#include <msvcrt/internal/file.h>
8
9 //might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
10
11 #undef _fmode
12 extern unsigned int _fmode;
13
14 FILE *  __alloc_file(void);
15
16
17 FILE* fopen(const char *file, const char *mode)
18 {
19   FILE *f;
20   int fd, rw, oflags = 0;
21   char tbchar;
22    
23   if (file == 0)
24     return 0;
25   if (mode == 0)
26     return 0;
27
28   f = __alloc_file();
29   if (f == NULL)
30     return NULL;
31
32   rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
33
34   switch (*mode)
35   {
36   case 'a':
37     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
38     break;
39   case 'r':
40     oflags = rw ? O_RDWR : O_RDONLY;
41     break;
42   case 'w':
43     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
44     break;
45   default:
46     return (NULL);
47   }
48   if (mode[1] == '+')
49     tbchar = mode[2];
50   else
51     tbchar = mode[1];
52   if (tbchar == 't')
53     oflags |= O_TEXT;
54   else if (tbchar == 'b')
55     oflags |= O_BINARY;
56   else
57     oflags |= (_fmode & (O_TEXT|O_BINARY));
58
59   fd = _open(file, oflags, 0);
60   if (fd < 0)
61     return NULL;
62
63 // ms crtdll ensures that writes will end up at the end of file in append mode
64 // we just move the file pointer to the end of file initially
65   if (*mode == 'a')
66     lseek(fd, 0, SEEK_END);
67
68   f->_cnt = 0;
69   f->_file = fd;
70   f->_bufsiz = 0;
71   if (rw)
72     f->_flag = _IOREAD | _IOWRT;
73   else if (*mode == 'r')
74     f->_flag = _IOREAD;
75   else
76     f->_flag = _IOWRT;
77
78   f->_base = f->_ptr = NULL;
79   return f;
80 }
81
82 FILE* _wfopen(const wchar_t *file, const wchar_t *mode)
83 {
84   FILE *f;
85   int fd, rw, oflags = 0;
86   wchar_t tbchar;
87    
88   if (file == 0)
89     return 0;
90   if (mode == 0)
91     return 0;
92
93   f = __alloc_file();
94   if (f == NULL)
95     return NULL;
96
97   rw = (mode[1] == L'+') || (mode[1] && (mode[2] == L'+'));
98
99   switch (*mode)
100   {
101   case L'a':
102     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
103     break;
104   case L'r':
105     oflags = rw ? O_RDWR : O_RDONLY;
106     break;
107   case L'w':
108     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
109     break;
110   default:
111     return (NULL);
112   }
113   if (mode[1] == L'+')
114     tbchar = mode[2];
115   else
116     tbchar = mode[1];
117   if (tbchar == L't')
118     oflags |= O_TEXT;
119   else if (tbchar == L'b')
120     oflags |= O_BINARY;
121   else
122     oflags |= (_fmode & (O_TEXT|O_BINARY));
123
124   fd = _wopen(file, oflags, 0);
125   if (fd < 0)
126     return NULL;
127
128 // ms crtdll ensures that writes will end up at the end of file in append mode
129 // we just move the file pointer to the end of file initially
130   if (*mode == L'a')
131     lseek(fd, 0, SEEK_END);
132
133   f->_cnt = 0;
134   f->_file = fd;
135   f->_bufsiz = 0;
136   if (rw)
137     f->_flag = _IOREAD | _IOWRT;
138   else if (*mode == L'r')
139     f->_flag = _IOREAD;
140   else
141     f->_flag = _IOWRT;
142
143   f->_base = f->_ptr = NULL;
144   return f;
145 }