2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
21 #include "directory.h"
33 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
35 typedef struct tagDOS_FILE
37 struct tagDOS_FILE
*next
;
38 int count
; /* Usage count (0 if free) */
46 /* Global files array */
47 static DOS_FILE DOSFiles
[MAX_OPEN_FILES
];
49 static DOS_FILE
*FILE_First
= DOSFiles
;
50 static DOS_FILE
*FILE_LastUsed
= DOSFiles
;
52 /* Small file handles array for boot-up, before the first PDB is created */
53 #define MAX_BOOT_HANDLES 4
54 static BYTE bootFileHandles
[MAX_BOOT_HANDLES
] = { 0xff, 0xff, 0xff, 0xff };
56 /***********************************************************************
59 * Allocate a DOS file.
61 static DOS_FILE
*FILE_Alloc(void)
63 DOS_FILE
*file
= FILE_First
;
64 if (file
) FILE_First
= file
->next
;
65 else if (FILE_LastUsed
>= &DOSFiles
[MAX_OPEN_FILES
-1])
67 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
70 else file
= ++FILE_LastUsed
;
72 file
->unix_handle
= -1;
73 file
->unix_name
= NULL
;
78 /***********************************************************************
83 static BOOL
FILE_Close( DOS_FILE
*file
)
85 if (!file
->count
) return FALSE
;
86 if (--file
->count
> 0) return TRUE
;
87 /* Now really close the file */
88 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
89 if (file
->unix_name
) free( file
->unix_name
);
90 file
->next
= FILE_First
;
96 /***********************************************************************
99 * Return a pointer to the current PDB files array.
101 static void FILE_GetPDBFiles( BYTE
**files
, WORD
*nbFiles
)
105 if ((pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() )) != NULL
)
107 *files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
108 *nbFiles
= pdb
->nbFiles
;
112 *files
= bootFileHandles
;
113 *nbFiles
= MAX_BOOT_HANDLES
;
118 /***********************************************************************
119 * FILE_AllocTaskHandle
121 * Allocate a task file handle for a DOS file.
123 static HFILE
FILE_AllocTaskHandle( DOS_FILE
*dos_file
)
128 FILE_GetPDBFiles( &files
, &nbFiles
);
129 fp
= files
+ 1; /* Don't use handle 0, as some programs don't like it */
130 for (i
= nbFiles
- 1; (i
> 0) && (*fp
!= 0xff); i
--, fp
++);
132 { /* No more handles or files */
133 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
136 *fp
= dos_file
? (BYTE
)(dos_file
- DOSFiles
) : 0;
138 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
139 (fp
- files
), *fp
, nbFiles
- i
, nbFiles
);
140 return (HFILE
)(fp
- files
);
144 /***********************************************************************
145 * FILE_FreeTaskHandle
147 * Free a per-task file handle.
149 static void FILE_FreeTaskHandle( HFILE handle
)
154 FILE_GetPDBFiles( &files
, &nbFiles
);
155 dprintf_file( stddeb
,"FILE_FreeTaskHandle: dos=%d file=%d\n",
156 handle
, files
[handle
] );
157 if ((handle
< 0) || (handle
>= (INT
)nbFiles
))
159 fprintf( stderr
, "FILE_FreeTaskHandle: invalid file handle %d\n",
163 files
[handle
] = 0xff;
167 /***********************************************************************
170 * Set the value of a task handle (no error checking).
172 static void FILE_SetTaskHandle( HFILE handle
, DOS_FILE
*file
)
177 FILE_GetPDBFiles( &files
, &nbFiles
);
178 files
[handle
] = (BYTE
)(file
- DOSFiles
);
182 /***********************************************************************
185 * Return the DOS file associated to a task file handle.
187 static DOS_FILE
*FILE_GetFile( HFILE handle
)
193 FILE_GetPDBFiles( &files
, &nbFiles
);
194 if ((handle
< 0) || (handle
>= (INT
)nbFiles
) ||
195 (files
[handle
] >= MAX_OPEN_FILES
) ||
196 !(file
= &DOSFiles
[files
[handle
]])->count
)
198 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
205 int FILE_GetUnixHandle( HFILE hFile
)
209 if (!(file
= FILE_GetFile( hFile
))) return -1;
210 return file
->unix_handle
;
214 /***********************************************************************
217 * Close all open files of a given PDB. Used on task termination.
219 void FILE_CloseAllFiles( HANDLE hPDB
)
223 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
226 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
227 dprintf_file(stddeb
,"FILE_CloseAllFiles: closing %d files\n",pdb
->nbFiles
);
228 for (count
= pdb
->nbFiles
; count
> 0; count
--, files
++)
230 if (*files
< MAX_OPEN_FILES
) FILE_Close( &DOSFiles
[*files
] );
236 /***********************************************************************
239 * Set the DOS error code from errno.
241 void FILE_SetDosError(void)
243 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
247 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
250 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
253 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
258 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
261 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
264 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
267 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
271 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
274 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
277 perror( "int21: unknown errno" );
278 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
284 /***********************************************************************
287 static DOS_FILE
*FILE_OpenUnixFile( const char *name
, int mode
)
292 if (!(file
= FILE_Alloc())) return NULL
;
293 if ((file
->unix_handle
= open( name
, mode
)) == -1)
295 if (Options
.allowReadOnly
&& (mode
== O_RDWR
))
297 if ((file
->unix_handle
= open( name
, O_RDONLY
)) != -1)
298 fprintf( stderr
, "Warning: could not open %s for writing, opening read-only.\n", name
);
301 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
307 if (S_ISDIR(st
.st_mode
))
309 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
314 /* File opened OK, now fill the DOS_FILE */
316 file
->unix_name
= xstrdup( name
);
317 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
322 /***********************************************************************
325 static DOS_FILE
*FILE_Open( LPCSTR path
, int mode
)
327 const char *unixName
;
329 dprintf_file(stddeb
, "FILE_Open: '%s' %04x\n", path
, mode
);
330 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
332 dprintf_file( stddeb
, "FILE_Open: opening device '%s'\n", unixName
);
333 if (!unixName
[0]) /* Non-existing device */
335 dprintf_file(stddeb
, "FILE_Open: Non-existing device\n");
336 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
340 else if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return NULL
;
341 return FILE_OpenUnixFile( unixName
, mode
);
345 /***********************************************************************
348 static DOS_FILE
*FILE_Create( LPCSTR path
, int mode
, int unique
)
351 const char *unixName
;
353 dprintf_file(stddeb
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
355 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
357 dprintf_file(stddeb
, "FILE_Create: creating device '%s'!\n", unixName
);
358 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
362 if (!(file
= FILE_Alloc())) return NULL
;
364 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
)))
369 if ((file
->unix_handle
= open( unixName
,
370 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
378 /* File created OK, now fill the DOS_FILE */
380 file
->unix_name
= xstrdup( unixName
);
381 DOSFS_ToDosDateTime( time(NULL
), &file
->filedate
, &file
->filetime
);
386 /***********************************************************************
389 int FILE_Unlink( LPCSTR path
)
391 const char *unixName
;
393 dprintf_file(stddeb
, "FILE_Unlink: '%s'\n", path
);
395 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
397 dprintf_file(stddeb
, "FILE_Unlink: removing device '%s'!\n", unixName
);
398 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
402 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
403 if (unlink( unixName
) == -1)
412 /***********************************************************************
415 * Stat a Unix path name. Return 1 if OK.
417 int FILE_Stat( LPCSTR unixName
, BYTE
*pattr
, DWORD
*psize
,
418 WORD
*pdate
, WORD
*ptime
)
422 if (stat( unixName
, &st
) == -1)
427 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
428 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
429 DOSFS_ToDosDateTime( st
.st_mtime
, pdate
, ptime
);
434 /***********************************************************************
437 * Get the date and time of a file.
439 int FILE_GetDateTime( HFILE hFile
, WORD
*pdate
, WORD
*ptime
, BOOL refresh
)
443 if (!(file
= FILE_GetFile( hFile
))) return 0;
447 if (fstat( file
->unix_handle
, &st
) == -1)
452 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
454 *pdate
= file
->filedate
;
455 *ptime
= file
->filetime
;
460 /***********************************************************************
463 * Set the date and time of a file.
465 int FILE_SetDateTime( HFILE hFile
, WORD date
, WORD time
)
469 struct utimbuf filetime
;
471 if (!(file
= FILE_GetFile( hFile
))) return 0;
472 newtm
.tm_sec
= (time
& 0x1f) * 2;
473 newtm
.tm_min
= (time
>> 5) & 0x3f;
474 newtm
.tm_hour
= (time
>> 11);
475 newtm
.tm_mday
= (date
& 0x1f);
476 newtm
.tm_mon
= ((date
>> 5) & 0x0f) - 1;
477 newtm
.tm_year
= (date
>> 9) + 80;
479 filetime
.actime
= filetime
.modtime
= mktime( &newtm
);
480 if (utime( file
->unix_name
, &filetime
) != -1) return 1;
486 /***********************************************************************
489 int FILE_Sync( HFILE hFile
)
493 if (!(file
= FILE_GetFile( hFile
))) return 0;
494 if (fsync( file
->unix_handle
) != -1) return 1;
500 /***********************************************************************
503 int FILE_MakeDir( LPCSTR path
)
505 const char *unixName
;
507 dprintf_file(stddeb
, "FILE_MakeDir: '%s'\n", path
);
509 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
511 dprintf_file(stddeb
, "FILE_MakeDir: device '%s'!\n", unixName
);
512 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
515 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
516 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
525 /***********************************************************************
528 int FILE_RemoveDir( LPCSTR path
)
530 const char *unixName
;
532 dprintf_file(stddeb
, "FILE_RemoveDir: '%s'\n", path
);
534 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
536 dprintf_file(stddeb
, "FILE_RemoveDir: device '%s'!\n", unixName
);
537 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
540 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
541 if (rmdir( unixName
) == -1)
550 /***********************************************************************
553 * dup() function for DOS handles.
555 HFILE
FILE_Dup( HFILE hFile
)
560 dprintf_file( stddeb
, "FILE_Dup for handle %d\n", hFile
);
561 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
562 if ((handle
= FILE_AllocTaskHandle( file
)) != HFILE_ERROR
) file
->count
++;
563 dprintf_file( stddeb
, "FILE_Dup return handle %d\n", handle
);
568 /***********************************************************************
571 * dup2() function for DOS handles.
573 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
576 PDB
*pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() );
577 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
579 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n", hFile1
);
580 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR
;
582 if ((hFile2
< 0) || (hFile2
>= (INT
)pdb
->nbFiles
))
584 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
587 if (files
[hFile2
] < MAX_OPEN_FILES
)
589 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
591 FILE_Close( &DOSFiles
[files
[hFile2
]] );
593 files
[hFile2
] = (BYTE
)(file
- DOSFiles
);
595 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n", hFile2
);
600 /***********************************************************************
603 INT32
FILE_Read( HFILE hFile
, LPVOID buffer
, UINT32 count
)
608 dprintf_file( stddeb
, "FILE_Read: %d %p %d\n", hFile
, buffer
, count
);
609 if (!(file
= FILE_GetFile( hFile
))) return -1;
610 if (!count
) return 0;
611 if ((result
= read( file
->unix_handle
, buffer
, count
)) == -1)
617 /***********************************************************************
618 * GetTempFileName (KERNEL.97)
620 INT
GetTempFileName( BYTE drive
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
623 UINT num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
626 if ((drive
& TF_FORCEDRIVE
) &&
627 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
629 drive
&= ~TF_FORCEDRIVE
;
630 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
634 if (drive
& TF_FORCEDRIVE
)
636 sprintf( buffer
, "%c:", drive
& ~TF_FORCEDRIVE
);
640 DIR_GetTempDosDir( buffer
, 132 ); /* buffer must be at least 144 */
641 strcat( buffer
, "\\" );
644 p
= buffer
+ strlen(buffer
);
645 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
646 sprintf( p
, "%04x.tmp", num
);
650 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
651 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
655 /* Now try to create it */
660 if ((file
= FILE_Create( buffer
, 0666, TRUE
)) != NULL
)
661 { /* We created it */
662 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
666 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
668 sprintf( p
, "%04x.tmp", num
);
669 } while (num
!= (unique
& 0xffff));
671 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
672 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
677 /***********************************************************************
678 * OpenFile (KERNEL.74)
680 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
684 WORD filedatetime
[2];
685 const char *unixName
, *dosName
;
687 int len
, i
, unixMode
;
689 ofs
->cBytes
= sizeof(OFSTRUCT
);
691 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
692 dprintf_file( stddeb
, "OpenFile: %s %04x\n", name
, mode
);
694 /* First allocate a task handle */
696 if ((hFileRet
= FILE_AllocTaskHandle( NULL
)) == HFILE_ERROR
)
698 ofs
->nErrCode
= DOS_ExtendedError
;
699 dprintf_file( stddeb
, "OpenFile: no more task handles.\n" );
703 /* OF_PARSE simply fills the structure */
707 if (!(dosName
= DOSFS_GetDosTrueName( name
, FALSE
))) goto error
;
708 lstrcpyn32A( ofs
->szPathName
, dosName
, sizeof(ofs
->szPathName
) );
709 ofs
->fFixedDisk
= (GetDriveType( dosName
[0]-'A' ) != DRIVE_REMOVABLE
);
710 dprintf_file( stddeb
, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
711 name
, ofs
->szPathName
, hFileRet
);
712 /* Return the handle, but close it first */
713 FILE_FreeTaskHandle( hFileRet
);
714 /* return hFileRet; */
715 return 0; /* Progman seems to like this better */
718 /* OF_CREATE is completely different from all other options, so
721 if (mode
& OF_CREATE
)
723 if (!(file
= FILE_Create( name
, 0666, FALSE
))) goto error
;
724 lstrcpyn32A( ofs
->szPathName
, DOSFS_GetDosTrueName( name
, FALSE
),
725 sizeof(ofs
->szPathName
) );
729 /* Now look for the file */
731 /* First try the current directory */
733 lstrcpyn32A( ofs
->szPathName
, name
, sizeof(ofs
->szPathName
) );
734 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
737 /* Now try some different paths if none was specified */
739 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
741 if (name
[1] == ':') name
+= 2;
742 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
743 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
744 if (!name
[0]) goto not_found
;
748 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
752 if ((len
= sizeof(ofs
->szPathName
) - strlen(name
) - 1) < 0) goto not_found
;
754 /* Try the Windows directory */
756 GetWindowsDirectory( ofs
->szPathName
, len
);
757 strcat( ofs
->szPathName
, "\\" );
758 strcat( ofs
->szPathName
, name
);
759 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
762 /* Try the Windows system directory */
764 GetSystemDirectory( ofs
->szPathName
, len
);
765 strcat( ofs
->szPathName
, "\\" );
766 strcat( ofs
->szPathName
, name
);
767 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
770 /* Try the path of the current executable */
772 if (GetCurrentTask())
774 GetModuleFileName( GetCurrentTask(), ofs
->szPathName
, len
);
775 if ((p
= strrchr( ofs
->szPathName
, '\\' )))
777 strcpy( p
+ 1, name
);
778 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)))
783 /* Try all directories in path */
787 if (!DIR_GetDosPath( i
, ofs
->szPathName
, len
)) goto not_found
;
788 strcat( ofs
->szPathName
, "\\" );
789 strcat( ofs
->szPathName
, name
);
790 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
795 dprintf_file( stddeb
, "OpenFile: found '%s'\n", unixName
);
796 lstrcpyn32A(ofs
->szPathName
, DOSFS_GetDosTrueName( ofs
->szPathName
, FALSE
),
797 sizeof(ofs
->szPathName
) );
799 if (mode
& OF_DELETE
)
801 if (unlink( unixName
) == -1) goto not_found
;
802 dprintf_file( stddeb
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
803 /* Return the handle, but close it first */
804 FILE_FreeTaskHandle( hFileRet
);
811 unixMode
= O_WRONLY
; break;
813 unixMode
= O_RDWR
; break;
816 unixMode
= O_RDONLY
; break;
819 if (!(file
= FILE_OpenUnixFile( unixName
, unixMode
))) goto not_found
;
820 filedatetime
[0] = file
->filedate
;
821 filedatetime
[1] = file
->filetime
;
822 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
824 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
827 dprintf_file( stddeb
, "OpenFile(%s): OF_VERIFY failed\n", name
);
828 /* FIXME: what error here? */
829 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
833 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
838 /* Return the handle, but close it first */
839 FILE_FreeTaskHandle( hFileRet
);
843 success
: /* We get here if the open was successful */
844 dprintf_file( stddeb
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
845 FILE_SetTaskHandle( hFileRet
, file
);
848 not_found
: /* We get here if the file does not exist */
849 dprintf_file( stddeb
, "OpenFile: '%s' not found\n", name
);
850 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
853 error
: /* We get here if there was an error opening the file */
854 ofs
->nErrCode
= DOS_ExtendedError
;
855 dprintf_file( stddeb
, "OpenFile(%s): return = HFILE_ERROR\n", name
);
856 FILE_FreeTaskHandle( hFileRet
);
861 /***********************************************************************
862 * _lclose (KERNEL.81)
864 HFILE
_lclose( HFILE hFile
)
868 dprintf_file( stddeb
, "_lclose: handle %d\n", hFile
);
869 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
871 FILE_FreeTaskHandle( hFile
);
876 /***********************************************************************
879 INT
_lread( HFILE hFile
, SEGPTR buffer
, WORD count
)
881 return (INT
)_hread( hFile
, buffer
, (LONG
)count
);
885 /***********************************************************************
886 * _lcreat (KERNEL.83)
888 HFILE
_lcreat( LPCSTR path
, INT attr
)
894 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
895 mode
= (attr
& 1) ? 0444 : 0666;
896 if (!(file
= FILE_Create( path
, mode
, FALSE
))) return HFILE_ERROR
;
897 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
903 /***********************************************************************
904 * _lcreat_uniq (Not a Windows API)
906 HFILE
_lcreat_uniq( LPCSTR path
, INT attr
)
912 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
913 mode
= (attr
& 1) ? 0444 : 0666;
914 if (!(file
= FILE_Create( path
, mode
, TRUE
))) return HFILE_ERROR
;
915 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
921 /***********************************************************************
922 * _llseek (KERNEL.84)
924 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
929 dprintf_file( stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
930 hFile
, lOffset
, nOrigin
);
932 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
935 case 1: origin
= SEEK_CUR
; break;
936 case 2: origin
= SEEK_END
; break;
937 default: origin
= SEEK_SET
; break;
940 if ((result
= lseek( file
->unix_handle
, lOffset
, origin
)) == -1)
946 /***********************************************************************
949 HFILE
_lopen( LPCSTR path
, INT mode
)
955 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
960 unixMode
= O_WRONLY
| O_TRUNC
;
970 if (!(file
= FILE_Open( path
, unixMode
))) return HFILE_ERROR
;
971 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
977 /***********************************************************************
978 * _lwrite (KERNEL.86)
980 INT
_lwrite( HFILE hFile
, LPCSTR buffer
, WORD count
)
982 return (INT
)_hwrite( hFile
, buffer
, (LONG
)count
);
986 /***********************************************************************
987 * _hread (KERNEL.349)
989 LONG
_hread( HFILE hFile
, SEGPTR buffer
, LONG count
)
994 dprintf_file( stddeb
, "_hread: %d %08lx %ld\n",
995 hFile
, (DWORD
)buffer
, count
);
997 /* Some programs pass a count larger than the allocated buffer */
998 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
999 if (count
> maxlen
) count
= maxlen
;
1001 return FILE_Read( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
1005 /***********************************************************************
1006 * _hwrite (KERNEL.350)
1008 LONG
_hwrite( HFILE hFile
, LPCSTR buffer
, LONG count
)
1013 dprintf_file( stddeb
, "_hwrite: %d %p %ld\n", hFile
, buffer
, count
);
1015 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
1017 if (count
== 0) /* Expand or truncate at current position */
1018 result
= ftruncate( file
->unix_handle
,
1019 lseek( file
->unix_handle
, 0, SEEK_CUR
) );
1021 result
= write( file
->unix_handle
, buffer
, count
);
1023 if (result
== -1) FILE_SetDosError();
1028 /***********************************************************************
1029 * SetHandleCount (KERNEL.199)
1031 WORD
SetHandleCount( WORD count
)
1033 HANDLE hPDB
= GetCurrentPDB();
1034 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1035 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1038 dprintf_file( stddeb
, "SetHandleCount(%d)\n", count
);
1040 if (count
< 20) count
= 20; /* No point in going below 20 */
1041 else if (count
> 254) count
= 254;
1043 /* If shrinking the table, make sure all extra file handles are closed */
1044 if (count
< pdb
->nbFiles
)
1046 for (i
= count
; i
< pdb
->nbFiles
; i
++)
1047 if (files
[i
] != 0xff) /* File open */
1049 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
,
1050 SA_Abort
, EL_Disk
);
1051 return pdb
->nbFiles
;
1057 if (pdb
->nbFiles
> 20)
1059 memcpy( pdb
->fileHandles
, files
, 20 );
1061 GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1062 pdb
->fileHandlesPtr
= (SEGPTR
)pdb
->fileHandles
;
1064 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1065 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1066 GlobalHandleToSel( hPDB
) );
1071 else /* More than 20, need a new file handles table */
1075 newfiles
= (BYTE
*)GlobalAlloc32( GMEM_FIXED
, count
);
1077 HANDLE newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1080 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1081 return pdb
->nbFiles
;
1083 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1084 #endif /* WINELIB */
1085 if (count
> pdb
->nbFiles
)
1087 memcpy( newfiles
, files
, pdb
->nbFiles
);
1088 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1090 else memcpy( newfiles
, files
, count
);
1092 if (pdb
->nbFiles
> 20) GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1093 pdb
->fileHandlesPtr
= (SEGPTR
)newfiles
;
1095 if (pdb
->nbFiles
> 20)
1096 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1097 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1098 #endif /* WINELIB */
1099 pdb
->nbFiles
= count
;
1101 return pdb
->nbFiles
;