Added a few authors.
[wine/multimedia.git] / win32 / file.c
blob43f22b80cc138f8957c9bc487c8e9f4cf0d1f5a9
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 * WriteFile (KERNEL32.578)
35 BOOL32 WINAPI WriteFile(HANDLE32 hFile, LPCVOID lpBuffer,
36 DWORD numberOfBytesToWrite,
37 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
39 K32OBJ *ioptr;
40 BOOL32 status = FALSE;
42 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
43 numberOfBytesToWrite);
45 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
46 K32OBJ_UNKNOWN, 0, NULL )))
47 return HFILE_ERROR32;
48 if (K32OBJ_OPS(ioptr)->write)
49 status = K32OBJ_OPS(ioptr)->write(ioptr, lpBuffer, numberOfBytesToWrite,
50 numberOfBytesWritten, lpOverlapped);
51 K32OBJ_DecCount( ioptr );
52 return status;
55 /***********************************************************************
56 * ReadFile (KERNEL32.428)
58 BOOL32 WINAPI ReadFile(HANDLE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToRead,
59 LPDWORD numberOfBytesRead, LPOVERLAPPED lpOverlapped)
61 K32OBJ *ioptr;
62 BOOL32 status = FALSE;
64 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
65 numberOfBytesToRead);
67 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
68 K32OBJ_UNKNOWN, 0, NULL )))
69 return HFILE_ERROR32;
70 if (K32OBJ_OPS(ioptr)->read)
71 status = K32OBJ_OPS(ioptr)->read(ioptr, lpBuffer, numberOfBytesToRead,
72 numberOfBytesRead, lpOverlapped);
73 K32OBJ_DecCount( ioptr );
74 return status;
78 /***********************************************************************
79 * ReadFileEx (KERNEL32.)
81 typedef
82 VOID
83 (CALLBACK *LPOVERLAPPED_COMPLETION_ROUTINE)(
84 DWORD dwErrorCode,
85 DWORD dwNumberOfBytesTransfered,
86 LPOVERLAPPED lpOverlapped
89 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
90 LPOVERLAPPED lpOverlapped,
91 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
94 FIXME(file, "file %d to buf %p num %ld %p func %p stub\n",
95 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
96 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
97 return 0;
101 /*************************************************************************
102 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
104 * Creates or opens an object, and returns a handle that can be used to
105 * access that object.
107 * PARAMS
109 * filename [I] pointer to filename to be accessed
110 * access [I] access mode requested
111 * sharing [I] share mode
112 * security [I] pointer to security attributes
113 * creation [I] ?
114 * attributes [I] ?
115 * template [I] handle to file with attributes to copy
117 * RETURNS
118 * Success: Open handle to specified file
119 * Failure: INVALID_HANDLE_VALUE
121 * NOTES
122 * Should call SetLastError() on failure.
124 * BUGS
126 * Doesn't support character devices, pipes, template files, or a
127 * lot of the 'attributes' flags yet.
129 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
130 LPSECURITY_ATTRIBUTES security, DWORD creation,
131 DWORD attributes, HANDLE32 template)
133 int access_flags, create_flags, share_flags;
134 HFILE32 to_dup = HFILE_ERROR32; /* handle to dup */
136 /* Translate the various flags to Unix-style.
138 access_flags = TranslateAccessFlags(access);
139 create_flags = TranslateCreationFlags(creation);
140 share_flags = TranslateShareFlags(sharing);
142 if(template)
143 FIXME(file, "template handles not supported.\n");
145 if(!filename)
146 return HFILE_ERROR32;
147 /* If the name starts with '\\?\' or '\\.\', ignore the first 4 chars.
149 if(!strncmp(filename, "\\\\?\\", 4) || !strncmp(filename, "\\\\.\\", 4))
151 if (filename[2] == '.')
152 return DEVICE_Open( filename+4, access_flags | create_flags );
154 filename += 4;
155 if (!strncmp(filename, "UNC", 3))
157 FIXME(file, "UNC names not supported.\n");
158 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
159 return HFILE_ERROR32;
163 /* If the name still starts with '\\', it's a UNC name.
165 if(!strncmp(filename, "\\\\", 2))
167 FIXME(file, "UNC names not supported.\n");
168 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
169 return HFILE_ERROR32;
172 /* If the name is either CONIN$ or CONOUT$, give them duplicated stdin
173 * or stdout, respectively.
175 if(!strcmp(filename, "CONIN$"))
176 to_dup = GetStdHandle( STD_INPUT_HANDLE );
177 else if(!strcmp(filename, "CONOUT$"))
178 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
180 if(to_dup != HFILE_ERROR32)
182 HFILE32 handle;
183 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
184 &handle, access, FALSE, 0 ))
185 handle = HFILE_ERROR32;
186 return handle;
188 return FILE_Open( filename, access_flags | create_flags,share_flags );
192 /*************************************************************************
193 * CreateFile32W (KERNEL32.48)
195 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
196 LPSECURITY_ATTRIBUTES security, DWORD creation,
197 DWORD attributes, HANDLE32 template)
199 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
200 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
201 attributes, template );
202 HeapFree( GetProcessHeap(), 0, afn );
203 return res;
206 static int TranslateAccessFlags(DWORD access_flags)
208 int rc = 0;
210 switch(access_flags)
212 case GENERIC_READ:
213 rc = O_RDONLY;
214 break;
216 case GENERIC_WRITE:
217 rc = O_WRONLY;
218 break;
220 case (GENERIC_READ | GENERIC_WRITE):
221 rc = O_RDWR;
222 break;
225 return rc;
228 static int TranslateCreationFlags(DWORD create_flags)
230 int rc = 0;
232 switch(create_flags)
234 case CREATE_NEW:
235 rc = O_CREAT | O_EXCL;
236 break;
238 case CREATE_ALWAYS:
239 rc = O_CREAT | O_TRUNC;
240 break;
242 case OPEN_EXISTING:
243 rc = 0;
244 break;
246 case OPEN_ALWAYS:
247 rc = O_CREAT;
248 break;
250 case TRUNCATE_EXISTING:
251 rc = O_TRUNC;
252 break;
255 return rc;
257 static int TranslateShareFlags(DWORD share_flags)
259 OPEN_SHARE_DENYNONE FILE_SHARE_READ | FILE_SHARE_WRITE
260 OPEN_SHARE_DENYREAD FILE_SHARE_WRITE
261 OPEN_SHARE_DENYREADWRITE 0
262 OPEN_SHARE_DENYWRITE FILE_SHARE_READ
265 switch(share_flags)
267 case FILE_SHARE_READ | FILE_SHARE_WRITE:
268 return OF_SHARE_DENY_NONE;
269 case FILE_SHARE_WRITE:
270 return OF_SHARE_DENY_READ;
271 case FILE_SHARE_READ:
272 return OF_SHARE_DENY_WRITE;
273 case 0:
274 return OF_SHARE_EXCLUSIVE;
275 default:
277 FIXME(file,"unknown sharing flags 0x%04lx\n",share_flags);
278 return OF_SHARE_EXCLUSIVE;
280 /**************************************************************************
281 * SetFileAttributes16 (KERNEL.421)
283 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
285 return SetFileAttributes32A( lpFileName, attributes );
289 /**************************************************************************
290 * SetFileAttributes32A (KERNEL32.490)
292 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
294 struct stat buf;
295 DOS_FULL_NAME full_name;
297 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
298 return FALSE;
300 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
301 if (attributes & FILE_ATTRIBUTE_NORMAL) {
302 attributes &= ~FILE_ATTRIBUTE_NORMAL;
303 if (attributes)
304 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
305 lpFileName,attributes);
307 if(stat(full_name.long_name,&buf)==-1)
309 SetLastError(ErrnoToLastError(errno));
310 return FALSE;
312 if (attributes & FILE_ATTRIBUTE_READONLY)
314 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
315 attributes &= ~FILE_ATTRIBUTE_READONLY;
317 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
318 if (attributes)
319 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
320 lpFileName,attributes);
321 if (-1==chmod(full_name.long_name,buf.st_mode))
323 SetLastError(ErrnoToLastError(errno));
324 return FALSE;
326 return TRUE;
330 /**************************************************************************
331 * SetFileAttributes32W (KERNEL32.491)
333 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
335 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
336 BOOL32 res = SetFileAttributes32A( afn, attributes );
337 HeapFree( GetProcessHeap(), 0, afn );
338 return res;
342 /**************************************************************************
343 * SetFileApisToOEM (KERNEL32.645)
345 VOID WINAPI SetFileApisToOEM(void)
347 /*FIXME(file,"(): stub!\n");*/
351 /**************************************************************************
352 * SetFileApisToANSI (KERNEL32.644)
354 VOID WINAPI SetFileApisToANSI(void)
356 /*FIXME(file,"(): stub\n");*/
360 /******************************************************************************
361 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
363 * RETURNS
364 * TRUE: Set of file functions is using ANSI code page
365 * FALSE: Set of file functions is using OEM code page
367 BOOL32 WINAPI AreFileApisANSI(void)
369 FIXME(file,"(void): stub\n");
370 return TRUE;