Release 960712
[wine/multimedia.git] / files / file.c
blob3264c08f7df5a24df8ff70a243a8c306c56cf485
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 "directory.h"
22 #include "dos_fs.h"
23 #include "drive.h"
24 #include "global.h"
25 #include "msdos.h"
26 #include "options.h"
27 #include "ldt.h"
28 #include "task.h"
29 #include "string32.h"
30 #include "stddebug.h"
31 #include "debug.h"
32 #include "xmalloc.h"
34 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
36 typedef struct tagDOS_FILE
38 struct tagDOS_FILE *next;
39 int count; /* Usage count (0 if free) */
40 int unix_handle;
41 int mode;
42 char *unix_name;
43 WORD filedate;
44 WORD filetime;
45 } DOS_FILE;
47 /* Global files array */
48 static DOS_FILE DOSFiles[MAX_OPEN_FILES];
50 static DOS_FILE *FILE_First = DOSFiles;
51 static DOS_FILE *FILE_LastUsed = DOSFiles;
53 /* Small file handles array for boot-up, before the first PDB is created */
54 #define MAX_BOOT_HANDLES 4
55 static BYTE bootFileHandles[MAX_BOOT_HANDLES] = { 0xff, 0xff, 0xff, 0xff };
57 /***********************************************************************
58 * FILE_Alloc
60 * Allocate a DOS file.
62 static DOS_FILE *FILE_Alloc(void)
64 DOS_FILE *file = FILE_First;
65 if (file) FILE_First = file->next;
66 else if (FILE_LastUsed >= &DOSFiles[MAX_OPEN_FILES-1])
68 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
69 return NULL;
71 else file = ++FILE_LastUsed;
72 file->count = 1;
73 file->unix_handle = -1;
74 file->unix_name = NULL;
75 return file;
79 /***********************************************************************
80 * FILE_Close
82 * Close a DOS file.
84 static BOOL FILE_Close( DOS_FILE *file )
86 if (!file->count) return FALSE;
87 if (--file->count > 0) return TRUE;
88 /* Now really close the file */
89 if (file->unix_handle != -1) close( file->unix_handle );
90 if (file->unix_name) free( file->unix_name );
91 file->next = FILE_First;
92 FILE_First = file;
93 return TRUE;
97 /***********************************************************************
98 * FILE_GetPDBFiles
100 * Return a pointer to the current PDB files array.
102 static void FILE_GetPDBFiles( BYTE **files, WORD *nbFiles )
104 PDB *pdb;
106 if ((pdb = (PDB *)GlobalLock16( GetCurrentPDB() )) != NULL)
108 *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
109 *nbFiles = pdb->nbFiles;
111 else
113 *files = bootFileHandles;
114 *nbFiles = MAX_BOOT_HANDLES;
119 /***********************************************************************
120 * FILE_AllocTaskHandle
122 * Allocate a task file handle for a DOS file.
124 static HFILE FILE_AllocTaskHandle( DOS_FILE *dos_file )
126 BYTE *files, *fp;
127 WORD i, nbFiles;
129 FILE_GetPDBFiles( &files, &nbFiles );
130 fp = files + 1; /* Don't use handle 0, as some programs don't like it */
131 for (i = nbFiles - 1; (i > 0) && (*fp != 0xff); i--, fp++);
132 if (!i)
133 { /* No more handles or files */
134 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
135 return -1;
137 *fp = dos_file ? (BYTE)(dos_file - DOSFiles) : 0;
138 dprintf_file(stddeb,
139 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
140 (fp - files), *fp, nbFiles - i, nbFiles );
141 return (HFILE)(fp - files);
145 /***********************************************************************
146 * FILE_FreeTaskHandle
148 * Free a per-task file handle.
150 static void FILE_FreeTaskHandle( HFILE handle )
152 BYTE *files;
153 WORD nbFiles;
155 FILE_GetPDBFiles( &files, &nbFiles );
156 dprintf_file( stddeb,"FILE_FreeTaskHandle: dos=%d file=%d\n",
157 handle, files[handle] );
158 if ((handle < 0) || (handle >= (INT)nbFiles))
160 fprintf( stderr, "FILE_FreeTaskHandle: invalid file handle %d\n",
161 handle );
162 return;
164 files[handle] = 0xff;
168 /***********************************************************************
169 * FILE_SetTaskHandle
171 * Set the value of a task handle (no error checking).
173 static void FILE_SetTaskHandle( HFILE handle, DOS_FILE *file )
175 BYTE *files;
176 WORD nbFiles;
178 FILE_GetPDBFiles( &files, &nbFiles );
179 files[handle] = (BYTE)(file - DOSFiles);
183 /***********************************************************************
184 * FILE_GetFile
186 * Return the DOS file associated to a task file handle.
188 static DOS_FILE *FILE_GetFile( HFILE handle )
190 BYTE *files;
191 WORD nbFiles;
192 DOS_FILE *file;
194 FILE_GetPDBFiles( &files, &nbFiles );
195 if ((handle < 0) || (handle >= (INT)nbFiles) ||
196 (files[handle] >= MAX_OPEN_FILES) ||
197 !(file = &DOSFiles[files[handle]])->count)
199 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
200 return NULL;
202 return file;
206 int FILE_GetUnixHandle( HFILE hFile )
208 DOS_FILE *file;
210 if (!(file = FILE_GetFile( hFile ))) return -1;
211 return file->unix_handle;
215 /***********************************************************************
216 * FILE_CloseAllFiles
218 * Close all open files of a given PDB. Used on task termination.
220 void FILE_CloseAllFiles( HANDLE hPDB )
222 BYTE *files;
223 WORD count;
224 PDB *pdb = (PDB *)GlobalLock16( hPDB );
226 if (!pdb) return;
227 files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
228 dprintf_file(stddeb,"FILE_CloseAllFiles: closing %d files\n",pdb->nbFiles);
229 for (count = pdb->nbFiles; count > 0; count--, files++)
231 if (*files < MAX_OPEN_FILES) FILE_Close( &DOSFiles[*files] );
232 *files = 0xff;
237 /***********************************************************************
238 * FILE_SetDosError
240 * Set the DOS error code from errno.
242 void FILE_SetDosError(void)
244 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
245 switch (errno)
247 case EAGAIN:
248 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
249 break;
250 case EBADF:
251 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
252 break;
253 case ENOSPC:
254 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
255 break;
256 case EACCES:
257 case EPERM:
258 case EROFS:
259 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
260 break;
261 case EBUSY:
262 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
263 break;
264 case ENOENT:
265 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
266 break;
267 case EISDIR:
268 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
269 break;
270 case ENFILE:
271 case EMFILE:
272 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
273 break;
274 case EEXIST:
275 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
276 break;
277 default:
278 perror( "int21: unknown errno" );
279 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
280 break;
285 /***********************************************************************
286 * FILE_OpenUnixFile
288 static DOS_FILE *FILE_OpenUnixFile( const char *name, int mode )
290 DOS_FILE *file;
291 struct stat st;
293 if (!(file = FILE_Alloc())) return NULL;
294 if ((file->unix_handle = open( name, mode )) == -1)
296 if (Options.allowReadOnly && (mode == O_RDWR))
298 if ((file->unix_handle = open( name, O_RDONLY )) != -1)
299 fprintf( stderr, "Warning: could not open %s for writing, opening read-only.\n", name );
302 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
304 FILE_SetDosError();
305 FILE_Close( file );
306 return NULL;
308 if (S_ISDIR(st.st_mode))
310 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
311 FILE_Close( file );
312 return NULL;
315 /* File opened OK, now fill the DOS_FILE */
317 file->unix_name = xstrdup( name );
318 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
319 return file;
323 /***********************************************************************
324 * FILE_Open
326 static DOS_FILE *FILE_Open( LPCSTR path, int mode )
328 const char *unixName;
330 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
331 if ((unixName = DOSFS_IsDevice( path )) != NULL)
333 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
334 if (!unixName[0]) /* Non-existing device */
336 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
337 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
338 return NULL;
341 else if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return NULL;
342 return FILE_OpenUnixFile( unixName, mode );
346 /***********************************************************************
347 * FILE_Create
349 static DOS_FILE *FILE_Create( LPCSTR path, int mode, int unique )
351 DOS_FILE *file;
352 const char *unixName;
354 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
356 if ((unixName = DOSFS_IsDevice( path )) != NULL)
358 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
359 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
360 return NULL;
363 if (!(file = FILE_Alloc())) return NULL;
365 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE )))
367 FILE_Close( file );
368 return NULL;
370 if ((file->unix_handle = open( unixName,
371 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
372 mode )) == -1)
374 FILE_SetDosError();
375 FILE_Close( file );
376 return NULL;
379 /* File created OK, now fill the DOS_FILE */
381 file->unix_name = xstrdup( unixName );
382 DOSFS_ToDosDateTime( time(NULL), &file->filedate, &file->filetime );
383 return file;
387 /***********************************************************************
388 * FILE_Stat
390 * Stat a Unix path name. Return 1 if OK.
392 int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
393 WORD *pdate, WORD *ptime )
395 struct stat st;
397 if (stat( unixName, &st ) == -1)
399 FILE_SetDosError();
400 return 0;
402 if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
403 if (psize) *psize = S_ISDIR(st.st_mode) ? 0 : st.st_size;
404 DOSFS_ToDosDateTime( st.st_mtime, pdate, ptime );
405 return 1;
409 /***********************************************************************
410 * FILE_GetDateTime
412 * Get the date and time of a file.
414 int FILE_GetDateTime( HFILE hFile, WORD *pdate, WORD *ptime, BOOL refresh )
416 DOS_FILE *file;
418 if (!(file = FILE_GetFile( hFile ))) return 0;
419 if (refresh)
421 struct stat st;
422 if (fstat( file->unix_handle, &st ) == -1)
424 FILE_SetDosError();
425 return 0;
427 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
429 *pdate = file->filedate;
430 *ptime = file->filetime;
431 return 1;
435 /***********************************************************************
436 * FILE_SetDateTime
438 * Set the date and time of a file.
440 int FILE_SetDateTime( HFILE hFile, WORD date, WORD time )
442 DOS_FILE *file;
443 struct tm newtm;
444 struct utimbuf filetime;
446 if (!(file = FILE_GetFile( hFile ))) return 0;
447 newtm.tm_sec = (time & 0x1f) * 2;
448 newtm.tm_min = (time >> 5) & 0x3f;
449 newtm.tm_hour = (time >> 11);
450 newtm.tm_mday = (date & 0x1f);
451 newtm.tm_mon = ((date >> 5) & 0x0f) - 1;
452 newtm.tm_year = (date >> 9) + 80;
454 filetime.actime = filetime.modtime = mktime( &newtm );
455 if (utime( file->unix_name, &filetime ) != -1) return 1;
456 FILE_SetDosError();
457 return 0;
461 /***********************************************************************
462 * FILE_Dup
464 * dup() function for DOS handles.
466 HFILE FILE_Dup( HFILE hFile )
468 DOS_FILE *file;
469 HFILE handle;
471 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
472 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
473 if ((handle = FILE_AllocTaskHandle( file )) != HFILE_ERROR) file->count++;
474 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
475 return handle;
479 /***********************************************************************
480 * FILE_Dup2
482 * dup2() function for DOS handles.
484 HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
486 DOS_FILE *file;
487 PDB *pdb = (PDB *)GlobalLock16( GetCurrentPDB() );
488 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
490 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
491 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR;
493 if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
495 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
496 return HFILE_ERROR;
498 if (files[hFile2] < MAX_OPEN_FILES)
500 dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
501 files[hFile2] );
502 FILE_Close( &DOSFiles[files[hFile2]] );
504 files[hFile2] = (BYTE)(file - DOSFiles);
505 file->count++;
506 dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n", hFile2 );
507 return hFile2;
511 /***********************************************************************
512 * FILE_Read
514 INT32 FILE_Read( HFILE hFile, LPVOID buffer, UINT32 count )
516 DOS_FILE *file;
517 INT32 result;
519 dprintf_file( stddeb, "FILE_Read: %d %p %d\n", hFile, buffer, count );
520 if (!(file = FILE_GetFile( hFile ))) return -1;
521 if (!count) return 0;
522 if ((result = read( file->unix_handle, buffer, count )) == -1)
523 FILE_SetDosError();
524 return result;
528 /***********************************************************************
529 * GetTempFileName16 (KERNEL.97)
531 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
532 LPSTR buffer )
534 char temppath[144];
536 if ((drive & TF_FORCEDRIVE) &&
537 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
539 drive &= ~TF_FORCEDRIVE;
540 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
541 drive );
544 if (drive & TF_FORCEDRIVE)
545 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
546 else
548 GetTempPath32A( 132, temppath );
549 strcat( temppath, "\\" );
551 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
555 /***********************************************************************
556 * GetTempFileName32A (KERNEL32.290)
558 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
559 LPSTR buffer)
561 LPSTR p;
562 int i;
563 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
565 if (!path) return 0;
566 strcpy( buffer, path );
567 p = buffer + strlen(buffer);
568 *p++ = '~';
569 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
570 sprintf( p, "%04x.tmp", num );
572 if (unique)
574 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
575 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
576 return unique;
579 /* Now try to create it */
583 DOS_FILE *file;
584 if ((file = FILE_Create( buffer, 0666, TRUE )) != NULL)
585 { /* We created it */
586 dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
587 FILE_Close( file );
588 break;
590 if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
591 num++;
592 sprintf( p, "%04x.tmp", num );
593 } while (num != (unique & 0xffff));
595 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
596 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
597 return num;
601 /***********************************************************************
602 * GetTempFileName32W (KERNEL32.291)
604 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
605 LPWSTR buffer )
607 LPSTR patha,prefixa;
608 char buffera[144];
609 UINT32 ret;
611 if (!path) return 0;
612 patha = STRING32_DupUniToAnsi(path);
613 prefixa = STRING32_DupUniToAnsi(prefix);
614 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
615 STRING32_AnsiToUni( buffer, buffera );
616 free(patha);
617 free(prefixa);
618 return ret;
622 /***********************************************************************
623 * OpenFile (KERNEL.74) (KERNEL32.396)
625 HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
627 DOS_FILE *file;
628 HFILE hFileRet;
629 WORD filedatetime[2];
630 const char *unixName, *dosName;
631 char *p;
632 int len, i, unixMode;
634 ofs->cBytes = sizeof(OFSTRUCT);
635 ofs->nErrCode = 0;
636 if (mode & OF_REOPEN) name = ofs->szPathName;
637 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
639 /* First allocate a task handle */
641 if ((hFileRet = FILE_AllocTaskHandle( NULL )) == HFILE_ERROR)
643 ofs->nErrCode = DOS_ExtendedError;
644 dprintf_file( stddeb, "OpenFile: no more task handles.\n" );
645 return HFILE_ERROR;
648 /* OF_PARSE simply fills the structure */
650 if (mode & OF_PARSE)
652 if (!(dosName = DOSFS_GetDosTrueName( name, FALSE ))) goto error;
653 lstrcpyn32A( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
654 ofs->fFixedDisk = (GetDriveType16( dosName[0]-'A' ) != DRIVE_REMOVABLE);
655 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
656 name, ofs->szPathName, hFileRet );
657 /* Return the handle, but close it first */
658 FILE_FreeTaskHandle( hFileRet );
659 /* return hFileRet; */
660 return 0; /* Progman seems to like this better */
663 /* OF_CREATE is completely different from all other options, so
664 handle it first */
666 if (mode & OF_CREATE)
668 if (!(file = FILE_Create( name, 0666, FALSE ))) goto error;
669 lstrcpyn32A( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
670 sizeof(ofs->szPathName) );
671 goto success;
674 /* Now look for the file */
676 /* First try the current directory */
678 lstrcpyn32A( ofs->szPathName, name, sizeof(ofs->szPathName) );
679 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
680 goto found;
682 /* Now try some different paths if none was specified */
684 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
686 if (name[1] == ':') name += 2;
687 if ((p = strrchr( name, '\\' ))) name = p + 1;
688 if ((p = strrchr( name, '/' ))) name = p + 1;
689 if (!name[0]) goto not_found;
691 else
693 if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
694 goto not_found;
697 if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
699 /* Try the Windows directory */
701 GetWindowsDirectory( ofs->szPathName, len );
702 strcat( ofs->szPathName, "\\" );
703 strcat( ofs->szPathName, name );
704 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
705 goto found;
707 /* Try the Windows system directory */
709 GetSystemDirectory32A( ofs->szPathName, len );
710 strcat( ofs->szPathName, "\\" );
711 strcat( ofs->szPathName, name );
712 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
713 goto found;
715 /* Try the path of the current executable */
717 if (GetCurrentTask())
719 GetModuleFileName( GetCurrentTask(), ofs->szPathName, len );
720 if ((p = strrchr( ofs->szPathName, '\\' )))
722 strcpy( p + 1, name );
723 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
724 goto found;
728 /* Try all directories in path */
730 for (i = 0; ; i++)
732 if (!DIR_GetDosPath( i, ofs->szPathName, len )) goto not_found;
733 strcat( ofs->szPathName, "\\" );
734 strcat( ofs->szPathName, name );
735 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
736 break;
739 found:
740 dprintf_file( stddeb, "OpenFile: found '%s'\n", unixName );
741 lstrcpyn32A(ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE),
742 sizeof(ofs->szPathName) );
744 if (mode & OF_DELETE)
746 if (unlink( unixName ) == -1) goto not_found;
747 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
748 /* Return the handle, but close it first */
749 FILE_FreeTaskHandle( hFileRet );
750 return hFileRet;
753 switch(mode & 3)
755 case OF_WRITE:
756 unixMode = O_WRONLY; break;
757 case OF_READWRITE:
758 unixMode = O_RDWR; break;
759 case OF_READ:
760 default:
761 unixMode = O_RDONLY; break;
764 if (!(file = FILE_OpenUnixFile( unixName, unixMode ))) goto not_found;
765 filedatetime[0] = file->filedate;
766 filedatetime[1] = file->filetime;
767 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
769 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
771 FILE_Close( file );
772 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
773 /* FIXME: what error here? */
774 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
775 goto error;
778 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
780 if (mode & OF_EXIST)
782 FILE_Close( file );
783 /* Return the handle, but close it first */
784 FILE_FreeTaskHandle( hFileRet );
785 return hFileRet;
788 success: /* We get here if the open was successful */
789 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
790 FILE_SetTaskHandle( hFileRet, file );
791 return hFileRet;
793 not_found: /* We get here if the file does not exist */
794 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
795 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
796 /* fall through */
798 error: /* We get here if there was an error opening the file */
799 ofs->nErrCode = DOS_ExtendedError;
800 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR\n", name );
801 FILE_FreeTaskHandle( hFileRet );
802 return HFILE_ERROR;
806 /***********************************************************************
807 * _lclose (KERNEL.81) (KERNEL32.592)
809 HFILE _lclose( HFILE hFile )
811 DOS_FILE *file;
813 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
814 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
815 FILE_Close( file );
816 FILE_FreeTaskHandle( hFile );
817 return 0;
821 /***********************************************************************
822 * _lread (KERNEL.82)
824 INT _lread( HFILE hFile, SEGPTR buffer, WORD count )
826 return (INT)_hread( hFile, buffer, (LONG)count );
830 /***********************************************************************
831 * _lcreat (KERNEL.83) (KERNEL32.593)
833 HFILE _lcreat( LPCSTR path, INT32 attr )
835 DOS_FILE *file;
836 HFILE handle;
837 int mode;
839 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
840 mode = (attr & 1) ? 0444 : 0666;
841 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
842 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
843 FILE_Close( file );
844 return handle;
848 /***********************************************************************
849 * _lcreat_uniq (Not a Windows API)
851 HFILE _lcreat_uniq( LPCSTR path, INT attr )
853 DOS_FILE *file;
854 HFILE handle;
855 int mode;
857 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
858 mode = (attr & 1) ? 0444 : 0666;
859 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
860 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
861 FILE_Close( file );
862 return handle;
866 /***********************************************************************
867 * _llseek (KERNEL.84)
869 LONG _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
871 DOS_FILE *file;
872 int origin, result;
874 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
875 hFile, lOffset, nOrigin);
877 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
878 switch(nOrigin)
880 case 1: origin = SEEK_CUR; break;
881 case 2: origin = SEEK_END; break;
882 default: origin = SEEK_SET; break;
885 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
886 FILE_SetDosError();
887 return result;
891 /***********************************************************************
892 * _lopen (KERNEL.85) (KERNEL32.595)
894 HFILE _lopen( LPCSTR path, INT32 mode )
896 DOS_FILE *file;
897 int unixMode;
898 HFILE handle;
900 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
902 switch(mode & 3)
904 case OF_WRITE:
905 unixMode = O_WRONLY | O_TRUNC;
906 break;
907 case OF_READWRITE:
908 unixMode = O_RDWR;
909 break;
910 case OF_READ:
911 default:
912 unixMode = O_RDONLY;
913 break;
915 if (!(file = FILE_Open( path, unixMode ))) return HFILE_ERROR;
916 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
917 FILE_Close( file );
918 return handle;
922 /***********************************************************************
923 * _lwrite (KERNEL.86)
925 INT _lwrite( HFILE hFile, LPCSTR buffer, WORD count )
927 return (INT)_hwrite( hFile, buffer, (LONG)count );
931 /***********************************************************************
932 * _hread (KERNEL.349)
934 LONG _hread( HFILE hFile, SEGPTR buffer, LONG count )
936 #ifndef WINELIB
937 LONG maxlen;
939 dprintf_file( stddeb, "_hread: %d %08lx %ld\n",
940 hFile, (DWORD)buffer, count );
942 /* Some programs pass a count larger than the allocated buffer */
943 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
944 if (count > maxlen) count = maxlen;
945 #endif
946 return FILE_Read( hFile, PTR_SEG_TO_LIN(buffer), count );
950 /***********************************************************************
951 * _hwrite (KERNEL.350)
953 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
955 DOS_FILE *file;
956 LONG result;
958 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
960 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
962 if (count == 0) /* Expand or truncate at current position */
963 result = ftruncate( file->unix_handle,
964 lseek( file->unix_handle, 0, SEEK_CUR ) );
965 else
966 result = write( file->unix_handle, buffer, count );
968 if (result == -1) FILE_SetDosError();
969 return result;
973 /***********************************************************************
974 * SetHandleCount (KERNEL.199)
976 WORD SetHandleCount( WORD count )
978 HANDLE hPDB = GetCurrentPDB();
979 PDB *pdb = (PDB *)GlobalLock16( hPDB );
980 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
981 WORD i;
983 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
985 if (count < 20) count = 20; /* No point in going below 20 */
986 else if (count > 254) count = 254;
988 /* If shrinking the table, make sure all extra file handles are closed */
989 if (count < pdb->nbFiles)
991 for (i = count; i < pdb->nbFiles; i++)
992 if (files[i] != 0xff) /* File open */
994 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
995 SA_Abort, EL_Disk );
996 return pdb->nbFiles;
1000 if (count == 20)
1002 if (pdb->nbFiles > 20)
1004 memcpy( pdb->fileHandles, files, 20 );
1005 #ifdef WINELIB
1006 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1007 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1008 #else
1009 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1010 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1011 GlobalHandleToSel( hPDB ) );
1012 #endif
1013 pdb->nbFiles = 20;
1016 else /* More than 20, need a new file handles table */
1018 BYTE *newfiles;
1019 #ifdef WINELIB
1020 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1021 #else
1022 HANDLE newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1023 if (!newhandle)
1025 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1026 return pdb->nbFiles;
1028 newfiles = (BYTE *)GlobalLock16( newhandle );
1029 #endif /* WINELIB */
1030 if (count > pdb->nbFiles)
1032 memcpy( newfiles, files, pdb->nbFiles );
1033 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1035 else memcpy( newfiles, files, count );
1036 #ifdef WINELIB
1037 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1038 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1039 #else
1040 if (pdb->nbFiles > 20)
1041 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1042 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1043 #endif /* WINELIB */
1044 pdb->nbFiles = count;
1046 return pdb->nbFiles;
1050 /***********************************************************************
1051 * FlushFileBuffers (KERNEL32.133)
1053 BOOL32 FlushFileBuffers( HFILE hFile )
1055 DOS_FILE *file;
1057 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1058 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1059 if (fsync( file->unix_handle ) != -1) return TRUE;
1060 FILE_SetDosError();
1061 return FALSE;
1065 /***********************************************************************
1066 * DeleteFile16 (KERNEL.146)
1068 BOOL16 DeleteFile16( LPCSTR path )
1070 return DeleteFile32A( path );
1074 /***********************************************************************
1075 * DeleteFile32A (KERNEL32.71)
1077 BOOL32 DeleteFile32A( LPCSTR path )
1079 const char *unixName;
1081 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1083 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1085 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1086 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1087 return FALSE;
1090 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1091 if (unlink( unixName ) == -1)
1093 FILE_SetDosError();
1094 return FALSE;
1096 return TRUE;
1100 /***********************************************************************
1101 * DeleteFile32W (KERNEL32.72)
1103 BOOL32 DeleteFile32W( LPCWSTR path )
1105 LPSTR xpath = STRING32_DupUniToAnsi(path);
1106 BOOL32 ret = RemoveDirectory32A( xpath );
1107 free(xpath);
1108 return ret;
1112 /***********************************************************************
1113 * CreateDirectory16 (KERNEL.144)
1115 BOOL16 CreateDirectory16( LPCSTR path, LPVOID dummy )
1117 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
1118 return (BOOL16)CreateDirectory32A( path, NULL );
1122 /***********************************************************************
1123 * CreateDirectory32A (KERNEL32.39)
1125 BOOL32 CreateDirectory32A( LPCSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1127 const char *unixName;
1129 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
1130 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1132 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
1133 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1134 return FALSE;
1136 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
1137 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
1139 FILE_SetDosError();
1140 return FALSE;
1142 return TRUE;
1146 /***********************************************************************
1147 * CreateDirectory32W (KERNEL32.42)
1149 BOOL32 CreateDirectory32W( LPCWSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1151 LPSTR xpath = STRING32_DupUniToAnsi(path);
1152 BOOL32 ret = CreateDirectory32A(xpath,lpsecattribs);
1153 free(xpath);
1154 return ret;
1158 /***********************************************************************
1159 * RemoveDirectory16 (KERNEL)
1161 BOOL16 RemoveDirectory16( LPCSTR path )
1163 return (BOOL16)RemoveDirectory32A( path );
1167 /***********************************************************************
1168 * RemoveDirectory32A (KERNEL32.437)
1170 BOOL32 RemoveDirectory32A( LPCSTR path )
1172 const char *unixName;
1174 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
1176 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1178 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
1179 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1180 return FALSE;
1182 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1183 if (rmdir( unixName ) == -1)
1185 FILE_SetDosError();
1186 return FALSE;
1188 return TRUE;
1192 /***********************************************************************
1193 * RemoveDirectory32W (KERNEL32.438)
1195 BOOL32 RemoveDirectory32W( LPCWSTR path )
1197 LPSTR xpath = STRING32_DupUniToAnsi(path);
1198 BOOL32 ret = RemoveDirectory32A( xpath );
1199 free(xpath);
1200 return ret;