Re-implemented using a real semaphore.
[wine/multimedia.git] / win32 / file.c
blob40d5699b642f48a4fadc48489ebdcc4b33647db0
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
5 */
7 #include <errno.h>
8 #include <sys/errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/mman.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <time.h>
17 #include "windows.h"
18 #include "winbase.h"
19 #include "winerror.h"
20 #include "file.h"
21 #include "device.h"
22 #include "process.h"
23 #include "heap.h"
24 #include "debug.h"
26 DWORD ErrnoToLastError(int errno_num);
28 static int TranslateCreationFlags(DWORD create_flags);
29 static int TranslateAccessFlags(DWORD access_flags);
30 static int TranslateShareFlags(DWORD share_flags);
32 /***********************************************************************
33 * ReadFileEx (KERNEL32.)
35 typedef
36 VOID
37 (CALLBACK *LPOVERLAPPED_COMPLETION_ROUTINE)(
38 DWORD dwErrorCode,
39 DWORD dwNumberOfBytesTransfered,
40 LPOVERLAPPED lpOverlapped
43 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
44 LPOVERLAPPED lpOverlapped,
45 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
48 FIXME(file, "file %d to buf %p num %ld %p func %p stub\n",
49 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
50 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
51 return 0;
55 /*************************************************************************
56 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
58 * Creates or opens an object, and returns a handle that can be used to
59 * access that object.
61 * PARAMS
63 * filename [I] pointer to filename to be accessed
64 * access [I] access mode requested
65 * sharing [I] share mode
66 * security [I] pointer to security attributes
67 * creation [I] ?
68 * attributes [I] ?
69 * template [I] handle to file with attributes to copy
71 * RETURNS
72 * Success: Open handle to specified file
73 * Failure: INVALID_HANDLE_VALUE
75 * NOTES
76 * Should call SetLastError() on failure.
78 * BUGS
80 * Doesn't support character devices, pipes, template files, or a
81 * lot of the 'attributes' flags yet.
83 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
84 LPSECURITY_ATTRIBUTES security, DWORD creation,
85 DWORD attributes, HANDLE32 template)
87 int access_flags, create_flags, share_flags;
88 HFILE32 to_dup = HFILE_ERROR32; /* handle to dup */
90 /* Translate the various flags to Unix-style.
92 access_flags = TranslateAccessFlags(access);
93 create_flags = TranslateCreationFlags(creation);
94 share_flags = TranslateShareFlags(sharing);
96 if(template)
97 FIXME(file, "template handles not supported.\n");
99 if(!filename)
100 return HFILE_ERROR32;
101 /* If the name starts with '\\?\' or '\\.\', ignore the first 4 chars.
103 if(!strncmp(filename, "\\\\?\\", 4) || !strncmp(filename, "\\\\.\\", 4))
105 if (filename[2] == '.')
106 return DEVICE_Open( filename+4, access_flags | create_flags );
108 filename += 4;
109 if (!strncmp(filename, "UNC", 3))
111 FIXME(file, "UNC name (%s) not supported.\n",filename);
112 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
113 return HFILE_ERROR32;
117 /* If the name still starts with '\\', it's a UNC name.
119 if(!strncmp(filename, "\\\\", 2))
121 FIXME(file, "UNC names not supported.\n");
122 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
123 return HFILE_ERROR32;
126 /* If the name is either CONIN$ or CONOUT$, give them duplicated stdin
127 * or stdout, respectively. The lower case version is also allowed. Most likely
128 * this should be a case ignore string compare.
130 if(!strcasecmp(filename, "CONIN$"))
131 to_dup = GetStdHandle( STD_INPUT_HANDLE );
132 else if(!strcasecmp(filename, "CONOUT$"))
133 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
135 if(to_dup != HFILE_ERROR32)
137 HFILE32 handle;
138 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
139 &handle, access, FALSE, 0 ))
140 handle = HFILE_ERROR32;
141 return handle;
143 return FILE_Open( filename, access_flags | create_flags,share_flags );
147 /*************************************************************************
148 * CreateFile32W (KERNEL32.48)
150 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
151 LPSECURITY_ATTRIBUTES security, DWORD creation,
152 DWORD attributes, HANDLE32 template)
154 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
155 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
156 attributes, template );
157 HeapFree( GetProcessHeap(), 0, afn );
158 return res;
161 static int TranslateAccessFlags(DWORD access_flags)
163 int rc = 0;
165 switch(access_flags)
167 case GENERIC_READ:
168 rc = O_RDONLY;
169 break;
171 case GENERIC_WRITE:
172 rc = O_WRONLY;
173 break;
175 case (GENERIC_READ | GENERIC_WRITE):
176 rc = O_RDWR;
177 break;
180 return rc;
183 static int TranslateCreationFlags(DWORD create_flags)
185 int rc = 0;
187 switch(create_flags)
189 case CREATE_NEW:
190 rc = O_CREAT | O_EXCL;
191 break;
193 case CREATE_ALWAYS:
194 rc = O_CREAT | O_TRUNC;
195 break;
197 case OPEN_EXISTING:
198 rc = 0;
199 break;
201 case OPEN_ALWAYS:
202 rc = O_CREAT;
203 break;
205 case TRUNCATE_EXISTING:
206 rc = O_TRUNC;
207 break;
210 return rc;
212 static int TranslateShareFlags(DWORD share_flags)
214 OPEN_SHARE_DENYNONE FILE_SHARE_READ | FILE_SHARE_WRITE
215 OPEN_SHARE_DENYREAD FILE_SHARE_WRITE
216 OPEN_SHARE_DENYREADWRITE 0
217 OPEN_SHARE_DENYWRITE FILE_SHARE_READ
220 switch(share_flags)
222 case FILE_SHARE_READ | FILE_SHARE_WRITE:
223 return OF_SHARE_DENY_NONE;
224 case FILE_SHARE_WRITE:
225 return OF_SHARE_DENY_READ;
226 case FILE_SHARE_READ:
227 return OF_SHARE_DENY_WRITE;
228 case 0:
229 return OF_SHARE_EXCLUSIVE;
230 default:
232 FIXME(file,"unknown sharing flags 0x%04lx\n",share_flags);
233 return OF_SHARE_EXCLUSIVE;
235 /**************************************************************************
236 * SetFileAttributes16 (KERNEL.421)
238 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
240 return SetFileAttributes32A( lpFileName, attributes );
244 /**************************************************************************
245 * SetFileAttributes32A (KERNEL32.490)
247 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
249 struct stat buf;
250 DOS_FULL_NAME full_name;
252 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
253 return FALSE;
255 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
256 if (attributes & FILE_ATTRIBUTE_NORMAL) {
257 attributes &= ~FILE_ATTRIBUTE_NORMAL;
258 if (attributes)
259 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
260 lpFileName,attributes);
262 if(stat(full_name.long_name,&buf)==-1)
264 SetLastError(ErrnoToLastError(errno));
265 return FALSE;
267 if (attributes & FILE_ATTRIBUTE_READONLY)
269 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
270 attributes &= ~FILE_ATTRIBUTE_READONLY;
272 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
273 if (attributes)
274 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
275 lpFileName,attributes);
276 if (-1==chmod(full_name.long_name,buf.st_mode))
278 SetLastError(ErrnoToLastError(errno));
279 return FALSE;
281 return TRUE;
285 /**************************************************************************
286 * SetFileAttributes32W (KERNEL32.491)
288 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
290 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
291 BOOL32 res = SetFileAttributes32A( afn, attributes );
292 HeapFree( GetProcessHeap(), 0, afn );
293 return res;
297 /**************************************************************************
298 * SetFileApisToOEM (KERNEL32.645)
300 VOID WINAPI SetFileApisToOEM(void)
302 /*FIXME(file,"(): stub!\n");*/
306 /**************************************************************************
307 * SetFileApisToANSI (KERNEL32.644)
309 VOID WINAPI SetFileApisToANSI(void)
311 /*FIXME(file,"(): stub\n");*/
315 /******************************************************************************
316 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
318 * RETURNS
319 * TRUE: Set of file functions is using ANSI code page
320 * FALSE: Set of file functions is using OEM code page
322 BOOL32 WINAPI AreFileApisANSI(void)
324 FIXME(file,"(void): stub\n");
325 return TRUE;