2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
11 #include <sys/types.h>
29 static int TranslateCreationFlags(DWORD create_flags
);
30 static int TranslateAccessFlags(DWORD access_flags
);
31 int TranslateProtectionFlags(DWORD
);
42 /***********************************************************************
43 * OpenFileMappingA (KERNEL32.397)
47 HANDLE32
OpenFileMapping(DWORD access
, BOOL inherit
,const char *fname
)
53 /***********************************************************************
54 * CreateFileMapping32A (KERNEL32.46)
56 HANDLE32
CreateFileMapping32A(HANDLE32 h
,LPSECURITY_ATTRIBUTES ats
,
57 DWORD pot
, DWORD sh
, DWORD hlow
, LPCSTR lpName
)
59 FILEMAP_OBJECT
*filemap_obj
;
61 dprintf_win32(stddeb
,"CreateFileMapping32A(%08x,%p,%ld,%ld,%ld,%s)\n",
62 h
,ats
,pot
,sh
,hlow
,lpName
65 SetLastError(ErrnoToLastError(errno
));
66 return INVALID_HANDLE_VALUE32
;
68 filemap_obj
=(FILEMAP_OBJECT
*)HeapAlloc( SystemHeap
, 0,
69 sizeof(FILEMAP_OBJECT
) );
70 if(filemap_obj
== NULL
) {
71 SetLastError(ERROR_UNKNOWN
);
74 if (h
==INVALID_HANDLE_VALUE32
)
75 h
=_lcreat32(lpName
,1);/*FIXME*/
77 filemap_obj
->hfile
= h
;
78 filemap_obj
->prot
= TranslateProtectionFlags(pot
);
79 filemap_obj
->size
= hlow
;
80 return (HANDLE32
)filemap_obj
;;
83 /***********************************************************************
84 * CreateFileMapping32W (KERNEL32.47)
87 HANDLE32
CreateFileMapping32W(HANDLE32 h
,LPSECURITY_ATTRIBUTES ats
,
88 DWORD pot
, DWORD sh
, DWORD hlow
, LPCWSTR lpName
)
90 LPSTR aname
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpName
);
91 HANDLE32 res
= CreateFileMapping32A(h
,ats
,pot
,sh
,hlow
,aname
);
92 HeapFree( GetProcessHeap(), 0, aname
);
97 /***********************************************************************
98 * MapViewOfFile (KERNEL32.385)
100 LPVOID
MapViewOfFile(HANDLE32 handle
, DWORD access
, DWORD offhi
,
101 DWORD offlo
, DWORD size
)
103 return MapViewOfFileEx(handle
,access
,offhi
,offlo
,size
,0);
106 /***********************************************************************
107 * MapViewOfFileEx (KERNEL32.386)
110 LPVOID
MapViewOfFileEx(HANDLE32 handle
, DWORD access
, DWORD offhi
,
111 DWORD offlo
, DWORD size
, DWORD st
)
113 FILEMAP_OBJECT
*fmap
= (FILEMAP_OBJECT
*)handle
;
115 if (!size
) size
= fmap
->size
;
117 return mmap ((caddr_t
)st
, size
, fmap
->prot
,
118 MAP_ANON
|MAP_PRIVATE
,
119 FILE_GetUnixHandle(fmap
->hfile
),
123 /***********************************************************************
124 * UnmapViewOfFile (KERNEL32.385)
126 BOOL32
UnmapViewOfFile(LPVOID address
) {
127 munmap(address
,/*hmm*/1); /* FIXME: size? */
132 /***********************************************************************
133 * WriteFile (KERNEL32.578)
135 BOOL32
WriteFile(HFILE32 hFile
, LPVOID lpBuffer
, DWORD numberOfBytesToWrite
,
136 LPDWORD numberOfBytesWritten
, LPOVERLAPPED lpOverlapped
)
140 res
= _lwrite32(hFile
,lpBuffer
,numberOfBytesToWrite
);
142 SetLastError(ErrnoToLastError(errno
));
145 if(numberOfBytesWritten
)
146 *numberOfBytesWritten
= res
;
150 /***********************************************************************
151 * ReadFile (KERNEL32.428)
153 BOOL32
ReadFile(HFILE32 hFile
, LPVOID lpBuffer
, DWORD numtoread
,
154 LPDWORD numread
, LPOVERLAPPED lpOverlapped
)
158 actual_read
= _lread32(hFile
,lpBuffer
,numtoread
);
159 if(actual_read
== -1) {
160 SetLastError(ErrnoToLastError(errno
));
164 *numread
= actual_read
;
170 /*************************************************************************
171 * CreateFile32A (KERNEL32.45)
173 * Doesn't support character devices, pipes, template files, or a
174 * lot of the 'attributes' flags yet.
176 HFILE32
CreateFile32A(LPCSTR filename
, DWORD access
, DWORD sharing
,
177 LPSECURITY_ATTRIBUTES security
, DWORD creation
,
178 DWORD attributes
, HANDLE32
template)
180 int access_flags
, create_flags
;
182 /* Translate the various flags to Unix-style.
184 access_flags
= TranslateAccessFlags(access
);
185 create_flags
= TranslateCreationFlags(creation
);
188 dprintf_file(stddeb
, "CreateFile: template handles not supported.\n");
190 /* If the name starts with '\\?' or '\\.', ignore the first 3 chars.
192 if(!strncmp(filename
, "\\\\?", 3) || !strncmp(filename
, "\\\\.", 3))
195 /* If the name still starts with '\\', it's a UNC name.
197 if(!strncmp(filename
, "\\\\", 2))
199 dprintf_file(stddeb
, "CreateFile: UNC names not supported.\n");
200 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
201 return HFILE_ERROR32
;
204 /* If the name is either CONIN$ or CONOUT$, give them stdin
205 * or stdout, respectively.
207 if(!strcmp(filename
, "CONIN$")) return GetStdHandle( STD_INPUT_HANDLE
);
208 if(!strcmp(filename
, "CONOUT$")) return GetStdHandle( STD_OUTPUT_HANDLE
);
210 return FILE_Open( filename
, access_flags
| create_flags
);
214 /*************************************************************************
215 * CreateFile32W (KERNEL32.48)
217 HFILE32
CreateFile32W(LPCWSTR filename
, DWORD access
, DWORD sharing
,
218 LPSECURITY_ATTRIBUTES security
, DWORD creation
,
219 DWORD attributes
, HANDLE32
template)
221 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, filename
);
222 HFILE32 res
= CreateFile32A( afn
, access
, sharing
, security
, creation
,
223 attributes
, template );
224 HeapFree( GetProcessHeap(), 0, afn
);
228 static int TranslateAccessFlags(DWORD access_flags
)
242 case (GENERIC_READ
| GENERIC_WRITE
):
250 static int TranslateCreationFlags(DWORD create_flags
)
257 rc
= O_CREAT
| O_EXCL
;
261 rc
= O_CREAT
| O_TRUNC
;
272 case TRUNCATE_EXISTING
:
281 /**************************************************************************
282 * SetFileAttributes16 (KERNEL.421)
284 BOOL16
SetFileAttributes16( LPCSTR lpFileName
, DWORD attributes
)
286 return SetFileAttributes32A( lpFileName
, attributes
);
290 /**************************************************************************
291 * SetFileAttributes32A (KERNEL32.490)
293 BOOL32
SetFileAttributes32A(LPCSTR lpFileName
, DWORD attributes
)
296 DOS_FULL_NAME full_name
;
298 if (!DOSFS_GetFullName( lpFileName
, TRUE
, &full_name
))
301 dprintf_file(stddeb
,"SetFileAttributes(%s,%lx)\n",lpFileName
,attributes
);
302 if(stat(full_name
.long_name
,&buf
)==-1)
304 SetLastError(ErrnoToLastError(errno
));
307 if (attributes
& FILE_ATTRIBUTE_READONLY
)
309 buf
.st_mode
&= ~0222; /* octal!, clear write permission bits */
310 attributes
&= ~FILE_ATTRIBUTE_READONLY
;
312 attributes
&= ~(FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
314 fprintf(stdnimp
,"SetFileAttributesA(%s):%lx attribute(s) not implemented.\n",lpFileName
,attributes
);
315 if (-1==chmod(full_name
.long_name
,buf
.st_mode
))
317 SetLastError(ErrnoToLastError(errno
));
323 /**************************************************************************
324 * SetFileAttributes32W (KERNEL32.491)
326 BOOL32
SetFileAttributes32W(LPCWSTR lpFileName
, DWORD attributes
)
328 LPSTR afn
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
329 BOOL32 res
= SetFileAttributes32A( afn
, attributes
);
330 HeapFree( GetProcessHeap(), 0, afn
);
334 VOID
SetFileApisToOEM()
336 fprintf(stdnimp
,"SetFileApisToOEM(),stub!\n");
339 VOID
SetFileApisToANSI()
341 fprintf(stdnimp
,"SetFileApisToANSI(),stub!\n");
344 BOOL32
AreFileApisANSI()
346 fprintf(stdnimp
,"AreFileApisANSI(),stub!\n");
353 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
354 DWORD nNumberOfBytesToLockLow
,DWORD nNumberOfBytesToLockHigh
)
356 fprintf(stdnimp
,"LockFile(%d,0x%08lx%08lx,0x%08lx%08lx),stub!\n",
357 hFile
,dwFileOffsetHigh
,dwFileOffsetLow
,
358 nNumberOfBytesToLockHigh
,nNumberOfBytesToLockLow
365 HFILE32 hFile
,DWORD dwFileOffsetLow
,DWORD dwFileOffsetHigh
,
366 DWORD nNumberOfBytesToUnlockLow
,DWORD nNumberOfBytesToUnlockHigh
)
368 fprintf(stdnimp
,"UnlockFile(%d,0x%08lx%08lx,0x%08lx%08lx),stub!\n",
369 hFile
,dwFileOffsetHigh
,dwFileOffsetLow
,
370 nNumberOfBytesToUnlockHigh
,nNumberOfBytesToUnlockLow