2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
13 #include <sys/errno.h>
20 #include "directory.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) */
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 /***********************************************************************
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
);
69 else file
= ++FILE_LastUsed
;
71 file
->unix_handle
= -1;
72 file
->unix_name
= NULL
;
77 /***********************************************************************
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
;
95 /***********************************************************************
98 * Return a pointer to the current PDB files array.
100 static void FILE_GetPDBFiles( BYTE
**files
, WORD
*nbFiles
)
104 if ((pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() )) != NULL
)
106 *files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
107 *nbFiles
= pdb
->nbFiles
;
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
)
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
++);
131 { /* No more handles or files */
132 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
135 *fp
= dos_file
? (BYTE
)(dos_file
- DOSFiles
) : 0;
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
)
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",
162 files
[handle
] = 0xff;
166 /***********************************************************************
169 * Set the value of a task handle (no error checking).
171 static void FILE_SetTaskHandle( HFILE handle
, DOS_FILE
*file
)
176 FILE_GetPDBFiles( &files
, &nbFiles
);
177 files
[handle
] = (BYTE
)(file
- DOSFiles
);
181 /***********************************************************************
184 * Return the DOS file associated to a task file handle.
186 static DOS_FILE
*FILE_GetFile( HFILE handle
)
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
);
204 int FILE_GetUnixHandle( HFILE hFile
)
208 if (!(file
= FILE_GetFile( hFile
))) return -1;
209 return file
->unix_handle
;
213 /***********************************************************************
216 * Close all open files of a given PDB. Used on task termination.
218 void FILE_CloseAllFiles( HANDLE hPDB
)
222 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
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
] );
235 /***********************************************************************
238 * Set the DOS error code from errno.
240 void FILE_SetDosError(void)
242 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
246 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
249 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
252 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
257 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
260 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
263 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
266 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
270 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
273 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
276 perror( "int21: unknown errno" );
277 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
283 /***********************************************************************
286 static DOS_FILE
*FILE_OpenUnixFile( const char *name
, int mode
)
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))
306 if (S_ISDIR(st
.st_mode
))
308 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
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
);
321 /***********************************************************************
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
);
339 else if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return NULL
;
340 return FILE_OpenUnixFile( unixName
, mode
);
344 /***********************************************************************
347 static DOS_FILE
*FILE_Create( LPCSTR path
, int mode
, int unique
)
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
);
361 if (!(file
= FILE_Alloc())) return NULL
;
363 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
)))
368 if ((file
->unix_handle
= open( unixName
,
369 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
377 /* File created OK, now fill the DOS_FILE */
379 file
->unix_name
= xstrdup( unixName
);
380 DOSFS_ToDosDateTime( time(NULL
), &file
->filedate
, &file
->filetime
);
385 /***********************************************************************
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
);
401 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
402 if (unlink( unixName
) == -1)
411 /***********************************************************************
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
)
421 if (stat( unixName
, &st
) == -1)
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
);
433 /***********************************************************************
436 * Get the date and time of a file.
438 int FILE_GetDateTime( HFILE hFile
, WORD
*pdate
, WORD
*ptime
, BOOL refresh
)
442 if (!(file
= FILE_GetFile( hFile
))) return 0;
446 if (fstat( file
->unix_handle
, &st
) == -1)
451 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
453 *pdate
= file
->filedate
;
454 *ptime
= file
->filetime
;
459 /***********************************************************************
462 * Set the date and time of a file.
464 int FILE_SetDateTime( HFILE hFile
, WORD date
, WORD time
)
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;
485 /***********************************************************************
488 int FILE_Sync( HFILE hFile
)
492 if (!(file
= FILE_GetFile( hFile
))) return 0;
493 if (fsync( file
->unix_handle
) != -1) return 1;
499 /***********************************************************************
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
);
514 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
515 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
524 /***********************************************************************
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
);
539 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
540 if (rmdir( unixName
) == -1)
549 /***********************************************************************
552 * dup() function for DOS handles.
554 HFILE
FILE_Dup( HFILE hFile
)
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
);
567 /***********************************************************************
570 * dup2() function for DOS handles.
572 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
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
);
586 if (files
[hFile2
] < MAX_OPEN_FILES
)
588 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
590 FILE_Close( &DOSFiles
[files
[hFile2
]] );
592 files
[hFile2
] = (BYTE
)(file
- DOSFiles
);
594 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n", hFile2
);
599 /***********************************************************************
602 LONG
FILE_Read( HFILE hFile
, void *buffer
, LONG count
)
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)
616 /***********************************************************************
617 * GetTempFileName (KERNEL.97)
619 INT
GetTempFileName( BYTE drive
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
622 UINT num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
625 if (drive
& TF_FORCEDRIVE
)
627 sprintf( buffer
, "%c:", drive
& ~TF_FORCEDRIVE
);
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
);
641 lstrcpyn( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
642 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
646 /* Now try to create it */
651 if ((file
= FILE_Create( buffer
, 0666, TRUE
)) != NULL
)
652 { /* We created it */
653 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
657 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
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
);
668 /***********************************************************************
669 * OpenFile (KERNEL.74)
671 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
675 WORD filedatetime
[2];
676 const char *unixName
, *dosName
;
678 int len
, i
, unixMode
;
680 ofs
->cBytes
= sizeof(OFSTRUCT
);
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" );
694 /* OF_PARSE simply fills the structure */
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
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
) );
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
)
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
;
739 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
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
)
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
)
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
)))
774 /* Try all directories in path */
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
)
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
);
802 unixMode
= O_WRONLY
; break;
804 unixMode
= O_RDWR
; break;
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
) ))
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
);
824 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
829 /* Return the handle, but close it first */
830 FILE_FreeTaskHandle( 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
);
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
);
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
);
852 /***********************************************************************
853 * _lclose (KERNEL.81)
855 HFILE
_lclose( HFILE hFile
)
859 dprintf_file( stddeb
, "_lclose: handle %d\n", hFile
);
860 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
862 FILE_FreeTaskHandle( hFile
);
867 /***********************************************************************
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
)
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
)
894 /***********************************************************************
895 * _lcreat_uniq (Not a Windows API)
897 HFILE
_lcreat_uniq( LPCSTR path
, INT attr
)
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
)
912 /***********************************************************************
913 * _llseek (KERNEL.84)
915 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
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
;
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)
937 /***********************************************************************
940 HFILE
_lopen( LPCSTR path
, INT mode
)
946 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
951 unixMode
= O_WRONLY
| O_TRUNC
;
961 if (!(file
= FILE_Open( path
, unixMode
))) return HFILE_ERROR
;
962 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
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
)
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
;
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
)
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
) );
1012 result
= write( file
->unix_handle
, buffer
, count
);
1014 if (result
== -1) FILE_SetDosError();
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
);
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
;
1048 if (pdb
->nbFiles
> 20)
1050 memcpy( pdb
->fileHandles
, files
, 20 );
1052 GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1053 pdb
->fileHandlesPtr
= (SEGPTR
)pdb
->fileHandles
;
1055 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1056 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1057 GlobalHandleToSel( hPDB
) );
1062 else /* More than 20, need a new file handles table */
1066 newfiles
= (BYTE
*)GlobalAlloc32( GMEM_FIXED
, count
);
1068 HANDLE newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
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
);
1083 if (pdb
->nbFiles
> 20) GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1084 pdb
->fileHandlesPtr
= (SEGPTR
)newfiles
;
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
;