Release 980503
[wine/hacks.git] / win32 / file.c
blobc999fb31d1fc695425e6ad6d61f271b45f18b1f4
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
5 */
7 #include <errno.h>
8 #include <stdio.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 "process.h"
22 #include "heap.h"
23 #include "debug.h"
25 DWORD ErrnoToLastError(int errno_num);
27 static int TranslateCreationFlags(DWORD create_flags);
28 static int TranslateAccessFlags(DWORD access_flags);
30 /***********************************************************************
31 * WriteFile (KERNEL32.578)
33 BOOL32 WINAPI WriteFile(HANDLE32 hFile, LPCVOID lpBuffer,
34 DWORD numberOfBytesToWrite,
35 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
37 K32OBJ *ioptr;
38 BOOL32 status = FALSE;
40 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
41 numberOfBytesToWrite);
43 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
44 K32OBJ_UNKNOWN, 0 )))
45 return HFILE_ERROR32;
46 if (K32OBJ_OPS(ioptr)->write)
47 status = K32OBJ_OPS(ioptr)->write(ioptr, lpBuffer, numberOfBytesToWrite,
48 numberOfBytesWritten, lpOverlapped);
49 K32OBJ_DecCount( ioptr );
50 return status;
53 /***********************************************************************
54 * ReadFile (KERNEL32.428)
56 BOOL32 WINAPI ReadFile(HANDLE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToRead,
57 LPDWORD numberOfBytesRead, LPOVERLAPPED lpOverlapped)
59 K32OBJ *ioptr;
60 BOOL32 status = FALSE;
62 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
63 numberOfBytesToRead);
65 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
66 K32OBJ_UNKNOWN, 0 )))
67 return HFILE_ERROR32;
68 if (K32OBJ_OPS(ioptr)->read)
69 status = K32OBJ_OPS(ioptr)->read(ioptr, lpBuffer, numberOfBytesToRead,
70 numberOfBytesRead, lpOverlapped);
71 K32OBJ_DecCount( ioptr );
72 return status;
76 /***********************************************************************
77 * ReadFileEx (KERNEL32.)
79 typedef
80 VOID
81 (WINAPI *LPOVERLAPPED_COMPLETION_ROUTINE)(
82 DWORD dwErrorCode,
83 DWORD dwNumberOfBytesTransfered,
84 LPOVERLAPPED lpOverlapped
87 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
88 LPOVERLAPPED lpOverlapped,
89 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
92 FIXME(file, "file %d to buf %p num %ld %p func %p stub\n",
93 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
94 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
95 return 0;
99 /*************************************************************************
100 * CreateFile32A (KERNEL32.45)
102 * Doesn't support character devices, pipes, template files, or a
103 * lot of the 'attributes' flags yet.
105 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
106 LPSECURITY_ATTRIBUTES security, DWORD creation,
107 DWORD attributes, HANDLE32 template)
109 int access_flags, create_flags;
111 /* Translate the various flags to Unix-style.
113 access_flags = TranslateAccessFlags(access);
114 create_flags = TranslateCreationFlags(creation);
116 if(template)
117 FIXME(file, "template handles not supported.\n");
119 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
121 if(!strncmp(filename, "\\\\?", 3) || !strncmp(filename, "\\\\.", 3))
122 filename += 3;
124 /* If the name still starts with '\\', it's a UNC name.
126 if(!strncmp(filename, "\\\\", 2))
128 FIXME(file, "UNC names not supported.\n");
129 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
130 return HFILE_ERROR32;
133 /* If the name is either CONIN$ or CONOUT$, give them stdin
134 * or stdout, respectively.
136 if(!strcmp(filename, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE );
137 if(!strcmp(filename, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE );
139 return FILE_Open( filename, access_flags | create_flags );
143 /*************************************************************************
144 * CreateFile32W (KERNEL32.48)
146 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
147 LPSECURITY_ATTRIBUTES security, DWORD creation,
148 DWORD attributes, HANDLE32 template)
150 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
151 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
152 attributes, template );
153 HeapFree( GetProcessHeap(), 0, afn );
154 return res;
157 static int TranslateAccessFlags(DWORD access_flags)
159 int rc = 0;
161 switch(access_flags)
163 case GENERIC_READ:
164 rc = O_RDONLY;
165 break;
167 case GENERIC_WRITE:
168 rc = O_WRONLY;
169 break;
171 case (GENERIC_READ | GENERIC_WRITE):
172 rc = O_RDWR;
173 break;
176 return rc;
179 static int TranslateCreationFlags(DWORD create_flags)
181 int rc = 0;
183 switch(create_flags)
185 case CREATE_NEW:
186 rc = O_CREAT | O_EXCL;
187 break;
189 case CREATE_ALWAYS:
190 rc = O_CREAT | O_TRUNC;
191 break;
193 case OPEN_EXISTING:
194 rc = 0;
195 break;
197 case OPEN_ALWAYS:
198 rc = O_CREAT;
199 break;
201 case TRUNCATE_EXISTING:
202 rc = O_TRUNC;
203 break;
206 return rc;
210 /**************************************************************************
211 * SetFileAttributes16 (KERNEL.421)
213 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
215 return SetFileAttributes32A( lpFileName, attributes );
219 /**************************************************************************
220 * SetFileAttributes32A (KERNEL32.490)
222 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
224 struct stat buf;
225 DOS_FULL_NAME full_name;
227 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
228 return FALSE;
230 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
231 if (attributes & FILE_ATTRIBUTE_NORMAL) {
232 attributes &= ~FILE_ATTRIBUTE_NORMAL;
233 if (attributes)
234 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
235 lpFileName,attributes);
237 if(stat(full_name.long_name,&buf)==-1)
239 SetLastError(ErrnoToLastError(errno));
240 return FALSE;
242 if (attributes & FILE_ATTRIBUTE_READONLY)
244 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
245 attributes &= ~FILE_ATTRIBUTE_READONLY;
247 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
248 if (attributes)
249 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
250 lpFileName,attributes);
251 if (-1==chmod(full_name.long_name,buf.st_mode))
253 SetLastError(ErrnoToLastError(errno));
254 return FALSE;
256 return TRUE;
260 /**************************************************************************
261 * SetFileAttributes32W (KERNEL32.491)
263 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
265 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
266 BOOL32 res = SetFileAttributes32A( afn, attributes );
267 HeapFree( GetProcessHeap(), 0, afn );
268 return res;
272 /**************************************************************************
273 * SetFileApisToOEM (KERNEL32.645)
275 VOID WINAPI SetFileApisToOEM(void)
277 /*FIXME(file,"(): stub!\n");*/
281 /**************************************************************************
282 * SetFileApisToANSI (KERNEL32.644)
284 VOID WINAPI SetFileApisToANSI(void)
286 /*FIXME(file,"(): stub\n");*/
290 /******************************************************************************
291 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
293 * RETURNS
294 * TRUE: Set of file functions is using ANSI code page
295 * FALSE: Set of file functions is using OEM code page
297 BOOL32 WINAPI AreFileApisANSI(void)
299 FIXME(file,"(void): stub\n");
300 return TRUE;