Release 980601
[wine/gsoc_dplay.git] / win32 / file.c
blob49a2d77a90cef0873ee4e3128fabcf381a41fd7a
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
5 */
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/mman.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <time.h>
16 #include "windows.h"
17 #include "winbase.h"
18 #include "winerror.h"
19 #include "file.h"
20 #include "process.h"
21 #include "heap.h"
22 #include "debug.h"
24 DWORD ErrnoToLastError(int errno_num);
26 static int TranslateCreationFlags(DWORD create_flags);
27 static int TranslateAccessFlags(DWORD access_flags);
29 /***********************************************************************
30 * WriteFile (KERNEL32.578)
32 BOOL32 WINAPI WriteFile(HANDLE32 hFile, LPCVOID lpBuffer,
33 DWORD numberOfBytesToWrite,
34 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
36 K32OBJ *ioptr;
37 BOOL32 status = FALSE;
39 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
40 numberOfBytesToWrite);
42 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
43 K32OBJ_UNKNOWN, 0 )))
44 return HFILE_ERROR32;
45 if (K32OBJ_OPS(ioptr)->write)
46 status = K32OBJ_OPS(ioptr)->write(ioptr, lpBuffer, numberOfBytesToWrite,
47 numberOfBytesWritten, lpOverlapped);
48 K32OBJ_DecCount( ioptr );
49 return status;
52 /***********************************************************************
53 * ReadFile (KERNEL32.428)
55 BOOL32 WINAPI ReadFile(HANDLE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToRead,
56 LPDWORD numberOfBytesRead, LPOVERLAPPED lpOverlapped)
58 K32OBJ *ioptr;
59 BOOL32 status = FALSE;
61 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
62 numberOfBytesToRead);
64 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
65 K32OBJ_UNKNOWN, 0 )))
66 return HFILE_ERROR32;
67 if (K32OBJ_OPS(ioptr)->read)
68 status = K32OBJ_OPS(ioptr)->read(ioptr, lpBuffer, numberOfBytesToRead,
69 numberOfBytesRead, lpOverlapped);
70 K32OBJ_DecCount( ioptr );
71 return status;
75 /***********************************************************************
76 * ReadFileEx (KERNEL32.)
78 typedef
79 VOID
80 (WINAPI *LPOVERLAPPED_COMPLETION_ROUTINE)(
81 DWORD dwErrorCode,
82 DWORD dwNumberOfBytesTransfered,
83 LPOVERLAPPED lpOverlapped
86 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
87 LPOVERLAPPED lpOverlapped,
88 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
91 FIXME(file, "file %d to buf %p num %ld %p func %p stub\n",
92 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
93 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
94 return 0;
98 /*************************************************************************
99 * CreateFile32A (KERNEL32.45)
101 * Doesn't support character devices, pipes, template files, or a
102 * lot of the 'attributes' flags yet.
104 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
105 LPSECURITY_ATTRIBUTES security, DWORD creation,
106 DWORD attributes, HANDLE32 template)
108 int access_flags, create_flags;
110 /* Translate the various flags to Unix-style.
112 access_flags = TranslateAccessFlags(access);
113 create_flags = TranslateCreationFlags(creation);
115 if(template)
116 FIXME(file, "template handles not supported.\n");
118 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
120 if(!strncmp(filename, "\\\\?", 3) || !strncmp(filename, "\\\\.", 3))
121 filename += 3;
123 /* If the name still starts with '\\', it's a UNC name.
125 if(!strncmp(filename, "\\\\", 2))
127 FIXME(file, "UNC names not supported.\n");
128 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
129 return HFILE_ERROR32;
132 /* If the name is either CONIN$ or CONOUT$, give them stdin
133 * or stdout, respectively.
135 if(!strcmp(filename, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE );
136 if(!strcmp(filename, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE );
138 return FILE_Open( filename, access_flags | create_flags );
142 /*************************************************************************
143 * CreateFile32W (KERNEL32.48)
145 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
146 LPSECURITY_ATTRIBUTES security, DWORD creation,
147 DWORD attributes, HANDLE32 template)
149 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
150 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
151 attributes, template );
152 HeapFree( GetProcessHeap(), 0, afn );
153 return res;
156 static int TranslateAccessFlags(DWORD access_flags)
158 int rc = 0;
160 switch(access_flags)
162 case GENERIC_READ:
163 rc = O_RDONLY;
164 break;
166 case GENERIC_WRITE:
167 rc = O_WRONLY;
168 break;
170 case (GENERIC_READ | GENERIC_WRITE):
171 rc = O_RDWR;
172 break;
175 return rc;
178 static int TranslateCreationFlags(DWORD create_flags)
180 int rc = 0;
182 switch(create_flags)
184 case CREATE_NEW:
185 rc = O_CREAT | O_EXCL;
186 break;
188 case CREATE_ALWAYS:
189 rc = O_CREAT | O_TRUNC;
190 break;
192 case OPEN_EXISTING:
193 rc = 0;
194 break;
196 case OPEN_ALWAYS:
197 rc = O_CREAT;
198 break;
200 case TRUNCATE_EXISTING:
201 rc = O_TRUNC;
202 break;
205 return rc;
209 /**************************************************************************
210 * SetFileAttributes16 (KERNEL.421)
212 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
214 return SetFileAttributes32A( lpFileName, attributes );
218 /**************************************************************************
219 * SetFileAttributes32A (KERNEL32.490)
221 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
223 struct stat buf;
224 DOS_FULL_NAME full_name;
226 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
227 return FALSE;
229 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
230 if (attributes & FILE_ATTRIBUTE_NORMAL) {
231 attributes &= ~FILE_ATTRIBUTE_NORMAL;
232 if (attributes)
233 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
234 lpFileName,attributes);
236 if(stat(full_name.long_name,&buf)==-1)
238 SetLastError(ErrnoToLastError(errno));
239 return FALSE;
241 if (attributes & FILE_ATTRIBUTE_READONLY)
243 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
244 attributes &= ~FILE_ATTRIBUTE_READONLY;
246 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
247 if (attributes)
248 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
249 lpFileName,attributes);
250 if (-1==chmod(full_name.long_name,buf.st_mode))
252 SetLastError(ErrnoToLastError(errno));
253 return FALSE;
255 return TRUE;
259 /**************************************************************************
260 * SetFileAttributes32W (KERNEL32.491)
262 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
264 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
265 BOOL32 res = SetFileAttributes32A( afn, attributes );
266 HeapFree( GetProcessHeap(), 0, afn );
267 return res;
271 /**************************************************************************
272 * SetFileApisToOEM (KERNEL32.645)
274 VOID WINAPI SetFileApisToOEM(void)
276 /*FIXME(file,"(): stub!\n");*/
280 /**************************************************************************
281 * SetFileApisToANSI (KERNEL32.644)
283 VOID WINAPI SetFileApisToANSI(void)
285 /*FIXME(file,"(): stub\n");*/
289 /******************************************************************************
290 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
292 * RETURNS
293 * TRUE: Set of file functions is using ANSI code page
294 * FALSE: Set of file functions is using OEM code page
296 BOOL32 WINAPI AreFileApisANSI(void)
298 FIXME(file,"(void): stub\n");
299 return TRUE;