Release 970720
[wine/gsoc-2012-control.git] / win32 / file.c
blob170c91a39eaac24583a8f0a5e3e5b094f952bd07
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"
28 DWORD ErrnoToLastError(int errno_num);
30 static int TranslateCreationFlags(DWORD create_flags);
31 static int TranslateAccessFlags(DWORD access_flags);
33 /***********************************************************************
34 * WriteFile (KERNEL32.578)
36 BOOL32 WriteFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToWrite,
37 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
39 LONG res;
41 res = _lwrite32(hFile,lpBuffer,numberOfBytesToWrite);
42 if (res==-1) {
43 SetLastError(ErrnoToLastError(errno));
44 return FALSE;
46 if(numberOfBytesWritten)
47 *numberOfBytesWritten = res;
48 return TRUE;
51 /***********************************************************************
52 * ReadFile (KERNEL32.428)
54 BOOL32 ReadFile(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
55 LPDWORD numread, LPOVERLAPPED lpOverlapped)
57 int actual_read;
59 actual_read = _lread32(hFile,lpBuffer,numtoread);
60 if(actual_read == -1) {
61 SetLastError(ErrnoToLastError(errno));
62 return FALSE;
64 if(numread)
65 *numread = actual_read;
67 return TRUE;
71 /*************************************************************************
72 * CreateFile32A (KERNEL32.45)
74 * Doesn't support character devices, pipes, template files, or a
75 * lot of the 'attributes' flags yet.
77 HFILE32 CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
78 LPSECURITY_ATTRIBUTES security, DWORD creation,
79 DWORD attributes, HANDLE32 template)
81 int access_flags, create_flags;
83 /* Translate the various flags to Unix-style.
85 access_flags = TranslateAccessFlags(access);
86 create_flags = TranslateCreationFlags(creation);
88 if(template)
89 dprintf_file(stddeb, "CreateFile: template handles not supported.\n");
91 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
93 if(!strncmp(filename, "\\\\?", 3) || !strncmp(filename, "\\\\.", 3))
94 filename += 3;
96 /* If the name still starts with '\\', it's a UNC name.
98 if(!strncmp(filename, "\\\\", 2))
100 dprintf_file(stddeb, "CreateFile: UNC names not supported.\n");
101 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
102 return HFILE_ERROR32;
105 /* If the name is either CONIN$ or CONOUT$, give them stdin
106 * or stdout, respectively.
108 if(!strcmp(filename, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE );
109 if(!strcmp(filename, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE );
111 return FILE_Open( filename, access_flags | create_flags );
115 /*************************************************************************
116 * CreateFile32W (KERNEL32.48)
118 HFILE32 CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
119 LPSECURITY_ATTRIBUTES security, DWORD creation,
120 DWORD attributes, HANDLE32 template)
122 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
123 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
124 attributes, template );
125 HeapFree( GetProcessHeap(), 0, afn );
126 return res;
129 static int TranslateAccessFlags(DWORD access_flags)
131 int rc = 0;
133 switch(access_flags)
135 case GENERIC_READ:
136 rc = O_RDONLY;
137 break;
139 case GENERIC_WRITE:
140 rc = O_WRONLY;
141 break;
143 case (GENERIC_READ | GENERIC_WRITE):
144 rc = O_RDWR;
145 break;
148 return rc;
151 static int TranslateCreationFlags(DWORD create_flags)
153 int rc = 0;
155 switch(create_flags)
157 case CREATE_NEW:
158 rc = O_CREAT | O_EXCL;
159 break;
161 case CREATE_ALWAYS:
162 rc = O_CREAT | O_TRUNC;
163 break;
165 case OPEN_EXISTING:
166 rc = 0;
167 break;
169 case OPEN_ALWAYS:
170 rc = O_CREAT;
171 break;
173 case TRUNCATE_EXISTING:
174 rc = O_TRUNC;
175 break;
178 return rc;
182 /**************************************************************************
183 * SetFileAttributes16 (KERNEL.421)
185 BOOL16 SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
187 return SetFileAttributes32A( lpFileName, attributes );
191 /**************************************************************************
192 * SetFileAttributes32A (KERNEL32.490)
194 BOOL32 SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
196 struct stat buf;
197 DOS_FULL_NAME full_name;
199 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
200 return FALSE;
202 dprintf_file(stddeb,"SetFileAttributes(%s,%lx)\n",lpFileName,attributes);
203 if(stat(full_name.long_name,&buf)==-1)
205 SetLastError(ErrnoToLastError(errno));
206 return FALSE;
208 if (attributes & FILE_ATTRIBUTE_READONLY)
210 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
211 attributes &= ~FILE_ATTRIBUTE_READONLY;
213 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
214 if (attributes)
215 fprintf(stdnimp,"SetFileAttributesA(%s):%lx attribute(s) not implemented.\n",lpFileName,attributes);
216 if (-1==chmod(full_name.long_name,buf.st_mode))
218 SetLastError(ErrnoToLastError(errno));
219 return FALSE;
221 return TRUE;
224 /**************************************************************************
225 * SetFileAttributes32W (KERNEL32.491)
227 BOOL32 SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
229 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
230 BOOL32 res = SetFileAttributes32A( afn, attributes );
231 HeapFree( GetProcessHeap(), 0, afn );
232 return res;
235 VOID SetFileApisToOEM()
237 fprintf(stdnimp,"SetFileApisToOEM(),stub!\n");
240 VOID SetFileApisToANSI()
242 fprintf(stdnimp,"SetFileApisToANSI(),stub!\n");
245 BOOL32 AreFileApisANSI()
247 fprintf(stdnimp,"AreFileApisANSI(),stub!\n");
248 return TRUE;