update for HEAD-2003021201
[reactos.git] / lib / msvcrt / io / wopen.c
1 /* $Id$
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 0
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;
76 #else
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;
87 #endif
88
89     if ((_oflag & S_IREAD) == S_IREAD)
90         dwShareMode |= FILE_SHARE_READ;
91
92     if ((_oflag & S_IWRITE) == S_IWRITE)
93         dwShareMode |= FILE_SHARE_WRITE;
94
95     if ((_oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
96         dwCreationDistribution |= CREATE_NEW;
97
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;
103     }
104     else if ((_oflag & _O_APPEND) == _O_APPEND)
105         dwCreationDistribution |= OPEN_EXISTING;
106     else if ((_oflag &  _O_CREAT) == _O_CREAT)
107         dwCreationDistribution |= OPEN_ALWAYS;
108     else
109         dwCreationDistribution |= OPEN_EXISTING;
110
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;
115
116     if ((_oflag &  _O_TEMPORARY) == _O_TEMPORARY)
117         dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
118
119     if ((_oflag &  _O_SHORT_LIVED) == _O_SHORT_LIVED)
120         dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
121
122     if (_oflag & _O_NOINHERIT)
123         sa.bInheritHandle = FALSE;
124
125     hFile = CreateFileW(_path,
126                dwDesiredAccess,
127                dwShareMode,
128                &sa,
129                dwCreationDistribution,
130                dwFlagsAndAttributes,
131                NULL);
132     if (hFile == (HANDLE)-1)
133         return -1;
134     return __fileno_alloc(hFile,_oflag);
135 }
136
137 int _wsopen(wchar_t* path, int access, int shflag, int mode)
138 {
139     return _wopen((path), (access)|(shflag), (mode));
140 }