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 int FILE_Unlink( LPCSTR path
)
392 const char *unixName
;
394 dprintf_file(stddeb
, "FILE_Unlink: '%s'\n", path
);
396 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
398 dprintf_file(stddeb
, "FILE_Unlink: removing device '%s'!\n", unixName
);
399 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
403 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
404 if (unlink( unixName
) == -1)
413 /***********************************************************************
416 * Stat a Unix path name. Return 1 if OK.
418 int FILE_Stat( LPCSTR unixName
, BYTE
*pattr
, DWORD
*psize
,
419 WORD
*pdate
, WORD
*ptime
)
423 if (stat( unixName
, &st
) == -1)
428 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
429 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
430 DOSFS_ToDosDateTime( st
.st_mtime
, pdate
, ptime
);
435 /***********************************************************************
438 * Get the date and time of a file.
440 int FILE_GetDateTime( HFILE hFile
, WORD
*pdate
, WORD
*ptime
, BOOL refresh
)
444 if (!(file
= FILE_GetFile( hFile
))) return 0;
448 if (fstat( file
->unix_handle
, &st
) == -1)
453 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
455 *pdate
= file
->filedate
;
456 *ptime
= file
->filetime
;
461 /***********************************************************************
464 * Set the date and time of a file.
466 int FILE_SetDateTime( HFILE hFile
, WORD date
, WORD time
)
470 struct utimbuf filetime
;
472 if (!(file
= FILE_GetFile( hFile
))) return 0;
473 newtm
.tm_sec
= (time
& 0x1f) * 2;
474 newtm
.tm_min
= (time
>> 5) & 0x3f;
475 newtm
.tm_hour
= (time
>> 11);
476 newtm
.tm_mday
= (date
& 0x1f);
477 newtm
.tm_mon
= ((date
>> 5) & 0x0f) - 1;
478 newtm
.tm_year
= (date
>> 9) + 80;
480 filetime
.actime
= filetime
.modtime
= mktime( &newtm
);
481 if (utime( file
->unix_name
, &filetime
) != -1) return 1;
487 /***********************************************************************
490 int FILE_RemoveDir( LPCSTR path
)
492 const char *unixName
;
494 dprintf_file(stddeb
, "FILE_RemoveDir: '%s'\n", path
);
496 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
498 dprintf_file(stddeb
, "FILE_RemoveDir: device '%s'!\n", unixName
);
499 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
502 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return 0;
503 if (rmdir( unixName
) == -1)
512 /***********************************************************************
515 * dup() function for DOS handles.
517 HFILE
FILE_Dup( HFILE hFile
)
522 dprintf_file( stddeb
, "FILE_Dup for handle %d\n", hFile
);
523 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
524 if ((handle
= FILE_AllocTaskHandle( file
)) != HFILE_ERROR
) file
->count
++;
525 dprintf_file( stddeb
, "FILE_Dup return handle %d\n", handle
);
530 /***********************************************************************
533 * dup2() function for DOS handles.
535 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
538 PDB
*pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() );
539 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
541 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n", hFile1
);
542 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR
;
544 if ((hFile2
< 0) || (hFile2
>= (INT
)pdb
->nbFiles
))
546 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
549 if (files
[hFile2
] < MAX_OPEN_FILES
)
551 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
553 FILE_Close( &DOSFiles
[files
[hFile2
]] );
555 files
[hFile2
] = (BYTE
)(file
- DOSFiles
);
557 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n", hFile2
);
562 /***********************************************************************
565 INT32
FILE_Read( HFILE hFile
, LPVOID buffer
, UINT32 count
)
570 dprintf_file( stddeb
, "FILE_Read: %d %p %d\n", hFile
, buffer
, count
);
571 if (!(file
= FILE_GetFile( hFile
))) return -1;
572 if (!count
) return 0;
573 if ((result
= read( file
->unix_handle
, buffer
, count
)) == -1)
579 /***********************************************************************
580 * GetTempFileName (KERNEL.97)
582 INT
GetTempFileName( BYTE drive
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
585 UINT num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
588 if ((drive
& TF_FORCEDRIVE
) &&
589 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
591 drive
&= ~TF_FORCEDRIVE
;
592 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
596 if (drive
& TF_FORCEDRIVE
)
598 sprintf( buffer
, "%c:", drive
& ~TF_FORCEDRIVE
);
602 DIR_GetTempDosDir( buffer
, 132 ); /* buffer must be at least 144 */
603 strcat( buffer
, "\\" );
606 p
= buffer
+ strlen(buffer
);
607 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
608 sprintf( p
, "%04x.tmp", num
);
612 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
613 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
617 /* Now try to create it */
622 if ((file
= FILE_Create( buffer
, 0666, TRUE
)) != NULL
)
623 { /* We created it */
624 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
628 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
630 sprintf( p
, "%04x.tmp", num
);
631 } while (num
!= (unique
& 0xffff));
633 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
634 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
639 /***********************************************************************
640 * OpenFile (KERNEL.74)
642 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT mode
)
646 WORD filedatetime
[2];
647 const char *unixName
, *dosName
;
649 int len
, i
, unixMode
;
651 ofs
->cBytes
= sizeof(OFSTRUCT
);
653 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
654 dprintf_file( stddeb
, "OpenFile: %s %04x\n", name
, mode
);
656 /* First allocate a task handle */
658 if ((hFileRet
= FILE_AllocTaskHandle( NULL
)) == HFILE_ERROR
)
660 ofs
->nErrCode
= DOS_ExtendedError
;
661 dprintf_file( stddeb
, "OpenFile: no more task handles.\n" );
665 /* OF_PARSE simply fills the structure */
669 if (!(dosName
= DOSFS_GetDosTrueName( name
, FALSE
))) goto error
;
670 lstrcpyn32A( ofs
->szPathName
, dosName
, sizeof(ofs
->szPathName
) );
671 ofs
->fFixedDisk
= (GetDriveType( dosName
[0]-'A' ) != DRIVE_REMOVABLE
);
672 dprintf_file( stddeb
, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
673 name
, ofs
->szPathName
, hFileRet
);
674 /* Return the handle, but close it first */
675 FILE_FreeTaskHandle( hFileRet
);
676 /* return hFileRet; */
677 return 0; /* Progman seems to like this better */
680 /* OF_CREATE is completely different from all other options, so
683 if (mode
& OF_CREATE
)
685 if (!(file
= FILE_Create( name
, 0666, FALSE
))) goto error
;
686 lstrcpyn32A( ofs
->szPathName
, DOSFS_GetDosTrueName( name
, FALSE
),
687 sizeof(ofs
->szPathName
) );
691 /* Now look for the file */
693 /* First try the current directory */
695 lstrcpyn32A( ofs
->szPathName
, name
, sizeof(ofs
->szPathName
) );
696 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
699 /* Now try some different paths if none was specified */
701 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
703 if (name
[1] == ':') name
+= 2;
704 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
705 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
706 if (!name
[0]) goto not_found
;
710 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
714 if ((len
= sizeof(ofs
->szPathName
) - strlen(name
) - 1) < 0) goto not_found
;
716 /* Try the Windows directory */
718 GetWindowsDirectory( ofs
->szPathName
, len
);
719 strcat( ofs
->szPathName
, "\\" );
720 strcat( ofs
->szPathName
, name
);
721 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
724 /* Try the Windows system directory */
726 GetSystemDirectory( ofs
->szPathName
, len
);
727 strcat( ofs
->szPathName
, "\\" );
728 strcat( ofs
->szPathName
, name
);
729 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
732 /* Try the path of the current executable */
734 if (GetCurrentTask())
736 GetModuleFileName( GetCurrentTask(), ofs
->szPathName
, len
);
737 if ((p
= strrchr( ofs
->szPathName
, '\\' )))
739 strcpy( p
+ 1, name
);
740 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)))
745 /* Try all directories in path */
749 if (!DIR_GetDosPath( i
, ofs
->szPathName
, len
)) goto not_found
;
750 strcat( ofs
->szPathName
, "\\" );
751 strcat( ofs
->szPathName
, name
);
752 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
757 dprintf_file( stddeb
, "OpenFile: found '%s'\n", unixName
);
758 lstrcpyn32A(ofs
->szPathName
, DOSFS_GetDosTrueName( ofs
->szPathName
, FALSE
),
759 sizeof(ofs
->szPathName
) );
761 if (mode
& OF_DELETE
)
763 if (unlink( unixName
) == -1) goto not_found
;
764 dprintf_file( stddeb
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
765 /* Return the handle, but close it first */
766 FILE_FreeTaskHandle( hFileRet
);
773 unixMode
= O_WRONLY
; break;
775 unixMode
= O_RDWR
; break;
778 unixMode
= O_RDONLY
; break;
781 if (!(file
= FILE_OpenUnixFile( unixName
, unixMode
))) goto not_found
;
782 filedatetime
[0] = file
->filedate
;
783 filedatetime
[1] = file
->filetime
;
784 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
786 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
789 dprintf_file( stddeb
, "OpenFile(%s): OF_VERIFY failed\n", name
);
790 /* FIXME: what error here? */
791 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
795 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
800 /* Return the handle, but close it first */
801 FILE_FreeTaskHandle( hFileRet
);
805 success
: /* We get here if the open was successful */
806 dprintf_file( stddeb
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
807 FILE_SetTaskHandle( hFileRet
, file
);
810 not_found
: /* We get here if the file does not exist */
811 dprintf_file( stddeb
, "OpenFile: '%s' not found\n", name
);
812 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
815 error
: /* We get here if there was an error opening the file */
816 ofs
->nErrCode
= DOS_ExtendedError
;
817 dprintf_file( stddeb
, "OpenFile(%s): return = HFILE_ERROR\n", name
);
818 FILE_FreeTaskHandle( hFileRet
);
823 /***********************************************************************
824 * _lclose (KERNEL.81)
826 HFILE
_lclose( HFILE hFile
)
830 dprintf_file( stddeb
, "_lclose: handle %d\n", hFile
);
831 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
833 FILE_FreeTaskHandle( hFile
);
838 /***********************************************************************
841 INT
_lread( HFILE hFile
, SEGPTR buffer
, WORD count
)
843 return (INT
)_hread( hFile
, buffer
, (LONG
)count
);
847 /***********************************************************************
848 * _lcreat (KERNEL.83)
850 HFILE
_lcreat( LPCSTR path
, INT attr
)
856 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
857 mode
= (attr
& 1) ? 0444 : 0666;
858 if (!(file
= FILE_Create( path
, mode
, FALSE
))) return HFILE_ERROR
;
859 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
865 /***********************************************************************
866 * _lcreat_uniq (Not a Windows API)
868 HFILE
_lcreat_uniq( LPCSTR path
, INT attr
)
874 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
875 mode
= (attr
& 1) ? 0444 : 0666;
876 if (!(file
= FILE_Create( path
, mode
, TRUE
))) return HFILE_ERROR
;
877 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
883 /***********************************************************************
884 * _llseek (KERNEL.84)
886 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT nOrigin
)
891 dprintf_file( stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
892 hFile
, lOffset
, nOrigin
);
894 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
897 case 1: origin
= SEEK_CUR
; break;
898 case 2: origin
= SEEK_END
; break;
899 default: origin
= SEEK_SET
; break;
902 if ((result
= lseek( file
->unix_handle
, lOffset
, origin
)) == -1)
908 /***********************************************************************
911 HFILE
_lopen( LPCSTR path
, INT mode
)
917 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
922 unixMode
= O_WRONLY
| O_TRUNC
;
932 if (!(file
= FILE_Open( path
, unixMode
))) return HFILE_ERROR
;
933 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
939 /***********************************************************************
940 * _lwrite (KERNEL.86)
942 INT
_lwrite( HFILE hFile
, LPCSTR buffer
, WORD count
)
944 return (INT
)_hwrite( hFile
, buffer
, (LONG
)count
);
948 /***********************************************************************
949 * _hread (KERNEL.349)
951 LONG
_hread( HFILE hFile
, SEGPTR buffer
, LONG count
)
956 dprintf_file( stddeb
, "_hread: %d %08lx %ld\n",
957 hFile
, (DWORD
)buffer
, count
);
959 /* Some programs pass a count larger than the allocated buffer */
960 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
961 if (count
> maxlen
) count
= maxlen
;
963 return FILE_Read( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
967 /***********************************************************************
968 * _hwrite (KERNEL.350)
970 LONG
_hwrite( HFILE hFile
, LPCSTR buffer
, LONG count
)
975 dprintf_file( stddeb
, "_hwrite: %d %p %ld\n", hFile
, buffer
, count
);
977 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
979 if (count
== 0) /* Expand or truncate at current position */
980 result
= ftruncate( file
->unix_handle
,
981 lseek( file
->unix_handle
, 0, SEEK_CUR
) );
983 result
= write( file
->unix_handle
, buffer
, count
);
985 if (result
== -1) FILE_SetDosError();
990 /***********************************************************************
991 * SetHandleCount (KERNEL.199)
993 WORD
SetHandleCount( WORD count
)
995 HANDLE hPDB
= GetCurrentPDB();
996 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
997 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1000 dprintf_file( stddeb
, "SetHandleCount(%d)\n", count
);
1002 if (count
< 20) count
= 20; /* No point in going below 20 */
1003 else if (count
> 254) count
= 254;
1005 /* If shrinking the table, make sure all extra file handles are closed */
1006 if (count
< pdb
->nbFiles
)
1008 for (i
= count
; i
< pdb
->nbFiles
; i
++)
1009 if (files
[i
] != 0xff) /* File open */
1011 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
,
1012 SA_Abort
, EL_Disk
);
1013 return pdb
->nbFiles
;
1019 if (pdb
->nbFiles
> 20)
1021 memcpy( pdb
->fileHandles
, files
, 20 );
1023 GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1024 pdb
->fileHandlesPtr
= (SEGPTR
)pdb
->fileHandles
;
1026 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1027 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1028 GlobalHandleToSel( hPDB
) );
1033 else /* More than 20, need a new file handles table */
1037 newfiles
= (BYTE
*)GlobalAlloc32( GMEM_FIXED
, count
);
1039 HANDLE newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1042 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1043 return pdb
->nbFiles
;
1045 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1046 #endif /* WINELIB */
1047 if (count
> pdb
->nbFiles
)
1049 memcpy( newfiles
, files
, pdb
->nbFiles
);
1050 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1052 else memcpy( newfiles
, files
, count
);
1054 if (pdb
->nbFiles
> 20) GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1055 pdb
->fileHandlesPtr
= (SEGPTR
)newfiles
;
1057 if (pdb
->nbFiles
> 20)
1058 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1059 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1060 #endif /* WINELIB */
1061 pdb
->nbFiles
= count
;
1063 return pdb
->nbFiles
;
1067 /***********************************************************************
1068 * FlushFileBuffers (KERNEL32.133)
1070 BOOL32
FlushFileBuffers( HFILE hFile
)
1074 dprintf_file( stddeb
, "FlushFileBuffers(%d)\n", hFile
);
1075 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1076 if (fsync( file
->unix_handle
) != -1) return TRUE
;
1082 /***********************************************************************
1083 * CreateDirectory16 (KERNEL.144)
1085 BOOL16
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
1087 dprintf_file( stddeb
,"CreateDirectory16(%s,%p)\n", path
, dummy
);
1088 return (BOOL16
)CreateDirectory32A( path
, NULL
);
1092 /***********************************************************************
1093 * CreateDirectory32A (KERNEL32.39)
1095 BOOL32
CreateDirectory32A( LPCSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1097 const char *unixName
;
1099 dprintf_file( stddeb
, "CreateDirectory32A(%s,%p)\n", path
, lpsecattribs
);
1100 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1102 dprintf_file(stddeb
, "CreateDirectory: device '%s'!\n", unixName
);
1103 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1106 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
1107 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
1116 /***********************************************************************
1117 * CreateDirectory32W (KERNEL32.42)
1119 BOOL32
CreateDirectory32W( LPCWSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1121 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1122 BOOL32 ret
= CreateDirectory32A(xpath
,lpsecattribs
);