tcharize a couple of routines
[reactos.git] / reactos / lib / crt / 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 <sys/types.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <io.h>
32 #include <fcntl.h>
33 #include <tchar.h>
34 #include <internal/file.h>
35
36 //might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
37
38
39
40 FILE* _tfopen(const _TCHAR *file, const _TCHAR *mode)
41 {
42 FILE *f;
43 int fd, rw, oflags = 0;
44
45 if (file == 0)
46 return 0;
47 if (mode == 0)
48 return 0;
49
50 f = __alloc_file();
51 if (f == NULL)
52 return NULL;
53
54 rw = (_tcschr(mode, '+') == NULL) ? 0 : 1;
55 if (_tcschr(mode, 'a'))
56 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
57 if (_tcschr(mode, 'r'))
58 oflags = rw ? O_RDWR : O_RDONLY;
59 if (_tcschr(mode, 'w'))
60 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
61 if (_tcschr(mode, 't'))
62 oflags |= O_TEXT;
63 else if (_tcschr(mode, 'b'))
64 oflags |= O_BINARY;
65 else
66 oflags |= (_fmode& (O_TEXT|O_BINARY));
67
68 fd = _topen(file, oflags, 0);
69 if (fd < 0)
70 return NULL;
71
72 // msvcrt ensures that writes will end up at the end of file in append mode
73 // we just move the file pointer to the end of file initially
74
75 if (_tcschr(mode, 'a'))
76 _lseek(fd, 0, SEEK_END);
77
78 f->_cnt = 0;
79 f->_file = fd;
80 f->_bufsiz = 0;
81 if (rw)
82 f->_flag = _IOREAD | _IOWRT;
83 else if (_tcschr(mode, 'r'))
84 f->_flag = _IOREAD;
85 else
86 f->_flag = _IOWRT;
87
88 if (_tcschr(mode, 't'))
89 f->_flag |= _IOTEXT;
90 else if (_tcschr(mode, 'b'))
91 f->_flag |= _IOBINARY;
92 else if (_fmode& O_BINARY)
93 f->_flag |= _IOBINARY;
94
95 f->_base = f->_ptr = NULL;
96 return f;
97 }