Release 980104
[wine/multimedia.git] / win32 / file.c
blobf634dcebf1b5844e0fd2c87daee0ff9366d2eb0a
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 "stddebug.h"
23 #define DEBUG_WIN32
24 #include "debug.h"
26 DWORD ErrnoToLastError(int errno_num);
28 static int TranslateCreationFlags(DWORD create_flags);
29 static int TranslateAccessFlags(DWORD access_flags);
31 /***********************************************************************
32 * WriteFile (KERNEL32.578)
34 BOOL32 WINAPI WriteFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToWrite,
35 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
37 LONG res;
39 res = _lwrite32(hFile,lpBuffer,numberOfBytesToWrite);
40 if (res==-1) {
41 SetLastError(ErrnoToLastError(errno));
42 return FALSE;
44 if(numberOfBytesWritten)
45 *numberOfBytesWritten = res;
46 return TRUE;
49 /***********************************************************************
50 * ReadFile (KERNEL32.428)
52 BOOL32 WINAPI ReadFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
53 LPDWORD numread, LPOVERLAPPED lpOverlapped)
55 int actual_read;
57 actual_read = _lread32(hFile,lpBuffer,numtoread);
58 if(actual_read == -1) {
59 SetLastError(ErrnoToLastError(errno));
60 return FALSE;
62 if(numread)
63 *numread = actual_read;
65 return TRUE;
69 /***********************************************************************
70 * ReadFileEx (KERNEL32.)
72 typedef /* from winbase.h */
73 VOID
74 (WINAPI *LPOVERLAPPED_COMPLETION_ROUTINE)(
75 DWORD dwErrorCode,
76 DWORD dwNumberOfBytesTransfered,
77 LPOVERLAPPED lpOverlapped
80 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
81 LPOVERLAPPED lpOverlapped,
82 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
85 fprintf(stdnimp,"ReadFileEx file %d to buf %p num %ld %p func %p stub\n",
86 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
87 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
88 return 0;
92 /*************************************************************************
93 * CreateFile32A (KERNEL32.45)
95 * Doesn't support character devices, pipes, template files, or a
96 * lot of the 'attributes' flags yet.
98 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
99 LPSECURITY_ATTRIBUTES security, DWORD creation,
100 DWORD attributes, HANDLE32 template)
102 int access_flags, create_flags;
104 /* Translate the various flags to Unix-style.
106 access_flags = TranslateAccessFlags(access);
107 create_flags = TranslateCreationFlags(creation);
109 if(template)
110 dprintf_file(stddeb, "CreateFile: template handles not supported.\n");
112 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
114 if(!strncmp(filename, "\\\\?", 3) || !strncmp(filename, "\\\\.", 3))
115 filename += 3;
117 /* If the name still starts with '\\', it's a UNC name.
119 if(!strncmp(filename, "\\\\", 2))
121 dprintf_file(stddeb, "CreateFile: UNC names not supported.\n");
122 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
123 return HFILE_ERROR32;
126 /* If the name is either CONIN$ or CONOUT$, give them stdin
127 * or stdout, respectively.
129 if(!strcmp(filename, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE );
130 if(!strcmp(filename, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE );
132 return FILE_Open( filename, access_flags | create_flags );
136 /*************************************************************************
137 * CreateFile32W (KERNEL32.48)
139 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
140 LPSECURITY_ATTRIBUTES security, DWORD creation,
141 DWORD attributes, HANDLE32 template)
143 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
144 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
145 attributes, template );
146 HeapFree( GetProcessHeap(), 0, afn );
147 return res;
150 static int TranslateAccessFlags(DWORD access_flags)
152 int rc = 0;
154 switch(access_flags)
156 case GENERIC_READ:
157 rc = O_RDONLY;
158 break;
160 case GENERIC_WRITE:
161 rc = O_WRONLY;
162 break;
164 case (GENERIC_READ | GENERIC_WRITE):
165 rc = O_RDWR;
166 break;
169 return rc;
172 static int TranslateCreationFlags(DWORD create_flags)
174 int rc = 0;
176 switch(create_flags)
178 case CREATE_NEW:
179 rc = O_CREAT | O_EXCL;
180 break;
182 case CREATE_ALWAYS:
183 rc = O_CREAT | O_TRUNC;
184 break;
186 case OPEN_EXISTING:
187 rc = 0;
188 break;
190 case OPEN_ALWAYS:
191 rc = O_CREAT;
192 break;
194 case TRUNCATE_EXISTING:
195 rc = O_TRUNC;
196 break;
199 return rc;
203 /**************************************************************************
204 * SetFileAttributes16 (KERNEL.421)
206 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
208 return SetFileAttributes32A( lpFileName, attributes );
212 /**************************************************************************
213 * SetFileAttributes32A (KERNEL32.490)
215 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
217 struct stat buf;
218 DOS_FULL_NAME full_name;
220 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
221 return FALSE;
223 dprintf_file(stddeb,"SetFileAttributes(%s,%lx)\n",lpFileName,attributes);
224 if (attributes & FILE_ATTRIBUTE_NORMAL) {
225 attributes &= ~FILE_ATTRIBUTE_NORMAL;
226 if (attributes)
227 fprintf(stdnimp,"SetFileAttributesA(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
228 lpFileName,attributes);
230 if(stat(full_name.long_name,&buf)==-1)
232 SetLastError(ErrnoToLastError(errno));
233 return FALSE;
235 if (attributes & FILE_ATTRIBUTE_READONLY)
237 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
238 attributes &= ~FILE_ATTRIBUTE_READONLY;
240 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
241 if (attributes)
242 fprintf(stdnimp,"SetFileAttributesA(%s):%lx attribute(s) not implemented.\n",lpFileName,attributes);
243 if (-1==chmod(full_name.long_name,buf.st_mode))
245 SetLastError(ErrnoToLastError(errno));
246 return FALSE;
248 return TRUE;
252 /**************************************************************************
253 * SetFileAttributes32W (KERNEL32.491)
255 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
257 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
258 BOOL32 res = SetFileAttributes32A( afn, attributes );
259 HeapFree( GetProcessHeap(), 0, afn );
260 return res;
264 /**************************************************************************
265 * SetFileApisToOEM (KERNEL32.645)
267 VOID WINAPI SetFileApisToOEM(void)
269 /*fprintf(stdnimp,"SetFileApisToOEM(),stub!\n");*/
273 /**************************************************************************
274 * SetFileApisToANSI (KERNEL32.644)
276 VOID WINAPI SetFileApisToANSI(void)
278 /*fprintf(stdnimp,"SetFileApisToANSI(),stub!\n");*/
282 /**************************************************************************
283 * AreFileApisANSI (KERNEL32.105)
285 BOOL32 WINAPI AreFileApisANSI(void)
287 fprintf(stdnimp,"AreFileApisANSI(),stub!\n");
288 return TRUE;