2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
21 #include "directory.h"
34 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
36 typedef struct tagDOS_FILE
38 struct tagDOS_FILE
*next
;
39 int count
; /* Usage count (0 if free) */
47 /* Global files array */
48 static DOS_FILE DOSFiles
[MAX_OPEN_FILES
];
50 static DOS_FILE
*FILE_First
= DOSFiles
;
51 static DOS_FILE
*FILE_LastUsed
= DOSFiles
;
53 /* Small file handles array for boot-up, before the first PDB is created */
54 #define MAX_BOOT_HANDLES 4
55 static BYTE bootFileHandles
[MAX_BOOT_HANDLES
] = { 0xff, 0xff, 0xff, 0xff };
57 /***********************************************************************
60 * Allocate a DOS file.
62 static DOS_FILE
*FILE_Alloc(void)
64 DOS_FILE
*file
= FILE_First
;
65 if (file
) FILE_First
= file
->next
;
66 else if (FILE_LastUsed
>= &DOSFiles
[MAX_OPEN_FILES
-1])
68 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
71 else file
= ++FILE_LastUsed
;
73 file
->unix_handle
= -1;
74 file
->unix_name
= NULL
;
79 /***********************************************************************
84 static BOOL
FILE_Close( DOS_FILE
*file
)
86 if (!file
->count
) return FALSE
;
87 if (--file
->count
> 0) return TRUE
;
88 /* Now really close the file */
89 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
90 if (file
->unix_name
) free( file
->unix_name
);
91 file
->next
= FILE_First
;
97 /***********************************************************************
100 * Return a pointer to the current PDB files array.
102 static void FILE_GetPDBFiles( BYTE
**files
, WORD
*nbFiles
)
106 if ((pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() )) != NULL
)
108 *files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
109 *nbFiles
= pdb
->nbFiles
;
113 *files
= bootFileHandles
;
114 *nbFiles
= MAX_BOOT_HANDLES
;
119 /***********************************************************************
120 * FILE_AllocTaskHandle
122 * Allocate a task file handle for a DOS file.
124 static HFILE
FILE_AllocTaskHandle( DOS_FILE
*dos_file
)
129 FILE_GetPDBFiles( &files
, &nbFiles
);
130 fp
= files
+ 1; /* Don't use handle 0, as some programs don't like it */
131 for (i
= nbFiles
- 1; (i
> 0) && (*fp
!= 0xff); i
--, fp
++);
133 { /* No more handles or files */
134 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
137 *fp
= dos_file
? (BYTE
)(dos_file
- DOSFiles
) : 0;
139 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
140 (fp
- files
), *fp
, nbFiles
- i
, nbFiles
);
141 return (HFILE
)(fp
- files
);
145 /***********************************************************************
146 * FILE_FreeTaskHandle
148 * Free a per-task file handle.
150 static void FILE_FreeTaskHandle( HFILE handle
)
155 FILE_GetPDBFiles( &files
, &nbFiles
);
156 dprintf_file( stddeb
,"FILE_FreeTaskHandle: dos=%d file=%d\n",
157 handle
, files
[handle
] );
158 if ((handle
< 0) || (handle
>= (INT
)nbFiles
))
160 fprintf( stderr
, "FILE_FreeTaskHandle: invalid file handle %d\n",
164 files
[handle
] = 0xff;
168 /***********************************************************************
171 * Set the value of a task handle (no error checking).
173 static void FILE_SetTaskHandle( HFILE handle
, DOS_FILE
*file
)
178 FILE_GetPDBFiles( &files
, &nbFiles
);
179 files
[handle
] = (BYTE
)(file
- DOSFiles
);
183 /***********************************************************************
186 * Return the DOS file associated to a task file handle.
188 static DOS_FILE
*FILE_GetFile( HFILE handle
)
194 FILE_GetPDBFiles( &files
, &nbFiles
);
195 if ((handle
< 0) || (handle
>= (INT
)nbFiles
) ||
196 (files
[handle
] >= MAX_OPEN_FILES
) ||
197 !(file
= &DOSFiles
[files
[handle
]])->count
)
199 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
206 int FILE_GetUnixHandle( HFILE hFile
)
210 if (!(file
= FILE_GetFile( hFile
))) return -1;
211 return file
->unix_handle
;
215 /***********************************************************************
218 * Close all open files of a given PDB. Used on task termination.
220 void FILE_CloseAllFiles( HANDLE hPDB
)
224 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
227 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
228 dprintf_file(stddeb
,"FILE_CloseAllFiles: closing %d files\n",pdb
->nbFiles
);
229 for (count
= pdb
->nbFiles
; count
> 0; count
--, files
++)
231 if (*files
< MAX_OPEN_FILES
) FILE_Close( &DOSFiles
[*files
] );
237 /***********************************************************************
240 * Set the DOS error code from errno.
242 void FILE_SetDosError(void)
244 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
248 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
251 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
254 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
259 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
262 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
265 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
268 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
272 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
275 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
278 perror( "int21: unknown errno" );
279 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
285 /***********************************************************************
288 static DOS_FILE
*FILE_OpenUnixFile( const char *name
, int mode
)
293 if (!(file
= FILE_Alloc())) return NULL
;
294 if ((file
->unix_handle
= open( name
, mode
)) == -1)
296 if (Options
.allowReadOnly
&& (mode
== O_RDWR
))
298 if ((file
->unix_handle
= open( name
, O_RDONLY
)) != -1)
299 fprintf( stderr
, "Warning: could not open %s for writing, opening read-only.\n", name
);
302 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
308 if (S_ISDIR(st
.st_mode
))
310 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
315 /* File opened OK, now fill the DOS_FILE */
317 file
->unix_name
= xstrdup( name
);
318 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
323 /***********************************************************************
326 static DOS_FILE
*FILE_Open( LPCSTR path
, int mode
)
328 const char *unixName
;
330 dprintf_file(stddeb
, "FILE_Open: '%s' %04x\n", path
, mode
);
331 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
333 dprintf_file( stddeb
, "FILE_Open: opening device '%s'\n", unixName
);
334 if (!unixName
[0]) /* Non-existing device */
336 dprintf_file(stddeb
, "FILE_Open: Non-existing device\n");
337 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
341 else if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return NULL
;
342 return FILE_OpenUnixFile( unixName
, mode
);
346 /***********************************************************************
349 static DOS_FILE
*FILE_Create( LPCSTR path
, int mode
, int unique
)
352 const char *unixName
;
354 dprintf_file(stddeb
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
356 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
358 dprintf_file(stddeb
, "FILE_Create: creating device '%s'!\n", unixName
);
359 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
363 if (!(file
= FILE_Alloc())) return NULL
;
365 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
)))
370 if ((file
->unix_handle
= open( unixName
,
371 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
379 /* File created OK, now fill the DOS_FILE */
381 file
->unix_name
= xstrdup( unixName
);
382 DOSFS_ToDosDateTime( time(NULL
), &file
->filedate
, &file
->filetime
);
387 /***********************************************************************
390 * Stat a Unix path name. Return 1 if OK.
392 int FILE_Stat( LPCSTR unixName
, BYTE
*pattr
, DWORD
*psize
,
393 WORD
*pdate
, WORD
*ptime
)
397 if (stat( unixName
, &st
) == -1)
402 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
403 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
404 DOSFS_ToDosDateTime( st
.st_mtime
, pdate
, ptime
);
409 /***********************************************************************
412 * Get the date and time of a file.
414 int FILE_GetDateTime( HFILE hFile
, WORD
*pdate
, WORD
*ptime
, BOOL refresh
)
418 if (!(file
= FILE_GetFile( hFile
))) return 0;
422 if (fstat( file
->unix_handle
, &st
) == -1)
427 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
429 *pdate
= file
->filedate
;
430 *ptime
= file
->filetime
;
435 /***********************************************************************
438 * Set the date and time of a file.
440 int FILE_SetDateTime( HFILE hFile
, WORD date
, WORD time
)
444 struct utimbuf filetime
;
446 if (!(file
= FILE_GetFile( hFile
))) return 0;
447 newtm
.tm_sec
= (time
& 0x1f) * 2;
448 newtm
.tm_min
= (time
>> 5) & 0x3f;
449 newtm
.tm_hour
= (time
>> 11);
450 newtm
.tm_mday
= (date
& 0x1f);
451 newtm
.tm_mon
= ((date
>> 5) & 0x0f) - 1;
452 newtm
.tm_year
= (date
>> 9) + 80;
454 filetime
.actime
= filetime
.modtime
= mktime( &newtm
);
455 if (utime( file
->unix_name
, &filetime
) != -1) return 1;
461 /***********************************************************************
464 * dup() function for DOS handles.
466 HFILE
FILE_Dup( HFILE hFile
)
471 dprintf_file( stddeb
, "FILE_Dup for handle %d\n", hFile
);
472 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
473 if ((handle
= FILE_AllocTaskHandle( file
)) != HFILE_ERROR
) file
->count
++;
474 dprintf_file( stddeb
, "FILE_Dup return handle %d\n", handle
);
479 /***********************************************************************
482 * dup2() function for DOS handles.
484 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
487 PDB
*pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() );
488 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
490 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n", hFile1
);
491 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR
;
493 if ((hFile2
< 0) || (hFile2
>= (INT
)pdb
->nbFiles
))
495 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
498 if (files
[hFile2
] < MAX_OPEN_FILES
)
500 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
502 FILE_Close( &DOSFiles
[files
[hFile2
]] );
504 files
[hFile2
] = (BYTE
)(file
- DOSFiles
);
506 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n", hFile2
);
511 /***********************************************************************
514 INT32
FILE_Read( HFILE hFile
, LPVOID buffer
, UINT32 count
)
519 dprintf_file( stddeb
, "FILE_Read: %d %p %d\n", hFile
, buffer
, count
);
520 if (!(file
= FILE_GetFile( hFile
))) return -1;
521 if (!count
) return 0;
522 if ((result
= read( file
->unix_handle
, buffer
, count
)) == -1)
528 /***********************************************************************
529 * GetTempFileName16 (KERNEL.97)
531 UINT16
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
536 if ((drive
& TF_FORCEDRIVE
) &&
537 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
539 drive
&= ~TF_FORCEDRIVE
;
540 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
544 if (drive
& TF_FORCEDRIVE
)
545 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
548 GetTempPath32A( 132, temppath
);
549 strcat( temppath
, "\\" );
551 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
555 /***********************************************************************
556 * GetTempFileName32A (KERNEL32.290)
558 UINT32
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
563 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
566 strcpy( buffer
, path
);
567 p
= buffer
+ strlen(buffer
);
569 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
570 sprintf( p
, "%04x.tmp", num
);
574 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
575 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
579 /* Now try to create it */
584 if ((file
= FILE_Create( buffer
, 0666, TRUE
)) != NULL
)
585 { /* We created it */
586 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
590 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
592 sprintf( p
, "%04x.tmp", num
);
593 } while (num
!= (unique
& 0xffff));
595 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
596 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
601 /***********************************************************************
602 * GetTempFileName32W (KERNEL32.291)
604 UINT32
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
612 patha
= STRING32_DupUniToAnsi(path
);
613 prefixa
= STRING32_DupUniToAnsi(prefix
);
614 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
615 STRING32_AnsiToUni( buffer
, buffera
);
622 /***********************************************************************
623 * OpenFile (KERNEL.74) (KERNEL32.396)
625 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
629 WORD filedatetime
[2];
630 const char *unixName
, *dosName
;
632 int len
, i
, unixMode
;
634 ofs
->cBytes
= sizeof(OFSTRUCT
);
636 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
637 dprintf_file( stddeb
, "OpenFile: %s %04x\n", name
, mode
);
639 /* First allocate a task handle */
641 if ((hFileRet
= FILE_AllocTaskHandle( NULL
)) == HFILE_ERROR
)
643 ofs
->nErrCode
= DOS_ExtendedError
;
644 dprintf_file( stddeb
, "OpenFile: no more task handles.\n" );
648 /* OF_PARSE simply fills the structure */
652 if (!(dosName
= DOSFS_GetDosTrueName( name
, FALSE
))) goto error
;
653 lstrcpyn32A( ofs
->szPathName
, dosName
, sizeof(ofs
->szPathName
) );
654 ofs
->fFixedDisk
= (GetDriveType16( dosName
[0]-'A' ) != DRIVE_REMOVABLE
);
655 dprintf_file( stddeb
, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
656 name
, ofs
->szPathName
, hFileRet
);
657 /* Return the handle, but close it first */
658 FILE_FreeTaskHandle( hFileRet
);
659 /* return hFileRet; */
660 return 0; /* Progman seems to like this better */
663 /* OF_CREATE is completely different from all other options, so
666 if (mode
& OF_CREATE
)
668 if (!(file
= FILE_Create( name
, 0666, FALSE
))) goto error
;
669 lstrcpyn32A( ofs
->szPathName
, DOSFS_GetDosTrueName( name
, FALSE
),
670 sizeof(ofs
->szPathName
) );
674 /* Now look for the file */
676 /* First try the current directory */
678 lstrcpyn32A( ofs
->szPathName
, name
, sizeof(ofs
->szPathName
) );
679 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
682 /* Now try some different paths if none was specified */
684 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
686 if (name
[1] == ':') name
+= 2;
687 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
688 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
689 if (!name
[0]) goto not_found
;
693 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
697 if ((len
= sizeof(ofs
->szPathName
) - strlen(name
) - 1) < 0) goto not_found
;
699 /* Try the Windows directory */
701 GetWindowsDirectory( ofs
->szPathName
, len
);
702 strcat( ofs
->szPathName
, "\\" );
703 strcat( ofs
->szPathName
, name
);
704 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
707 /* Try the Windows system directory */
709 GetSystemDirectory32A( ofs
->szPathName
, len
);
710 strcat( ofs
->szPathName
, "\\" );
711 strcat( ofs
->szPathName
, name
);
712 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
715 /* Try the path of the current executable */
717 if (GetCurrentTask())
719 GetModuleFileName( GetCurrentTask(), ofs
->szPathName
, len
);
720 if ((p
= strrchr( ofs
->szPathName
, '\\' )))
722 strcpy( p
+ 1, name
);
723 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)))
728 /* Try all directories in path */
732 if (!DIR_GetDosPath( i
, ofs
->szPathName
, len
)) goto not_found
;
733 strcat( ofs
->szPathName
, "\\" );
734 strcat( ofs
->szPathName
, name
);
735 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
740 dprintf_file( stddeb
, "OpenFile: found '%s'\n", unixName
);
741 lstrcpyn32A(ofs
->szPathName
, DOSFS_GetDosTrueName( ofs
->szPathName
, FALSE
),
742 sizeof(ofs
->szPathName
) );
744 if (mode
& OF_DELETE
)
746 if (unlink( unixName
) == -1) goto not_found
;
747 dprintf_file( stddeb
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
748 /* Return the handle, but close it first */
749 FILE_FreeTaskHandle( hFileRet
);
756 unixMode
= O_WRONLY
; break;
758 unixMode
= O_RDWR
; break;
761 unixMode
= O_RDONLY
; break;
764 if (!(file
= FILE_OpenUnixFile( unixName
, unixMode
))) goto not_found
;
765 filedatetime
[0] = file
->filedate
;
766 filedatetime
[1] = file
->filetime
;
767 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
769 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
772 dprintf_file( stddeb
, "OpenFile(%s): OF_VERIFY failed\n", name
);
773 /* FIXME: what error here? */
774 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
778 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
783 /* Return the handle, but close it first */
784 FILE_FreeTaskHandle( hFileRet
);
788 success
: /* We get here if the open was successful */
789 dprintf_file( stddeb
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
790 FILE_SetTaskHandle( hFileRet
, file
);
793 not_found
: /* We get here if the file does not exist */
794 dprintf_file( stddeb
, "OpenFile: '%s' not found\n", name
);
795 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
798 error
: /* We get here if there was an error opening the file */
799 ofs
->nErrCode
= DOS_ExtendedError
;
800 dprintf_file( stddeb
, "OpenFile(%s): return = HFILE_ERROR\n", name
);
801 FILE_FreeTaskHandle( hFileRet
);
806 /***********************************************************************
807 * _lclose (KERNEL.81) (KERNEL32.592)
809 HFILE
_lclose( HFILE hFile
)
813 dprintf_file( stddeb
, "_lclose: handle %d\n", hFile
);
814 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
816 FILE_FreeTaskHandle( hFile
);
821 /***********************************************************************
824 INT
_lread( HFILE hFile
, SEGPTR buffer
, WORD count
)
826 return (INT
)_hread( hFile
, buffer
, (LONG
)count
);
830 /***********************************************************************
831 * _lcreat (KERNEL.83) (KERNEL32.593)
833 HFILE
_lcreat( LPCSTR path
, INT32 attr
)
839 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
840 mode
= (attr
& 1) ? 0444 : 0666;
841 if (!(file
= FILE_Create( path
, mode
, FALSE
))) return HFILE_ERROR
;
842 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
848 /***********************************************************************
849 * _lcreat_uniq (Not a Windows API)
851 HFILE
_lcreat_uniq( LPCSTR path
, INT attr
)
857 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
858 mode
= (attr
& 1) ? 0444 : 0666;
859 if (!(file
= FILE_Create( path
, mode
, TRUE
))) return HFILE_ERROR
;
860 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
866 /***********************************************************************
867 * _llseek (KERNEL.84)
869 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
874 dprintf_file( stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
875 hFile
, lOffset
, nOrigin
);
877 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
880 case 1: origin
= SEEK_CUR
; break;
881 case 2: origin
= SEEK_END
; break;
882 default: origin
= SEEK_SET
; break;
885 if ((result
= lseek( file
->unix_handle
, lOffset
, origin
)) == -1)
891 /***********************************************************************
892 * _lopen (KERNEL.85) (KERNEL32.595)
894 HFILE
_lopen( LPCSTR path
, INT32 mode
)
900 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
905 unixMode
= O_WRONLY
| O_TRUNC
;
915 if (!(file
= FILE_Open( path
, unixMode
))) return HFILE_ERROR
;
916 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
922 /***********************************************************************
923 * _lwrite (KERNEL.86)
925 INT
_lwrite( HFILE hFile
, LPCSTR buffer
, WORD count
)
927 return (INT
)_hwrite( hFile
, buffer
, (LONG
)count
);
931 /***********************************************************************
932 * _hread (KERNEL.349)
934 LONG
_hread( HFILE hFile
, SEGPTR buffer
, LONG count
)
939 dprintf_file( stddeb
, "_hread: %d %08lx %ld\n",
940 hFile
, (DWORD
)buffer
, count
);
942 /* Some programs pass a count larger than the allocated buffer */
943 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
944 if (count
> maxlen
) count
= maxlen
;
946 return FILE_Read( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
950 /***********************************************************************
951 * _hwrite (KERNEL.350)
953 LONG
_hwrite( HFILE hFile
, LPCSTR buffer
, LONG count
)
958 dprintf_file( stddeb
, "_hwrite: %d %p %ld\n", hFile
, buffer
, count
);
960 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
962 if (count
== 0) /* Expand or truncate at current position */
963 result
= ftruncate( file
->unix_handle
,
964 lseek( file
->unix_handle
, 0, SEEK_CUR
) );
966 result
= write( file
->unix_handle
, buffer
, count
);
968 if (result
== -1) FILE_SetDosError();
973 /***********************************************************************
974 * SetHandleCount (KERNEL.199)
976 WORD
SetHandleCount( WORD count
)
978 HANDLE hPDB
= GetCurrentPDB();
979 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
980 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
983 dprintf_file( stddeb
, "SetHandleCount(%d)\n", count
);
985 if (count
< 20) count
= 20; /* No point in going below 20 */
986 else if (count
> 254) count
= 254;
988 /* If shrinking the table, make sure all extra file handles are closed */
989 if (count
< pdb
->nbFiles
)
991 for (i
= count
; i
< pdb
->nbFiles
; i
++)
992 if (files
[i
] != 0xff) /* File open */
994 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
,
1002 if (pdb
->nbFiles
> 20)
1004 memcpy( pdb
->fileHandles
, files
, 20 );
1006 GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1007 pdb
->fileHandlesPtr
= (SEGPTR
)pdb
->fileHandles
;
1009 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1010 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1011 GlobalHandleToSel( hPDB
) );
1016 else /* More than 20, need a new file handles table */
1020 newfiles
= (BYTE
*)GlobalAlloc32( GMEM_FIXED
, count
);
1022 HANDLE newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1025 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1026 return pdb
->nbFiles
;
1028 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1029 #endif /* WINELIB */
1030 if (count
> pdb
->nbFiles
)
1032 memcpy( newfiles
, files
, pdb
->nbFiles
);
1033 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1035 else memcpy( newfiles
, files
, count
);
1037 if (pdb
->nbFiles
> 20) GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1038 pdb
->fileHandlesPtr
= (SEGPTR
)newfiles
;
1040 if (pdb
->nbFiles
> 20)
1041 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1042 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1043 #endif /* WINELIB */
1044 pdb
->nbFiles
= count
;
1046 return pdb
->nbFiles
;
1050 /***********************************************************************
1051 * FlushFileBuffers (KERNEL32.133)
1053 BOOL32
FlushFileBuffers( HFILE hFile
)
1057 dprintf_file( stddeb
, "FlushFileBuffers(%d)\n", hFile
);
1058 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1059 if (fsync( file
->unix_handle
) != -1) return TRUE
;
1065 /***********************************************************************
1066 * DeleteFile16 (KERNEL.146)
1068 BOOL16
DeleteFile16( LPCSTR path
)
1070 return DeleteFile32A( path
);
1074 /***********************************************************************
1075 * DeleteFile32A (KERNEL32.71)
1077 BOOL32
DeleteFile32A( LPCSTR path
)
1079 const char *unixName
;
1081 dprintf_file(stddeb
, "DeleteFile: '%s'\n", path
);
1083 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1085 dprintf_file(stddeb
, "DeleteFile: removing device '%s'!\n", unixName
);
1086 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1090 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return FALSE
;
1091 if (unlink( unixName
) == -1)
1100 /***********************************************************************
1101 * DeleteFile32W (KERNEL32.72)
1103 BOOL32
DeleteFile32W( LPCWSTR path
)
1105 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1106 BOOL32 ret
= RemoveDirectory32A( xpath
);
1112 /***********************************************************************
1113 * CreateDirectory16 (KERNEL.144)
1115 BOOL16
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
1117 dprintf_file( stddeb
,"CreateDirectory16(%s,%p)\n", path
, dummy
);
1118 return (BOOL16
)CreateDirectory32A( path
, NULL
);
1122 /***********************************************************************
1123 * CreateDirectory32A (KERNEL32.39)
1125 BOOL32
CreateDirectory32A( LPCSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1127 const char *unixName
;
1129 dprintf_file( stddeb
, "CreateDirectory32A(%s,%p)\n", path
, lpsecattribs
);
1130 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1132 dprintf_file(stddeb
, "CreateDirectory: device '%s'!\n", unixName
);
1133 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1136 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
1137 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
1146 /***********************************************************************
1147 * CreateDirectory32W (KERNEL32.42)
1149 BOOL32
CreateDirectory32W( LPCWSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1151 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1152 BOOL32 ret
= CreateDirectory32A(xpath
,lpsecattribs
);
1158 /***********************************************************************
1159 * RemoveDirectory16 (KERNEL)
1161 BOOL16
RemoveDirectory16( LPCSTR path
)
1163 return (BOOL16
)RemoveDirectory32A( path
);
1167 /***********************************************************************
1168 * RemoveDirectory32A (KERNEL32.437)
1170 BOOL32
RemoveDirectory32A( LPCSTR path
)
1172 const char *unixName
;
1174 dprintf_file(stddeb
, "RemoveDirectory: '%s'\n", path
);
1176 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1178 dprintf_file(stddeb
, "RemoveDirectory: device '%s'!\n", unixName
);
1179 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1182 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return FALSE
;
1183 if (rmdir( unixName
) == -1)
1192 /***********************************************************************
1193 * RemoveDirectory32W (KERNEL32.438)
1195 BOOL32
RemoveDirectory32W( LPCWSTR path
)
1197 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1198 BOOL32 ret
= RemoveDirectory32A( xpath
);