f8bd7a29e2562392cb5867c42eb846aca443f710
[reactos.git] / reactos / lib / msvcrt / io / wopen.c
1 /* $Id: wopen.c,v 1.1 2002/11/24 18:42:22 robd Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/msvcrt/io/open.c
6 * PURPOSE: Opens a file and translates handles to fileno
7 * PROGRAMER: Boudewijn Dekker
8 * UPDATE HISTORY:
9 * 28/12/98: Created
10 */
11
12 // rember to interlock the allocation of fileno when making this thread safe
13 // possibly store extra information at the handle
14
15 #include <windows.h>
16 #if !defined(NDEBUG) && defined(DBG)
17 #include <msvcrt/stdarg.h>
18 #endif
19 #include <msvcrt/io.h>
20 #include <msvcrt/fcntl.h>
21 #include <msvcrt/sys/stat.h>
22 #include <msvcrt/stdlib.h>
23 #include <msvcrt/internal/file.h>
24 #include <msvcrt/string.h>
25 #include <msvcrt/share.h>
26 #include <msvcrt/errno.h>
27
28 #define NDEBUG
29 #include <msvcrt/msvcrtdbg.h>
30
31
32 int _wopen(const wchar_t* _path, int _oflag, ...)
33 {
34 #if !defined(NDEBUG) && defined(DBG)
35 va_list arg;
36 int pmode;
37 #endif
38 HANDLE hFile;
39 DWORD dwDesiredAccess = 0;
40 DWORD dwShareMode = 0;
41 DWORD dwCreationDistribution = 0;
42 DWORD dwFlagsAndAttributes = 0;
43 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
44
45 #if !defined(NDEBUG) && defined(DBG)
46 va_start(arg, _oflag);
47 pmode = va_arg(arg, int);
48 #endif
49
50 // DPRINT("_wopen('%S', %x, (%x))\n", _path, _oflag, pmode);
51
52 if (( _oflag & S_IREAD ) == S_IREAD)
53 dwShareMode = FILE_SHARE_READ;
54 else if ( ( _oflag & S_IWRITE) == S_IWRITE ) {
55 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
56 }
57
58 /*
59 *
60 * _O_BINARY Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.)
61 * _O_TEXT Opens file in text (translated) mode. (For more information, see Text and Binary Mode File I/O and fopen.)
62 *
63 * _O_APPEND Moves file pointer to end of file before every write operation.
64 */
65 if (( _oflag & _O_RDWR ) == _O_RDWR )
66 dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
67 FILE_WRITE_DATA | FILE_READ_ATTRIBUTES |
68 FILE_WRITE_ATTRIBUTES;
69 else if (( _oflag & O_RDONLY ) == O_RDONLY )
70 dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
71 FILE_WRITE_ATTRIBUTES;
72 else if (( _oflag & _O_WRONLY ) == _O_WRONLY )
73 dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
74 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
75
76 if (( _oflag & S_IREAD ) == S_IREAD )
77 dwShareMode |= FILE_SHARE_READ;
78
79 if (( _oflag & S_IWRITE ) == S_IWRITE )
80 dwShareMode |= FILE_SHARE_WRITE;
81
82 if (( _oflag & (_O_CREAT | _O_EXCL ) ) == (_O_CREAT | _O_EXCL) )
83 dwCreationDistribution |= CREATE_NEW;
84
85 else if (( _oflag & O_TRUNC ) == O_TRUNC ) {
86 if (( _oflag & O_CREAT ) == O_CREAT )
87 dwCreationDistribution |= CREATE_ALWAYS;
88 else if (( _oflag & O_RDONLY ) != O_RDONLY )
89 dwCreationDistribution |= TRUNCATE_EXISTING;
90 }
91 else if (( _oflag & _O_APPEND ) == _O_APPEND )
92 dwCreationDistribution |= OPEN_EXISTING;
93 else if (( _oflag & _O_CREAT ) == _O_CREAT )
94 dwCreationDistribution |= OPEN_ALWAYS;
95 else
96 dwCreationDistribution |= OPEN_EXISTING;
97
98 if (( _oflag & _O_RANDOM ) == _O_RANDOM )
99 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
100 if (( _oflag & _O_SEQUENTIAL ) == _O_SEQUENTIAL )
101 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
102
103 if (( _oflag & _O_TEMPORARY ) == _O_TEMPORARY )
104 dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
105
106 if (( _oflag & _O_SHORT_LIVED ) == _O_SHORT_LIVED )
107 dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
108
109 if (_oflag & _O_NOINHERIT)
110 sa.bInheritHandle = FALSE;
111
112 hFile = CreateFileW(_path,
113 dwDesiredAccess,
114 dwShareMode,
115 &sa,
116 dwCreationDistribution,
117 dwFlagsAndAttributes,
118 NULL);
119 if (hFile == (HANDLE)-1)
120 return -1;
121 return __fileno_alloc(hFile,_oflag);
122 }
123
124 int _wsopen(wchar_t *path, int access, int shflag, int mode)
125 {
126 return _wopen((path), (access)|(shflag), (mode));
127 }