Combobox should clear its current selection on response to
[wine/hacks.git] / files / file.c
blobb1b5a980656962f29aa8967b00cd4d3b64974e7a
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
7 * TODO:
8 * Fix the CopyFileEx methods to implement the "extended" functionality.
9 * Right now, they simply call the CopyFile method.
12 #include "config.h"
14 #include <assert.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #ifdef HAVE_SYS_ERRNO_H
22 #include <sys/errno.h>
23 #endif
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #include <sys/time.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <utime.h>
34 #include "winerror.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wine/winbase16.h"
38 #include "wine/port.h"
39 #include "drive.h"
40 #include "file.h"
41 #include "heap.h"
42 #include "msdos.h"
43 #include "task.h"
44 #include "wincon.h"
45 #include "debugtools.h"
47 #include "server.h"
49 DEFAULT_DEBUG_CHANNEL(file);
51 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
52 #define MAP_ANON MAP_ANONYMOUS
53 #endif
55 /* Size of per-process table of DOS handles */
56 #define DOS_TABLE_SIZE 256
58 static HANDLE dos_handles[DOS_TABLE_SIZE];
61 /***********************************************************************
62 * FILE_ConvertOFMode
64 * Convert OF_* mode into flags for CreateFile.
66 static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
68 switch(mode & 0x03)
70 case OF_READ: *access = GENERIC_READ; break;
71 case OF_WRITE: *access = GENERIC_WRITE; break;
72 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
73 default: *access = 0; break;
75 switch(mode & 0x70)
77 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
78 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
79 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
80 case OF_SHARE_DENY_NONE:
81 case OF_SHARE_COMPAT:
82 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
87 /***********************************************************************
88 * FILE_strcasecmp
90 * locale-independent case conversion for file I/O
92 int FILE_strcasecmp( const char *str1, const char *str2 )
94 for (;;)
96 int ret = FILE_toupper(*str1) - FILE_toupper(*str2);
97 if (ret || !*str1) return ret;
98 str1++;
99 str2++;
104 /***********************************************************************
105 * FILE_strncasecmp
107 * locale-independent case conversion for file I/O
109 int FILE_strncasecmp( const char *str1, const char *str2, int len )
111 int ret = 0;
112 for ( ; len > 0; len--, str1++, str2++)
113 if ((ret = FILE_toupper(*str1) - FILE_toupper(*str2)) || !*str1) break;
114 return ret;
118 /***********************************************************************
119 * FILE_SetDosError
121 * Set the DOS error code from errno.
123 void FILE_SetDosError(void)
125 int save_errno = errno; /* errno gets overwritten by printf */
127 TRACE("errno = %d %s\n", errno, strerror(errno));
128 switch (save_errno)
130 case EAGAIN:
131 SetLastError( ERROR_SHARING_VIOLATION );
132 break;
133 case EBADF:
134 SetLastError( ERROR_INVALID_HANDLE );
135 break;
136 case ENOSPC:
137 SetLastError( ERROR_HANDLE_DISK_FULL );
138 break;
139 case EACCES:
140 case EPERM:
141 case EROFS:
142 SetLastError( ERROR_ACCESS_DENIED );
143 break;
144 case EBUSY:
145 SetLastError( ERROR_LOCK_VIOLATION );
146 break;
147 case ENOENT:
148 SetLastError( ERROR_FILE_NOT_FOUND );
149 break;
150 case EISDIR:
151 SetLastError( ERROR_CANNOT_MAKE );
152 break;
153 case ENFILE:
154 case EMFILE:
155 SetLastError( ERROR_NO_MORE_FILES );
156 break;
157 case EEXIST:
158 SetLastError( ERROR_FILE_EXISTS );
159 break;
160 case EINVAL:
161 case ESPIPE:
162 SetLastError( ERROR_SEEK );
163 break;
164 case ENOTEMPTY:
165 SetLastError( ERROR_DIR_NOT_EMPTY );
166 break;
167 case ENOEXEC:
168 SetLastError( ERROR_BAD_FORMAT );
169 break;
170 default:
171 WARN( "unknown file error: %s", strerror(save_errno) );
172 SetLastError( ERROR_GEN_FAILURE );
173 break;
175 errno = save_errno;
179 /***********************************************************************
180 * FILE_DupUnixHandle
182 * Duplicate a Unix handle into a task handle.
184 HANDLE FILE_DupUnixHandle( int fd, DWORD access )
186 struct alloc_file_handle_request *req = get_req_buffer();
187 req->access = access;
188 server_call_fd( REQ_ALLOC_FILE_HANDLE, fd );
189 return req->handle;
193 /***********************************************************************
194 * FILE_GetUnixHandle
196 * Retrieve the Unix handle corresponding to a file handle.
198 int FILE_GetUnixHandle( HANDLE handle, DWORD access )
200 int ret, fd = -1;
201 SERVER_START_REQ
203 struct get_handle_fd_request *req = wine_server_alloc_req( sizeof(*req), 0 );
204 req->handle = handle;
205 req->access = access;
206 if (!(ret = server_call( REQ_GET_HANDLE_FD ))) fd = req->fd;
208 SERVER_END_REQ;
209 if (!ret)
211 if (fd == -1) return wine_server_recv_fd( handle, 1 );
212 fd = dup(fd);
214 return fd;
218 /*************************************************************************
219 * FILE_OpenConsole
221 * Open a handle to the current process console.
222 * Returns 0 on failure.
224 static HANDLE FILE_OpenConsole( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
226 HANDLE ret;
228 SERVER_START_REQ
230 struct open_console_request *req = server_alloc_req( sizeof(*req), 0 );
232 req->output = output;
233 req->access = access;
234 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
235 SetLastError(0);
236 server_call( REQ_OPEN_CONSOLE );
237 ret = req->handle;
239 SERVER_END_REQ;
240 return ret;
244 /***********************************************************************
245 * FILE_CreateFile
247 * Implementation of CreateFile. Takes a Unix path name.
248 * Returns 0 on failure.
250 HANDLE FILE_CreateFile( LPCSTR filename, DWORD access, DWORD sharing,
251 LPSECURITY_ATTRIBUTES sa, DWORD creation,
252 DWORD attributes, HANDLE template, BOOL fail_read_only )
254 DWORD err;
255 HANDLE ret;
256 size_t len = strlen(filename);
258 if (len > REQUEST_MAX_VAR_SIZE)
260 FIXME("filename '%s' too long\n", filename );
261 SetLastError( ERROR_INVALID_PARAMETER );
262 return 0;
265 restart:
266 SERVER_START_REQ
268 struct create_file_request *req = server_alloc_req( sizeof(*req), len );
269 req->access = access;
270 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
271 req->sharing = sharing;
272 req->create = creation;
273 req->attrs = attributes;
274 memcpy( server_data_ptr(req), filename, len );
275 SetLastError(0);
276 err = server_call( REQ_CREATE_FILE );
277 ret = req->handle;
279 SERVER_END_REQ;
281 /* If write access failed, retry without GENERIC_WRITE */
283 if (!ret && !fail_read_only && (access & GENERIC_WRITE))
285 if ((err == STATUS_MEDIA_WRITE_PROTECTED) || (err == STATUS_ACCESS_DENIED))
287 TRACE("Write access failed for file '%s', trying without "
288 "write access\n", filename);
289 access &= ~GENERIC_WRITE;
290 goto restart;
294 if (!ret)
295 WARN("Unable to create file '%s' (GLE %ld)\n", filename,
296 GetLastError());
298 return ret;
302 /***********************************************************************
303 * FILE_CreateDevice
305 * Same as FILE_CreateFile but for a device
306 * Returns 0 on failure.
308 HANDLE FILE_CreateDevice( int client_id, DWORD access, LPSECURITY_ATTRIBUTES sa )
310 HANDLE ret;
311 SERVER_START_REQ
313 struct create_device_request *req = server_alloc_req( sizeof(*req), 0 );
315 req->access = access;
316 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
317 req->id = client_id;
318 SetLastError(0);
319 server_call( REQ_CREATE_DEVICE );
320 ret = req->handle;
322 SERVER_END_REQ;
323 return ret;
327 /*************************************************************************
328 * CreateFileA [KERNEL32.45] Creates or opens a file or other object
330 * Creates or opens an object, and returns a handle that can be used to
331 * access that object.
333 * PARAMS
335 * filename [I] pointer to filename to be accessed
336 * access [I] access mode requested
337 * sharing [I] share mode
338 * sa [I] pointer to security attributes
339 * creation [I] how to create the file
340 * attributes [I] attributes for newly created file
341 * template [I] handle to file with extended attributes to copy
343 * RETURNS
344 * Success: Open handle to specified file
345 * Failure: INVALID_HANDLE_VALUE
347 * NOTES
348 * Should call SetLastError() on failure.
350 * BUGS
352 * Doesn't support character devices, pipes, template files, or a
353 * lot of the 'attributes' flags yet.
355 HANDLE WINAPI CreateFileA( LPCSTR filename, DWORD access, DWORD sharing,
356 LPSECURITY_ATTRIBUTES sa, DWORD creation,
357 DWORD attributes, HANDLE template )
359 DOS_FULL_NAME full_name;
360 HANDLE ret;
362 if (!filename)
364 SetLastError( ERROR_INVALID_PARAMETER );
365 return INVALID_HANDLE_VALUE;
367 TRACE("%s %s%s%s%s%s%s%s\n",filename,
368 ((access & GENERIC_READ)==GENERIC_READ)?"GENERIC_READ ":"",
369 ((access & GENERIC_WRITE)==GENERIC_WRITE)?"GENERIC_WRITE ":"",
370 (!access)?"QUERY_ACCESS ":"",
371 ((sharing & FILE_SHARE_READ)==FILE_SHARE_READ)?"FILE_SHARE_READ ":"",
372 ((sharing & FILE_SHARE_WRITE)==FILE_SHARE_WRITE)?"FILE_SHARE_WRITE ":"",
373 ((sharing & FILE_SHARE_DELETE)==FILE_SHARE_DELETE)?"FILE_SHARE_DELETE ":"",
374 (creation ==CREATE_NEW)?"CREATE_NEW":
375 (creation ==CREATE_ALWAYS)?"CREATE_ALWAYS ":
376 (creation ==OPEN_EXISTING)?"OPEN_EXISTING ":
377 (creation ==OPEN_ALWAYS)?"OPEN_ALWAYS ":
378 (creation ==TRUNCATE_EXISTING)?"TRUNCATE_EXISTING ":"");
380 /* If the name starts with '\\?\', ignore the first 4 chars. */
381 if (!strncmp(filename, "\\\\?\\", 4))
383 filename += 4;
384 if (!strncmp(filename, "UNC\\", 4))
386 FIXME("UNC name (%s) not supported.\n", filename );
387 SetLastError( ERROR_PATH_NOT_FOUND );
388 return INVALID_HANDLE_VALUE;
392 if (!strncmp(filename, "\\\\.\\", 4)) {
393 if (!DOSFS_GetDevice( filename ))
395 ret = DEVICE_Open( filename+4, access, sa );
396 goto done;
398 else
399 filename+=4; /* fall into DOSFS_Device case below */
402 /* If the name still starts with '\\', it's a UNC name. */
403 if (!strncmp(filename, "\\\\", 2))
405 FIXME("UNC name (%s) not supported.\n", filename );
406 SetLastError( ERROR_PATH_NOT_FOUND );
407 return INVALID_HANDLE_VALUE;
410 /* If the name contains a DOS wild card (* or ?), do no create a file */
411 if(strchr(filename,'*') || strchr(filename,'?'))
412 return INVALID_HANDLE_VALUE;
414 /* Open a console for CONIN$ or CONOUT$ */
415 if (!strcasecmp(filename, "CONIN$"))
417 ret = FILE_OpenConsole( FALSE, access, sa );
418 goto done;
420 if (!strcasecmp(filename, "CONOUT$"))
422 ret = FILE_OpenConsole( TRUE, access, sa );
423 goto done;
426 if (DOSFS_GetDevice( filename ))
428 TRACE("opening device '%s'\n", filename );
430 if (!(ret = DOSFS_OpenDevice( filename, access )))
432 /* Do not silence this please. It is a critical error. -MM */
433 ERR("Couldn't open device '%s'!\n",filename);
434 SetLastError( ERROR_FILE_NOT_FOUND );
436 goto done;
439 /* check for filename, don't check for last entry if creating */
440 if (!DOSFS_GetFullName( filename,
441 (creation == OPEN_EXISTING) ||
442 (creation == TRUNCATE_EXISTING),
443 &full_name )) {
444 WARN("Unable to get full filename from '%s' (GLE %ld)\n",
445 filename, GetLastError());
446 return INVALID_HANDLE_VALUE;
449 ret = FILE_CreateFile( full_name.long_name, access, sharing,
450 sa, creation, attributes, template,
451 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
452 done:
453 if (!ret) ret = INVALID_HANDLE_VALUE;
454 return ret;
459 /*************************************************************************
460 * CreateFileW (KERNEL32.48)
462 HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
463 LPSECURITY_ATTRIBUTES sa, DWORD creation,
464 DWORD attributes, HANDLE template)
466 LPSTR afn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
467 HANDLE res = CreateFileA( afn, access, sharing, sa, creation, attributes, template );
468 HeapFree( GetProcessHeap(), 0, afn );
469 return res;
473 /***********************************************************************
474 * FILE_FillInfo
476 * Fill a file information from a struct stat.
478 static void FILE_FillInfo( struct stat *st, BY_HANDLE_FILE_INFORMATION *info )
480 if (S_ISDIR(st->st_mode))
481 info->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
482 else
483 info->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
484 if (!(st->st_mode & S_IWUSR))
485 info->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
487 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftCreationTime );
488 RtlSecondsSince1970ToTime( st->st_mtime, &info->ftLastWriteTime );
489 RtlSecondsSince1970ToTime( st->st_atime, &info->ftLastAccessTime );
491 info->dwVolumeSerialNumber = 0; /* FIXME */
492 info->nFileSizeHigh = 0;
493 info->nFileSizeLow = S_ISDIR(st->st_mode) ? 0 : st->st_size;
494 info->nNumberOfLinks = st->st_nlink;
495 info->nFileIndexHigh = 0;
496 info->nFileIndexLow = st->st_ino;
500 /***********************************************************************
501 * FILE_Stat
503 * Stat a Unix path name. Return TRUE if OK.
505 BOOL FILE_Stat( LPCSTR unixName, BY_HANDLE_FILE_INFORMATION *info )
507 struct stat st;
509 if (lstat( unixName, &st ) == -1)
511 FILE_SetDosError();
512 return FALSE;
514 if (!S_ISLNK(st.st_mode)) FILE_FillInfo( &st, info );
515 else
517 /* do a "real" stat to find out
518 about the type of the symlink destination */
519 if (stat( unixName, &st ) == -1)
521 FILE_SetDosError();
522 return FALSE;
524 FILE_FillInfo( &st, info );
525 info->dwFileAttributes |= FILE_ATTRIBUTE_SYMLINK;
527 return TRUE;
531 /***********************************************************************
532 * GetFileInformationByHandle (KERNEL32.219)
534 DWORD WINAPI GetFileInformationByHandle( HANDLE hFile,
535 BY_HANDLE_FILE_INFORMATION *info )
537 DWORD ret;
538 if (!info) return 0;
540 SERVER_START_REQ
542 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
543 req->handle = hFile;
544 if ((ret = !server_call( REQ_GET_FILE_INFO )))
546 RtlSecondsSince1970ToTime( req->write_time, &info->ftCreationTime );
547 RtlSecondsSince1970ToTime( req->write_time, &info->ftLastWriteTime );
548 RtlSecondsSince1970ToTime( req->access_time, &info->ftLastAccessTime );
549 info->dwFileAttributes = req->attr;
550 info->dwVolumeSerialNumber = req->serial;
551 info->nFileSizeHigh = req->size_high;
552 info->nFileSizeLow = req->size_low;
553 info->nNumberOfLinks = req->links;
554 info->nFileIndexHigh = req->index_high;
555 info->nFileIndexLow = req->index_low;
558 SERVER_END_REQ;
559 return ret;
563 /**************************************************************************
564 * GetFileAttributes16 (KERNEL.420)
566 DWORD WINAPI GetFileAttributes16( LPCSTR name )
568 return GetFileAttributesA( name );
572 /**************************************************************************
573 * GetFileAttributesA (KERNEL32.217)
575 DWORD WINAPI GetFileAttributesA( LPCSTR name )
577 DOS_FULL_NAME full_name;
578 BY_HANDLE_FILE_INFORMATION info;
580 if (name == NULL || *name=='\0') return -1;
582 if (!DOSFS_GetFullName( name, TRUE, &full_name )) return -1;
583 if (!FILE_Stat( full_name.long_name, &info )) return -1;
584 return info.dwFileAttributes;
588 /**************************************************************************
589 * GetFileAttributesW (KERNEL32.218)
591 DWORD WINAPI GetFileAttributesW( LPCWSTR name )
593 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
594 DWORD res = GetFileAttributesA( nameA );
595 HeapFree( GetProcessHeap(), 0, nameA );
596 return res;
600 /***********************************************************************
601 * GetFileSize (KERNEL32.220)
603 DWORD WINAPI GetFileSize( HANDLE hFile, LPDWORD filesizehigh )
605 BY_HANDLE_FILE_INFORMATION info;
606 if (!GetFileInformationByHandle( hFile, &info )) return 0;
607 if (filesizehigh) *filesizehigh = info.nFileSizeHigh;
608 return info.nFileSizeLow;
612 /***********************************************************************
613 * GetFileTime (KERNEL32.221)
615 BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
616 FILETIME *lpLastAccessTime,
617 FILETIME *lpLastWriteTime )
619 BY_HANDLE_FILE_INFORMATION info;
620 if (!GetFileInformationByHandle( hFile, &info )) return FALSE;
621 if (lpCreationTime) *lpCreationTime = info.ftCreationTime;
622 if (lpLastAccessTime) *lpLastAccessTime = info.ftLastAccessTime;
623 if (lpLastWriteTime) *lpLastWriteTime = info.ftLastWriteTime;
624 return TRUE;
627 /***********************************************************************
628 * CompareFileTime (KERNEL32.28)
630 INT WINAPI CompareFileTime( LPFILETIME x, LPFILETIME y )
632 if (!x || !y) return -1;
634 if (x->dwHighDateTime > y->dwHighDateTime)
635 return 1;
636 if (x->dwHighDateTime < y->dwHighDateTime)
637 return -1;
638 if (x->dwLowDateTime > y->dwLowDateTime)
639 return 1;
640 if (x->dwLowDateTime < y->dwLowDateTime)
641 return -1;
642 return 0;
645 /***********************************************************************
646 * FILE_GetTempFileName : utility for GetTempFileName
648 static UINT FILE_GetTempFileName( LPCSTR path, LPCSTR prefix, UINT unique,
649 LPSTR buffer, BOOL isWin16 )
651 static UINT unique_temp;
652 DOS_FULL_NAME full_name;
653 int i;
654 LPSTR p;
655 UINT num;
657 if ( !path || !prefix || !buffer ) return 0;
659 if (!unique_temp) unique_temp = time(NULL) & 0xffff;
660 num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
662 strcpy( buffer, path );
663 p = buffer + strlen(buffer);
665 /* add a \, if there isn't one and path is more than just the drive letter ... */
666 if ( !((strlen(buffer) == 2) && (buffer[1] == ':'))
667 && ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
669 if (isWin16) *p++ = '~';
670 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
671 sprintf( p, "%04x.tmp", num );
673 /* Now try to create it */
675 if (!unique)
679 HFILE handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
680 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
681 if (handle != INVALID_HANDLE_VALUE)
682 { /* We created it */
683 TRACE("created %s\n",
684 buffer);
685 CloseHandle( handle );
686 break;
688 if (GetLastError() != ERROR_FILE_EXISTS)
689 break; /* No need to go on */
690 num++;
691 sprintf( p, "%04x.tmp", num );
692 } while (num != (unique & 0xffff));
695 /* Get the full path name */
697 if (DOSFS_GetFullName( buffer, FALSE, &full_name ))
699 /* Check if we have write access in the directory */
700 if ((p = strrchr( full_name.long_name, '/' ))) *p = '\0';
701 if (access( full_name.long_name, W_OK ) == -1)
702 WARN("returns '%s', which doesn't seem to be writeable.\n",
703 buffer);
705 TRACE("returning %s\n", buffer );
706 return unique ? unique : num;
710 /***********************************************************************
711 * GetTempFileNameA (KERNEL32.290)
713 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
714 LPSTR buffer)
716 return FILE_GetTempFileName(path, prefix, unique, buffer, FALSE);
719 /***********************************************************************
720 * GetTempFileNameW (KERNEL32.291)
722 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
723 LPWSTR buffer )
725 LPSTR patha,prefixa;
726 char buffera[144];
727 UINT ret;
729 if (!path) return 0;
730 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
731 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
732 ret = FILE_GetTempFileName( patha, prefixa, unique, buffera, FALSE );
733 MultiByteToWideChar( CP_ACP, 0, buffera, -1, buffer, MAX_PATH );
734 HeapFree( GetProcessHeap(), 0, patha );
735 HeapFree( GetProcessHeap(), 0, prefixa );
736 return ret;
740 /***********************************************************************
741 * GetTempFileName16 (KERNEL.97)
743 UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
744 LPSTR buffer )
746 char temppath[144];
748 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
749 drive |= DRIVE_GetCurrentDrive() + 'A';
751 if ((drive & TF_FORCEDRIVE) &&
752 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
754 drive &= ~TF_FORCEDRIVE;
755 WARN("invalid drive %d specified\n", drive );
758 if (drive & TF_FORCEDRIVE)
759 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
760 else
761 GetTempPathA( 132, temppath );
762 return (UINT16)FILE_GetTempFileName( temppath, prefix, unique, buffer, TRUE );
765 /***********************************************************************
766 * FILE_DoOpenFile
768 * Implementation of OpenFile16() and OpenFile32().
770 static HFILE FILE_DoOpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode,
771 BOOL win32 )
773 HFILE hFileRet;
774 FILETIME filetime;
775 WORD filedatetime[2];
776 DOS_FULL_NAME full_name;
777 DWORD access, sharing;
778 char *p;
780 if (!ofs) return HFILE_ERROR;
782 TRACE("%s %s %s %s%s%s%s%s%s%s%s%s\n",name,
783 ((mode & 0x3 )==OF_READ)?"OF_READ":
784 ((mode & 0x3 )==OF_WRITE)?"OF_WRITE":
785 ((mode & 0x3 )==OF_READWRITE)?"OF_READWRITE":"unknown",
786 ((mode & 0x70 )==OF_SHARE_COMPAT)?"OF_SHARE_COMPAT":
787 ((mode & 0x70 )==OF_SHARE_DENY_NONE)?"OF_SHARE_DENY_NONE":
788 ((mode & 0x70 )==OF_SHARE_DENY_READ)?"OF_SHARE_DENY_READ":
789 ((mode & 0x70 )==OF_SHARE_DENY_WRITE)?"OF_SHARE_DENY_WRITE":
790 ((mode & 0x70 )==OF_SHARE_EXCLUSIVE)?"OF_SHARE_EXCLUSIVE":"unknown",
791 ((mode & OF_PARSE )==OF_PARSE)?"OF_PARSE ":"",
792 ((mode & OF_DELETE )==OF_DELETE)?"OF_DELETE ":"",
793 ((mode & OF_VERIFY )==OF_VERIFY)?"OF_VERIFY ":"",
794 ((mode & OF_SEARCH )==OF_SEARCH)?"OF_SEARCH ":"",
795 ((mode & OF_CANCEL )==OF_CANCEL)?"OF_CANCEL ":"",
796 ((mode & OF_CREATE )==OF_CREATE)?"OF_CREATE ":"",
797 ((mode & OF_PROMPT )==OF_PROMPT)?"OF_PROMPT ":"",
798 ((mode & OF_EXIST )==OF_EXIST)?"OF_EXIST ":"",
799 ((mode & OF_REOPEN )==OF_REOPEN)?"OF_REOPEN ":""
803 ofs->cBytes = sizeof(OFSTRUCT);
804 ofs->nErrCode = 0;
805 if (mode & OF_REOPEN) name = ofs->szPathName;
807 if (!name) {
808 ERR("called with `name' set to NULL ! Please debug.\n");
809 return HFILE_ERROR;
812 TRACE("%s %04x\n", name, mode );
814 /* the watcom 10.6 IDE relies on a valid path returned in ofs->szPathName
815 Are there any cases where getting the path here is wrong?
816 Uwe Bonnes 1997 Apr 2 */
817 if (!GetFullPathNameA( name, sizeof(ofs->szPathName),
818 ofs->szPathName, NULL )) goto error;
819 FILE_ConvertOFMode( mode, &access, &sharing );
821 /* OF_PARSE simply fills the structure */
823 if (mode & OF_PARSE)
825 ofs->fFixedDisk = (GetDriveType16( ofs->szPathName[0]-'A' )
826 != DRIVE_REMOVABLE);
827 TRACE("(%s): OF_PARSE, res = '%s'\n",
828 name, ofs->szPathName );
829 return 0;
832 /* OF_CREATE is completely different from all other options, so
833 handle it first */
835 if (mode & OF_CREATE)
837 if ((hFileRet = CreateFileA( name, GENERIC_READ | GENERIC_WRITE,
838 sharing, NULL, CREATE_ALWAYS,
839 FILE_ATTRIBUTE_NORMAL, -1 ))== INVALID_HANDLE_VALUE)
840 goto error;
841 goto success;
844 /* If OF_SEARCH is set, ignore the given path */
846 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
848 /* First try the file name as is */
849 if (DOSFS_GetFullName( name, TRUE, &full_name )) goto found;
850 /* Now remove the path */
851 if (name[0] && (name[1] == ':')) name += 2;
852 if ((p = strrchr( name, '\\' ))) name = p + 1;
853 if ((p = strrchr( name, '/' ))) name = p + 1;
854 if (!name[0]) goto not_found;
857 /* Now look for the file */
859 if (!DIR_SearchPath( NULL, name, NULL, &full_name, win32 )) goto not_found;
861 found:
862 TRACE("found %s = %s\n",
863 full_name.long_name, full_name.short_name );
864 lstrcpynA( ofs->szPathName, full_name.short_name,
865 sizeof(ofs->szPathName) );
867 if (mode & OF_SHARE_EXCLUSIVE)
868 /* Some InstallShield version uses OF_SHARE_EXCLUSIVE
869 on the file <tempdir>/_ins0432._mp to determine how
870 far installation has proceeded.
871 _ins0432._mp is an executable and while running the
872 application expects the open with OF_SHARE_ to fail*/
873 /* Probable FIXME:
874 As our loader closes the files after loading the executable,
875 we can't find the running executable with FILE_InUse.
876 Perhaps the loader should keep the file open.
877 Recheck against how Win handles that case */
879 char *last = strrchr(full_name.long_name,'/');
880 if (!last)
881 last = full_name.long_name - 1;
882 if (GetModuleHandle16(last+1))
884 TRACE("Denying shared open for %s\n",full_name.long_name);
885 return HFILE_ERROR;
889 if (mode & OF_DELETE)
891 if (unlink( full_name.long_name ) == -1) goto not_found;
892 TRACE("(%s): OF_DELETE return = OK\n", name);
893 return 1;
896 hFileRet = FILE_CreateFile( full_name.long_name, access, sharing,
897 NULL, OPEN_EXISTING, 0, 0,
898 DRIVE_GetFlags(full_name.drive) & DRIVE_FAIL_READ_ONLY );
899 if (!hFileRet) goto not_found;
901 GetFileTime( hFileRet, NULL, NULL, &filetime );
902 FileTimeToDosDateTime( &filetime, &filedatetime[0], &filedatetime[1] );
903 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
905 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
907 CloseHandle( hFileRet );
908 WARN("(%s): OF_VERIFY failed\n", name );
909 /* FIXME: what error here? */
910 SetLastError( ERROR_FILE_NOT_FOUND );
911 goto error;
914 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
916 success: /* We get here if the open was successful */
917 TRACE("(%s): OK, return = %d\n", name, hFileRet );
918 if (win32)
920 if (mode & OF_EXIST) /* Return the handle, but close it first */
921 CloseHandle( hFileRet );
923 else
925 hFileRet = Win32HandleToDosFileHandle( hFileRet );
926 if (hFileRet == HFILE_ERROR16) goto error;
927 if (mode & OF_EXIST) /* Return the handle, but close it first */
928 _lclose16( hFileRet );
930 return hFileRet;
932 not_found: /* We get here if the file does not exist */
933 WARN("'%s' not found or sharing violation\n", name );
934 SetLastError( ERROR_FILE_NOT_FOUND );
935 /* fall through */
937 error: /* We get here if there was an error opening the file */
938 ofs->nErrCode = GetLastError();
939 WARN("(%s): return = HFILE_ERROR error= %d\n",
940 name,ofs->nErrCode );
941 return HFILE_ERROR;
945 /***********************************************************************
946 * OpenFile16 (KERNEL.74)
948 HFILE16 WINAPI OpenFile16( LPCSTR name, OFSTRUCT *ofs, UINT16 mode )
950 return FILE_DoOpenFile( name, ofs, mode, FALSE );
954 /***********************************************************************
955 * OpenFile (KERNEL32.396)
957 HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
959 return FILE_DoOpenFile( name, ofs, mode, TRUE );
963 /***********************************************************************
964 * FILE_InitProcessDosHandles
966 * Allocates the default DOS handles for a process. Called either by
967 * Win32HandleToDosFileHandle below or by the DOSVM stuff.
969 static void FILE_InitProcessDosHandles( void )
971 dos_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
972 dos_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
973 dos_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
974 dos_handles[3] = GetStdHandle(STD_ERROR_HANDLE);
975 dos_handles[4] = GetStdHandle(STD_ERROR_HANDLE);
978 /***********************************************************************
979 * Win32HandleToDosFileHandle (KERNEL32.21)
981 * Allocate a DOS handle for a Win32 handle. The Win32 handle is no
982 * longer valid after this function (even on failure).
984 * Note: this is not exactly right, since on Win95 the Win32 handles
985 * are on top of DOS handles and we do it the other way
986 * around. Should be good enough though.
988 HFILE WINAPI Win32HandleToDosFileHandle( HANDLE handle )
990 int i;
992 if (!handle || (handle == INVALID_HANDLE_VALUE))
993 return HFILE_ERROR;
995 for (i = 5; i < DOS_TABLE_SIZE; i++)
996 if (!dos_handles[i])
998 dos_handles[i] = handle;
999 TRACE("Got %d for h32 %d\n", i, handle );
1000 return (HFILE)i;
1002 CloseHandle( handle );
1003 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1004 return HFILE_ERROR;
1008 /***********************************************************************
1009 * DosFileHandleToWin32Handle (KERNEL32.20)
1011 * Return the Win32 handle for a DOS handle.
1013 * Note: this is not exactly right, since on Win95 the Win32 handles
1014 * are on top of DOS handles and we do it the other way
1015 * around. Should be good enough though.
1017 HANDLE WINAPI DosFileHandleToWin32Handle( HFILE handle )
1019 HFILE16 hfile = (HFILE16)handle;
1020 if (hfile < 5 && !dos_handles[hfile]) FILE_InitProcessDosHandles();
1021 if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
1023 SetLastError( ERROR_INVALID_HANDLE );
1024 return INVALID_HANDLE_VALUE;
1026 return dos_handles[hfile];
1030 /***********************************************************************
1031 * DisposeLZ32Handle (KERNEL32.22)
1033 * Note: this is not entirely correct, we should only close the
1034 * 32-bit handle and not the 16-bit one, but we cannot do
1035 * this because of the way our DOS handles are implemented.
1036 * It shouldn't break anything though.
1038 void WINAPI DisposeLZ32Handle( HANDLE handle )
1040 int i;
1042 if (!handle || (handle == INVALID_HANDLE_VALUE)) return;
1044 for (i = 5; i < DOS_TABLE_SIZE; i++)
1045 if (dos_handles[i] == handle)
1047 dos_handles[i] = 0;
1048 CloseHandle( handle );
1049 break;
1054 /***********************************************************************
1055 * FILE_Dup2
1057 * dup2() function for DOS handles.
1059 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
1061 HANDLE new_handle;
1063 if (hFile1 < 5 && !dos_handles[hFile1]) FILE_InitProcessDosHandles();
1065 if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) || !dos_handles[hFile1])
1067 SetLastError( ERROR_INVALID_HANDLE );
1068 return HFILE_ERROR16;
1070 if (hFile2 < 5)
1072 FIXME("stdio handle closed, need proper conversion\n" );
1073 SetLastError( ERROR_INVALID_HANDLE );
1074 return HFILE_ERROR16;
1076 if (!DuplicateHandle( GetCurrentProcess(), dos_handles[hFile1],
1077 GetCurrentProcess(), &new_handle,
1078 0, FALSE, DUPLICATE_SAME_ACCESS ))
1079 return HFILE_ERROR16;
1080 if (dos_handles[hFile2]) CloseHandle( dos_handles[hFile2] );
1081 dos_handles[hFile2] = new_handle;
1082 return hFile2;
1086 /***********************************************************************
1087 * _lclose16 (KERNEL.81)
1089 HFILE16 WINAPI _lclose16( HFILE16 hFile )
1091 if (hFile < 5)
1093 FIXME("stdio handle closed, need proper conversion\n" );
1094 SetLastError( ERROR_INVALID_HANDLE );
1095 return HFILE_ERROR16;
1097 if ((hFile >= DOS_TABLE_SIZE) || !dos_handles[hFile])
1099 SetLastError( ERROR_INVALID_HANDLE );
1100 return HFILE_ERROR16;
1102 TRACE("%d (handle32=%d)\n", hFile, dos_handles[hFile] );
1103 CloseHandle( dos_handles[hFile] );
1104 dos_handles[hFile] = 0;
1105 return 0;
1109 /***********************************************************************
1110 * _lclose (KERNEL32.592)
1112 HFILE WINAPI _lclose( HFILE hFile )
1114 TRACE("handle %d\n", hFile );
1115 return CloseHandle( hFile ) ? 0 : HFILE_ERROR;
1118 /***********************************************************************
1119 * GetOverlappedResult (KERNEL32.360)
1121 * Check the result of an Asynchronous data transfer from a file.
1123 * RETURNS
1124 * TRUE on success
1125 * FALSE on failure
1127 * If successful (and relevant) lpTransfered will hold the number of
1128 * bytes transfered during the async operation.
1130 * BUGS
1132 * Currently only works for WaitCommEvent, ReadFile, WriteFile
1133 * with communications ports.
1136 BOOL WINAPI GetOverlappedResult(
1137 HANDLE hFile, /* [I] handle of file to check on */
1138 LPOVERLAPPED lpOverlapped, /* [I/O] pointer to overlapped */
1139 LPDWORD lpTransferred, /* [I/O] number of bytes transfered */
1140 BOOL bWait /* [I] wait for the transfer to complete ? */
1142 DWORD r;
1144 TRACE("(%d %p %p %x)\n", hFile, lpOverlapped, lpTransferred, bWait);
1146 if(lpOverlapped==NULL)
1148 ERR("lpOverlapped was null\n");
1149 return FALSE;
1151 if(!lpOverlapped->hEvent)
1153 ERR("lpOverlapped->hEvent was null\n");
1154 return FALSE;
1157 do {
1158 TRACE("waiting on %p\n",lpOverlapped);
1159 r = WaitForSingleObjectEx(lpOverlapped->hEvent, bWait?INFINITE:0, TRUE);
1160 TRACE("wait on %p returned %ld\n",lpOverlapped,r);
1161 } while (r==STATUS_USER_APC);
1163 if(lpTransferred)
1164 *lpTransferred = lpOverlapped->Offset;
1166 SetLastError(lpOverlapped->Internal);
1168 return (r==WAIT_OBJECT_0);
1171 /***********************************************************************
1172 * ReadFile (KERNEL32.428)
1174 BOOL WINAPI ReadFile( HANDLE hFile, LPVOID buffer, DWORD bytesToRead,
1175 LPDWORD bytesRead, LPOVERLAPPED overlapped )
1177 int unix_handle, result;
1179 TRACE("%d %p %ld\n", hFile, buffer, bytesToRead );
1181 if (bytesRead) *bytesRead = 0; /* Do this before anything else */
1182 if (!bytesToRead) return TRUE;
1184 if ( overlapped ) {
1185 SetLastError ( ERROR_INVALID_PARAMETER );
1186 return FALSE;
1189 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_READ );
1190 if (unix_handle == -1) return FALSE;
1191 while ((result = read( unix_handle, buffer, bytesToRead )) == -1)
1193 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1194 if ((errno == EFAULT) && !IsBadWritePtr( buffer, bytesToRead )) continue;
1195 FILE_SetDosError();
1196 break;
1198 close( unix_handle );
1199 if (result == -1) return FALSE;
1200 if (bytesRead) *bytesRead = result;
1201 return TRUE;
1205 /***********************************************************************
1206 * WriteFile (KERNEL32.578)
1208 BOOL WINAPI WriteFile( HANDLE hFile, LPCVOID buffer, DWORD bytesToWrite,
1209 LPDWORD bytesWritten, LPOVERLAPPED overlapped )
1211 int unix_handle, result;
1213 TRACE("%d %p %ld\n", hFile, buffer, bytesToWrite );
1215 if (bytesWritten) *bytesWritten = 0; /* Do this before anything else */
1216 if (!bytesToWrite) return TRUE;
1218 if ( overlapped ) {
1219 SetLastError ( ERROR_INVALID_PARAMETER );
1220 return FALSE;
1223 unix_handle = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
1224 if (unix_handle == -1) return FALSE;
1225 while ((result = write( unix_handle, buffer, bytesToWrite )) == -1)
1227 if ((errno == EAGAIN) || (errno == EINTR)) continue;
1228 if ((errno == EFAULT) && !IsBadReadPtr( buffer, bytesToWrite )) continue;
1229 if (errno == ENOSPC)
1230 SetLastError( ERROR_DISK_FULL );
1231 else
1232 FILE_SetDosError();
1233 break;
1235 close( unix_handle );
1236 if (result == -1) return FALSE;
1237 if (bytesWritten) *bytesWritten = result;
1238 return TRUE;
1242 /***********************************************************************
1243 * WIN16_hread
1245 LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
1247 LONG maxlen;
1249 TRACE("%d %08lx %ld\n",
1250 hFile, (DWORD)buffer, count );
1252 /* Some programs pass a count larger than the allocated buffer */
1253 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1254 if (count > maxlen) count = maxlen;
1255 return _lread(DosFileHandleToWin32Handle(hFile), MapSL(buffer), count );
1259 /***********************************************************************
1260 * WIN16_lread
1262 UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
1264 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
1268 /***********************************************************************
1269 * _lread (KERNEL32.596)
1271 UINT WINAPI _lread( HFILE handle, LPVOID buffer, UINT count )
1273 DWORD result;
1274 if (!ReadFile( handle, buffer, count, &result, NULL )) return -1;
1275 return result;
1279 /***********************************************************************
1280 * _lread16 (KERNEL.82)
1282 UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
1284 return (UINT16)_lread(DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1288 /***********************************************************************
1289 * _lcreat16 (KERNEL.83)
1291 HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
1293 return Win32HandleToDosFileHandle( _lcreat( path, attr ) );
1297 /***********************************************************************
1298 * _lcreat (KERNEL32.593)
1300 HFILE WINAPI _lcreat( LPCSTR path, INT attr )
1302 /* Mask off all flags not explicitly allowed by the doc */
1303 attr &= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1304 TRACE("%s %02x\n", path, attr );
1305 return CreateFileA( path, GENERIC_READ | GENERIC_WRITE,
1306 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1307 CREATE_ALWAYS, attr, -1 );
1311 /***********************************************************************
1312 * SetFilePointer (KERNEL32.492)
1314 DWORD WINAPI SetFilePointer( HANDLE hFile, LONG distance, LONG *highword,
1315 DWORD method )
1317 DWORD ret = 0xffffffff;
1319 if (highword &&
1320 ((distance >= 0 && *highword != 0) || (distance < 0 && *highword != -1)))
1322 FIXME("64-bit offsets not supported yet\n"
1323 "SetFilePointer(%08x,%08lx,%08lx,%08lx)\n",
1324 hFile,distance,*highword,method);
1325 SetLastError( ERROR_INVALID_PARAMETER );
1326 return ret;
1328 TRACE("handle %d offset %ld origin %ld\n",
1329 hFile, distance, method );
1331 SERVER_START_REQ
1333 struct set_file_pointer_request *req = server_alloc_req( sizeof(*req), 0 );
1334 req->handle = hFile;
1335 req->low = distance;
1336 req->high = highword ? *highword : (distance >= 0) ? 0 : -1;
1337 /* FIXME: assumes 1:1 mapping between Windows and Unix seek constants */
1338 req->whence = method;
1339 SetLastError( 0 );
1340 if (!server_call( REQ_SET_FILE_POINTER ))
1342 ret = req->new_low;
1343 if (highword) *highword = req->new_high;
1346 SERVER_END_REQ;
1347 return ret;
1351 /***********************************************************************
1352 * _llseek16 (KERNEL.84)
1354 * FIXME:
1355 * Seeking before the start of the file should be allowed for _llseek16,
1356 * but cause subsequent I/O operations to fail (cf. interrupt list)
1359 LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
1361 return SetFilePointer( DosFileHandleToWin32Handle(hFile), lOffset, NULL, nOrigin );
1365 /***********************************************************************
1366 * _llseek (KERNEL32.594)
1368 LONG WINAPI _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1370 return SetFilePointer( hFile, lOffset, NULL, nOrigin );
1374 /***********************************************************************
1375 * _lopen16 (KERNEL.85)
1377 HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
1379 return Win32HandleToDosFileHandle( _lopen( path, mode ) );
1383 /***********************************************************************
1384 * _lopen (KERNEL32.595)
1386 HFILE WINAPI _lopen( LPCSTR path, INT mode )
1388 DWORD access, sharing;
1390 TRACE("('%s',%04x)\n", path, mode );
1391 FILE_ConvertOFMode( mode, &access, &sharing );
1392 return CreateFileA( path, access, sharing, NULL, OPEN_EXISTING, 0, -1 );
1396 /***********************************************************************
1397 * _lwrite16 (KERNEL.86)
1399 UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
1401 return (UINT16)_hwrite( DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
1404 /***********************************************************************
1405 * _lwrite (KERNEL32.761)
1407 UINT WINAPI _lwrite( HFILE hFile, LPCSTR buffer, UINT count )
1409 return (UINT)_hwrite( hFile, buffer, (LONG)count );
1413 /***********************************************************************
1414 * _hread16 (KERNEL.349)
1416 LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
1418 return _lread( DosFileHandleToWin32Handle(hFile), buffer, count );
1422 /***********************************************************************
1423 * _hread (KERNEL32.590)
1425 LONG WINAPI _hread( HFILE hFile, LPVOID buffer, LONG count)
1427 return _lread( hFile, buffer, count );
1431 /***********************************************************************
1432 * _hwrite16 (KERNEL.350)
1434 LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
1436 return _hwrite( DosFileHandleToWin32Handle(hFile), buffer, count );
1440 /***********************************************************************
1441 * _hwrite (KERNEL32.591)
1443 * experimentation yields that _lwrite:
1444 * o truncates the file at the current position with
1445 * a 0 len write
1446 * o returns 0 on a 0 length write
1447 * o works with console handles
1450 LONG WINAPI _hwrite( HFILE handle, LPCSTR buffer, LONG count )
1452 DWORD result;
1454 TRACE("%d %p %ld\n", handle, buffer, count );
1456 if (!count)
1458 /* Expand or truncate at current position */
1459 if (!SetEndOfFile( handle )) return HFILE_ERROR;
1460 return 0;
1462 if (!WriteFile( handle, buffer, count, &result, NULL ))
1463 return HFILE_ERROR;
1464 return result;
1468 /***********************************************************************
1469 * SetHandleCount16 (KERNEL.199)
1471 UINT16 WINAPI SetHandleCount16( UINT16 count )
1473 HGLOBAL16 hPDB = GetCurrentPDB16();
1474 PDB16 *pdb = (PDB16 *)GlobalLock16( hPDB );
1475 BYTE *files = MapSL( pdb->fileHandlesPtr );
1477 TRACE("(%d)\n", count );
1479 if (count < 20) count = 20; /* No point in going below 20 */
1480 else if (count > 254) count = 254;
1482 if (count == 20)
1484 if (pdb->nbFiles > 20)
1486 memcpy( pdb->fileHandles, files, 20 );
1487 GlobalFree16( pdb->hFileHandles );
1488 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1489 GlobalHandleToSel16( hPDB ) );
1490 pdb->hFileHandles = 0;
1491 pdb->nbFiles = 20;
1494 else /* More than 20, need a new file handles table */
1496 BYTE *newfiles;
1497 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1498 if (!newhandle)
1500 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1501 return pdb->nbFiles;
1503 newfiles = (BYTE *)GlobalLock16( newhandle );
1505 if (count > pdb->nbFiles)
1507 memcpy( newfiles, files, pdb->nbFiles );
1508 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1510 else memcpy( newfiles, files, count );
1511 if (pdb->nbFiles > 20) GlobalFree16( pdb->hFileHandles );
1512 pdb->fileHandlesPtr = K32WOWGlobalLock16( newhandle );
1513 pdb->hFileHandles = newhandle;
1514 pdb->nbFiles = count;
1516 return pdb->nbFiles;
1520 /*************************************************************************
1521 * SetHandleCount (KERNEL32.494)
1523 UINT WINAPI SetHandleCount( UINT count )
1525 return min( 256, count );
1529 /***********************************************************************
1530 * FlushFileBuffers (KERNEL32.133)
1532 BOOL WINAPI FlushFileBuffers( HANDLE hFile )
1534 BOOL ret;
1535 SERVER_START_REQ
1537 struct flush_file_request *req = server_alloc_req( sizeof(*req), 0 );
1538 req->handle = hFile;
1539 ret = !server_call( REQ_FLUSH_FILE );
1541 SERVER_END_REQ;
1542 return ret;
1546 /**************************************************************************
1547 * SetEndOfFile (KERNEL32.483)
1549 BOOL WINAPI SetEndOfFile( HANDLE hFile )
1551 BOOL ret;
1552 SERVER_START_REQ
1554 struct truncate_file_request *req = server_alloc_req( sizeof(*req), 0 );
1555 req->handle = hFile;
1556 ret = !server_call( REQ_TRUNCATE_FILE );
1558 SERVER_END_REQ;
1559 return ret;
1563 /***********************************************************************
1564 * DeleteFile16 (KERNEL.146)
1566 BOOL16 WINAPI DeleteFile16( LPCSTR path )
1568 return DeleteFileA( path );
1572 /***********************************************************************
1573 * DeleteFileA (KERNEL32.71)
1575 BOOL WINAPI DeleteFileA( LPCSTR path )
1577 DOS_FULL_NAME full_name;
1579 TRACE("'%s'\n", path );
1581 if (!*path)
1583 ERR("Empty path passed\n");
1584 return FALSE;
1586 if (DOSFS_GetDevice( path ))
1588 WARN("cannot remove DOS device '%s'!\n", path);
1589 SetLastError( ERROR_FILE_NOT_FOUND );
1590 return FALSE;
1593 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
1594 if (unlink( full_name.long_name ) == -1)
1596 FILE_SetDosError();
1597 return FALSE;
1599 return TRUE;
1603 /***********************************************************************
1604 * DeleteFileW (KERNEL32.72)
1606 BOOL WINAPI DeleteFileW( LPCWSTR path )
1608 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1609 BOOL ret = DeleteFileA( xpath );
1610 HeapFree( GetProcessHeap(), 0, xpath );
1611 return ret;
1615 /***********************************************************************
1616 * GetFileType (KERNEL32.222)
1618 DWORD WINAPI GetFileType( HANDLE hFile )
1620 DWORD ret = FILE_TYPE_UNKNOWN;
1621 SERVER_START_REQ
1623 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
1624 req->handle = hFile;
1625 if (!server_call( REQ_GET_FILE_INFO )) ret = req->type;
1627 SERVER_END_REQ;
1628 return ret;
1632 /**************************************************************************
1633 * MoveFileExA (KERNEL32.???)
1635 BOOL WINAPI MoveFileExA( LPCSTR fn1, LPCSTR fn2, DWORD flag )
1637 DOS_FULL_NAME full_name1, full_name2;
1639 TRACE("(%s,%s,%04lx)\n", fn1, fn2, flag);
1641 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1643 if (fn2) /* !fn2 means delete fn1 */
1645 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 ))
1647 /* target exists, check if we may overwrite */
1648 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1650 /* FIXME: Use right error code */
1651 SetLastError( ERROR_ACCESS_DENIED );
1652 return FALSE;
1655 else if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1657 /* Source name and target path are valid */
1659 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1661 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1662 Perhaps we should queue these command and execute it
1663 when exiting... What about using on_exit(2)
1665 FIXME("Please move existing file '%s' to file '%s' when Wine has finished\n",
1666 full_name1.long_name, full_name2.long_name);
1667 return TRUE;
1670 if (full_name1.drive != full_name2.drive)
1672 /* use copy, if allowed */
1673 if (!(flag & MOVEFILE_COPY_ALLOWED))
1675 /* FIXME: Use right error code */
1676 SetLastError( ERROR_FILE_EXISTS );
1677 return FALSE;
1679 return CopyFileA( fn1, fn2, !(flag & MOVEFILE_REPLACE_EXISTING) );
1681 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1683 FILE_SetDosError();
1684 return FALSE;
1686 return TRUE;
1688 else /* fn2 == NULL means delete source */
1690 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1692 if (flag & MOVEFILE_COPY_ALLOWED) {
1693 WARN("Illegal flag\n");
1694 SetLastError( ERROR_GEN_FAILURE );
1695 return FALSE;
1697 /* FIXME: (bon@elektron.ikp.physik.th-darmstadt.de 970706)
1698 Perhaps we should queue these command and execute it
1699 when exiting... What about using on_exit(2)
1701 FIXME("Please delete file '%s' when Wine has finished\n",
1702 full_name1.long_name);
1703 return TRUE;
1706 if (unlink( full_name1.long_name ) == -1)
1708 FILE_SetDosError();
1709 return FALSE;
1711 return TRUE; /* successfully deleted */
1715 /**************************************************************************
1716 * MoveFileExW (KERNEL32.???)
1718 BOOL WINAPI MoveFileExW( LPCWSTR fn1, LPCWSTR fn2, DWORD flag )
1720 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1721 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1722 BOOL res = MoveFileExA( afn1, afn2, flag );
1723 HeapFree( GetProcessHeap(), 0, afn1 );
1724 HeapFree( GetProcessHeap(), 0, afn2 );
1725 return res;
1729 /**************************************************************************
1730 * MoveFileA (KERNEL32.387)
1732 * Move file or directory
1734 BOOL WINAPI MoveFileA( LPCSTR fn1, LPCSTR fn2 )
1736 DOS_FULL_NAME full_name1, full_name2;
1737 struct stat fstat;
1739 TRACE("(%s,%s)\n", fn1, fn2 );
1741 if (!DOSFS_GetFullName( fn1, TRUE, &full_name1 )) return FALSE;
1742 if (DOSFS_GetFullName( fn2, TRUE, &full_name2 )) {
1743 /* The new name must not already exist */
1744 SetLastError(ERROR_ALREADY_EXISTS);
1745 return FALSE;
1747 if (!DOSFS_GetFullName( fn2, FALSE, &full_name2 )) return FALSE;
1749 if (full_name1.drive == full_name2.drive) /* move */
1750 if (rename( full_name1.long_name, full_name2.long_name ) == -1)
1752 FILE_SetDosError();
1753 return FALSE;
1755 else return TRUE;
1756 else /*copy */ {
1757 if (stat( full_name1.long_name, &fstat ))
1759 WARN("Invalid source file %s\n",
1760 full_name1.long_name);
1761 FILE_SetDosError();
1762 return FALSE;
1764 if (S_ISDIR(fstat.st_mode)) {
1765 /* No Move for directories across file systems */
1766 /* FIXME: Use right error code */
1767 SetLastError( ERROR_GEN_FAILURE );
1768 return FALSE;
1770 else
1771 return CopyFileA(fn1, fn2, TRUE); /*fail, if exist */
1776 /**************************************************************************
1777 * MoveFileW (KERNEL32.390)
1779 BOOL WINAPI MoveFileW( LPCWSTR fn1, LPCWSTR fn2 )
1781 LPSTR afn1 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn1 );
1782 LPSTR afn2 = HEAP_strdupWtoA( GetProcessHeap(), 0, fn2 );
1783 BOOL res = MoveFileA( afn1, afn2 );
1784 HeapFree( GetProcessHeap(), 0, afn1 );
1785 HeapFree( GetProcessHeap(), 0, afn2 );
1786 return res;
1790 /**************************************************************************
1791 * CopyFileA (KERNEL32.36)
1793 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists )
1795 HFILE h1, h2;
1796 BY_HANDLE_FILE_INFORMATION info;
1797 UINT count;
1798 BOOL ret = FALSE;
1799 int mode;
1800 char buffer[2048];
1802 if ((h1 = _lopen( source, OF_READ )) == HFILE_ERROR) return FALSE;
1803 if (!GetFileInformationByHandle( h1, &info ))
1805 CloseHandle( h1 );
1806 return FALSE;
1808 mode = (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
1809 if ((h2 = CreateFileA( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1810 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
1811 info.dwFileAttributes, h1 )) == HFILE_ERROR)
1813 CloseHandle( h1 );
1814 return FALSE;
1816 while ((count = _lread( h1, buffer, sizeof(buffer) )) > 0)
1818 char *p = buffer;
1819 while (count > 0)
1821 INT res = _lwrite( h2, p, count );
1822 if (res <= 0) goto done;
1823 p += res;
1824 count -= res;
1827 ret = TRUE;
1828 done:
1829 CloseHandle( h1 );
1830 CloseHandle( h2 );
1831 return ret;
1835 /**************************************************************************
1836 * CopyFileW (KERNEL32.37)
1838 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists)
1840 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, source );
1841 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, dest );
1842 BOOL ret = CopyFileA( sourceA, destA, fail_if_exists );
1843 HeapFree( GetProcessHeap(), 0, sourceA );
1844 HeapFree( GetProcessHeap(), 0, destA );
1845 return ret;
1849 /**************************************************************************
1850 * CopyFileExA (KERNEL32.858)
1852 * This implementation ignores most of the extra parameters passed-in into
1853 * the "ex" version of the method and calls the CopyFile method.
1854 * It will have to be fixed eventually.
1856 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename,
1857 LPCSTR destFilename,
1858 LPPROGRESS_ROUTINE progressRoutine,
1859 LPVOID appData,
1860 LPBOOL cancelFlagPointer,
1861 DWORD copyFlags)
1863 BOOL failIfExists = FALSE;
1866 * Interpret the only flag that CopyFile can interpret.
1868 if ( (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0)
1870 failIfExists = TRUE;
1873 return CopyFileA(sourceFilename, destFilename, failIfExists);
1876 /**************************************************************************
1877 * CopyFileExW (KERNEL32.859)
1879 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename,
1880 LPCWSTR destFilename,
1881 LPPROGRESS_ROUTINE progressRoutine,
1882 LPVOID appData,
1883 LPBOOL cancelFlagPointer,
1884 DWORD copyFlags)
1886 LPSTR sourceA = HEAP_strdupWtoA( GetProcessHeap(), 0, sourceFilename );
1887 LPSTR destA = HEAP_strdupWtoA( GetProcessHeap(), 0, destFilename );
1889 BOOL ret = CopyFileExA(sourceA,
1890 destA,
1891 progressRoutine,
1892 appData,
1893 cancelFlagPointer,
1894 copyFlags);
1896 HeapFree( GetProcessHeap(), 0, sourceA );
1897 HeapFree( GetProcessHeap(), 0, destA );
1899 return ret;
1903 /***********************************************************************
1904 * SetFileTime (KERNEL32.650)
1906 BOOL WINAPI SetFileTime( HANDLE hFile,
1907 const FILETIME *lpCreationTime,
1908 const FILETIME *lpLastAccessTime,
1909 const FILETIME *lpLastWriteTime )
1911 BOOL ret;
1912 SERVER_START_REQ
1914 struct set_file_time_request *req = server_alloc_req( sizeof(*req), 0 );
1915 req->handle = hFile;
1916 if (lpLastAccessTime)
1917 RtlTimeToSecondsSince1970( lpLastAccessTime, (DWORD *)&req->access_time );
1918 else
1919 req->access_time = 0; /* FIXME */
1920 if (lpLastWriteTime)
1921 RtlTimeToSecondsSince1970( lpLastWriteTime, (DWORD *)&req->write_time );
1922 else
1923 req->write_time = 0; /* FIXME */
1924 ret = !server_call( REQ_SET_FILE_TIME );
1926 SERVER_END_REQ;
1927 return ret;
1931 /**************************************************************************
1932 * LockFile (KERNEL32.511)
1934 BOOL WINAPI LockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1935 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh )
1937 BOOL ret;
1938 SERVER_START_REQ
1940 struct lock_file_request *req = server_alloc_req( sizeof(*req), 0 );
1942 req->handle = hFile;
1943 req->offset_low = dwFileOffsetLow;
1944 req->offset_high = dwFileOffsetHigh;
1945 req->count_low = nNumberOfBytesToLockLow;
1946 req->count_high = nNumberOfBytesToLockHigh;
1947 ret = !server_call( REQ_LOCK_FILE );
1949 SERVER_END_REQ;
1950 return ret;
1953 /**************************************************************************
1954 * LockFileEx [KERNEL32.512]
1956 * Locks a byte range within an open file for shared or exclusive access.
1958 * RETURNS
1959 * success: TRUE
1960 * failure: FALSE
1961 * NOTES
1963 * Per Microsoft docs, the third parameter (reserved) must be set to 0.
1965 BOOL WINAPI LockFileEx( HANDLE hFile, DWORD flags, DWORD reserved,
1966 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1967 LPOVERLAPPED pOverlapped )
1969 FIXME("hFile=%d,flags=%ld,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
1970 hFile, flags, reserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
1971 pOverlapped);
1972 if (reserved == 0)
1973 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1974 else
1976 ERR("reserved == %ld: Supposed to be 0??\n", reserved);
1977 SetLastError(ERROR_INVALID_PARAMETER);
1980 return FALSE;
1984 /**************************************************************************
1985 * UnlockFile (KERNEL32.703)
1987 BOOL WINAPI UnlockFile( HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
1988 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh )
1990 BOOL ret;
1991 SERVER_START_REQ
1993 struct unlock_file_request *req = server_alloc_req( sizeof(*req), 0 );
1995 req->handle = hFile;
1996 req->offset_low = dwFileOffsetLow;
1997 req->offset_high = dwFileOffsetHigh;
1998 req->count_low = nNumberOfBytesToUnlockLow;
1999 req->count_high = nNumberOfBytesToUnlockHigh;
2000 ret = !server_call( REQ_UNLOCK_FILE );
2002 SERVER_END_REQ;
2003 return ret;
2007 /**************************************************************************
2008 * UnlockFileEx (KERNEL32.705)
2010 BOOL WINAPI UnlockFileEx(
2011 HFILE hFile,
2012 DWORD dwReserved,
2013 DWORD nNumberOfBytesToUnlockLow,
2014 DWORD nNumberOfBytesToUnlockHigh,
2015 LPOVERLAPPED lpOverlapped
2018 FIXME("hFile=%d,reserved=%ld,lowbytes=%ld,highbytes=%ld,overlapped=%p: stub.\n",
2019 hFile, dwReserved, nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh,
2020 lpOverlapped);
2021 if (dwReserved == 0)
2022 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2023 else
2025 ERR("reserved == %ld: Supposed to be 0??\n", dwReserved);
2026 SetLastError(ERROR_INVALID_PARAMETER);
2029 return FALSE;
2033 #if 0
2035 struct DOS_FILE_LOCK {
2036 struct DOS_FILE_LOCK * next;
2037 DWORD base;
2038 DWORD len;
2039 DWORD processId;
2040 FILE_OBJECT * dos_file;
2041 /* char * unix_name;*/
2044 typedef struct DOS_FILE_LOCK DOS_FILE_LOCK;
2046 static DOS_FILE_LOCK *locks = NULL;
2047 static void DOS_RemoveFileLocks(FILE_OBJECT *file);
2050 /* Locks need to be mirrored because unix file locking is based
2051 * on the pid. Inside of wine there can be multiple WINE processes
2052 * that share the same unix pid.
2053 * Read's and writes should check these locks also - not sure
2054 * how critical that is at this point (FIXME).
2057 static BOOL DOS_AddLock(FILE_OBJECT *file, struct flock *f)
2059 DOS_FILE_LOCK *curr;
2060 DWORD processId;
2062 processId = GetCurrentProcessId();
2064 /* check if lock overlaps a current lock for the same file */
2065 #if 0
2066 for (curr = locks; curr; curr = curr->next) {
2067 if (strcmp(curr->unix_name, file->unix_name) == 0) {
2068 if ((f->l_start == curr->base) && (f->l_len == curr->len))
2069 return TRUE;/* region is identic */
2070 if ((f->l_start < (curr->base + curr->len)) &&
2071 ((f->l_start + f->l_len) > curr->base)) {
2072 /* region overlaps */
2073 return FALSE;
2077 #endif
2079 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(DOS_FILE_LOCK) );
2080 curr->processId = GetCurrentProcessId();
2081 curr->base = f->l_start;
2082 curr->len = f->l_len;
2083 /* curr->unix_name = HEAP_strdupA( GetProcessHeap(), 0, file->unix_name);*/
2084 curr->next = locks;
2085 curr->dos_file = file;
2086 locks = curr;
2087 return TRUE;
2090 static void DOS_RemoveFileLocks(FILE_OBJECT *file)
2092 DWORD processId;
2093 DOS_FILE_LOCK **curr;
2094 DOS_FILE_LOCK *rem;
2096 processId = GetCurrentProcessId();
2097 curr = &locks;
2098 while (*curr) {
2099 if ((*curr)->dos_file == file) {
2100 rem = *curr;
2101 *curr = (*curr)->next;
2102 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2103 HeapFree( GetProcessHeap(), 0, rem );
2105 else
2106 curr = &(*curr)->next;
2110 static BOOL DOS_RemoveLock(FILE_OBJECT *file, struct flock *f)
2112 DWORD processId;
2113 DOS_FILE_LOCK **curr;
2114 DOS_FILE_LOCK *rem;
2116 processId = GetCurrentProcessId();
2117 for (curr = &locks; *curr; curr = &(*curr)->next) {
2118 if ((*curr)->processId == processId &&
2119 (*curr)->dos_file == file &&
2120 (*curr)->base == f->l_start &&
2121 (*curr)->len == f->l_len) {
2122 /* this is the same lock */
2123 rem = *curr;
2124 *curr = (*curr)->next;
2125 /* HeapFree( GetProcessHeap(), 0, rem->unix_name );*/
2126 HeapFree( GetProcessHeap(), 0, rem );
2127 return TRUE;
2130 /* no matching lock found */
2131 return FALSE;
2135 /**************************************************************************
2136 * LockFile (KERNEL32.511)
2138 BOOL WINAPI LockFile(
2139 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2140 DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh )
2142 struct flock f;
2143 FILE_OBJECT *file;
2145 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2146 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2147 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
2149 if (dwFileOffsetHigh || nNumberOfBytesToLockHigh) {
2150 FIXME("Unimplemented bytes > 32bits\n");
2151 return FALSE;
2154 f.l_start = dwFileOffsetLow;
2155 f.l_len = nNumberOfBytesToLockLow;
2156 f.l_whence = SEEK_SET;
2157 f.l_pid = 0;
2158 f.l_type = F_WRLCK;
2160 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2162 /* shadow locks internally */
2163 if (!DOS_AddLock(file, &f)) {
2164 SetLastError( ERROR_LOCK_VIOLATION );
2165 return FALSE;
2168 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2169 #ifdef USE_UNIX_LOCKS
2170 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2171 if (errno == EACCES || errno == EAGAIN) {
2172 SetLastError( ERROR_LOCK_VIOLATION );
2174 else {
2175 FILE_SetDosError();
2177 /* remove our internal copy of the lock */
2178 DOS_RemoveLock(file, &f);
2179 return FALSE;
2181 #endif
2182 return TRUE;
2186 /**************************************************************************
2187 * UnlockFile (KERNEL32.703)
2189 BOOL WINAPI UnlockFile(
2190 HFILE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,
2191 DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh )
2193 FILE_OBJECT *file;
2194 struct flock f;
2196 TRACE("handle %d offsetlow=%ld offsethigh=%ld nbyteslow=%ld nbyteshigh=%ld\n",
2197 hFile, dwFileOffsetLow, dwFileOffsetHigh,
2198 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
2200 if (dwFileOffsetHigh || nNumberOfBytesToUnlockHigh) {
2201 WARN("Unimplemented bytes > 32bits\n");
2202 return FALSE;
2205 f.l_start = dwFileOffsetLow;
2206 f.l_len = nNumberOfBytesToUnlockLow;
2207 f.l_whence = SEEK_SET;
2208 f.l_pid = 0;
2209 f.l_type = F_UNLCK;
2211 if (!(file = FILE_GetFile(hFile,0,NULL))) return FALSE;
2213 DOS_RemoveLock(file, &f); /* ok if fails - may be another wine */
2215 /* FIXME: Unix locking commented out for now, doesn't work with Excel */
2216 #ifdef USE_UNIX_LOCKS
2217 if (fcntl(file->unix_handle, F_SETLK, &f) == -1) {
2218 FILE_SetDosError();
2219 return FALSE;
2221 #endif
2222 return TRUE;
2224 #endif
2226 /**************************************************************************
2227 * GetFileAttributesExA [KERNEL32.874]
2229 BOOL WINAPI GetFileAttributesExA(
2230 LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2231 LPVOID lpFileInformation)
2233 DOS_FULL_NAME full_name;
2234 BY_HANDLE_FILE_INFORMATION info;
2236 if (lpFileName == NULL) return FALSE;
2237 if (lpFileInformation == NULL) return FALSE;
2239 if (fInfoLevelId == GetFileExInfoStandard) {
2240 LPWIN32_FILE_ATTRIBUTE_DATA lpFad =
2241 (LPWIN32_FILE_ATTRIBUTE_DATA) lpFileInformation;
2242 if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name )) return FALSE;
2243 if (!FILE_Stat( full_name.long_name, &info )) return FALSE;
2245 lpFad->dwFileAttributes = info.dwFileAttributes;
2246 lpFad->ftCreationTime = info.ftCreationTime;
2247 lpFad->ftLastAccessTime = info.ftLastAccessTime;
2248 lpFad->ftLastWriteTime = info.ftLastWriteTime;
2249 lpFad->nFileSizeHigh = info.nFileSizeHigh;
2250 lpFad->nFileSizeLow = info.nFileSizeLow;
2252 else {
2253 FIXME("invalid info level %d!\n", fInfoLevelId);
2254 return FALSE;
2257 return TRUE;
2261 /**************************************************************************
2262 * GetFileAttributesExW [KERNEL32.875]
2264 BOOL WINAPI GetFileAttributesExW(
2265 LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
2266 LPVOID lpFileInformation)
2268 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
2269 BOOL res =
2270 GetFileAttributesExA( nameA, fInfoLevelId, lpFileInformation);
2271 HeapFree( GetProcessHeap(), 0, nameA );
2272 return res;