2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
12 #include <sys/errno.h>
18 #include "directory.h"
29 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
37 /* Global files array */
38 static DOS_FILE DOSFiles
[MAX_OPEN_FILES
];
41 /***********************************************************************
44 * Allocate a file from the DOS files array.
46 static BYTE
FILE_AllocDOSFile( int unix_handle
)
49 for (i
= 0; i
< MAX_OPEN_FILES
; i
++) if (!DOSFiles
[i
].mode
)
51 DOSFiles
[i
].unix_handle
= unix_handle
;
59 /***********************************************************************
62 * Free a file from the DOS files array.
64 static BOOL
FILE_FreeDOSFile( BYTE handle
)
66 if (handle
>= MAX_OPEN_FILES
) return FALSE
;
67 if (!DOSFiles
[handle
].mode
) return FALSE
;
68 DOSFiles
[handle
].mode
= 0;
73 /***********************************************************************
76 * Return the Unix handle for a global DOS file handle.
78 static int FILE_GetUnixHandle( BYTE handle
)
80 if (handle
>= MAX_OPEN_FILES
) return -1;
81 if (!DOSFiles
[handle
].mode
) return -1;
82 return DOSFiles
[handle
].unix_handle
;
86 /***********************************************************************
87 * FILE_AllocTaskHandle
89 * Allocate a per-task file handle.
91 static HFILE
FILE_AllocTaskHandle( int unix_handle
)
93 PDB
*pdb
= (PDB
*)GlobalLock( GetCurrentPDB() );
99 fprintf(stderr
,"FILE_MakeTaskHandle: internal error, no current PDB.\n");
102 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
103 fp
= files
+ 1; /* Don't use handle 0, as some programs don't like it */
104 for (i
= pdb
->nbFiles
- 1; (i
> 0) && (*fp
!= 0xff); i
--, fp
++);
105 if (!i
|| (*fp
= FILE_AllocDOSFile( unix_handle
)) == 0xff)
106 { /* No more handles or files */
107 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
111 "FILE_AllocTaskHandle: returning task handle %d, file %d for unix handle %d file %d of %d \n",
112 (fp
- files
), *fp
, unix_handle
, pdb
->nbFiles
- i
, pdb
->nbFiles
);
113 return (HFILE
)(fp
- files
);
117 /***********************************************************************
118 * FILE_FreeTaskHandle
120 * Free a per-task file handle.
122 static void FILE_FreeTaskHandle( HFILE handle
)
124 PDB
*pdb
= (PDB
*)GlobalLock( GetCurrentPDB() );
129 fprintf(stderr
,"FILE_FreeTaskHandle: internal error, no current PDB.\n");
132 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
133 dprintf_file( stddeb
,"FILE_FreeTaskHandle: dos=%d file=%d\n",
134 handle
, files
[handle
] );
135 if ((handle
< 0) || (handle
>= (INT
)pdb
->nbFiles
) ||
136 !FILE_FreeDOSFile( files
[handle
] ))
138 fprintf( stderr
, "FILE_FreeTaskHandle: invalid file handle %d\n",
142 files
[handle
] = 0xff;
146 /***********************************************************************
147 * FILE_GetUnixTaskHandle
149 * Return the Unix file handle associated to a task file handle.
151 int FILE_GetUnixTaskHandle( HFILE handle
)
153 PDB
*pdb
= (PDB
*)GlobalLock( GetCurrentPDB() );
159 fprintf(stderr
,"FILE_GetUnixHandle: internal error, no current PDB.\n");
162 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
163 if ((handle
< 0) || (handle
>= (INT
)pdb
->nbFiles
) ||
164 ((unix_handle
= FILE_GetUnixHandle( files
[handle
] )) == -1))
166 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
173 /***********************************************************************
176 * Close all open files of a given PDB. Used on task termination.
178 void FILE_CloseAllFiles( HANDLE hPDB
)
182 PDB
*pdb
= (PDB
*)GlobalLock( hPDB
);
186 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
187 dprintf_file(stddeb
,"FILE_CloseAllFiles: closing %d files\n",pdb
->nbFiles
);
188 for (count
= pdb
->nbFiles
; count
> 0; count
--, files
++)
192 if ((unix_handle
= FILE_GetUnixHandle( *files
)) != -1)
194 close( unix_handle
);
195 FILE_FreeDOSFile( *files
);
203 /***********************************************************************
206 * Set the DOS error code from errno.
208 void FILE_SetDosError(void)
210 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
214 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
217 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
220 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
225 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
228 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
231 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
234 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
238 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
241 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
244 perror( "int21: unknown errno" );
245 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
251 /***********************************************************************
254 static int FILE_OpenUnixFile( const char *name
, int mode
)
259 if ((handle
= open( name
, mode
)) == -1)
261 if (Options
.allowReadOnly
&& (mode
== O_RDWR
))
263 if ((handle
= open( name
, O_RDONLY
)) != -1)
264 fprintf( stderr
, "Warning: could not open %s for writing, opening read-only.\n", name
);
267 if (handle
!= -1) /* Make sure it's not a directory */
269 if ((fstat( handle
, &st
) == -1))
275 else if (S_ISDIR(st
.st_mode
))
277 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
286 /***********************************************************************
289 int FILE_Open( LPCSTR path
, int mode
)
291 const char *unixName
;
293 dprintf_file(stddeb
, "FILE_Open: '%s' %04x\n", path
, mode
);
294 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
296 dprintf_file( stddeb
, "FILE_Open: opening device '%s'\n", unixName
);
297 if (!unixName
[0]) /* Non-existing device */
299 dprintf_file(stddeb
, "FILE_Open: Non-existing device\n");
300 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
304 else if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return -1;
305 return FILE_OpenUnixFile( unixName
, mode
);
309 /***********************************************************************
312 int FILE_Create( LPCSTR path
, int mode
, int unique
)
314 const char *unixName
;
317 dprintf_file(stddeb
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
319 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
321 dprintf_file(stddeb
, "FILE_Create: creating device '%s'!\n", unixName
);
322 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
326 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return -1;
327 if ((handle
= open( unixName
,
328 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
335 /***********************************************************************
338 int FILE_Unlink( LPCSTR path
)
340 const char *unixName
;
342 dprintf_file(stddeb
, "FILE_Unlink: '%s'\n", path
);
344 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
346 dprintf_file(stddeb
, "FILE_Unlink: removing device '%s'!\n", unixName
);
347 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
351 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
352 if (unlink( unixName
) == -1)
361 /***********************************************************************
364 * Stat a Unix path name. Return 1 if OK.
366 int FILE_Stat( LPCSTR unixName
, BYTE
*pattr
, DWORD
*psize
,
367 WORD
*pdate
, WORD
*ptime
)
371 if (stat( unixName
, &st
) == -1)
376 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
377 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
378 DOSFS_ToDosDateTime( &st
.st_mtime
, pdate
, ptime
);
383 /***********************************************************************
386 * Stat a DOS handle. Return 1 if OK.
388 int FILE_Fstat( HFILE hFile
, BYTE
*pattr
, DWORD
*psize
,
389 WORD
*pdate
, WORD
*ptime
)
394 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return 0;
395 if (fstat( handle
, &st
) == -1)
400 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
401 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
402 DOSFS_ToDosDateTime( &st
.st_mtime
, pdate
, ptime
);
407 /***********************************************************************
410 int FILE_MakeDir( LPCSTR path
)
412 const char *unixName
;
414 dprintf_file(stddeb
, "FILE_MakeDir: '%s'\n", path
);
416 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
418 dprintf_file(stddeb
, "FILE_MakeDir: device '%s'!\n", unixName
);
419 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
422 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
423 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
432 /***********************************************************************
435 int FILE_RemoveDir( LPCSTR path
)
437 const char *unixName
;
439 dprintf_file(stddeb
, "FILE_RemoveDir: '%s'\n", path
);
441 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
443 dprintf_file(stddeb
, "FILE_RemoveDir: device '%s'!\n", unixName
);
444 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
447 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
448 if (rmdir( unixName
) == -1)
457 /***********************************************************************
460 * dup() function for DOS handles.
462 HFILE
FILE_Dup( HFILE hFile
)
464 int handle
, newhandle
;
467 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return HFILE_ERROR
;
468 dprintf_file( stddeb
, "FILE_Dup for handle %d\n",handle
);
469 if ((newhandle
= dup(handle
)) == -1)
474 if ((dosHandle
= FILE_AllocTaskHandle( newhandle
)) == HFILE_ERROR
)
476 dprintf_file( stddeb
, "FILE_Dup return handle %d\n",dosHandle
);
481 /***********************************************************************
484 * dup2() function for DOS handles.
486 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
488 PDB
*pdb
= (PDB
*)GlobalLock( GetCurrentPDB() );
490 int handle
, newhandle
;
492 if ((handle
= FILE_GetUnixTaskHandle( hFile1
)) == -1) return HFILE_ERROR
;
493 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n",handle
);
494 if ((hFile2
< 0) || (hFile2
>= (INT
)pdb
->nbFiles
))
496 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
500 if ((newhandle
= dup(handle
)) == -1)
505 if (newhandle
>= 0xff)
507 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
511 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
512 if (files
[hFile2
] != 0xff)
514 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
516 close( files
[hFile2
] );
518 files
[hFile2
] = (BYTE
)newhandle
;
519 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n",newhandle
);
524 /***********************************************************************
527 * Implementation of API function OpenFile(). Returns a Unix file handle.
529 int FILE_OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
531 const char *unixName
, *dosName
;
533 int handle
, len
, i
, unixMode
;
536 ofs
->cBytes
= sizeof(OFSTRUCT
);
538 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
539 dprintf_file( stddeb
, "FILE_Openfile: %s %04x\n", name
, mode
);
541 /* OF_PARSE simply fills the structure */
545 if (!(dosName
= DOSFS_GetDosTrueName( name
, FALSE
)))
547 ofs
->nErrCode
= DOS_ExtendedError
;
548 dprintf_file( stddeb
, "FILE_Openfile: %s return = -1\n", name
);
551 lstrcpyn( ofs
->szPathName
, dosName
, sizeof(ofs
->szPathName
) );
552 ofs
->fFixedDisk
= (GetDriveType( dosName
[0]-'A' ) != DRIVE_REMOVABLE
);
553 dprintf_file( stddeb
, "FILE_Openfile: %s return = 0\n", name
);
557 /* OF_CREATE is completely different from all other options, so
560 if (mode
& OF_CREATE
)
562 if ((unixName
= DOSFS_GetUnixFileName( name
, FALSE
)) == NULL
)
564 ofs
->nErrCode
= DOS_ExtendedError
;
565 dprintf_file( stddeb
, "FILE_Openfile: %s return = -1\n", name
);
568 dprintf_file( stddeb
, "FILE_OpenFile: creating '%s'\n", unixName
);
569 handle
= open( unixName
, O_TRUNC
| O_RDWR
| O_CREAT
, 0666 );
573 ofs
->nErrCode
= DOS_ExtendedError
;
574 dprintf_file( stddeb
, "FILE_Openfile: %s return = -1\n", name
);
577 lstrcpyn( ofs
->szPathName
, DOSFS_GetDosTrueName( name
, FALSE
),
578 sizeof(ofs
->szPathName
) );
579 dprintf_file( stddeb
, "FILE_Openfile: %s return = %d \n", name
, handle
);
583 /* Now look for the file */
585 /* First try the current directory */
587 lstrcpyn( ofs
->szPathName
, name
, sizeof(ofs
->szPathName
) );
588 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
591 /* Now try some different paths if none was specified */
593 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
595 if (name
[1] == ':') name
+= 2;
596 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
597 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
598 if (!name
[0]) goto not_found
;
602 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
606 if ((len
= sizeof(ofs
->szPathName
) - strlen(name
) - 1) < 0) goto not_found
;
608 /* Try the Windows directory */
610 GetWindowsDirectory( ofs
->szPathName
, len
);
611 strcat( ofs
->szPathName
, "\\" );
612 strcat( ofs
->szPathName
, name
);
613 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
616 /* Try the Windows system directory */
618 GetSystemDirectory( ofs
->szPathName
, len
);
619 strcat( ofs
->szPathName
, "\\" );
620 strcat( ofs
->szPathName
, name
);
621 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
624 /* Try the path of the current executable */
626 if (GetCurrentTask())
628 GetModuleFileName( GetCurrentTask(), ofs
->szPathName
, len
);
629 if ((p
= strrchr( ofs
->szPathName
, '\\' )))
631 strcpy( p
+ 1, name
);
632 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)))
637 /* Try all directories in path */
641 if (!DIR_GetDosPath( i
, ofs
->szPathName
, len
)) break;
642 strcat( ofs
->szPathName
, "\\" );
643 strcat( ofs
->szPathName
, name
);
644 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
649 dprintf_file( stddeb
, "FILE_OpenFile: '%s' not found\n", name
);
650 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
651 ofs
->nErrCode
= ER_FileNotFound
;
652 dprintf_file( stddeb
, "FILE_Openfile: %s return =-1\n", name
);
656 dprintf_file( stddeb
, "FILE_OpenFile: found '%s'\n", unixName
);
657 lstrcpyn( ofs
->szPathName
, DOSFS_GetDosTrueName( ofs
->szPathName
, FALSE
),
658 sizeof(ofs
->szPathName
) );
662 dprintf_file( stddeb
, "FILE_Openfile: %s return = 0\n", name
);
665 if (mode
& OF_DELETE
)
667 if (unlink( unixName
) == -1) goto not_found
;
668 dprintf_file( stddeb
, "FILE_Openfile: %s return = 0\n", name
);
675 unixMode
= O_WRONLY
; break;
677 unixMode
= O_RDWR
; break;
680 unixMode
= O_RDONLY
; break;
683 if ((handle
= FILE_OpenUnixFile( unixName
, unixMode
)) == -1)
686 if (fstat( handle
, &st
) != -1)
688 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
690 if (memcmp( ofs
->reserved
, &st
.st_mtime
, sizeof(ofs
->reserved
) ))
692 dprintf_file( stddeb
, "FILE_Openfile: %s return = -1\n", name
);
696 memcpy( ofs
->reserved
, &st
.st_mtime
, sizeof(ofs
->reserved
) );
699 if (mode
& OF_EXIST
) close( handle
);
700 dprintf_file( stddeb
, "FILE_Openfile: %s return = %d\n", name
,handle
);
706 /***********************************************************************
709 LONG
FILE_Read( HFILE hFile
, LPSTR buffer
, LONG count
)
714 dprintf_file( stddeb
, "FILE_Read: %d %p %ld\n", hFile
, buffer
, count
);
715 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return -1;
716 if (!count
) return 0;
717 if ((result
= read( handle
, buffer
, count
)) == -1) FILE_SetDosError();
722 /***********************************************************************
723 * GetTempFileName (KERNEL.97)
725 INT
GetTempFileName( BYTE drive
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
728 UINT num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
731 if (drive
& TF_FORCEDRIVE
)
733 sprintf( buffer
, "%c:", drive
& ~TF_FORCEDRIVE
);
737 DIR_GetTempDosDir( buffer
, 132 ); /* buffer must be at least 144 */
738 strcat( buffer
, "\\" );
741 p
= buffer
+ strlen(buffer
);
742 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
743 sprintf( p
, "%04x.tmp", num
);
747 lstrcpyn( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
748 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
752 /* Now try to create it */
756 if ((handle
= FILE_Create( buffer
, 0666, TRUE
)) != -1)
757 { /* We created it */
758 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
762 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
764 sprintf( p
, "%04x.tmp", num
);
765 } while (num
!= (unique
& 0xffff));
767 lstrcpyn( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
768 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
773 /***********************************************************************
774 * OpenFile (KERNEL.74)
776 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
781 dprintf_file( stddeb
, "OpenFile %s \n",name
);
782 if ((unixHandle
= FILE_OpenFile( name
, ofs
, mode
)) == -1)
784 if ((handle
= FILE_AllocTaskHandle( unixHandle
)) == HFILE_ERROR
)
786 ofs
->nErrCode
= DOS_ExtendedError
;
787 if (unixHandle
) close( unixHandle
);
789 if (!unixHandle
) FILE_FreeTaskHandle( handle
);
794 /***********************************************************************
795 * _lclose (KERNEL.81)
797 HFILE
_lclose( HFILE hFile
)
802 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return HFILE_ERROR
;
803 dprintf_file( stddeb
, "_lclose: doshandle %d unixhandle %d\n", hFile
,handle
);
806 fprintf( stderr
, "_lclose: internal error: closing handle %d\n", handle
);
809 FILE_FreeTaskHandle( hFile
);
815 /***********************************************************************
818 INT
_lread( HFILE hFile
, SEGPTR buffer
, WORD count
)
820 return (INT
)_hread( hFile
, buffer
, (LONG
)count
);
824 /***********************************************************************
825 * _lcreat (KERNEL.83)
827 INT
_lcreat( LPCSTR path
, INT attr
)
829 int unixHandle
, mode
;
832 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
833 mode
= (attr
& 1) ? 0444 : 0666;
834 if ((unixHandle
= FILE_Create( path
, mode
, FALSE
)) == -1)
836 if ((handle
= FILE_AllocTaskHandle( unixHandle
)) == HFILE_ERROR
)
842 /***********************************************************************
843 * _lcreat_uniq (Not a Windows API)
845 INT
_lcreat_uniq( LPCSTR path
, INT attr
)
847 int unixHandle
, mode
;
850 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
851 mode
= (attr
& 1) ? 0444 : 0666;
852 if ((unixHandle
= FILE_Create( path
, mode
, TRUE
)) == -1)
854 if ((handle
= FILE_AllocTaskHandle( unixHandle
)) == HFILE_ERROR
)
860 /***********************************************************************
861 * _llseek (KERNEL.84)
863 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
865 int handle
, origin
, result
;
867 dprintf_file( stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
868 hFile
, lOffset
, nOrigin
);
870 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return HFILE_ERROR
;
873 case 1: origin
= SEEK_CUR
; break;
874 case 2: origin
= SEEK_END
; break;
875 default: origin
= SEEK_SET
; break;
878 if ((result
= lseek( handle
, lOffset
, origin
)) == -1) FILE_SetDosError();
879 return (result
== -1) ? HFILE_ERROR
: result
;
883 /***********************************************************************
886 HFILE
_lopen( LPCSTR path
, INT mode
)
892 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
897 unixMode
= O_WRONLY
| O_TRUNC
;
907 if ((unixHandle
= FILE_Open( path
, unixMode
)) == -1) return HFILE_ERROR
;
908 if ((handle
= FILE_AllocTaskHandle( unixHandle
)) == HFILE_ERROR
)
914 /***********************************************************************
915 * _lwrite (KERNEL.86)
917 INT
_lwrite( HFILE hFile
, LPCSTR buffer
, WORD count
)
919 return (INT
)_hwrite( hFile
, buffer
, (LONG
)count
);
923 /***********************************************************************
924 * _hread (KERNEL.349)
926 LONG
_hread( HFILE hFile
, SEGPTR buffer
, LONG count
)
931 dprintf_file( stddeb
, "_hread: %d %08lx %ld\n",
932 hFile
, (DWORD
)buffer
, count
);
934 /* Some programs pass a count larger than the allocated buffer */
935 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
936 if (count
> maxlen
) count
= maxlen
;
938 return FILE_Read( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
942 /***********************************************************************
943 * _hwrite (KERNEL.350)
945 LONG
_hwrite( HFILE hFile
, LPCSTR buffer
, LONG count
)
950 dprintf_file( stddeb
, "_hwrite: %d %p %ld\n", hFile
, buffer
, count
);
952 if ((handle
= FILE_GetUnixTaskHandle( hFile
)) == -1) return HFILE_ERROR
;
954 if (count
== 0) /* Expand or truncate at current position */
955 result
= ftruncate( handle
, lseek( handle
, 0, SEEK_CUR
) );
957 result
= write( handle
, buffer
, count
);
959 if (result
== -1) FILE_SetDosError();
960 return (result
== -1) ? HFILE_ERROR
: result
;
964 /***********************************************************************
965 * SetHandleCount (KERNEL.199)
967 WORD
SetHandleCount( WORD count
)
969 HANDLE hPDB
= GetCurrentPDB();
970 PDB
*pdb
= (PDB
*)GlobalLock( hPDB
);
971 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
974 dprintf_file( stddeb
, "SetHandleCount(%d)\n", count
);
976 if (count
< 20) count
= 20; /* No point in going below 20 */
977 else if (count
> 254) count
= 254;
979 /* If shrinking the table, make sure all extra file handles are closed */
980 if (count
< pdb
->nbFiles
)
982 for (i
= count
; i
< pdb
->nbFiles
; i
++)
983 if (files
[i
] != 0xff) /* File open */
985 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
,
993 if (pdb
->nbFiles
> 20)
995 memcpy( pdb
->fileHandles
, files
, 20 );
997 GlobalFree( pdb
->fileHandlesPtr
);
998 pdb
->fileHandlesPtr
= pdb
->fileHandles
;
1000 GlobalFree( GlobalHandle( SELECTOROF(pdb
->fileHandlesPtr
) ));
1001 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1002 GlobalHandleToSel( hPDB
) );
1007 else /* More than 20, need a new file handles table */
1010 HANDLE newhandle
= GlobalAlloc( GMEM_MOVEABLE
, count
);
1013 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1014 return pdb
->nbFiles
;
1016 newfiles
= (BYTE
*)GlobalLock( newhandle
);
1017 if (count
> pdb
->nbFiles
)
1019 memcpy( newfiles
, files
, pdb
->nbFiles
);
1020 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1022 else memcpy( newfiles
, files
, count
);
1024 if (pdb
->nbFiles
> 20) GlobalFree( pdb
->fileHandlesPtr
);
1026 if (pdb
->nbFiles
> 20)
1027 GlobalFree( GlobalHandle( SELECTOROF(pdb
->fileHandlesPtr
) ));
1029 pdb
->fileHandlesPtr
= WIN16_GlobalLock( newhandle
);
1030 pdb
->nbFiles
= count
;
1032 return pdb
->nbFiles
;