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
12 // rember to interlock the allocation of fileno when making this thread safe
13 // possibly store extra information at the handle
16 #if !defined(NDEBUG) && defined(DBG)
17 #include <msvcrt/stdarg.h>
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>
29 #include <msvcrt/msvcrtdbg.h>
32 int _wopen(const wchar_t* _path, int _oflag, ...)
34 #if !defined(NDEBUG) && defined(DBG)
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};
45 #if !defined(NDEBUG) && defined(DBG)
46 va_start(arg, _oflag);
47 pmode = va_arg(arg, int);
50 // DPRINT("_wopen('%S', %x, (%x))\n", _path, _oflag, pmode);
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;
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.)
63 * _O_APPEND Moves file pointer to end of file before every write operation.
66 if ((_oflag & _O_RDWR) == _O_RDWR)
67 dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
68 FILE_WRITE_DATA | FILE_READ_ATTRIBUTES |
69 FILE_WRITE_ATTRIBUTES;
70 else if ((_oflag & O_RDONLY) == O_RDONLY)
71 dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
72 FILE_WRITE_ATTRIBUTES;
73 else if ((_oflag & _O_WRONLY) == _O_WRONLY)
74 dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
75 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
77 if ((_oflag & _O_WRONLY) == _O_WRONLY)
78 dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
79 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
80 else if ((_oflag & _O_RDWR) == _O_RDWR)
81 dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
82 FILE_WRITE_DATA | FILE_READ_ATTRIBUTES |
83 FILE_WRITE_ATTRIBUTES;
84 else //if ((_oflag & O_RDONLY) == O_RDONLY)
85 dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
86 FILE_WRITE_ATTRIBUTES;
89 if ((_oflag & S_IREAD) == S_IREAD)
90 dwShareMode |= FILE_SHARE_READ;
92 if ((_oflag & S_IWRITE) == S_IWRITE)
93 dwShareMode |= FILE_SHARE_WRITE;
95 if ((_oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
96 dwCreationDistribution |= CREATE_NEW;
98 else if ((_oflag & O_TRUNC) == O_TRUNC) {
99 if ((_oflag & O_CREAT) == O_CREAT)
100 dwCreationDistribution |= CREATE_ALWAYS;
101 else if ((_oflag & O_RDONLY) != O_RDONLY)
102 dwCreationDistribution |= TRUNCATE_EXISTING;
104 else if ((_oflag & _O_APPEND) == _O_APPEND)
105 dwCreationDistribution |= OPEN_EXISTING;
106 else if ((_oflag & _O_CREAT) == _O_CREAT)
107 dwCreationDistribution |= OPEN_ALWAYS;
109 dwCreationDistribution |= OPEN_EXISTING;
111 if ((_oflag & _O_RANDOM) == _O_RANDOM)
112 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
113 if ((_oflag & _O_SEQUENTIAL) == _O_SEQUENTIAL)
114 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
116 if ((_oflag & _O_TEMPORARY) == _O_TEMPORARY)
117 dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
119 if ((_oflag & _O_SHORT_LIVED) == _O_SHORT_LIVED)
120 dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
122 if (_oflag & _O_NOINHERIT)
123 sa.bInheritHandle = FALSE;
125 hFile = CreateFileW(_path,
129 dwCreationDistribution,
130 dwFlagsAndAttributes,
132 if (hFile == (HANDLE)-1)
134 return __fileno_alloc(hFile,_oflag);
137 int _wsopen(wchar_t* path, int access, int shflag, int mode)
139 return _wopen((path), (access)|(shflag), (mode));