Release 960506
[wine/multimedia.git] / files / file.c
blobe9881a0f40f567e24364bf03d8f89d8327d6ddae
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/errno.h>
14 #include <sys/stat.h>
15 #include <time.h>
16 #include <unistd.h>
17 #include <utime.h>
19 #include "windows.h"
20 #include "directory.h"
21 #include "dos_fs.h"
22 #include "drive.h"
23 #include "global.h"
24 #include "msdos.h"
25 #include "options.h"
26 #include "ldt.h"
27 #include "task.h"
28 #include "stddebug.h"
29 #include "debug.h"
30 #include "xmalloc.h"
32 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
34 typedef struct tagDOS_FILE
36 struct tagDOS_FILE *next;
37 int count; /* Usage count (0 if free) */
38 int unix_handle;
39 int mode;
40 char *unix_name;
41 WORD filedate;
42 WORD filetime;
43 } DOS_FILE;
45 /* Global files array */
46 static DOS_FILE DOSFiles[MAX_OPEN_FILES] = { { 0, }, };
48 static DOS_FILE *FILE_First = DOSFiles;
49 static DOS_FILE *FILE_LastUsed = DOSFiles;
51 /* Small file handles array for boot-up, before the first PDB is created */
52 #define MAX_BOOT_HANDLES 4
53 static BYTE bootFileHandles[MAX_BOOT_HANDLES] = { 0xff, 0xff, 0xff, 0xff };
55 /***********************************************************************
56 * FILE_Alloc
58 * Allocate a DOS file.
60 static DOS_FILE *FILE_Alloc(void)
62 DOS_FILE *file = FILE_First;
63 if (file) FILE_First = file->next;
64 else if (FILE_LastUsed >= &DOSFiles[MAX_OPEN_FILES-1])
66 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
67 return NULL;
69 else file = ++FILE_LastUsed;
70 file->count = 1;
71 file->unix_handle = -1;
72 file->unix_name = NULL;
73 return file;
77 /***********************************************************************
78 * FILE_Close
80 * Close a DOS file.
82 static BOOL FILE_Close( DOS_FILE *file )
84 if (!file->count) return FALSE;
85 if (--file->count > 0) return TRUE;
86 /* Now really close the file */
87 if (file->unix_handle != -1) close( file->unix_handle );
88 if (file->unix_name) free( file->unix_name );
89 file->next = FILE_First;
90 FILE_First = file;
91 return TRUE;
95 /***********************************************************************
96 * FILE_GetPDBFiles
98 * Return a pointer to the current PDB files array.
100 static void FILE_GetPDBFiles( BYTE **files, WORD *nbFiles )
102 PDB *pdb;
104 if ((pdb = (PDB *)GlobalLock16( GetCurrentPDB() )) != NULL)
106 *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
107 *nbFiles = pdb->nbFiles;
109 else
111 *files = bootFileHandles;
112 *nbFiles = MAX_BOOT_HANDLES;
117 /***********************************************************************
118 * FILE_AllocTaskHandle
120 * Allocate a task file handle for a DOS file.
122 static HFILE FILE_AllocTaskHandle( DOS_FILE *dos_file )
124 BYTE *files, *fp;
125 WORD i, nbFiles;
127 FILE_GetPDBFiles( &files, &nbFiles );
128 fp = files + 1; /* Don't use handle 0, as some programs don't like it */
129 for (i = nbFiles - 1; (i > 0) && (*fp != 0xff); i--, fp++);
130 if (!i)
131 { /* No more handles or files */
132 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
133 return -1;
135 *fp = dos_file ? (BYTE)(dos_file - DOSFiles) : 0;
136 dprintf_file(stddeb,
137 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
138 (fp - files), *fp, nbFiles - i, nbFiles );
139 return (HFILE)(fp - files);
143 /***********************************************************************
144 * FILE_FreeTaskHandle
146 * Free a per-task file handle.
148 static void FILE_FreeTaskHandle( HFILE handle )
150 BYTE *files;
151 WORD nbFiles;
153 FILE_GetPDBFiles( &files, &nbFiles );
154 dprintf_file( stddeb,"FILE_FreeTaskHandle: dos=%d file=%d\n",
155 handle, files[handle] );
156 if ((handle < 0) || (handle >= (INT)nbFiles))
158 fprintf( stderr, "FILE_FreeTaskHandle: invalid file handle %d\n",
159 handle );
160 return;
162 files[handle] = 0xff;
166 /***********************************************************************
167 * FILE_SetTaskHandle
169 * Set the value of a task handle (no error checking).
171 static void FILE_SetTaskHandle( HFILE handle, DOS_FILE *file )
173 BYTE *files;
174 WORD nbFiles;
176 FILE_GetPDBFiles( &files, &nbFiles );
177 files[handle] = (BYTE)(file - DOSFiles);
181 /***********************************************************************
182 * FILE_GetFile
184 * Return the DOS file associated to a task file handle.
186 static DOS_FILE *FILE_GetFile( HFILE handle )
188 BYTE *files;
189 WORD nbFiles;
190 DOS_FILE *file;
192 FILE_GetPDBFiles( &files, &nbFiles );
193 if ((handle < 0) || (handle >= (INT)nbFiles) ||
194 (files[handle] >= MAX_OPEN_FILES) ||
195 !(file = &DOSFiles[files[handle]])->count)
197 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
198 return NULL;
200 return file;
204 int FILE_GetUnixHandle( HFILE hFile )
206 DOS_FILE *file;
208 if (!(file = FILE_GetFile( hFile ))) return -1;
209 return file->unix_handle;
213 /***********************************************************************
214 * FILE_CloseAllFiles
216 * Close all open files of a given PDB. Used on task termination.
218 void FILE_CloseAllFiles( HANDLE hPDB )
220 BYTE *files;
221 WORD count;
222 PDB *pdb = (PDB *)GlobalLock16( hPDB );
224 if (!pdb) return;
225 files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
226 dprintf_file(stddeb,"FILE_CloseAllFiles: closing %d files\n",pdb->nbFiles);
227 for (count = pdb->nbFiles; count > 0; count--, files++)
229 if (*files < MAX_OPEN_FILES) FILE_Close( &DOSFiles[*files] );
230 *files = 0xff;
235 /***********************************************************************
236 * FILE_SetDosError
238 * Set the DOS error code from errno.
240 void FILE_SetDosError(void)
242 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
243 switch (errno)
245 case EAGAIN:
246 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
247 break;
248 case EBADF:
249 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
250 break;
251 case ENOSPC:
252 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
253 break;
254 case EACCES:
255 case EPERM:
256 case EROFS:
257 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
258 break;
259 case EBUSY:
260 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
261 break;
262 case ENOENT:
263 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
264 break;
265 case EISDIR:
266 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
267 break;
268 case ENFILE:
269 case EMFILE:
270 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
271 break;
272 case EEXIST:
273 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
274 break;
275 default:
276 perror( "int21: unknown errno" );
277 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
278 break;
283 /***********************************************************************
284 * FILE_OpenUnixFile
286 static DOS_FILE *FILE_OpenUnixFile( const char *name, int mode )
288 DOS_FILE *file;
289 struct stat st;
291 if (!(file = FILE_Alloc())) return NULL;
292 if ((file->unix_handle = open( name, mode )) == -1)
294 if (Options.allowReadOnly && (mode == O_RDWR))
296 if ((file->unix_handle = open( name, O_RDONLY )) != -1)
297 fprintf( stderr, "Warning: could not open %s for writing, opening read-only.\n", name );
300 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
302 FILE_SetDosError();
303 FILE_Close( file );
304 return NULL;
306 if (S_ISDIR(st.st_mode))
308 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
309 FILE_Close( file );
310 return NULL;
313 /* File opened OK, now fill the DOS_FILE */
315 file->unix_name = xstrdup( name );
316 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
317 return file;
321 /***********************************************************************
322 * FILE_Open
324 static DOS_FILE *FILE_Open( LPCSTR path, int mode )
326 const char *unixName;
328 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
329 if ((unixName = DOSFS_IsDevice( path )) != NULL)
331 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
332 if (!unixName[0]) /* Non-existing device */
334 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
335 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
336 return NULL;
339 else if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return NULL;
340 return FILE_OpenUnixFile( unixName, mode );
344 /***********************************************************************
345 * FILE_Create
347 static DOS_FILE *FILE_Create( LPCSTR path, int mode, int unique )
349 DOS_FILE *file;
350 const char *unixName;
352 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
354 if ((unixName = DOSFS_IsDevice( path )) != NULL)
356 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
357 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
358 return NULL;
361 if (!(file = FILE_Alloc())) return NULL;
363 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE )))
365 FILE_Close( file );
366 return NULL;
368 if ((file->unix_handle = open( unixName,
369 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
370 mode )) == -1)
372 FILE_SetDosError();
373 FILE_Close( file );
374 return NULL;
377 /* File created OK, now fill the DOS_FILE */
379 file->unix_name = xstrdup( unixName );
380 DOSFS_ToDosDateTime( time(NULL), &file->filedate, &file->filetime );
381 return file;
385 /***********************************************************************
386 * FILE_Unlink
388 int FILE_Unlink( LPCSTR path )
390 const char *unixName;
392 dprintf_file(stddeb, "FILE_Unlink: '%s'\n", path );
394 if ((unixName = DOSFS_IsDevice( path )) != NULL)
396 dprintf_file(stddeb, "FILE_Unlink: removing device '%s'!\n", unixName);
397 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
398 return 0;
401 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return 0;
402 if (unlink( unixName ) == -1)
404 FILE_SetDosError();
405 return 0;
407 return 1;
411 /***********************************************************************
412 * FILE_Stat
414 * Stat a Unix path name. Return 1 if OK.
416 int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
417 WORD *pdate, WORD *ptime )
419 struct stat st;
421 if (stat( unixName, &st ) == -1)
423 FILE_SetDosError();
424 return 0;
426 if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
427 if (psize) *psize = S_ISDIR(st.st_mode) ? 0 : st.st_size;
428 DOSFS_ToDosDateTime( st.st_mtime, pdate, ptime );
429 return 1;
433 /***********************************************************************
434 * FILE_GetDateTime
436 * Get the date and time of a file.
438 int FILE_GetDateTime( HFILE hFile, WORD *pdate, WORD *ptime, BOOL refresh )
440 DOS_FILE *file;
442 if (!(file = FILE_GetFile( hFile ))) return 0;
443 if (refresh)
445 struct stat st;
446 if (fstat( file->unix_handle, &st ) == -1)
448 FILE_SetDosError();
449 return 0;
451 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
453 *pdate = file->filedate;
454 *ptime = file->filetime;
455 return 1;
459 /***********************************************************************
460 * FILE_SetDateTime
462 * Set the date and time of a file.
464 int FILE_SetDateTime( HFILE hFile, WORD date, WORD time )
466 DOS_FILE *file;
467 struct tm newtm;
468 struct utimbuf filetime;
470 if (!(file = FILE_GetFile( hFile ))) return 0;
471 newtm.tm_sec = (time & 0x1f) * 2;
472 newtm.tm_min = (time >> 5) & 0x3f;
473 newtm.tm_hour = (time >> 11);
474 newtm.tm_mday = (date & 0x1f);
475 newtm.tm_mon = ((date >> 5) & 0x0f) - 1;
476 newtm.tm_year = (date >> 9) + 80;
478 filetime.actime = filetime.modtime = mktime( &newtm );
479 if (utime( file->unix_name, &filetime ) != -1) return 1;
480 FILE_SetDosError();
481 return 0;
485 /***********************************************************************
486 * FILE_Sync
488 int FILE_Sync( HFILE hFile )
490 DOS_FILE *file;
492 if (!(file = FILE_GetFile( hFile ))) return 0;
493 if (fsync( file->unix_handle ) != -1) return 1;
494 FILE_SetDosError();
495 return 0;
499 /***********************************************************************
500 * FILE_MakeDir
502 int FILE_MakeDir( LPCSTR path )
504 const char *unixName;
506 dprintf_file(stddeb, "FILE_MakeDir: '%s'\n", path );
508 if ((unixName = DOSFS_IsDevice( path )) != NULL)
510 dprintf_file(stddeb, "FILE_MakeDir: device '%s'!\n", unixName);
511 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
512 return 0;
514 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
515 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
517 FILE_SetDosError();
518 return 0;
520 return 1;
524 /***********************************************************************
525 * FILE_RemoveDir
527 int FILE_RemoveDir( LPCSTR path )
529 const char *unixName;
531 dprintf_file(stddeb, "FILE_RemoveDir: '%s'\n", path );
533 if ((unixName = DOSFS_IsDevice( path )) != NULL)
535 dprintf_file(stddeb, "FILE_RemoveDir: device '%s'!\n", unixName);
536 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
537 return 0;
539 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return 0;
540 if (rmdir( unixName ) == -1)
542 FILE_SetDosError();
543 return 0;
545 return 1;
549 /***********************************************************************
550 * FILE_Dup
552 * dup() function for DOS handles.
554 HFILE FILE_Dup( HFILE hFile )
556 DOS_FILE *file;
557 HFILE handle;
559 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
560 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
561 if ((handle = FILE_AllocTaskHandle( file )) != HFILE_ERROR) file->count++;
562 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
563 return handle;
567 /***********************************************************************
568 * FILE_Dup2
570 * dup2() function for DOS handles.
572 HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
574 DOS_FILE *file;
575 PDB *pdb = (PDB *)GlobalLock16( GetCurrentPDB() );
576 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
578 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
579 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR;
581 if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
583 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
584 return HFILE_ERROR;
586 if (files[hFile2] < MAX_OPEN_FILES)
588 dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
589 files[hFile2] );
590 FILE_Close( &DOSFiles[files[hFile2]] );
592 files[hFile2] = (BYTE)(file - DOSFiles);
593 file->count++;
594 dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n", hFile2 );
595 return hFile2;
599 /***********************************************************************
600 * FILE_Read
602 LONG FILE_Read( HFILE hFile, void *buffer, LONG count )
604 DOS_FILE *file;
605 LONG result;
607 dprintf_file( stddeb, "FILE_Read: %d %p %ld\n", hFile, buffer, count );
608 if (!(file = FILE_GetFile( hFile ))) return -1;
609 if (!count) return 0;
610 if ((result = read( file->unix_handle, buffer, count )) == -1)
611 FILE_SetDosError();
612 return result;
616 /***********************************************************************
617 * GetTempFileName (KERNEL.97)
619 INT GetTempFileName( BYTE drive, LPCSTR prefix, UINT unique, LPSTR buffer )
621 int i;
622 UINT num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
623 char *p;
625 if (drive & TF_FORCEDRIVE)
627 sprintf( buffer, "%c:", drive & ~TF_FORCEDRIVE );
629 else
631 DIR_GetTempDosDir( buffer, 132 ); /* buffer must be at least 144 */
632 strcat( buffer, "\\" );
635 p = buffer + strlen(buffer);
636 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
637 sprintf( p, "%04x.tmp", num );
639 if (unique)
641 lstrcpyn( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
642 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
643 return unique;
646 /* Now try to create it */
650 DOS_FILE *file;
651 if ((file = FILE_Create( buffer, 0666, TRUE )) != NULL)
652 { /* We created it */
653 dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
654 FILE_Close( file );
655 break;
657 if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
658 num++;
659 sprintf( p, "%04x.tmp", num );
660 } while (num != (unique & 0xffff));
662 lstrcpyn( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
663 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
664 return num;
668 /***********************************************************************
669 * OpenFile (KERNEL.74)
671 HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
673 DOS_FILE *file;
674 HFILE hFileRet;
675 WORD filedatetime[2];
676 const char *unixName, *dosName;
677 char *p;
678 int len, i, unixMode;
680 ofs->cBytes = sizeof(OFSTRUCT);
681 ofs->nErrCode = 0;
682 if (mode & OF_REOPEN) name = ofs->szPathName;
683 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
685 /* First allocate a task handle */
687 if ((hFileRet = FILE_AllocTaskHandle( NULL )) == HFILE_ERROR)
689 ofs->nErrCode = DOS_ExtendedError;
690 dprintf_file( stddeb, "OpenFile: no more task handles.\n" );
691 return HFILE_ERROR;
694 /* OF_PARSE simply fills the structure */
696 if (mode & OF_PARSE)
698 if (!(dosName = DOSFS_GetDosTrueName( name, FALSE ))) goto error;
699 lstrcpyn( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
700 ofs->fFixedDisk = (GetDriveType( dosName[0]-'A' ) != DRIVE_REMOVABLE);
701 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
702 name, ofs->szPathName, hFileRet );
703 /* Return the handle, but close it first */
704 FILE_FreeTaskHandle( hFileRet );
705 /* return hFileRet; */
706 return 0; /* Progman seems to like this better */
709 /* OF_CREATE is completely different from all other options, so
710 handle it first */
712 if (mode & OF_CREATE)
714 if (!(file = FILE_Create( name, 0666, FALSE ))) goto error;
715 lstrcpyn( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
716 sizeof(ofs->szPathName) );
717 goto success;
720 /* Now look for the file */
722 /* First try the current directory */
724 lstrcpyn( ofs->szPathName, name, sizeof(ofs->szPathName) );
725 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
726 goto found;
728 /* Now try some different paths if none was specified */
730 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
732 if (name[1] == ':') name += 2;
733 if ((p = strrchr( name, '\\' ))) name = p + 1;
734 if ((p = strrchr( name, '/' ))) name = p + 1;
735 if (!name[0]) goto not_found;
737 else
739 if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
740 goto not_found;
743 if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
745 /* Try the Windows directory */
747 GetWindowsDirectory( ofs->szPathName, len );
748 strcat( ofs->szPathName, "\\" );
749 strcat( ofs->szPathName, name );
750 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
751 goto found;
753 /* Try the Windows system directory */
755 GetSystemDirectory( ofs->szPathName, len );
756 strcat( ofs->szPathName, "\\" );
757 strcat( ofs->szPathName, name );
758 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
759 goto found;
761 /* Try the path of the current executable */
763 if (GetCurrentTask())
765 GetModuleFileName( GetCurrentTask(), ofs->szPathName, len );
766 if ((p = strrchr( ofs->szPathName, '\\' )))
768 strcpy( p + 1, name );
769 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
770 goto found;
774 /* Try all directories in path */
776 for (i = 0; ; i++)
778 if (!DIR_GetDosPath( i, ofs->szPathName, len )) goto not_found;
779 strcat( ofs->szPathName, "\\" );
780 strcat( ofs->szPathName, name );
781 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
782 break;
785 found:
786 dprintf_file( stddeb, "OpenFile: found '%s'\n", unixName );
787 lstrcpyn( ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE ),
788 sizeof(ofs->szPathName) );
790 if (mode & OF_DELETE)
792 if (unlink( unixName ) == -1) goto not_found;
793 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
794 /* Return the handle, but close it first */
795 FILE_FreeTaskHandle( hFileRet );
796 return hFileRet;
799 switch(mode & 3)
801 case OF_WRITE:
802 unixMode = O_WRONLY; break;
803 case OF_READWRITE:
804 unixMode = O_RDWR; break;
805 case OF_READ:
806 default:
807 unixMode = O_RDONLY; break;
810 if (!(file = FILE_OpenUnixFile( unixName, unixMode ))) goto not_found;
811 filedatetime[0] = file->filedate;
812 filedatetime[1] = file->filetime;
813 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
815 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
817 FILE_Close( file );
818 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
819 /* FIXME: what error here? */
820 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
821 goto error;
824 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
826 if (mode & OF_EXIST)
828 FILE_Close( file );
829 /* Return the handle, but close it first */
830 FILE_FreeTaskHandle( hFileRet );
831 return hFileRet;
834 success: /* We get here if the open was successful */
835 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
836 FILE_SetTaskHandle( hFileRet, file );
837 return hFileRet;
839 not_found: /* We get here if the file does not exist */
840 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
841 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
842 /* fall through */
844 error: /* We get here if there was an error opening the file */
845 ofs->nErrCode = DOS_ExtendedError;
846 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR\n", name );
847 FILE_FreeTaskHandle( hFileRet );
848 return HFILE_ERROR;
852 /***********************************************************************
853 * _lclose (KERNEL.81)
855 HFILE _lclose( HFILE hFile )
857 DOS_FILE *file;
859 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
860 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
861 FILE_Close( file );
862 FILE_FreeTaskHandle( hFile );
863 return 0;
867 /***********************************************************************
868 * _lread (KERNEL.82)
870 INT _lread( HFILE hFile, SEGPTR buffer, WORD count )
872 return (INT)_hread( hFile, buffer, (LONG)count );
876 /***********************************************************************
877 * _lcreat (KERNEL.83)
879 HFILE _lcreat( LPCSTR path, INT attr )
881 DOS_FILE *file;
882 HFILE handle;
883 int mode;
885 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
886 mode = (attr & 1) ? 0444 : 0666;
887 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
888 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
889 FILE_Close( file );
890 return handle;
894 /***********************************************************************
895 * _lcreat_uniq (Not a Windows API)
897 HFILE _lcreat_uniq( LPCSTR path, INT attr )
899 DOS_FILE *file;
900 HFILE handle;
901 int mode;
903 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
904 mode = (attr & 1) ? 0444 : 0666;
905 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
906 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
907 FILE_Close( file );
908 return handle;
912 /***********************************************************************
913 * _llseek (KERNEL.84)
915 LONG _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
917 DOS_FILE *file;
918 int origin, result;
920 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
921 hFile, lOffset, nOrigin);
923 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
924 switch(nOrigin)
926 case 1: origin = SEEK_CUR; break;
927 case 2: origin = SEEK_END; break;
928 default: origin = SEEK_SET; break;
931 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
932 FILE_SetDosError();
933 return result;
937 /***********************************************************************
938 * _lopen (KERNEL.85)
940 HFILE _lopen( LPCSTR path, INT mode )
942 DOS_FILE *file;
943 int unixMode;
944 HFILE handle;
946 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
948 switch(mode & 3)
950 case OF_WRITE:
951 unixMode = O_WRONLY | O_TRUNC;
952 break;
953 case OF_READWRITE:
954 unixMode = O_RDWR;
955 break;
956 case OF_READ:
957 default:
958 unixMode = O_RDONLY;
959 break;
961 if (!(file = FILE_Open( path, unixMode ))) return HFILE_ERROR;
962 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
963 FILE_Close( file );
964 return handle;
968 /***********************************************************************
969 * _lwrite (KERNEL.86)
971 INT _lwrite( HFILE hFile, LPCSTR buffer, WORD count )
973 return (INT)_hwrite( hFile, buffer, (LONG)count );
977 /***********************************************************************
978 * _hread (KERNEL.349)
980 LONG _hread( HFILE hFile, SEGPTR buffer, LONG count )
982 #ifndef WINELIB
983 LONG maxlen;
985 dprintf_file( stddeb, "_hread: %d %08lx %ld\n",
986 hFile, (DWORD)buffer, count );
988 /* Some programs pass a count larger than the allocated buffer */
989 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
990 if (count > maxlen) count = maxlen;
991 #endif
992 return FILE_Read( hFile, PTR_SEG_TO_LIN(buffer), count );
996 /***********************************************************************
997 * _hwrite (KERNEL.350)
999 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
1001 DOS_FILE *file;
1002 LONG result;
1004 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
1006 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1008 if (count == 0) /* Expand or truncate at current position */
1009 result = ftruncate( file->unix_handle,
1010 lseek( file->unix_handle, 0, SEEK_CUR ) );
1011 else
1012 result = write( file->unix_handle, buffer, count );
1014 if (result == -1) FILE_SetDosError();
1015 return result;
1019 /***********************************************************************
1020 * SetHandleCount (KERNEL.199)
1022 WORD SetHandleCount( WORD count )
1024 HANDLE hPDB = GetCurrentPDB();
1025 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1026 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1027 WORD i;
1029 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
1031 if (count < 20) count = 20; /* No point in going below 20 */
1032 else if (count > 254) count = 254;
1034 /* If shrinking the table, make sure all extra file handles are closed */
1035 if (count < pdb->nbFiles)
1037 for (i = count; i < pdb->nbFiles; i++)
1038 if (files[i] != 0xff) /* File open */
1040 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
1041 SA_Abort, EL_Disk );
1042 return pdb->nbFiles;
1046 if (count == 20)
1048 if (pdb->nbFiles > 20)
1050 memcpy( pdb->fileHandles, files, 20 );
1051 #ifdef WINELIB
1052 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1053 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1054 #else
1055 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1056 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1057 GlobalHandleToSel( hPDB ) );
1058 #endif
1059 pdb->nbFiles = 20;
1062 else /* More than 20, need a new file handles table */
1064 BYTE *newfiles;
1065 #ifdef WINELIB
1066 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1067 #else
1068 HANDLE newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1069 if (!newhandle)
1071 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1072 return pdb->nbFiles;
1074 newfiles = (BYTE *)GlobalLock16( newhandle );
1075 #endif /* WINELIB */
1076 if (count > pdb->nbFiles)
1078 memcpy( newfiles, files, pdb->nbFiles );
1079 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1081 else memcpy( newfiles, files, count );
1082 #ifdef WINELIB
1083 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1084 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1085 #else
1086 if (pdb->nbFiles > 20)
1087 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1088 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1089 #endif /* WINELIB */
1090 pdb->nbFiles = count;
1092 return pdb->nbFiles;