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
);
30 static int TranslateShareFlags(DWORD share_flags
);
32 /***********************************************************************
33 * WriteFile (KERNEL32.578)
35 BOOL32 WINAPI
WriteFile(HANDLE32 hFile
, LPCVOID lpBuffer
,
36 DWORD numberOfBytesToWrite
,
37 LPDWORD numberOfBytesWritten
, LPOVERLAPPED lpOverlapped
)
40 BOOL32 status
= FALSE
;
42 TRACE(file
, "%d %p %ld\n", hFile
, lpBuffer
,
43 numberOfBytesToWrite
);
45 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), hFile
,
46 K32OBJ_UNKNOWN
, 0, NULL
)))
48 if (K32OBJ_OPS(ioptr
)->write
)
49 status
= K32OBJ_OPS(ioptr
)->write(ioptr
, lpBuffer
, numberOfBytesToWrite
,
50 numberOfBytesWritten
, lpOverlapped
);
51 K32OBJ_DecCount( ioptr
);
55 /***********************************************************************
56 * ReadFile (KERNEL32.428)
58 BOOL32 WINAPI
ReadFile(HANDLE32 hFile
, LPVOID lpBuffer
, DWORD numberOfBytesToRead
,
59 LPDWORD numberOfBytesRead
, LPOVERLAPPED lpOverlapped
)
62 BOOL32 status
= FALSE
;
64 TRACE(file
, "%d %p %ld\n", hFile
, lpBuffer
,
67 if (!(ioptr
= HANDLE_GetObjPtr( PROCESS_Current(), hFile
,
68 K32OBJ_UNKNOWN
, 0, NULL
)))
70 if (K32OBJ_OPS(ioptr
)->read
)
71 status
= K32OBJ_OPS(ioptr
)->read(ioptr
, lpBuffer
, numberOfBytesToRead
,
72 numberOfBytesRead
, lpOverlapped
);
73 K32OBJ_DecCount( ioptr
);
78 /***********************************************************************
79 * ReadFileEx (KERNEL32.)
83 (CALLBACK
*LPOVERLAPPED_COMPLETION_ROUTINE
)(
85 DWORD dwNumberOfBytesTransfered
,
86 LPOVERLAPPED lpOverlapped
89 BOOL32 WINAPI
ReadFileEx(HFILE32 hFile
, LPVOID lpBuffer
, DWORD numtoread
,
90 LPOVERLAPPED lpOverlapped
,
91 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
94 FIXME(file
, "file %d to buf %p num %ld %p func %p stub\n",
95 hFile
, lpBuffer
, numtoread
, lpOverlapped
, lpCompletionRoutine
);
96 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
101 /*************************************************************************
102 * CreateFile32A [KERNEL32.45] Creates or opens a file or other object
104 * Creates or opens an object, and returns a handle that can be used to
105 * access that object.
109 * filename [I] pointer to filename to be accessed
110 * access [I] access mode requested
111 * sharing [I] share mode
112 * security [I] pointer to security attributes
115 * template [I] handle to file with attributes to copy
118 * Success: Open handle to specified file
119 * Failure: INVALID_HANDLE_VALUE
122 * Should call SetLastError() on failure.
126 * Doesn't support character devices, pipes, template files, or a
127 * lot of the 'attributes' flags yet.
129 HFILE32 WINAPI
CreateFile32A(LPCSTR filename
, DWORD access
, DWORD sharing
,
130 LPSECURITY_ATTRIBUTES security
, DWORD creation
,
131 DWORD attributes
, HANDLE32
template)
133 int access_flags
, create_flags
, share_flags
;
134 HFILE32 to_dup
= HFILE_ERROR32
; /* handle to dup */
136 /* Translate the various flags to Unix-style.
138 access_flags
= TranslateAccessFlags(access
);
139 create_flags
= TranslateCreationFlags(creation
);
140 share_flags
= TranslateShareFlags(sharing
);
143 FIXME(file
, "template handles not supported.\n");
146 return HFILE_ERROR32
;
147 /* If the name starts with '\\?\' or '\\.\', ignore the first 4 chars.
149 if(!strncmp(filename
, "\\\\?\\", 4) || !strncmp(filename
, "\\\\.\\", 4))
151 if (filename
[2] == '.')
152 return DEVICE_Open( filename
+4, access_flags
| create_flags
);
155 if (!strncmp(filename
, "UNC", 3))
157 FIXME(file
, "UNC name (%s) not supported.\n",filename
);
158 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
159 return HFILE_ERROR32
;
163 /* If the name still starts with '\\', it's a UNC name.
165 if(!strncmp(filename
, "\\\\", 2))
167 FIXME(file
, "UNC names not supported.\n");
168 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
169 return HFILE_ERROR32
;
172 /* If the name is either CONIN$ or CONOUT$, give them duplicated stdin
173 * or stdout, respectively. The lower case version is also allowed. Most likely
174 * this should be a case ignore string compare.
176 if(!strcasecmp(filename
, "CONIN$"))
177 to_dup
= GetStdHandle( STD_INPUT_HANDLE
);
178 else if(!strcasecmp(filename
, "CONOUT$"))
179 to_dup
= GetStdHandle( STD_OUTPUT_HANDLE
);
181 if(to_dup
!= HFILE_ERROR32
)
184 if (!DuplicateHandle( GetCurrentProcess(), to_dup
, GetCurrentProcess(),
185 &handle
, access
, FALSE
, 0 ))
186 handle
= HFILE_ERROR32
;
189 return FILE_Open( filename
, access_flags
| create_flags
,share_flags
);
193 /*************************************************************************
194 * CreateFile32W (KERNEL32.48)
196 HFILE32 WINAPI
CreateFile32W(LPCWSTR filename
, DWORD access
, DWORD sharing
,
197 LPSECURITY_ATTRIBUTES security
, DWORD creation
,
198 DWORD attributes
, HANDLE32
template)
200 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
201 HFILE32 res
= CreateFile32A( afn
, access
, sharing
, security
, creation
,
202 attributes
, template );
203 HeapFree( GetProcessHeap(), 0, afn
);
207 static int TranslateAccessFlags(DWORD access_flags
)
221 case (GENERIC_READ
| GENERIC_WRITE
):
229 static int TranslateCreationFlags(DWORD create_flags
)
236 rc
= O_CREAT
| O_EXCL
;
240 rc
= O_CREAT
| O_TRUNC
;
251 case TRUNCATE_EXISTING
:
258 static int TranslateShareFlags(DWORD share_flags
)
260 OPEN_SHARE_DENYNONE FILE_SHARE_READ | FILE_SHARE_WRITE
261 OPEN_SHARE_DENYREAD FILE_SHARE_WRITE
262 OPEN_SHARE_DENYREADWRITE 0
263 OPEN_SHARE_DENYWRITE FILE_SHARE_READ
268 case FILE_SHARE_READ
| FILE_SHARE_WRITE
:
269 return OF_SHARE_DENY_NONE
;
270 case FILE_SHARE_WRITE
:
271 return OF_SHARE_DENY_READ
;
272 case FILE_SHARE_READ
:
273 return OF_SHARE_DENY_WRITE
;
275 return OF_SHARE_EXCLUSIVE
;
278 FIXME(file
,"unknown sharing flags 0x%04lx\n",share_flags
);
279 return OF_SHARE_EXCLUSIVE
;
281 /**************************************************************************
282 * SetFileAttributes16 (KERNEL.421)
284 BOOL16 WINAPI
SetFileAttributes16( LPCSTR lpFileName
, DWORD attributes
)
286 return SetFileAttributes32A( lpFileName
, attributes
);
290 /**************************************************************************
291 * SetFileAttributes32A (KERNEL32.490)
293 BOOL32 WINAPI
SetFileAttributes32A(LPCSTR lpFileName
, DWORD attributes
)
296 DOS_FULL_NAME full_name
;
298 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
))
301 TRACE(file
,"(%s,%lx)\n",lpFileName
,attributes
);
302 if (attributes
& FILE_ATTRIBUTE_NORMAL
) {
303 attributes
&= ~FILE_ATTRIBUTE_NORMAL
;
305 FIXME(file
,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
306 lpFileName
,attributes
);
308 if(stat(full_name
.long_name
,&buf
)==-1)
310 SetLastError(ErrnoToLastError(errno
));
313 if (attributes
& FILE_ATTRIBUTE_READONLY
)
315 buf
.st_mode
&= ~0222; /* octal!, clear write permission bits */
316 attributes
&= ~FILE_ATTRIBUTE_READONLY
;
318 attributes
&= ~(FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
320 FIXME(file
,"(%s):%lx attribute(s) not implemented.\n",
321 lpFileName
,attributes
);
322 if (-1==chmod(full_name
.long_name
,buf
.st_mode
))
324 SetLastError(ErrnoToLastError(errno
));
331 /**************************************************************************
332 * SetFileAttributes32W (KERNEL32.491)
334 BOOL32 WINAPI
SetFileAttributes32W(LPCWSTR lpFileName
, DWORD attributes
)
336 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
337 BOOL32 res
= SetFileAttributes32A( afn
, attributes
);
338 HeapFree( GetProcessHeap(), 0, afn
);
343 /**************************************************************************
344 * SetFileApisToOEM (KERNEL32.645)
346 VOID WINAPI
SetFileApisToOEM(void)
348 /*FIXME(file,"(): stub!\n");*/
352 /**************************************************************************
353 * SetFileApisToANSI (KERNEL32.644)
355 VOID WINAPI
SetFileApisToANSI(void)
357 /*FIXME(file,"(): stub\n");*/
361 /******************************************************************************
362 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
365 * TRUE: Set of file functions is using ANSI code page
366 * FALSE: Set of file functions is using OEM code page
368 BOOL32 WINAPI
AreFileApisANSI(void)
370 FIXME(file
,"(void): stub\n");