2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
11 #include <sys/types.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(HANDLE32 hFile, LPCVOID lpBuffer,
35 DWORD numberOfBytesToWrite,
36 LPDWORD numberOfBytesWritten, LPOVERLAPPED lpOverlapped)
39 BOOL32 status = FALSE;
41 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
42 numberOfBytesToWrite);
44 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
45 K32OBJ_UNKNOWN, 0, NULL )))
47 if (K32OBJ_OPS(ioptr)->write)
48 status = K32OBJ_OPS(ioptr)->write(ioptr, lpBuffer, numberOfBytesToWrite,
49 numberOfBytesWritten, lpOverlapped);
50 K32OBJ_DecCount( ioptr );
54 /***********************************************************************
55 * ReadFile (KERNEL32.428)
57 BOOL32 WINAPI ReadFile(HANDLE32 hFile, LPVOID lpBuffer, DWORD numberOfBytesToRead,
58 LPDWORD numberOfBytesRead, LPOVERLAPPED lpOverlapped)
61 BOOL32 status = FALSE;
63 TRACE(file, "%d %p %ld\n", hFile, lpBuffer,
66 if (!(ioptr = HANDLE_GetObjPtr( PROCESS_Current(), hFile,
67 K32OBJ_UNKNOWN, 0, NULL )))
69 if (K32OBJ_OPS(ioptr)->read)
70 status = K32OBJ_OPS(ioptr)->read(ioptr, lpBuffer, numberOfBytesToRead,
71 numberOfBytesRead, lpOverlapped);
72 K32OBJ_DecCount( ioptr );
77 /***********************************************************************
78 * ReadFileEx (KERNEL32.)
82 (CALLBACK *LPOVERLAPPED_COMPLETION_ROUTINE)(
84 DWORD dwNumberOfBytesTransfered,
85 LPOVERLAPPED lpOverlapped
88 BOOL32 WINAPI ReadFileEx(HFILE32 hFile, LPVOID lpBuffer, DWORD numtoread,
89 LPOVERLAPPED lpOverlapped,
90 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
93 FIXME(file, "file %d to buf %p num %ld %p func %p stub\n",
94 hFile, lpBuffer, numtoread, lpOverlapped, lpCompletionRoutine);
95 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
100 /*************************************************************************
101 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
103 * Creates or opens an object, and returns a handle that can be used to
104 * access that object.
108 * filename [I] pointer to filename to be accessed
109 * access [I] access mode requested
110 * sharing [I] share mode
111 * security [I] pointer to security attributes
114 * template [I] handle to file with attributes to copy
117 * Success: Open handle to specified file
118 * Failure: INVALID_HANDLE_VALUE
121 * Should call SetLastError() on failure.
125 * Doesn't support character devices, pipes, template files, or a
126 * lot of the 'attributes' flags yet.
128 HFILE32 WINAPI CreateFile32A(LPCSTR filename, DWORD access, DWORD sharing,
129 LPSECURITY_ATTRIBUTES security, DWORD creation,
130 DWORD attributes, HANDLE32 template)
132 int access_flags, create_flags;
133 HFILE32 to_dup = HFILE_ERROR32; /* handle to dup */
135 /* Translate the various flags to Unix-style.
137 access_flags = TranslateAccessFlags(access);
138 create_flags = TranslateCreationFlags(creation);
141 FIXME(file, "template handles not supported.\n");
144 return HFILE_ERROR32;
145 /* If the name starts with '\\?\' or '\\.\', ignore the first 4 chars.
147 if(!strncmp(filename, "\\\\?\\", 4) || !strncmp(filename, "\\\\.\\", 4))
149 if (filename[2] == '.')
150 return DEVICE_Open( filename+4, access_flags | create_flags );
153 if (!strncmp(filename, "UNC", 3))
155 FIXME(file, "UNC names not supported.\n");
156 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
157 return HFILE_ERROR32;
161 /* If the name still starts with '\\', it's a UNC name.
163 if(!strncmp(filename, "\\\\", 2))
165 FIXME(file, "UNC names not supported.\n");
166 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
167 return HFILE_ERROR32;
170 /* If the name is either CONIN$ or CONOUT$, give them duplicated stdin
171 * or stdout, respectively.
173 if(!strcmp(filename, "CONIN$"))
174 to_dup = GetStdHandle( STD_INPUT_HANDLE );
175 else if(!strcmp(filename, "CONOUT$"))
176 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
178 if(to_dup != HFILE_ERROR32)
181 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
182 &handle, access, FALSE, 0 ))
183 handle = HFILE_ERROR32;
186 return FILE_Open( filename, access_flags | create_flags );
190 /*************************************************************************
191 * CreateFile32W (KERNEL32.48)
193 HFILE32 WINAPI CreateFile32W(LPCWSTR filename, DWORD access, DWORD sharing,
194 LPSECURITY_ATTRIBUTES security, DWORD creation,
195 DWORD attributes, HANDLE32 template)
197 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
198 HFILE32 res = CreateFile32A( afn, access, sharing, security, creation,
199 attributes, template );
200 HeapFree( GetProcessHeap(), 0, afn );
204 static int TranslateAccessFlags(DWORD access_flags)
218 case (GENERIC_READ | GENERIC_WRITE):
226 static int TranslateCreationFlags(DWORD create_flags)
233 rc = O_CREAT | O_EXCL;
237 rc = O_CREAT | O_TRUNC;
248 case TRUNCATE_EXISTING:
257 /**************************************************************************
258 * SetFileAttributes16 (KERNEL.421)
260 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
262 return SetFileAttributes32A( lpFileName, attributes );
266 /**************************************************************************
267 * SetFileAttributes32A (KERNEL32.490)
269 BOOL32 WINAPI SetFileAttributes32A(LPCSTR lpFileName, DWORD attributes)
272 DOS_FULL_NAME full_name;
274 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
277 TRACE(file,"(%s,%lx)\n",lpFileName,attributes);
278 if (attributes & FILE_ATTRIBUTE_NORMAL) {
279 attributes &= ~FILE_ATTRIBUTE_NORMAL;
281 FIXME(file,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
282 lpFileName,attributes);
284 if(stat(full_name.long_name,&buf)==-1)
286 SetLastError(ErrnoToLastError(errno));
289 if (attributes & FILE_ATTRIBUTE_READONLY)
291 buf.st_mode &= ~0222; /* octal!, clear write permission bits */
292 attributes &= ~FILE_ATTRIBUTE_READONLY;
294 attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
296 FIXME(file,"(%s):%lx attribute(s) not implemented.\n",
297 lpFileName,attributes);
298 if (-1==chmod(full_name.long_name,buf.st_mode))
300 SetLastError(ErrnoToLastError(errno));
307 /**************************************************************************
308 * SetFileAttributes32W (KERNEL32.491)
310 BOOL32 WINAPI SetFileAttributes32W(LPCWSTR lpFileName, DWORD attributes)
312 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
313 BOOL32 res = SetFileAttributes32A( afn, attributes );
314 HeapFree( GetProcessHeap(), 0, afn );
319 /**************************************************************************
320 * SetFileApisToOEM (KERNEL32.645)
322 VOID WINAPI SetFileApisToOEM(void)
324 /*FIXME(file,"(): stub!\n");*/
328 /**************************************************************************
329 * SetFileApisToANSI (KERNEL32.644)
331 VOID WINAPI SetFileApisToANSI(void)
333 /*FIXME(file,"(): stub\n");*/
337 /******************************************************************************
338 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
341 * TRUE: Set of file functions is using ANSI code page
342 * FALSE: Set of file functions is using OEM code page
344 BOOL32 WINAPI AreFileApisANSI(void)
346 FIXME(file,"(void): stub\n");