6b74582582961ae3a3ac0e0a8eafd91f6353d530
[reactos.git] / posix / lib / psxdll / fcntl / open.c
1 /* $Id: open.c,v 1.1 2002/02/20 07:06:50 hyperion Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS system libraries
6 * FILE: subsys/psx/lib/psxdll/fcntl/open.c
7 * PURPOSE: Open a file
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 04/02/2002: Created
11 */
12
13 #include <ddk/ntddk.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <psx/path.h>
21 #include <psx/debug.h>
22 #include <psx/errno.h>
23 #include <psx/pdata.h>
24
25 int open(const char *path, int oflag, ...)
26 {
27 ANSI_STRING strPath;
28 UNICODE_STRING wstrPath;
29 int nRetVal;
30
31 RtlInitAnsiString(&strPath, (PCSZ)path);
32 RtlAnsiStringToUnicodeString(&wstrPath, &strPath, TRUE);
33
34 nRetVal = _Wopen(wstrPath.Buffer, oflag);
35
36 RtlFreeUnicodeString(&wstrPath);
37
38 return (nRetVal);
39 }
40
41 int _Wopen(const wchar_t *path, int oflag, ...)
42 {
43 OBJECT_ATTRIBUTES oaFileAttribs;
44 IO_STATUS_BLOCK isbStatus;
45 UNICODE_STRING wstrNativePath;
46 NTSTATUS nErrCode;
47 ULONG nDesiredAccess;
48 ULONG nCreateDisposition;
49 ULONG nCreateOptions;
50 HANDLE hFile;
51 #if 0
52 mode_t mFileMode;
53 #endif
54 int nFileNo;
55 __fdtable_t *pftTable;
56 __fildes_t fdDescriptor;
57
58 /* translate file access flag */
59 switch(oflag & O_ACCMODE)
60 {
61 case O_RDONLY:
62 {
63 nDesiredAccess = FILE_READ_ACCESS;
64 nCreateOptions = 0;
65 break;
66 }
67
68 case O_WRONLY:
69 {
70 nDesiredAccess = FILE_WRITE_ACCESS;
71 nCreateOptions = FILE_NON_DIRECTORY_FILE; /* required by the specification */
72 break;
73 }
74
75 case O_RDWR:
76 {
77 nDesiredAccess = FILE_READ_ACCESS | FILE_WRITE_ACCESS;
78 nCreateOptions = FILE_NON_DIRECTORY_FILE; /* required by the specification */
79 break;
80 }
81
82 default:
83 {
84 errno = EINVAL;
85 return (-1);
86 }
87
88 }
89
90 /* miscellaneous flags */
91 if((oflag & _O_DIRFILE) == _O_DIRFILE)
92 nCreateOptions |= FILE_DIRECTORY_FILE;
93
94 /* creation disposition */
95 if((oflag & O_CREAT) == O_CREAT)
96 if((oflag & O_EXCL) == O_EXCL)
97 nCreateDisposition = FILE_CREATE; /* O_CREAT + O_EXCL: create file, fail if file exists */
98 else
99 nCreateDisposition = FILE_OPEN_IF; /* O_CREAT: open file, create if file doesn't exist */
100 else if((oflag & O_TRUNC) == O_TRUNC)
101 nCreateDisposition = FILE_OVERWRITE; /* O_TRUNC: truncate existing file */
102 else
103 nCreateDisposition = FILE_OPEN; /* normal: open file, fail if file doesn't exist */
104
105 /* lock the environment */
106 __PdxAcquirePdataLock();
107
108 /* convert the path into a native path */
109 if(!__PdxPosixPathNameToNtPathName((LPWSTR)path, __PdxGetNativePathBuffer(), __PdxGetCurDir(), NULL))
110 {
111 __PdxReleasePdataLock();
112 return (-1);
113 }
114
115 /* set file generic object attributes */
116 oaFileAttribs.Length = sizeof(oaFileAttribs);
117 oaFileAttribs.RootDirectory = __PdxGetRootHandle();
118 oaFileAttribs.ObjectName = &wstrNativePath;
119 oaFileAttribs.Attributes = OBJ_INHERIT; /* child processes inherit all file descriptors */
120 oaFileAttribs.SecurityDescriptor = NULL;
121 oaFileAttribs.SecurityQualityOfService = NULL;
122
123 /* open or create the file */
124 nErrCode = NtCreateFile
125 (
126 &hFile,
127 nDesiredAccess | SYNCHRONIZE,
128 &oaFileAttribs,
129 &isbStatus,
130 NULL,
131 0,
132 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
133 nCreateDisposition,
134 FILE_SYNCHRONOUS_IO_NONALERT,
135 NULL,
136 nCreateOptions
137 );
138
139 /* failure */
140 if(!NT_SUCCESS(nErrCode))
141 {
142 ERR("NtCreateFile() failed with status 0x%08X", nErrCode);
143 __PdxReleasePdataLock();
144 errno = __status_to_errno(nErrCode);
145 return (-1);
146 }
147
148 /* initialize descriptor constructor */
149 memset(&fdDescriptor, 0, sizeof(fdDescriptor));
150 fdDescriptor.FileHandle = hFile;
151 fdDescriptor.OpenFlags = oflag;
152
153 /* allocate a new file descriptor */
154 nFileNo = fcntl(0, F_NEWFD, &fdDescriptor);
155
156 /* unlock the environment */
157 __PdxReleasePdataLock();
158
159 /* could not allocate the file descriptor */
160 if(nFileNo < 0)
161 {
162 NtClose(hFile);
163 return (-1);
164 }
165
166 /* return the file number */
167 return (nFileNo);
168 }
169
170 int creat(const char *path, mode_t mode)
171 {
172 return (open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
173 }
174
175 int _Wcreat(const wchar_t *path, mode_t mode)
176 {
177 return (_Wopen(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
178 }
179
180 /* EOF */
181