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 names not supported.\n");
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.
175 if(!strcmp(filename
, "CONIN$"))
176 to_dup
= GetStdHandle( STD_INPUT_HANDLE
);
177 else if(!strcmp(filename
, "CONOUT$"))
178 to_dup
= GetStdHandle( STD_OUTPUT_HANDLE
);
180 if(to_dup
!= HFILE_ERROR32
)
183 if (!DuplicateHandle( GetCurrentProcess(), to_dup
, GetCurrentProcess(),
184 &handle
, access
, FALSE
, 0 ))
185 handle
= HFILE_ERROR32
;
188 return FILE_Open( filename
, access_flags
| create_flags
,share_flags
);
192 /*************************************************************************
193 * CreateFile32W (KERNEL32.48)
195 HFILE32 WINAPI
CreateFile32W(LPCWSTR filename
, DWORD access
, DWORD sharing
,
196 LPSECURITY_ATTRIBUTES security
, DWORD creation
,
197 DWORD attributes
, HANDLE32
template)
199 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
200 HFILE32 res
= CreateFile32A( afn
, access
, sharing
, security
, creation
,
201 attributes
, template );
202 HeapFree( GetProcessHeap(), 0, afn
);
206 static int TranslateAccessFlags(DWORD access_flags
)
220 case (GENERIC_READ
| GENERIC_WRITE
):
228 static int TranslateCreationFlags(DWORD create_flags
)
235 rc
= O_CREAT
| O_EXCL
;
239 rc
= O_CREAT
| O_TRUNC
;
250 case TRUNCATE_EXISTING
:
257 static int TranslateShareFlags(DWORD share_flags
)
259 OPEN_SHARE_DENYNONE FILE_SHARE_READ | FILE_SHARE_WRITE
260 OPEN_SHARE_DENYREAD FILE_SHARE_WRITE
261 OPEN_SHARE_DENYREADWRITE 0
262 OPEN_SHARE_DENYWRITE FILE_SHARE_READ
267 case FILE_SHARE_READ
| FILE_SHARE_WRITE
:
268 return OF_SHARE_DENY_NONE
;
269 case FILE_SHARE_WRITE
:
270 return OF_SHARE_DENY_READ
;
271 case FILE_SHARE_READ
:
272 return OF_SHARE_DENY_WRITE
;
274 return OF_SHARE_EXCLUSIVE
;
277 FIXME(file
,"unknown sharing flags 0x%04lx\n",share_flags
);
278 return OF_SHARE_EXCLUSIVE
;
280 /**************************************************************************
281 * SetFileAttributes16 (KERNEL.421)
283 BOOL16 WINAPI
SetFileAttributes16( LPCSTR lpFileName
, DWORD attributes
)
285 return SetFileAttributes32A( lpFileName
, attributes
);
289 /**************************************************************************
290 * SetFileAttributes32A (KERNEL32.490)
292 BOOL32 WINAPI
SetFileAttributes32A(LPCSTR lpFileName
, DWORD attributes
)
295 DOS_FULL_NAME full_name
;
297 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
))
300 TRACE(file
,"(%s,%lx)\n",lpFileName
,attributes
);
301 if (attributes
& FILE_ATTRIBUTE_NORMAL
) {
302 attributes
&= ~FILE_ATTRIBUTE_NORMAL
;
304 FIXME(file
,"(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
305 lpFileName
,attributes
);
307 if(stat(full_name
.long_name
,&buf
)==-1)
309 SetLastError(ErrnoToLastError(errno
));
312 if (attributes
& FILE_ATTRIBUTE_READONLY
)
314 buf
.st_mode
&= ~0222; /* octal!, clear write permission bits */
315 attributes
&= ~FILE_ATTRIBUTE_READONLY
;
317 attributes
&= ~(FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
319 FIXME(file
,"(%s):%lx attribute(s) not implemented.\n",
320 lpFileName
,attributes
);
321 if (-1==chmod(full_name
.long_name
,buf
.st_mode
))
323 SetLastError(ErrnoToLastError(errno
));
330 /**************************************************************************
331 * SetFileAttributes32W (KERNEL32.491)
333 BOOL32 WINAPI
SetFileAttributes32W(LPCWSTR lpFileName
, DWORD attributes
)
335 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
336 BOOL32 res
= SetFileAttributes32A( afn
, attributes
);
337 HeapFree( GetProcessHeap(), 0, afn
);
342 /**************************************************************************
343 * SetFileApisToOEM (KERNEL32.645)
345 VOID WINAPI
SetFileApisToOEM(void)
347 /*FIXME(file,"(): stub!\n");*/
351 /**************************************************************************
352 * SetFileApisToANSI (KERNEL32.644)
354 VOID WINAPI
SetFileApisToANSI(void)
356 /*FIXME(file,"(): stub\n");*/
360 /******************************************************************************
361 * AreFileApisANSI [KERNEL32.105] Determines if file functions are using ANSI
364 * TRUE: Set of file functions is using ANSI code page
365 * FALSE: Set of file functions is using OEM code page
367 BOOL32 WINAPI
AreFileApisANSI(void)
369 FIXME(file
,"(void): stub\n");