1 /* $Id: open.c,v 1.1 2002/02/20 07:06:50 hyperion Exp $
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS system libraries
6 * FILE: subsys/psx/lib/psxdll/fcntl/open.c
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
13 #include <ddk/ntddk.h>
14 #include <sys/types.h>
21 #include <psx/debug.h>
22 #include <psx/errno.h>
23 #include <psx/pdata.h>
25 int open(const char *path
, int oflag
, ...)
28 UNICODE_STRING wstrPath
;
31 RtlInitAnsiString(&strPath
, (PCSZ
)path
);
32 RtlAnsiStringToUnicodeString(&wstrPath
, &strPath
, TRUE
);
34 nRetVal
= _Wopen(wstrPath
.Buffer
, oflag
);
36 RtlFreeUnicodeString(&wstrPath
);
41 int _Wopen(const wchar_t *path
, int oflag
, ...)
43 OBJECT_ATTRIBUTES oaFileAttribs
;
44 IO_STATUS_BLOCK isbStatus
;
45 UNICODE_STRING wstrNativePath
;
48 ULONG nCreateDisposition
;
55 __fdtable_t
*pftTable
;
56 __fildes_t fdDescriptor
;
58 /* translate file access flag */
59 switch(oflag
& O_ACCMODE
)
63 nDesiredAccess
= FILE_READ_ACCESS
;
70 nDesiredAccess
= FILE_WRITE_ACCESS
;
71 nCreateOptions
= FILE_NON_DIRECTORY_FILE
; /* required by the specification */
77 nDesiredAccess
= FILE_READ_ACCESS
| FILE_WRITE_ACCESS
;
78 nCreateOptions
= FILE_NON_DIRECTORY_FILE
; /* required by the specification */
90 /* miscellaneous flags */
91 if((oflag
& _O_DIRFILE
) == _O_DIRFILE
)
92 nCreateOptions
|= FILE_DIRECTORY_FILE
;
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 */
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 */
103 nCreateDisposition
= FILE_OPEN
; /* normal: open file, fail if file doesn't exist */
105 /* lock the environment */
106 __PdxAcquirePdataLock();
108 /* convert the path into a native path */
109 if(!__PdxPosixPathNameToNtPathName((LPWSTR
)path
, __PdxGetNativePathBuffer(), __PdxGetCurDir(), NULL
))
111 __PdxReleasePdataLock();
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
;
123 /* open or create the file */
124 nErrCode
= NtCreateFile
127 nDesiredAccess
| SYNCHRONIZE
,
132 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
134 FILE_SYNCHRONOUS_IO_NONALERT
,
140 if(!NT_SUCCESS(nErrCode
))
142 ERR("NtCreateFile() failed with status 0x%08X", nErrCode
);
143 __PdxReleasePdataLock();
144 errno
= __status_to_errno(nErrCode
);
148 /* initialize descriptor constructor */
149 memset(&fdDescriptor
, 0, sizeof(fdDescriptor
));
150 fdDescriptor
.FileHandle
= hFile
;
151 fdDescriptor
.OpenFlags
= oflag
;
153 /* allocate a new file descriptor */
154 nFileNo
= fcntl(0, F_NEWFD
, &fdDescriptor
);
156 /* unlock the environment */
157 __PdxReleasePdataLock();
159 /* could not allocate the file descriptor */
166 /* return the file number */
170 int creat(const char *path
, mode_t mode
)
172 return (open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
));
175 int _Wcreat(const wchar_t *path
, mode_t mode
)
177 return (_Wopen(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
));