IPaddress common control implementation. First try; needs more work to
[wine/multimedia.git] / win32 / file.c
blobc97175637bc10f46a1b958efebd3f82d02886721
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 name (%s) not supported.\n",filename);
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. The lower case version is also allowed. Most likely
174 * this should be a case ignore string compare.
176 if(!strcasecmp(filename, "CONIN$"))
177 to_dup = GetStdHandle( STD_INPUT_HANDLE );
178 else if(!strcasecmp(filename, "CONOUT$"))
179 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
181 if(to_dup != HFILE_ERROR32)
183 HFILE32 handle;
184 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
185 &handle, access, FALSE, 0 ))
186 handle = HFILE_ERROR32;
187 return handle;
189 return FILE_Open( filename, access_flags | create_flags,share_flags );
193 /*************************************************************************
194 * CreateFile32W (KERNEL32.48)
196 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
197 LPSECURITY_ATTRIBUTES security, DWORD creation,
198 DWORD attributes, HANDLE32 template)
200 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
201 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
202 attributes, template );
203 HeapFree( GetProcessHeap(), 0, afn );
204 return res;
207 static int TranslateAccessFlags(DWORD access_flags)
209 int rc = 0;
211 switch(access_flags)
213 case GENERIC_READ:
214 rc = O_RDONLY;
215 break;
217 case GENERIC_WRITE:
218 rc = O_WRONLY;
219 break;
221 case (GENERIC_READ | GENERIC_WRITE):
222 rc = O_RDWR;
223 break;
226 return rc;
229 static int TranslateCreationFlags(DWORD create_flags)
231 int rc = 0;
233 switch(create_flags)
235 case CREATE_NEW:
236 rc = O_CREAT | O_EXCL;
237 break;
239 case CREATE_ALWAYS:
240 rc = O_CREAT | O_TRUNC;
241 break;
243 case OPEN_EXISTING:
244 rc = 0;
245 break;
247 case OPEN_ALWAYS:
248 rc = O_CREAT;
249 break;
251 case TRUNCATE_EXISTING:
252 rc = O_TRUNC;
253 break;
256 return rc;
258 static int TranslateShareFlags(DWORD share_flags)
260 OPEN_SHARE_DENYNONE FILE_SHARE_READ | FILE_SHARE_WRITE
261 OPEN_SHARE_DENYREAD FILE_SHARE_WRITE
262 OPEN_SHARE_DENYREADWRITE 0
263 OPEN_SHARE_DENYWRITE FILE_SHARE_READ
266 switch(share_flags)
268 case FILE_SHARE_READ | FILE_SHARE_WRITE:
269 return OF_SHARE_DENY_NONE;
270 case FILE_SHARE_WRITE:
271 return OF_SHARE_DENY_READ;
272 case FILE_SHARE_READ:
273 return OF_SHARE_DENY_WRITE;
274 case 0:
275 return OF_SHARE_EXCLUSIVE;
276 default:
278 FIXME(file,"unknown sharing flags 0x%04lx\n",share_flags);
279 return OF_SHARE_EXCLUSIVE;
281 /**************************************************************************
282 * SetFileAttributes16 (KERNEL.421)
284 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
286 return SetFileAttributes32A( lpFileName, attributes );
290 /**************************************************************************
291 * SetFileAttributes32A (KERNEL32.490)
293 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
295 struct stat buf;
296 DOS_FULL_NAME full_name;
298 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
299 return FALSE;
301 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
302 if (attributes & FILE_ATTRIBUTE_NORMAL) {
303 attributes &= ~FILE_ATTRIBUTE_NORMAL;
304 if (attributes)
305 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
306 lpFileName,attributes);
308 if(stat(full_name.long_name,&buf)==-1)
310 SetLastError(ErrnoToLastError(errno));
311 return FALSE;
313 if (attributes & FILE_ATTRIBUTE_READONLY)
315 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
316 attributes &= ~FILE_ATTRIBUTE_READONLY;
318 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
319 if (attributes)
320 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
321 lpFileName,attributes);
322 if (-1==chmod(full_name.long_name,buf.st_mode))
324 SetLastError(ErrnoToLastError(errno));
325 return FALSE;
327 return TRUE;
331 /**************************************************************************
332 * SetFileAttributes32W (KERNEL32.491)
334 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
336 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
337 BOOL32 res = SetFileAttributes32A( afn, attributes );
338 HeapFree( GetProcessHeap(), 0, afn );
339 return res;
343 /**************************************************************************
344 * SetFileApisToOEM (KERNEL32.645)
346 VOID WINAPI SetFileApisToOEM(void)
348 /*FIXME(file,"(): stub!\n");*/
352 /**************************************************************************
353 * SetFileApisToANSI (KERNEL32.644)
355 VOID WINAPI SetFileApisToANSI(void)
357 /*FIXME(file,"(): stub\n");*/
361 /******************************************************************************
362 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
364 * RETURNS
365 * TRUE: Set of file functions is using ANSI code page
366 * FALSE: Set of file functions is using OEM code page
368 BOOL32 WINAPI AreFileApisANSI(void)
370 FIXME(file,"(void): stub\n");
371 return TRUE;