Release 970112
[wine.git] / win32 / file.c
blobc059531bd944d5ed7743c001fa070bc2fe618a39
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 "heap.h"
22 #include "handle32.h"
23 #include "xmalloc.h"
24 #include "stddebug.h"
25 #define DEBUG_WIN32
26 #include "debug.h"
29 static int TranslateCreationFlags(DWORD create_flags);
30 static int TranslateAccessFlags(DWORD access_flags);
31 int TranslateProtectionFlags(DWORD);
32 #ifndef MAP_ANON
33 #define MAP_ANON 0
34 #endif
36 typedef struct {
37 HFILE32 hfile;
38 int prot;
39 unsigned long size;
40 } FILEMAP_OBJECT;
42 /***********************************************************************
43 * OpenFileMappingA (KERNEL32.397)
44 * FIXME: stub
47 HANDLE32 OpenFileMapping(DWORD access, BOOL inherit,const char *fname)
49 return 0;
53 /***********************************************************************
54 * CreateFileMapping32A (KERNEL32.46)
56 HANDLE32 CreateFileMapping32A(HANDLE32 h,LPSECURITY_ATTRIBUTES ats,
57 DWORD pot, DWORD sh, DWORD hlow, LPCSTR lpName )
59 FILEMAP_OBJECT *filemap_obj;
61 dprintf_win32(stddeb,"CreateFileMapping32A(%08x,%p,%ld,%ld,%ld,%s)\n",
62 h,ats,pot,sh,hlow,lpName
64 if (sh) {
65 SetLastError(ErrnoToLastError(errno));
66 return INVALID_HANDLE_VALUE32;
68 filemap_obj=(FILEMAP_OBJECT *)HeapAlloc( SystemHeap, 0,
69 sizeof(FILEMAP_OBJECT) );
70 if(filemap_obj == NULL) {
71 SetLastError(ERROR_UNKNOWN);
72 return 0;
74 if (h==INVALID_HANDLE_VALUE32)
75 h=_lcreat32(lpName,1);/*FIXME*/
77 filemap_obj->hfile = h;
78 filemap_obj->prot = TranslateProtectionFlags(pot);
79 filemap_obj->size = hlow;
80 return (HANDLE32)filemap_obj;;
83 /***********************************************************************
84 * CreateFileMapping32W (KERNEL32.47)
87 HANDLE32 CreateFileMapping32W(HANDLE32 h,LPSECURITY_ATTRIBUTES ats,
88 DWORD pot, DWORD sh, DWORD hlow, LPCWSTR lpName)
90 LPSTR aname = HEAP_strdupWtoA( GetProcessHeap(), 0, lpName );
91 HANDLE32 res = CreateFileMapping32A(h,ats,pot,sh,hlow,aname);
92 HeapFree( GetProcessHeap(), 0, aname );
93 return res;
97 /***********************************************************************
98 * MapViewOfFile (KERNEL32.385)
100 LPVOID MapViewOfFile(HANDLE32 handle, DWORD access, DWORD offhi,
101 DWORD offlo, DWORD size)
103 return MapViewOfFileEx(handle,access,offhi,offlo,size,0);
106 /***********************************************************************
107 * MapViewOfFileEx (KERNEL32.386)
110 LPVOID MapViewOfFileEx(HANDLE32 handle, DWORD access, DWORD offhi,
111 DWORD offlo, DWORD size, DWORD st)
113 FILEMAP_OBJECT *fmap = (FILEMAP_OBJECT*)handle;
115 if (!size) size = fmap->size;
116 if (!size) size = 1;
117 return mmap ((caddr_t)st, size, fmap->prot,
118 MAP_ANON|MAP_PRIVATE,
119 FILE_GetUnixHandle(fmap->hfile),
120 offlo);
123 /***********************************************************************
124 * UnmapViewOfFile (KERNEL32.385)
126 BOOL32 UnmapViewOfFile(LPVOID address) {
127 munmap(address,/*hmm*/1); /* FIXME: size? */
128 return TRUE;
132 /***********************************************************************
133 * WriteFile (KERNEL32.578)
135 BOOL32 WriteFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToWrite,
136 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
138 LONG res;
140 res = _lwrite32(hFile,lpBuffer,numberOfBytesToWrite);
141 if (res==-1) {
142 SetLastError(ErrnoToLastError(errno));
143 return FALSE;
145 if(numberOfBytesWritten)
146 *numberOfBytesWritten = res;
147 return TRUE;
150 /***********************************************************************
151 * ReadFile (KERNEL32.428)
153 BOOL32 ReadFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
154 LPDWORD numread, LPOVERLAPPED lpOverlapped)
156 int actual_read;
158 actual_read = _lread32(hFile,lpBuffer,numtoread);
159 if(actual_read == -1) {
160 SetLastError(ErrnoToLastError(errno));
161 return FALSE;
163 if(numread)
164 *numread = actual_read;
166 return TRUE;
170 /*************************************************************************
171 * CreateFile32A (KERNEL32.45)
173 * Doesn't support character devices, pipes, template files, or a
174 * lot of the 'attributes' flags yet.
176 HFILE32 CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
177 LPSECURITY_ATTRIBUTES security, DWORD creation,
178 DWORD attributes, HANDLE32 template)
180 int access_flags, create_flags;
182 /* Translate the various flags to Unix-style.
184 access_flags = TranslateAccessFlags(access);
185 create_flags = TranslateCreationFlags(creation);
187 if(template)
188 dprintf_file(stddeb, "CreateFile: template handles not supported.\n");
190 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
192 if(!strncmp(filename, "\\\\?", 3) || !strncmp(filename, "\\\\.", 3))
193 filename += 3;
195 /* If the name still starts with '\\', it's a UNC name.
197 if(!strncmp(filename, "\\\\", 2))
199 dprintf_file(stddeb, "CreateFile: UNC names not supported.\n");
200 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
201 return HFILE_ERROR32;
204 /* If the name is either CONIN$ or CONOUT$, give them stdin
205 * or stdout, respectively.
207 if(!strcmp(filename, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE );
208 if(!strcmp(filename, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE );
210 return FILE_Open( filename, access_flags | create_flags );
214 /*************************************************************************
215 * CreateFile32W (KERNEL32.48)
217 HFILE32 CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
218 LPSECURITY_ATTRIBUTES security, DWORD creation,
219 DWORD attributes, HANDLE32 template)
221 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
222 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
223 attributes, template );
224 HeapFree( GetProcessHeap(), 0, afn );
225 return res;
228 static int TranslateAccessFlags(DWORD access_flags)
230 int rc = 0;
232 switch(access_flags)
234 case GENERIC_READ:
235 rc = O_RDONLY;
236 break;
238 case GENERIC_WRITE:
239 rc = O_WRONLY;
240 break;
242 case (GENERIC_READ | GENERIC_WRITE):
243 rc = O_RDWR;
244 break;
247 return rc;
250 static int TranslateCreationFlags(DWORD create_flags)
252 int rc = 0;
254 switch(create_flags)
256 case CREATE_NEW:
257 rc = O_CREAT | O_EXCL;
258 break;
260 case CREATE_ALWAYS:
261 rc = O_CREAT | O_TRUNC;
262 break;
264 case OPEN_EXISTING:
265 rc = 0;
266 break;
268 case OPEN_ALWAYS:
269 rc = O_CREAT;
270 break;
272 case TRUNCATE_EXISTING:
273 rc = O_TRUNC;
274 break;
277 return rc;
281 /**************************************************************************
282 * SetFileAttributes16 (KERNEL.421)
284 BOOL16 SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
286 return SetFileAttributes32A( lpFileName, attributes );
290 /**************************************************************************
291 * SetFileAttributes32A (KERNEL32.490)
293 BOOL32 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 dprintf_file(stddeb,"SetFileAttributes(%s,%lx)\n",lpFileName,attributes);
302 if(stat(full_name.long_name,&buf)==-1)
304 SetLastError(ErrnoToLastError(errno));
305 return FALSE;
307 if (attributes & FILE_ATTRIBUTE_READONLY)
309 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
310 attributes &= ~FILE_ATTRIBUTE_READONLY;
312 attributes &= ~(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
313 if (attributes)
314 fprintf(stdnimp,"SetFileAttributesA(%s):%lx attribute(s) not implemented.\n",lpFileName,attributes);
315 if (-1==chmod(full_name.long_name,buf.st_mode))
317 SetLastError(ErrnoToLastError(errno));
318 return FALSE;
320 return TRUE;
323 /**************************************************************************
324 * SetFileAttributes32W (KERNEL32.491)
326 BOOL32 SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
328 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
329 BOOL32 res = SetFileAttributes32A( afn, attributes );
330 HeapFree( GetProcessHeap(), 0, afn );
331 return res;
334 VOID SetFileApisToOEM()
336 fprintf(stdnimp,"SetFileApisToOEM(),stub!\n");
339 VOID SetFileApisToANSI()
341 fprintf(stdnimp,"SetFileApisToANSI(),stub!\n");
344 BOOL32 AreFileApisANSI()
346 fprintf(stdnimp,"AreFileApisANSI(),stub!\n");
347 return TRUE;
351 BOOL32
352 LockFile(
353 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
354 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
356 fprintf(stdnimp,"LockFile(%d,0x%08lx%08lx,0x%08lx%08lx),stub!\n",
357 hFile,dwFileOffsetHigh,dwFileOffsetLow,
358 nNumberOfBytesToLockHigh,nNumberOfBytesToLockLow
360 return TRUE;
363 BOOL32
364 UnlockFile(
365 HFILE32 hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
366 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
368 fprintf(stdnimp,"UnlockFile(%d,0x%08lx%08lx,0x%08lx%08lx),stub!\n",
369 hFile,dwFileOffsetHigh,dwFileOffsetLow,
370 nNumberOfBytesToUnlockHigh,nNumberOfBytesToUnlockLow
372 return TRUE;