Release 960928
[wine/multimedia.git] / files / file.c
blob2d786f24aa84603fe8925a6977bd67fc651553f8
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/errno.h>
15 #include <sys/stat.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <utime.h>
20 #include "windows.h"
21 #include "winerror.h"
22 #include "directory.h"
23 #include "dos_fs.h"
24 #include "drive.h"
25 #include "global.h"
26 #include "msdos.h"
27 #include "options.h"
28 #include "ldt.h"
29 #include "task.h"
30 #include "string32.h"
31 #include "stddebug.h"
32 #include "debug.h"
33 #include "xmalloc.h"
35 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
37 typedef struct tagDOS_FILE
39 struct tagDOS_FILE *next;
40 int count; /* Usage count (0 if free) */
41 int unix_handle;
42 int mode;
43 char *unix_name;
44 WORD filedate;
45 WORD filetime;
46 DWORD type; /* Type for win32 apps */
47 } DOS_FILE;
49 /* Global files array */
50 static DOS_FILE DOSFiles[MAX_OPEN_FILES];
52 static DOS_FILE *FILE_First = DOSFiles;
53 static DOS_FILE *FILE_LastUsed = DOSFiles;
55 /* Small file handles array for boot-up, before the first PDB is created */
56 #define MAX_BOOT_HANDLES 4
57 static BYTE bootFileHandles[MAX_BOOT_HANDLES] = { 0xff, 0xff, 0xff, 0xff };
59 /***********************************************************************
60 * FILE_Alloc
62 * Allocate a DOS file.
64 static DOS_FILE *FILE_Alloc(void)
66 DOS_FILE *file = FILE_First;
67 if (file) FILE_First = file->next;
68 else if (FILE_LastUsed >= &DOSFiles[MAX_OPEN_FILES-1])
70 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
71 return NULL;
73 else file = ++FILE_LastUsed;
74 file->count = 1;
75 file->unix_handle = -1;
76 file->unix_name = NULL;
77 file->type = FILE_TYPE_DISK;
78 return file;
82 /***********************************************************************
83 * FILE_Close
85 * Close a DOS file.
87 static BOOL FILE_Close( DOS_FILE *file )
89 if (!file->count) return FALSE;
90 if (--file->count > 0) return TRUE;
91 /* Now really close the file */
92 if (file->unix_handle != -1) close( file->unix_handle );
93 if (file->unix_name) free( file->unix_name );
94 file->next = FILE_First;
95 FILE_First = file;
96 return TRUE;
100 /***********************************************************************
101 * FILE_GetPDBFiles
103 * Return a pointer to the current PDB files array.
105 static void FILE_GetPDBFiles( BYTE **files, WORD *nbFiles )
107 PDB *pdb;
109 if ((pdb = (PDB *)GlobalLock16( GetCurrentPDB() )) != NULL)
111 *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
112 *nbFiles = pdb->nbFiles;
114 else
116 *files = bootFileHandles;
117 *nbFiles = MAX_BOOT_HANDLES;
122 /***********************************************************************
123 * FILE_AllocTaskHandle
125 * Allocate a task file handle for a DOS file.
127 static HFILE FILE_AllocTaskHandle( DOS_FILE *dos_file )
129 BYTE *files, *fp;
130 WORD i, nbFiles;
132 FILE_GetPDBFiles( &files, &nbFiles );
133 fp = files + 1; /* Don't use handle 0, as some programs don't like it */
134 for (i = nbFiles - 1; (i > 0) && (*fp != 0xff); i--, fp++);
135 if (!i)
136 { /* No more handles or files */
137 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
138 return -1;
140 *fp = dos_file ? (BYTE)(dos_file - DOSFiles) : 0;
141 dprintf_file(stddeb,
142 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
143 (fp - files), *fp, nbFiles - i, nbFiles );
144 return (HFILE)(fp - files);
148 /***********************************************************************
149 * FILE_FreeTaskHandle
151 * Free a per-task file handle.
153 static void FILE_FreeTaskHandle( HFILE handle )
155 BYTE *files;
156 WORD nbFiles;
158 FILE_GetPDBFiles( &files, &nbFiles );
159 dprintf_file( stddeb,"FILE_FreeTaskHandle: dos=%d file=%d\n",
160 handle, files[handle] );
161 if ((handle < 0) || (handle >= (INT)nbFiles))
163 fprintf( stderr, "FILE_FreeTaskHandle: invalid file handle %d\n",
164 handle );
165 return;
167 files[handle] = 0xff;
171 /***********************************************************************
172 * FILE_SetTaskHandle
174 * Set the value of a task handle (no error checking).
176 static void FILE_SetTaskHandle( HFILE handle, DOS_FILE *file )
178 BYTE *files;
179 WORD nbFiles;
181 FILE_GetPDBFiles( &files, &nbFiles );
182 files[handle] = (BYTE)(file - DOSFiles);
186 /***********************************************************************
187 * FILE_GetFile
189 * Return the DOS file associated to a task file handle.
191 static DOS_FILE *FILE_GetFile( HFILE handle )
193 BYTE *files;
194 WORD nbFiles;
195 DOS_FILE *file;
197 FILE_GetPDBFiles( &files, &nbFiles );
198 if ((handle < 0) || (handle >= (INT)nbFiles) ||
199 (files[handle] >= MAX_OPEN_FILES) ||
200 !(file = &DOSFiles[files[handle]])->count)
202 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
203 return NULL;
205 return file;
209 int FILE_GetUnixHandle( HFILE hFile )
211 DOS_FILE *file;
213 if (!(file = FILE_GetFile( hFile ))) return -1;
214 return file->unix_handle;
218 /***********************************************************************
219 * FILE_CloseAllFiles
221 * Close all open files of a given PDB. Used on task termination.
223 void FILE_CloseAllFiles( HANDLE16 hPDB )
225 BYTE *files;
226 WORD count;
227 PDB *pdb = (PDB *)GlobalLock16( hPDB );
229 if (!pdb) return;
230 files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
231 dprintf_file(stddeb,"FILE_CloseAllFiles: closing %d files\n",pdb->nbFiles);
232 for (count = pdb->nbFiles; count > 0; count--, files++)
234 if (*files < MAX_OPEN_FILES) FILE_Close( &DOSFiles[*files] );
235 *files = 0xff;
240 /***********************************************************************
241 * FILE_SetDosError
243 * Set the DOS error code from errno.
245 void FILE_SetDosError(void)
247 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
248 switch (errno)
250 case EAGAIN:
251 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
252 break;
253 case EBADF:
254 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
255 break;
256 case ENOSPC:
257 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
258 break;
259 case EACCES:
260 case EPERM:
261 case EROFS:
262 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
263 break;
264 case EBUSY:
265 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
266 break;
267 case ENOENT:
268 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
269 break;
270 case EISDIR:
271 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
272 break;
273 case ENFILE:
274 case EMFILE:
275 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
276 break;
277 case EEXIST:
278 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
279 break;
280 default:
281 perror( "int21: unknown errno" );
282 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
283 break;
288 /***********************************************************************
289 * FILE_DupUnixHandle
291 * Duplicate a Unix handle into a task handle.
293 HFILE FILE_DupUnixHandle( int fd )
295 HFILE handle;
296 DOS_FILE *file;
298 if (!(file = FILE_Alloc())) return HFILE_ERROR;
299 if ((file->unix_handle = dup(fd)) == -1)
301 FILE_SetDosError();
302 FILE_Close( file );
303 return HFILE_ERROR;
305 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
306 FILE_Close( file );
307 return handle;
311 /***********************************************************************
312 * FILE_OpenUnixFile
314 static DOS_FILE *FILE_OpenUnixFile( const char *name, int mode )
316 DOS_FILE *file;
317 struct stat st;
319 if (!(file = FILE_Alloc())) return NULL;
320 if ((file->unix_handle = open( name, mode, 0666 )) == -1)
322 if (Options.allowReadOnly && (mode == O_RDWR))
324 if ((file->unix_handle = open( name, O_RDONLY )) != -1)
325 fprintf( stderr, "Warning: could not open %s for writing, opening read-only.\n", name );
328 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
330 FILE_SetDosError();
331 FILE_Close( file );
332 return NULL;
334 if (S_ISDIR(st.st_mode))
336 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
337 FILE_Close( file );
338 return NULL;
341 /* File opened OK, now fill the DOS_FILE */
343 file->unix_name = xstrdup( name );
344 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
345 return file;
349 /***********************************************************************
350 * FILE_Open
352 HFILE FILE_Open( LPCSTR path, INT32 mode )
354 const char *unixName;
355 DOS_FILE *file;
356 HFILE handle;
358 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
359 if ((unixName = DOSFS_IsDevice( path )) != NULL)
361 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
362 if (!unixName[0]) /* Non-existing device */
364 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
365 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
366 return HFILE_ERROR;
369 else if (!(unixName = DOSFS_GetUnixFileName( path, TRUE )))
370 return HFILE_ERROR;
372 if (!(file = FILE_OpenUnixFile( unixName, mode ))) return HFILE_ERROR;
373 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
374 FILE_Close( file );
375 return handle;
379 /***********************************************************************
380 * FILE_Create
382 static DOS_FILE *FILE_Create( LPCSTR path, int mode, int unique )
384 DOS_FILE *file;
385 const char *unixName;
387 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
389 if ((unixName = DOSFS_IsDevice( path )) != NULL)
391 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
392 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
393 return NULL;
396 if (!(file = FILE_Alloc())) return NULL;
398 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE )))
400 FILE_Close( file );
401 return NULL;
403 if ((file->unix_handle = open( unixName,
404 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
405 mode )) == -1)
407 FILE_SetDosError();
408 FILE_Close( file );
409 return NULL;
412 /* File created OK, now fill the DOS_FILE */
414 file->unix_name = xstrdup( unixName );
415 DOSFS_ToDosDateTime( time(NULL), &file->filedate, &file->filetime );
416 return file;
420 /***********************************************************************
421 * FILE_Stat
423 * Stat a Unix path name. Return 1 if OK.
425 int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
426 WORD *pdate, WORD *ptime )
428 struct stat st;
430 if (stat( unixName, &st ) == -1)
432 FILE_SetDosError();
433 return 0;
435 if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
436 if (psize) *psize = S_ISDIR(st.st_mode) ? 0 : st.st_size;
437 DOSFS_ToDosDateTime( st.st_mtime, pdate, ptime );
438 return 1;
442 /***********************************************************************
443 * FILE_GetDateTime
445 * Get the date and time of a file.
447 int FILE_GetDateTime( HFILE hFile, WORD *pdate, WORD *ptime, BOOL32 refresh )
449 DOS_FILE *file;
451 if (!(file = FILE_GetFile( hFile ))) return 0;
452 if (refresh)
454 struct stat st;
455 if (fstat( file->unix_handle, &st ) == -1)
457 FILE_SetDosError();
458 return 0;
460 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
462 *pdate = file->filedate;
463 *ptime = file->filetime;
464 return 1;
468 /***********************************************************************
469 * FILE_SetDateTime
471 * Set the date and time of a file.
473 int FILE_SetDateTime( HFILE hFile, WORD date, WORD time )
475 DOS_FILE *file;
476 struct tm newtm;
477 struct utimbuf filetime;
479 if (!(file = FILE_GetFile( hFile ))) return 0;
480 newtm.tm_sec = (time & 0x1f) * 2;
481 newtm.tm_min = (time >> 5) & 0x3f;
482 newtm.tm_hour = (time >> 11);
483 newtm.tm_mday = (date & 0x1f);
484 newtm.tm_mon = ((date >> 5) & 0x0f) - 1;
485 newtm.tm_year = (date >> 9) + 80;
487 filetime.actime = filetime.modtime = mktime( &newtm );
488 if (utime( file->unix_name, &filetime ) != -1) return 1;
489 FILE_SetDosError();
490 return 0;
494 /***********************************************************************
495 * FILE_Dup
497 * dup() function for DOS handles.
499 HFILE FILE_Dup( HFILE hFile )
501 DOS_FILE *file;
502 HFILE handle;
504 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
505 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
506 if ((handle = FILE_AllocTaskHandle( file )) != HFILE_ERROR) file->count++;
507 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
508 return handle;
512 /***********************************************************************
513 * FILE_Dup2
515 * dup2() function for DOS handles.
517 HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
519 DOS_FILE *file;
520 PDB *pdb = (PDB *)GlobalLock16( GetCurrentPDB() );
521 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
523 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
524 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR;
526 if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
528 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
529 return HFILE_ERROR;
531 if (files[hFile2] < MAX_OPEN_FILES)
533 dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
534 files[hFile2] );
535 FILE_Close( &DOSFiles[files[hFile2]] );
537 files[hFile2] = (BYTE)(file - DOSFiles);
538 file->count++;
539 dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n", hFile2 );
540 return hFile2;
544 /***********************************************************************
545 * GetTempFileName16 (KERNEL.97)
547 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
548 LPSTR buffer )
550 char temppath[144];
552 if ((drive & TF_FORCEDRIVE) &&
553 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
555 drive &= ~TF_FORCEDRIVE;
556 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
557 drive );
560 if (drive & TF_FORCEDRIVE)
561 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
562 else
564 GetTempPath32A( 132, temppath );
565 strcat( temppath, "\\" );
567 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
571 /***********************************************************************
572 * GetTempFileName32A (KERNEL32.290)
574 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
575 LPSTR buffer)
577 LPSTR p;
578 int i;
579 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
581 if (!path) return 0;
582 strcpy( buffer, path );
583 p = buffer + strlen(buffer);
584 *p++ = '~';
585 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
586 sprintf( p, "%04x.tmp", num );
588 if (unique)
590 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
591 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
592 if (-1==access(DOSFS_GetUnixFileName(buffer,TRUE),W_OK))
593 fprintf(stderr,"Warning: GetTempFileName returns '%s', which doesn't seem to be writeable. Please check your configuration file if this generates a failure.\n",buffer);
594 return unique;
597 /* Now try to create it */
601 DOS_FILE *file;
602 if ((file = FILE_Create( buffer, 0666, TRUE )) != NULL)
603 { /* We created it */
604 dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
605 FILE_Close( file );
606 break;
608 if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
609 num++;
610 sprintf( p, "%04x.tmp", num );
611 } while (num != (unique & 0xffff));
613 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
614 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
615 if (-1==access(DOSFS_GetUnixFileName(buffer,TRUE),W_OK))
616 fprintf(stderr,"Warning: GetTempFileName returns '%s', which doesn't seem to be writeable. Please check your configuration file if this generates a failure.\n",buffer);
617 return num;
621 /***********************************************************************
622 * GetTempFileName32W (KERNEL32.291)
624 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
625 LPWSTR buffer )
627 LPSTR patha,prefixa;
628 char buffera[144];
629 UINT32 ret;
631 if (!path) return 0;
632 patha = STRING32_DupUniToAnsi(path);
633 prefixa = STRING32_DupUniToAnsi(prefix);
634 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
635 STRING32_AnsiToUni( buffer, buffera );
636 free(patha);
637 free(prefixa);
638 return ret;
642 /***********************************************************************
643 * OpenFile (KERNEL.74) (KERNEL32.396)
645 HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
647 DOS_FILE *file;
648 HFILE hFileRet;
649 WORD filedatetime[2];
650 const char *unixName, *dosName;
651 char *p;
652 int len, i, unixMode;
654 ofs->cBytes = sizeof(OFSTRUCT);
655 ofs->nErrCode = 0;
656 if (mode & OF_REOPEN) name = ofs->szPathName;
657 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
659 /* First allocate a task handle */
661 if ((hFileRet = FILE_AllocTaskHandle( NULL )) == HFILE_ERROR)
663 ofs->nErrCode = DOS_ExtendedError;
664 dprintf_file( stddeb, "OpenFile: no more task handles.\n" );
665 return HFILE_ERROR;
668 /* OF_PARSE simply fills the structure */
670 if (mode & OF_PARSE)
672 if (!(dosName = DOSFS_GetDosTrueName( name, FALSE ))) goto error;
673 lstrcpyn32A( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
674 ofs->fFixedDisk = (GetDriveType16( dosName[0]-'A' ) != DRIVE_REMOVABLE);
675 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
676 name, ofs->szPathName, hFileRet );
677 /* Return the handle, but close it first */
678 FILE_FreeTaskHandle( hFileRet );
679 /* return hFileRet; */
680 return 0; /* Progman seems to like this better */
683 /* OF_CREATE is completely different from all other options, so
684 handle it first */
686 if (mode & OF_CREATE)
688 if (!(file = FILE_Create( name, 0666, FALSE ))) goto error;
689 lstrcpyn32A( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
690 sizeof(ofs->szPathName) );
691 goto success;
694 /* Now look for the file */
696 /* First try the current directory */
698 lstrcpyn32A( ofs->szPathName, name, sizeof(ofs->szPathName) );
699 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
700 goto found;
702 /* Now try some different paths if none was specified */
704 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
706 if (name[1] == ':') name += 2;
707 if ((p = strrchr( name, '\\' ))) name = p + 1;
708 if ((p = strrchr( name, '/' ))) name = p + 1;
709 if (!name[0]) goto not_found;
711 else
713 if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
714 goto not_found;
717 if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
719 /* Try the Windows directory */
721 GetWindowsDirectory32A( ofs->szPathName, len );
722 strcat( ofs->szPathName, "\\" );
723 strcat( ofs->szPathName, name );
724 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
725 goto found;
727 /* Try the Windows system directory */
729 GetSystemDirectory32A( ofs->szPathName, len );
730 strcat( ofs->szPathName, "\\" );
731 strcat( ofs->szPathName, name );
732 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
733 goto found;
735 /* Try the path of the current executable */
737 if (GetCurrentTask())
739 GetModuleFileName( GetCurrentTask(), ofs->szPathName, len );
740 if ((p = strrchr( ofs->szPathName, '\\' )))
742 strcpy( p + 1, name );
743 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
744 goto found;
748 /* Try all directories in path */
750 for (i = 0; ; i++)
752 if (!DIR_GetDosPath( i, ofs->szPathName, len )) goto not_found;
753 strcat( ofs->szPathName, "\\" );
754 strcat( ofs->szPathName, name );
755 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
756 break;
759 found:
760 dprintf_file( stddeb, "OpenFile: found '%s'\n", unixName );
761 lstrcpyn32A(ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE),
762 sizeof(ofs->szPathName) );
764 if (mode & OF_DELETE)
766 if (unlink( unixName ) == -1) goto not_found;
767 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
768 /* Return the handle, but close it first */
769 FILE_FreeTaskHandle( hFileRet );
770 return hFileRet;
773 switch(mode & 3)
775 case OF_WRITE:
776 unixMode = O_WRONLY; break;
777 case OF_READWRITE:
778 unixMode = O_RDWR; break;
779 case OF_READ:
780 default:
781 unixMode = O_RDONLY; break;
784 if (!(file = FILE_OpenUnixFile( unixName, unixMode ))) goto not_found;
785 filedatetime[0] = file->filedate;
786 filedatetime[1] = file->filetime;
787 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
789 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
791 FILE_Close( file );
792 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
793 /* FIXME: what error here? */
794 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
795 goto error;
798 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
800 if (mode & OF_EXIST)
802 FILE_Close( file );
803 /* Return the handle, but close it first */
804 FILE_FreeTaskHandle( hFileRet );
805 return hFileRet;
808 success: /* We get here if the open was successful */
809 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
810 FILE_SetTaskHandle( hFileRet, file );
811 return hFileRet;
813 not_found: /* We get here if the file does not exist */
814 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
815 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
816 /* fall through */
818 error: /* We get here if there was an error opening the file */
819 ofs->nErrCode = DOS_ExtendedError;
820 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR\n", name );
821 FILE_FreeTaskHandle( hFileRet );
822 return HFILE_ERROR;
825 /***********************************************************************
826 * SearchPath32A (KERNEL32.447)
827 * Code borrowed from OpenFile above.
829 DWORD SearchPath32A(
830 LPCSTR path,LPCSTR fn,LPCSTR ext,DWORD buflen,LPSTR buf,LPSTR *lastpart
832 LPCSTR unixName;
833 INT32 len;
834 char testpath[1000]; /* should be enough for now */
835 char *name,*p;
836 int i;
838 if (ext==NULL)
839 ext = "";
840 name=(char*)xmalloc(strlen(fn)+strlen(ext)+1);
841 strcpy(name,fn);
842 strcat(name,ext);
844 dprintf_file(stddeb,"SearchPath32A(%s,%s,%s,%ld,%p,%p)\n",
845 path,fn,ext,buflen,buf,lastpart
847 if (path) {
848 strcpy(testpath,path);
849 strcat(testpath,"\\");
850 strcat(testpath,name);
851 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL) {
852 goto found;
853 } else {
854 strcpy(testpath,name);
855 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
856 goto found;
857 return 0;
860 if ((len=sizeof(testpath)-strlen(name)-1)<0)
861 return 0;
863 /* Try the path of the current executable */
864 if (GetCurrentTask()) {
865 GetModuleFileName(GetCurrentTask(),testpath,len);
866 if ((p=strrchr(testpath,'\\'))) {
867 strcpy(p+1,name);
868 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE)))
869 goto found;
873 /* Try the current directory */
874 lstrcpyn32A(testpath,name,sizeof(testpath) );
875 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
876 goto found;
878 /* Try the Windows directory */
879 GetWindowsDirectory32A(testpath,len);
880 strcat(testpath,"\\");
881 strcat(testpath,name);
882 if ((unixName = DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
883 goto found;
885 /* Try the Windows system directory */
886 GetSystemDirectory32A(testpath,len);
887 strcat(testpath,"\\");
888 strcat(testpath,name);
889 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
890 goto found;
892 /* Try all directories in path */
894 for (i=0;;i++)
896 if (!DIR_GetDosPath(i,testpath,len))
897 return 0;
898 strcat(testpath,"\\");
899 strcat(testpath,name);
900 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
901 break;
904 found:
905 strncpy(buf,testpath,buflen);
906 if (NULL!=(p=strrchr(testpath,'\\')))
907 p=p+1;
908 else
909 p=testpath;
910 if (lastpart) {
911 if (p-testpath<buflen)
912 *lastpart=(p-testpath)+buf;
913 else
914 *lastpart=NULL;
916 dprintf_file(stddeb," -> found %s,last part is %s\n",testpath,p);
917 return strlen(testpath);
920 /***********************************************************************
921 * SearchPath32W (KERNEL32.448)
923 DWORD SearchPath32W(
924 LPCWSTR path,LPCWSTR fn,LPCWSTR ext,DWORD buflen,LPWSTR buf,
925 LPWSTR *lastpart
927 LPSTR pathA = path?STRING32_DupUniToAnsi(path):NULL;
928 LPSTR fnA = STRING32_DupUniToAnsi(fn);
929 LPSTR extA = ext?STRING32_DupUniToAnsi(fn):NULL;
930 LPSTR lastpartA;
931 LPSTR bufA = (char*)xmalloc(buflen+1);
932 DWORD ret;
934 ret=SearchPath32A(pathA,fnA,extA,buflen,bufA,&lastpartA);
935 lstrcpynAtoW(buf,bufA,buflen);
936 if (lastpart) {
937 if (lastpartA)
938 *lastpart = buf+(lastpartA-bufA);
939 else
940 *lastpart = NULL;
942 free(bufA);
943 free(fnA);
944 if (pathA) free(pathA);
945 if (extA) free(extA);
946 return ret;
949 /***********************************************************************
950 * _lclose (KERNEL.81) (KERNEL32.592)
952 HFILE _lclose( HFILE hFile )
954 DOS_FILE *file;
956 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
957 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
958 FILE_Close( file );
959 FILE_FreeTaskHandle( hFile );
960 return 0;
964 /***********************************************************************
965 * WIN16_hread
967 LONG WIN16_hread( HFILE hFile, SEGPTR buffer, LONG count )
969 LONG maxlen;
971 dprintf_file( stddeb, "_hread16: %d %08lx %ld\n",
972 hFile, (DWORD)buffer, count );
974 /* Some programs pass a count larger than the allocated buffer */
975 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
976 if (count > maxlen) count = maxlen;
977 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
981 /***********************************************************************
982 * WIN16_lread
984 UINT16 WIN16_lread( HFILE hFile, SEGPTR buffer, UINT16 count )
986 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
990 /***********************************************************************
991 * _lread32 (KERNEL32.596)
993 UINT32 _lread32( HFILE hFile, LPVOID buffer, UINT32 count )
995 DOS_FILE *file;
996 UINT32 result;
998 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
999 if (!(file = FILE_GetFile( hFile ))) return -1;
1000 if (!count) return 0;
1001 if ((result = read( file->unix_handle, buffer, count )) == -1)
1002 FILE_SetDosError();
1003 return result;
1007 /***********************************************************************
1008 * _lread16 (KERNEL.82)
1010 UINT16 _lread16( HFILE hFile, LPVOID buffer, UINT16 count )
1012 return (UINT16)_lread32( hFile, buffer, (LONG)count );
1016 /***********************************************************************
1017 * _lcreat (KERNEL.83) (KERNEL32.593)
1019 HFILE _lcreat( LPCSTR path, INT32 attr )
1021 DOS_FILE *file;
1022 HFILE handle;
1023 int mode;
1025 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1026 mode = (attr & 1) ? 0444 : 0666;
1027 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
1028 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1029 FILE_Close( file );
1030 return handle;
1034 /***********************************************************************
1035 * _lcreat_uniq (Not a Windows API)
1037 HFILE _lcreat_uniq( LPCSTR path, INT32 attr )
1039 DOS_FILE *file;
1040 HFILE handle;
1041 int mode;
1043 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1044 mode = (attr & 1) ? 0444 : 0666;
1045 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
1046 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1047 FILE_Close( file );
1048 return handle;
1052 /***********************************************************************
1053 * _llseek (KERNEL.84) (KERNEL32.594)
1055 LONG _llseek( HFILE hFile, LONG lOffset, INT32 nOrigin )
1057 DOS_FILE *file;
1058 int origin, result;
1060 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
1061 hFile, lOffset, nOrigin);
1063 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1064 switch(nOrigin)
1066 case 1: origin = SEEK_CUR; break;
1067 case 2: origin = SEEK_END; break;
1068 default: origin = SEEK_SET; break;
1071 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
1072 FILE_SetDosError();
1073 return result;
1077 /***********************************************************************
1078 * _lopen (KERNEL.85) (KERNEL32.595)
1080 HFILE _lopen( LPCSTR path, INT32 mode )
1082 INT32 unixMode;
1084 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
1086 switch(mode & 3)
1088 case OF_WRITE:
1089 unixMode = O_WRONLY | O_TRUNC;
1090 break;
1091 case OF_READWRITE:
1092 unixMode = O_RDWR;
1093 break;
1094 case OF_READ:
1095 default:
1096 unixMode = O_RDONLY;
1097 break;
1099 return FILE_Open( path, unixMode );
1103 /***********************************************************************
1104 * _lwrite16 (KERNEL.86)
1106 UINT16 _lwrite16( HFILE hFile, LPCSTR buffer, UINT16 count )
1108 return (UINT16)_hwrite( hFile, buffer, (LONG)count );
1111 /***********************************************************************
1112 * _lwrite32 (KERNEL.86)
1114 UINT32 _lwrite32( HFILE hFile, LPCSTR buffer, UINT32 count )
1116 return (UINT32)_hwrite( hFile, buffer, (LONG)count );
1120 /***********************************************************************
1121 * _hread (KERNEL.349)
1123 LONG _hread( HFILE hFile, LPVOID buffer, LONG count)
1125 return _lread32( hFile, buffer, count );
1129 /***********************************************************************
1130 * _hwrite (KERNEL.350)
1132 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
1134 DOS_FILE *file;
1135 LONG result;
1137 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
1139 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1141 if (count == 0) /* Expand or truncate at current position */
1142 result = ftruncate( file->unix_handle,
1143 lseek( file->unix_handle, 0, SEEK_CUR ) );
1144 else
1145 result = write( file->unix_handle, buffer, count );
1147 if (result == -1) FILE_SetDosError();
1148 return result;
1152 /***********************************************************************
1153 * SetHandleCount16 (KERNEL.199)
1155 UINT16 SetHandleCount16( UINT16 count )
1157 HANDLE hPDB = GetCurrentPDB();
1158 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1159 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1160 WORD i;
1162 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
1164 if (count < 20) count = 20; /* No point in going below 20 */
1165 else if (count > 254) count = 254;
1167 /* If shrinking the table, make sure all extra file handles are closed */
1168 if (count < pdb->nbFiles)
1170 for (i = count; i < pdb->nbFiles; i++)
1171 if (files[i] != 0xff) /* File open */
1173 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
1174 SA_Abort, EL_Disk );
1175 return pdb->nbFiles;
1179 if (count == 20)
1181 if (pdb->nbFiles > 20)
1183 memcpy( pdb->fileHandles, files, 20 );
1184 #ifdef WINELIB
1185 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1186 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1187 #else
1188 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1189 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1190 GlobalHandleToSel( hPDB ) );
1191 #endif
1192 pdb->nbFiles = 20;
1195 else /* More than 20, need a new file handles table */
1197 BYTE *newfiles;
1198 #ifdef WINELIB
1199 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1200 #else
1201 HANDLE newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1202 if (!newhandle)
1204 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1205 return pdb->nbFiles;
1207 newfiles = (BYTE *)GlobalLock16( newhandle );
1208 #endif /* WINELIB */
1209 if (count > pdb->nbFiles)
1211 memcpy( newfiles, files, pdb->nbFiles );
1212 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1214 else memcpy( newfiles, files, count );
1215 #ifdef WINELIB
1216 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1217 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1218 #else
1219 if (pdb->nbFiles > 20)
1220 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1221 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1222 #endif /* WINELIB */
1223 pdb->nbFiles = count;
1225 return pdb->nbFiles;
1229 /***********************************************************************
1230 * FlushFileBuffers (KERNEL32.133)
1232 BOOL32 FlushFileBuffers( HFILE hFile )
1234 DOS_FILE *file;
1236 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1237 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1238 if (fsync( file->unix_handle ) != -1) return TRUE;
1239 FILE_SetDosError();
1240 return FALSE;
1244 /***********************************************************************
1245 * DeleteFile16 (KERNEL.146)
1247 BOOL16 DeleteFile16( LPCSTR path )
1249 return DeleteFile32A( path );
1253 /***********************************************************************
1254 * DeleteFile32A (KERNEL32.71)
1256 BOOL32 DeleteFile32A( LPCSTR path )
1258 const char *unixName;
1260 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1262 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1264 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1265 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1266 return FALSE;
1269 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1270 if (unlink( unixName ) == -1)
1272 FILE_SetDosError();
1273 return FALSE;
1275 return TRUE;
1279 /***********************************************************************
1280 * DeleteFile32W (KERNEL32.72)
1282 BOOL32 DeleteFile32W( LPCWSTR path )
1284 LPSTR xpath = STRING32_DupUniToAnsi(path);
1285 BOOL32 ret = RemoveDirectory32A( xpath );
1286 free(xpath);
1287 return ret;
1291 /***********************************************************************
1292 * CreateDirectory16 (KERNEL.144)
1294 BOOL16 CreateDirectory16( LPCSTR path, LPVOID dummy )
1296 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
1297 return (BOOL16)CreateDirectory32A( path, NULL );
1301 /***********************************************************************
1302 * CreateDirectory32A (KERNEL32.39)
1304 BOOL32 CreateDirectory32A( LPCSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1306 const char *unixName;
1308 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
1309 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1311 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
1312 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1313 return FALSE;
1315 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
1316 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
1318 FILE_SetDosError();
1319 return FALSE;
1321 return TRUE;
1325 /***********************************************************************
1326 * CreateDirectory32W (KERNEL32.42)
1328 BOOL32 CreateDirectory32W( LPCWSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1330 LPSTR xpath = STRING32_DupUniToAnsi(path);
1331 BOOL32 ret = CreateDirectory32A(xpath,lpsecattribs);
1332 free(xpath);
1333 return ret;
1337 /***********************************************************************
1338 * RemoveDirectory16 (KERNEL)
1340 BOOL16 RemoveDirectory16( LPCSTR path )
1342 return (BOOL16)RemoveDirectory32A( path );
1346 /***********************************************************************
1347 * RemoveDirectory32A (KERNEL32.437)
1349 BOOL32 RemoveDirectory32A( LPCSTR path )
1351 const char *unixName;
1353 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
1355 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1357 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
1358 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1359 return FALSE;
1361 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1362 if (rmdir( unixName ) == -1)
1364 FILE_SetDosError();
1365 return FALSE;
1367 return TRUE;
1371 /***********************************************************************
1372 * RemoveDirectory32W (KERNEL32.438)
1374 BOOL32 RemoveDirectory32W( LPCWSTR path )
1376 LPSTR xpath = STRING32_DupUniToAnsi(path);
1377 BOOL32 ret = RemoveDirectory32A( xpath );
1378 free(xpath);
1379 return ret;
1383 /***********************************************************************
1384 * FILE_SetFileType
1386 BOOL32 FILE_SetFileType( HFILE hFile, DWORD type )
1388 DOS_FILE *file = FILE_GetFile(hFile);
1389 if (!file) return FALSE;
1390 file->type = type;
1391 return TRUE;
1395 /***********************************************************************
1396 * GetFileType (KERNEL32.222)
1398 DWORD GetFileType( HFILE hFile )
1400 DOS_FILE *file = FILE_GetFile(hFile);
1402 if (!file)
1404 SetLastError( ERROR_INVALID_HANDLE );
1405 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1407 return file->type;