2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
22 #include "directory.h"
35 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
37 typedef struct tagDOS_FILE
39 struct tagDOS_FILE
*next
;
40 int count
; /* Usage count (0 if free) */
46 DWORD type
; /* Type for win32 apps */
49 /* Global files array */
50 static DOS_FILE DOSFiles
[MAX_OPEN_FILES
];
52 static DOS_FILE
*FILE_First
= DOSFiles
;
53 static DOS_FILE
*FILE_LastUsed
= DOSFiles
;
55 /* Small file handles array for boot-up, before the first PDB is created */
56 #define MAX_BOOT_HANDLES 4
57 static BYTE bootFileHandles
[MAX_BOOT_HANDLES
] = { 0xff, 0xff, 0xff, 0xff };
59 /***********************************************************************
62 * Allocate a DOS file.
64 static DOS_FILE
*FILE_Alloc(void)
66 DOS_FILE
*file
= FILE_First
;
67 if (file
) FILE_First
= file
->next
;
68 else if (FILE_LastUsed
>= &DOSFiles
[MAX_OPEN_FILES
-1])
70 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
73 else file
= ++FILE_LastUsed
;
75 file
->unix_handle
= -1;
76 file
->unix_name
= NULL
;
77 file
->type
= FILE_TYPE_DISK
;
82 /***********************************************************************
87 static BOOL
FILE_Close( DOS_FILE
*file
)
89 if (!file
->count
) return FALSE
;
90 if (--file
->count
> 0) return TRUE
;
91 /* Now really close the file */
92 if (file
->unix_handle
!= -1) close( file
->unix_handle
);
93 if (file
->unix_name
) free( file
->unix_name
);
94 file
->next
= FILE_First
;
100 /***********************************************************************
103 * Return a pointer to the current PDB files array.
105 static void FILE_GetPDBFiles( BYTE
**files
, WORD
*nbFiles
)
109 if ((pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() )) != NULL
)
111 *files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
112 *nbFiles
= pdb
->nbFiles
;
116 *files
= bootFileHandles
;
117 *nbFiles
= MAX_BOOT_HANDLES
;
122 /***********************************************************************
123 * FILE_AllocTaskHandle
125 * Allocate a task file handle for a DOS file.
127 static HFILE
FILE_AllocTaskHandle( DOS_FILE
*dos_file
)
132 FILE_GetPDBFiles( &files
, &nbFiles
);
133 fp
= files
+ 1; /* Don't use handle 0, as some programs don't like it */
134 for (i
= nbFiles
- 1; (i
> 0) && (*fp
!= 0xff); i
--, fp
++);
136 { /* No more handles or files */
137 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
, SA_Abort
, EL_Disk
);
140 *fp
= dos_file
? (BYTE
)(dos_file
- DOSFiles
) : 0;
142 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
143 (fp
- files
), *fp
, nbFiles
- i
, nbFiles
);
144 return (HFILE
)(fp
- files
);
148 /***********************************************************************
149 * FILE_FreeTaskHandle
151 * Free a per-task file handle.
153 static void FILE_FreeTaskHandle( HFILE handle
)
158 FILE_GetPDBFiles( &files
, &nbFiles
);
159 dprintf_file( stddeb
,"FILE_FreeTaskHandle: dos=%d file=%d\n",
160 handle
, files
[handle
] );
161 if ((handle
< 0) || (handle
>= (INT
)nbFiles
))
163 fprintf( stderr
, "FILE_FreeTaskHandle: invalid file handle %d\n",
167 files
[handle
] = 0xff;
171 /***********************************************************************
174 * Set the value of a task handle (no error checking).
176 static void FILE_SetTaskHandle( HFILE handle
, DOS_FILE
*file
)
181 FILE_GetPDBFiles( &files
, &nbFiles
);
182 files
[handle
] = (BYTE
)(file
- DOSFiles
);
186 /***********************************************************************
189 * Return the DOS file associated to a task file handle.
191 static DOS_FILE
*FILE_GetFile( HFILE handle
)
197 FILE_GetPDBFiles( &files
, &nbFiles
);
198 if ((handle
< 0) || (handle
>= (INT
)nbFiles
) ||
199 (files
[handle
] >= MAX_OPEN_FILES
) ||
200 !(file
= &DOSFiles
[files
[handle
]])->count
)
202 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
209 int FILE_GetUnixHandle( HFILE hFile
)
213 if (!(file
= FILE_GetFile( hFile
))) return -1;
214 return file
->unix_handle
;
218 /***********************************************************************
221 * Close all open files of a given PDB. Used on task termination.
223 void FILE_CloseAllFiles( HANDLE16 hPDB
)
227 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
230 files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
231 dprintf_file(stddeb
,"FILE_CloseAllFiles: closing %d files\n",pdb
->nbFiles
);
232 for (count
= pdb
->nbFiles
; count
> 0; count
--, files
++)
234 if (*files
< MAX_OPEN_FILES
) FILE_Close( &DOSFiles
[*files
] );
240 /***********************************************************************
243 * Set the DOS error code from errno.
245 void FILE_SetDosError(void)
247 dprintf_file(stddeb
, "FILE_SetDosError: errno = %d\n", errno
);
251 DOS_ERROR( ER_ShareViolation
, EC_Temporary
, SA_Retry
, EL_Disk
);
254 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
257 DOS_ERROR( ER_DiskFull
, EC_MediaError
, SA_Abort
, EL_Disk
);
262 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
265 DOS_ERROR( ER_LockViolation
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
268 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
271 DOS_ERROR( ER_CanNotMakeDir
, EC_AccessDenied
, SA_Abort
, EL_Unknown
);
275 DOS_ERROR( ER_NoMoreFiles
, EC_MediaError
, SA_Abort
, EL_Unknown
);
278 DOS_ERROR( ER_FileExists
, EC_Exists
, SA_Abort
, EL_Disk
);
281 perror( "int21: unknown errno" );
282 DOS_ERROR( ER_GeneralFailure
, EC_SystemFailure
, SA_Abort
, EL_Unknown
);
288 /***********************************************************************
291 * Duplicate a Unix handle into a task handle.
293 HFILE
FILE_DupUnixHandle( int fd
)
298 if (!(file
= FILE_Alloc())) return HFILE_ERROR
;
299 if ((file
->unix_handle
= dup(fd
)) == -1)
305 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
311 /***********************************************************************
314 static DOS_FILE
*FILE_OpenUnixFile( const char *name
, int mode
)
319 if (!(file
= FILE_Alloc())) return NULL
;
320 if ((file
->unix_handle
= open( name
, mode
, 0666 )) == -1)
322 if (Options
.allowReadOnly
&& (mode
== O_RDWR
))
324 if ((file
->unix_handle
= open( name
, O_RDONLY
)) != -1)
325 fprintf( stderr
, "Warning: could not open %s for writing, opening read-only.\n", name
);
328 if ((file
->unix_handle
== -1) || (fstat( file
->unix_handle
, &st
) == -1))
334 if (S_ISDIR(st
.st_mode
))
336 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
341 /* File opened OK, now fill the DOS_FILE */
343 file
->unix_name
= xstrdup( name
);
344 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
349 /***********************************************************************
352 HFILE
FILE_Open( LPCSTR path
, INT32 mode
)
354 const char *unixName
;
358 dprintf_file(stddeb
, "FILE_Open: '%s' %04x\n", path
, mode
);
359 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
361 dprintf_file( stddeb
, "FILE_Open: opening device '%s'\n", unixName
);
362 if (!unixName
[0]) /* Non-existing device */
364 dprintf_file(stddeb
, "FILE_Open: Non-existing device\n");
365 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
369 else if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
)))
372 if (!(file
= FILE_OpenUnixFile( unixName
, mode
))) return HFILE_ERROR
;
373 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
379 /***********************************************************************
382 static DOS_FILE
*FILE_Create( LPCSTR path
, int mode
, int unique
)
385 const char *unixName
;
387 dprintf_file(stddeb
, "FILE_Create: '%s' %04x %d\n", path
, mode
, unique
);
389 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
391 dprintf_file(stddeb
, "FILE_Create: creating device '%s'!\n", unixName
);
392 DOS_ERROR( ER_AccessDenied
, EC_NotFound
, SA_Abort
, EL_Disk
);
396 if (!(file
= FILE_Alloc())) return NULL
;
398 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
)))
403 if ((file
->unix_handle
= open( unixName
,
404 O_CREAT
| O_TRUNC
| O_RDWR
| (unique
? O_EXCL
: 0),
412 /* File created OK, now fill the DOS_FILE */
414 file
->unix_name
= xstrdup( unixName
);
415 DOSFS_ToDosDateTime( time(NULL
), &file
->filedate
, &file
->filetime
);
420 /***********************************************************************
423 * Stat a Unix path name. Return 1 if OK.
425 int FILE_Stat( LPCSTR unixName
, BYTE
*pattr
, DWORD
*psize
,
426 WORD
*pdate
, WORD
*ptime
)
430 if (stat( unixName
, &st
) == -1)
435 if (pattr
) *pattr
= FA_ARCHIVE
| (S_ISDIR(st
.st_mode
) ? FA_DIRECTORY
: 0);
436 if (psize
) *psize
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
437 DOSFS_ToDosDateTime( st
.st_mtime
, pdate
, ptime
);
442 /***********************************************************************
445 * Get the date and time of a file.
447 int FILE_GetDateTime( HFILE hFile
, WORD
*pdate
, WORD
*ptime
, BOOL32 refresh
)
451 if (!(file
= FILE_GetFile( hFile
))) return 0;
455 if (fstat( file
->unix_handle
, &st
) == -1)
460 DOSFS_ToDosDateTime( st
.st_mtime
, &file
->filedate
, &file
->filetime
);
462 *pdate
= file
->filedate
;
463 *ptime
= file
->filetime
;
468 /***********************************************************************
471 * Set the date and time of a file.
473 int FILE_SetDateTime( HFILE hFile
, WORD date
, WORD time
)
477 struct utimbuf filetime
;
479 if (!(file
= FILE_GetFile( hFile
))) return 0;
480 newtm
.tm_sec
= (time
& 0x1f) * 2;
481 newtm
.tm_min
= (time
>> 5) & 0x3f;
482 newtm
.tm_hour
= (time
>> 11);
483 newtm
.tm_mday
= (date
& 0x1f);
484 newtm
.tm_mon
= ((date
>> 5) & 0x0f) - 1;
485 newtm
.tm_year
= (date
>> 9) + 80;
487 filetime
.actime
= filetime
.modtime
= mktime( &newtm
);
488 if (utime( file
->unix_name
, &filetime
) != -1) return 1;
494 /***********************************************************************
497 * dup() function for DOS handles.
499 HFILE
FILE_Dup( HFILE hFile
)
504 dprintf_file( stddeb
, "FILE_Dup for handle %d\n", hFile
);
505 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
506 if ((handle
= FILE_AllocTaskHandle( file
)) != HFILE_ERROR
) file
->count
++;
507 dprintf_file( stddeb
, "FILE_Dup return handle %d\n", handle
);
512 /***********************************************************************
515 * dup2() function for DOS handles.
517 HFILE
FILE_Dup2( HFILE hFile1
, HFILE hFile2
)
520 PDB
*pdb
= (PDB
*)GlobalLock16( GetCurrentPDB() );
521 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
523 dprintf_file( stddeb
, "FILE_Dup2 for handle %d\n", hFile1
);
524 if (!(file
= FILE_GetFile( hFile1
))) return HFILE_ERROR
;
526 if ((hFile2
< 0) || (hFile2
>= (INT
)pdb
->nbFiles
))
528 DOS_ERROR( ER_InvalidHandle
, EC_ProgramError
, SA_Abort
, EL_Disk
);
531 if (files
[hFile2
] < MAX_OPEN_FILES
)
533 dprintf_file( stddeb
, "FILE_Dup2 closing old handle2 %d\n",
535 FILE_Close( &DOSFiles
[files
[hFile2
]] );
537 files
[hFile2
] = (BYTE
)(file
- DOSFiles
);
539 dprintf_file( stddeb
, "FILE_Dup2 return handle2 %d\n", hFile2
);
544 /***********************************************************************
545 * GetTempFileName16 (KERNEL.97)
547 UINT16
GetTempFileName16( BYTE drive
, LPCSTR prefix
, UINT16 unique
,
552 if ((drive
& TF_FORCEDRIVE
) &&
553 !DRIVE_IsValid( toupper(drive
& ~TF_FORCEDRIVE
) - 'A' ))
555 drive
&= ~TF_FORCEDRIVE
;
556 fprintf( stderr
, "Warning: GetTempFileName: invalid drive %d specified\n",
560 if (drive
& TF_FORCEDRIVE
)
561 sprintf(temppath
,"%c:", drive
& ~TF_FORCEDRIVE
);
564 GetTempPath32A( 132, temppath
);
565 strcat( temppath
, "\\" );
567 return (UINT16
)GetTempFileName32A( temppath
, prefix
, unique
, buffer
);
571 /***********************************************************************
572 * GetTempFileName32A (KERNEL32.290)
574 UINT32
GetTempFileName32A( LPCSTR path
, LPCSTR prefix
, UINT32 unique
,
579 UINT32 num
= unique
? (unique
& 0xffff) : time(NULL
) & 0xffff;
582 strcpy( buffer
, path
);
583 p
= buffer
+ strlen(buffer
);
585 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
586 sprintf( p
, "%04x.tmp", num
);
590 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
591 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
592 if (-1==access(DOSFS_GetUnixFileName(buffer
,TRUE
),W_OK
))
593 fprintf(stderr
,"Warning: GetTempFileName returns '%s', which doesn't seem to be writeable. Please check your configuration file if this generates a failure.\n",buffer
);
597 /* Now try to create it */
602 if ((file
= FILE_Create( buffer
, 0666, TRUE
)) != NULL
)
603 { /* We created it */
604 dprintf_file( stddeb
, "GetTempFileName: created %s\n", buffer
);
608 if (DOS_ExtendedError
!= ER_FileExists
) break; /* No need to go on */
610 sprintf( p
, "%04x.tmp", num
);
611 } while (num
!= (unique
& 0xffff));
613 lstrcpyn32A( buffer
, DOSFS_GetDosTrueName( buffer
, FALSE
), 144 );
614 dprintf_file( stddeb
, "GetTempFileName: returning %s\n", buffer
);
615 if (-1==access(DOSFS_GetUnixFileName(buffer
,TRUE
),W_OK
))
616 fprintf(stderr
,"Warning: GetTempFileName returns '%s', which doesn't seem to be writeable. Please check your configuration file if this generates a failure.\n",buffer
);
621 /***********************************************************************
622 * GetTempFileName32W (KERNEL32.291)
624 UINT32
GetTempFileName32W( LPCWSTR path
, LPCWSTR prefix
, UINT32 unique
,
632 patha
= STRING32_DupUniToAnsi(path
);
633 prefixa
= STRING32_DupUniToAnsi(prefix
);
634 ret
= GetTempFileName32A( patha
, prefixa
, unique
, buffera
);
635 STRING32_AnsiToUni( buffer
, buffera
);
642 /***********************************************************************
643 * OpenFile (KERNEL.74) (KERNEL32.396)
645 HFILE
OpenFile( LPCSTR name
, OFSTRUCT
*ofs
, UINT32 mode
)
649 WORD filedatetime
[2];
650 const char *unixName
, *dosName
;
652 int len
, i
, unixMode
;
654 ofs
->cBytes
= sizeof(OFSTRUCT
);
656 if (mode
& OF_REOPEN
) name
= ofs
->szPathName
;
657 dprintf_file( stddeb
, "OpenFile: %s %04x\n", name
, mode
);
659 /* First allocate a task handle */
661 if ((hFileRet
= FILE_AllocTaskHandle( NULL
)) == HFILE_ERROR
)
663 ofs
->nErrCode
= DOS_ExtendedError
;
664 dprintf_file( stddeb
, "OpenFile: no more task handles.\n" );
668 /* OF_PARSE simply fills the structure */
672 if (!(dosName
= DOSFS_GetDosTrueName( name
, FALSE
))) goto error
;
673 lstrcpyn32A( ofs
->szPathName
, dosName
, sizeof(ofs
->szPathName
) );
674 ofs
->fFixedDisk
= (GetDriveType16( dosName
[0]-'A' ) != DRIVE_REMOVABLE
);
675 dprintf_file( stddeb
, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
676 name
, ofs
->szPathName
, hFileRet
);
677 /* Return the handle, but close it first */
678 FILE_FreeTaskHandle( hFileRet
);
679 /* return hFileRet; */
680 return 0; /* Progman seems to like this better */
683 /* OF_CREATE is completely different from all other options, so
686 if (mode
& OF_CREATE
)
688 if (!(file
= FILE_Create( name
, 0666, FALSE
))) goto error
;
689 lstrcpyn32A( ofs
->szPathName
, DOSFS_GetDosTrueName( name
, FALSE
),
690 sizeof(ofs
->szPathName
) );
694 /* Now look for the file */
696 /* First try the current directory */
698 lstrcpyn32A( ofs
->szPathName
, name
, sizeof(ofs
->szPathName
) );
699 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
702 /* Now try some different paths if none was specified */
704 if ((mode
& OF_SEARCH
) && !(mode
& OF_REOPEN
))
706 if (name
[1] == ':') name
+= 2;
707 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
708 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
709 if (!name
[0]) goto not_found
;
713 if ((name
[1] == ':') || strchr( name
, '/' ) || strchr( name
, '\\' ))
717 if ((len
= sizeof(ofs
->szPathName
) - strlen(name
) - 1) < 0) goto not_found
;
719 /* Try the Windows directory */
721 GetWindowsDirectory32A( ofs
->szPathName
, len
);
722 strcat( ofs
->szPathName
, "\\" );
723 strcat( ofs
->szPathName
, name
);
724 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
727 /* Try the Windows system directory */
729 GetSystemDirectory32A( ofs
->szPathName
, len
);
730 strcat( ofs
->szPathName
, "\\" );
731 strcat( ofs
->szPathName
, name
);
732 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
735 /* Try the path of the current executable */
737 if (GetCurrentTask())
739 GetModuleFileName( GetCurrentTask(), ofs
->szPathName
, len
);
740 if ((p
= strrchr( ofs
->szPathName
, '\\' )))
742 strcpy( p
+ 1, name
);
743 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)))
748 /* Try all directories in path */
752 if (!DIR_GetDosPath( i
, ofs
->szPathName
, len
)) goto not_found
;
753 strcat( ofs
->szPathName
, "\\" );
754 strcat( ofs
->szPathName
, name
);
755 if ((unixName
= DOSFS_GetUnixFileName( ofs
->szPathName
, TRUE
)) != NULL
)
760 dprintf_file( stddeb
, "OpenFile: found '%s'\n", unixName
);
761 lstrcpyn32A(ofs
->szPathName
, DOSFS_GetDosTrueName( ofs
->szPathName
, FALSE
),
762 sizeof(ofs
->szPathName
) );
764 if (mode
& OF_DELETE
)
766 if (unlink( unixName
) == -1) goto not_found
;
767 dprintf_file( stddeb
, "OpenFile(%s): OF_DELETE return = OK\n", name
);
768 /* Return the handle, but close it first */
769 FILE_FreeTaskHandle( hFileRet
);
776 unixMode
= O_WRONLY
; break;
778 unixMode
= O_RDWR
; break;
781 unixMode
= O_RDONLY
; break;
784 if (!(file
= FILE_OpenUnixFile( unixName
, unixMode
))) goto not_found
;
785 filedatetime
[0] = file
->filedate
;
786 filedatetime
[1] = file
->filetime
;
787 if ((mode
& OF_VERIFY
) && (mode
& OF_REOPEN
))
789 if (memcmp( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) ))
792 dprintf_file( stddeb
, "OpenFile(%s): OF_VERIFY failed\n", name
);
793 /* FIXME: what error here? */
794 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
798 memcpy( ofs
->reserved
, filedatetime
, sizeof(ofs
->reserved
) );
803 /* Return the handle, but close it first */
804 FILE_FreeTaskHandle( hFileRet
);
808 success
: /* We get here if the open was successful */
809 dprintf_file( stddeb
, "OpenFile(%s): OK, return = %d\n", name
, hFileRet
);
810 FILE_SetTaskHandle( hFileRet
, file
);
813 not_found
: /* We get here if the file does not exist */
814 dprintf_file( stddeb
, "OpenFile: '%s' not found\n", name
);
815 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
818 error
: /* We get here if there was an error opening the file */
819 ofs
->nErrCode
= DOS_ExtendedError
;
820 dprintf_file( stddeb
, "OpenFile(%s): return = HFILE_ERROR\n", name
);
821 FILE_FreeTaskHandle( hFileRet
);
825 /***********************************************************************
826 * SearchPath32A (KERNEL32.447)
827 * Code borrowed from OpenFile above.
830 LPCSTR path
,LPCSTR fn
,LPCSTR ext
,DWORD buflen
,LPSTR buf
,LPSTR
*lastpart
834 char testpath
[1000]; /* should be enough for now */
840 name
=(char*)xmalloc(strlen(fn
)+strlen(ext
)+1);
844 dprintf_file(stddeb
,"SearchPath32A(%s,%s,%s,%ld,%p,%p)\n",
845 path
,fn
,ext
,buflen
,buf
,lastpart
848 strcpy(testpath
,path
);
849 strcat(testpath
,"\\");
850 strcat(testpath
,name
);
851 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
) {
854 strcpy(testpath
,name
);
855 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
)
860 if ((len
=sizeof(testpath
)-strlen(name
)-1)<0)
863 /* Try the path of the current executable */
864 if (GetCurrentTask()) {
865 GetModuleFileName(GetCurrentTask(),testpath
,len
);
866 if ((p
=strrchr(testpath
,'\\'))) {
868 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
)))
873 /* Try the current directory */
874 lstrcpyn32A(testpath
,name
,sizeof(testpath
) );
875 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
)
878 /* Try the Windows directory */
879 GetWindowsDirectory32A(testpath
,len
);
880 strcat(testpath
,"\\");
881 strcat(testpath
,name
);
882 if ((unixName
= DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
)
885 /* Try the Windows system directory */
886 GetSystemDirectory32A(testpath
,len
);
887 strcat(testpath
,"\\");
888 strcat(testpath
,name
);
889 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
)
892 /* Try all directories in path */
896 if (!DIR_GetDosPath(i
,testpath
,len
))
898 strcat(testpath
,"\\");
899 strcat(testpath
,name
);
900 if ((unixName
=DOSFS_GetUnixFileName((LPCSTR
)testpath
,TRUE
))!=NULL
)
905 strncpy(buf
,testpath
,buflen
);
906 if (NULL
!=(p
=strrchr(testpath
,'\\')))
911 if (p
-testpath
<buflen
)
912 *lastpart
=(p
-testpath
)+buf
;
916 dprintf_file(stddeb
," -> found %s,last part is %s\n",testpath
,p
);
917 return strlen(testpath
);
920 /***********************************************************************
921 * SearchPath32W (KERNEL32.448)
924 LPCWSTR path
,LPCWSTR fn
,LPCWSTR ext
,DWORD buflen
,LPWSTR buf
,
927 LPSTR pathA
= path
?STRING32_DupUniToAnsi(path
):NULL
;
928 LPSTR fnA
= STRING32_DupUniToAnsi(fn
);
929 LPSTR extA
= ext
?STRING32_DupUniToAnsi(fn
):NULL
;
931 LPSTR bufA
= (char*)xmalloc(buflen
+1);
934 ret
=SearchPath32A(pathA
,fnA
,extA
,buflen
,bufA
,&lastpartA
);
935 lstrcpynAtoW(buf
,bufA
,buflen
);
938 *lastpart
= buf
+(lastpartA
-bufA
);
944 if (pathA
) free(pathA
);
945 if (extA
) free(extA
);
949 /***********************************************************************
950 * _lclose (KERNEL.81) (KERNEL32.592)
952 HFILE
_lclose( HFILE hFile
)
956 dprintf_file( stddeb
, "_lclose: handle %d\n", hFile
);
957 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
959 FILE_FreeTaskHandle( hFile
);
964 /***********************************************************************
967 LONG
WIN16_hread( HFILE hFile
, SEGPTR buffer
, LONG count
)
971 dprintf_file( stddeb
, "_hread16: %d %08lx %ld\n",
972 hFile
, (DWORD
)buffer
, count
);
974 /* Some programs pass a count larger than the allocated buffer */
975 maxlen
= GetSelectorLimit( SELECTOROF(buffer
) ) - OFFSETOF(buffer
) + 1;
976 if (count
> maxlen
) count
= maxlen
;
977 return _lread32( hFile
, PTR_SEG_TO_LIN(buffer
), count
);
981 /***********************************************************************
984 UINT16
WIN16_lread( HFILE hFile
, SEGPTR buffer
, UINT16 count
)
986 return (UINT16
)WIN16_hread( hFile
, buffer
, (LONG
)count
);
990 /***********************************************************************
991 * _lread32 (KERNEL32.596)
993 UINT32
_lread32( HFILE hFile
, LPVOID buffer
, UINT32 count
)
998 dprintf_file( stddeb
, "_lread32: %d %p %d\n", hFile
, buffer
, count
);
999 if (!(file
= FILE_GetFile( hFile
))) return -1;
1000 if (!count
) return 0;
1001 if ((result
= read( file
->unix_handle
, buffer
, count
)) == -1)
1007 /***********************************************************************
1008 * _lread16 (KERNEL.82)
1010 UINT16
_lread16( HFILE hFile
, LPVOID buffer
, UINT16 count
)
1012 return (UINT16
)_lread32( hFile
, buffer
, (LONG
)count
);
1016 /***********************************************************************
1017 * _lcreat (KERNEL.83) (KERNEL32.593)
1019 HFILE
_lcreat( LPCSTR path
, INT32 attr
)
1025 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
1026 mode
= (attr
& 1) ? 0444 : 0666;
1027 if (!(file
= FILE_Create( path
, mode
, FALSE
))) return HFILE_ERROR
;
1028 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
1034 /***********************************************************************
1035 * _lcreat_uniq (Not a Windows API)
1037 HFILE
_lcreat_uniq( LPCSTR path
, INT32 attr
)
1043 dprintf_file( stddeb
, "_lcreat: %s %02x\n", path
, attr
);
1044 mode
= (attr
& 1) ? 0444 : 0666;
1045 if (!(file
= FILE_Create( path
, mode
, TRUE
))) return HFILE_ERROR
;
1046 if ((handle
= FILE_AllocTaskHandle( file
)) == HFILE_ERROR
)
1052 /***********************************************************************
1053 * _llseek (KERNEL.84) (KERNEL32.594)
1055 LONG
_llseek( HFILE hFile
, LONG lOffset
, INT32 nOrigin
)
1060 dprintf_file( stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
1061 hFile
, lOffset
, nOrigin
);
1063 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
1066 case 1: origin
= SEEK_CUR
; break;
1067 case 2: origin
= SEEK_END
; break;
1068 default: origin
= SEEK_SET
; break;
1071 if ((result
= lseek( file
->unix_handle
, lOffset
, origin
)) == -1)
1077 /***********************************************************************
1078 * _lopen (KERNEL.85) (KERNEL32.595)
1080 HFILE
_lopen( LPCSTR path
, INT32 mode
)
1084 dprintf_file(stddeb
, "_lopen('%s',%04x)\n", path
, mode
);
1089 unixMode
= O_WRONLY
| O_TRUNC
;
1096 unixMode
= O_RDONLY
;
1099 return FILE_Open( path
, unixMode
);
1103 /***********************************************************************
1104 * _lwrite16 (KERNEL.86)
1106 UINT16
_lwrite16( HFILE hFile
, LPCSTR buffer
, UINT16 count
)
1108 return (UINT16
)_hwrite( hFile
, buffer
, (LONG
)count
);
1111 /***********************************************************************
1112 * _lwrite32 (KERNEL.86)
1114 UINT32
_lwrite32( HFILE hFile
, LPCSTR buffer
, UINT32 count
)
1116 return (UINT32
)_hwrite( hFile
, buffer
, (LONG
)count
);
1120 /***********************************************************************
1121 * _hread (KERNEL.349)
1123 LONG
_hread( HFILE hFile
, LPVOID buffer
, LONG count
)
1125 return _lread32( hFile
, buffer
, count
);
1129 /***********************************************************************
1130 * _hwrite (KERNEL.350)
1132 LONG
_hwrite( HFILE hFile
, LPCSTR buffer
, LONG count
)
1137 dprintf_file( stddeb
, "_hwrite: %d %p %ld\n", hFile
, buffer
, count
);
1139 if (!(file
= FILE_GetFile( hFile
))) return HFILE_ERROR
;
1141 if (count
== 0) /* Expand or truncate at current position */
1142 result
= ftruncate( file
->unix_handle
,
1143 lseek( file
->unix_handle
, 0, SEEK_CUR
) );
1145 result
= write( file
->unix_handle
, buffer
, count
);
1147 if (result
== -1) FILE_SetDosError();
1152 /***********************************************************************
1153 * SetHandleCount16 (KERNEL.199)
1155 UINT16
SetHandleCount16( UINT16 count
)
1157 HGLOBAL16 hPDB
= GetCurrentPDB();
1158 PDB
*pdb
= (PDB
*)GlobalLock16( hPDB
);
1159 BYTE
*files
= PTR_SEG_TO_LIN( pdb
->fileHandlesPtr
);
1162 dprintf_file( stddeb
, "SetHandleCount(%d)\n", count
);
1164 if (count
< 20) count
= 20; /* No point in going below 20 */
1165 else if (count
> 254) count
= 254;
1167 /* If shrinking the table, make sure all extra file handles are closed */
1168 if (count
< pdb
->nbFiles
)
1170 for (i
= count
; i
< pdb
->nbFiles
; i
++)
1171 if (files
[i
] != 0xff) /* File open */
1173 DOS_ERROR( ER_TooManyOpenFiles
, EC_ProgramError
,
1174 SA_Abort
, EL_Disk
);
1175 return pdb
->nbFiles
;
1181 if (pdb
->nbFiles
> 20)
1183 memcpy( pdb
->fileHandles
, files
, 20 );
1185 GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1186 pdb
->fileHandlesPtr
= (SEGPTR
)pdb
->fileHandles
;
1188 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1189 pdb
->fileHandlesPtr
= (SEGPTR
)MAKELONG( 0x18,
1190 GlobalHandleToSel( hPDB
) );
1195 else /* More than 20, need a new file handles table */
1199 newfiles
= (BYTE
*)GlobalAlloc32( GMEM_FIXED
, count
);
1201 HGLOBAL16 newhandle
= GlobalAlloc16( GMEM_MOVEABLE
, count
);
1204 DOS_ERROR( ER_OutOfMemory
, EC_OutOfResource
, SA_Abort
, EL_Memory
);
1205 return pdb
->nbFiles
;
1207 newfiles
= (BYTE
*)GlobalLock16( newhandle
);
1208 #endif /* WINELIB */
1209 if (count
> pdb
->nbFiles
)
1211 memcpy( newfiles
, files
, pdb
->nbFiles
);
1212 memset( newfiles
+ pdb
->nbFiles
, 0xff, count
- pdb
->nbFiles
);
1214 else memcpy( newfiles
, files
, count
);
1216 if (pdb
->nbFiles
> 20) GlobalFree32( (HGLOBAL32
)pdb
->fileHandlesPtr
);
1217 pdb
->fileHandlesPtr
= (SEGPTR
)newfiles
;
1219 if (pdb
->nbFiles
> 20)
1220 GlobalFree16( GlobalHandle16( SELECTOROF(pdb
->fileHandlesPtr
) ));
1221 pdb
->fileHandlesPtr
= WIN16_GlobalLock16( newhandle
);
1222 #endif /* WINELIB */
1223 pdb
->nbFiles
= count
;
1225 return pdb
->nbFiles
;
1229 /***********************************************************************
1230 * FlushFileBuffers (KERNEL32.133)
1232 BOOL32
FlushFileBuffers( HFILE hFile
)
1236 dprintf_file( stddeb
, "FlushFileBuffers(%d)\n", hFile
);
1237 if (!(file
= FILE_GetFile( hFile
))) return FALSE
;
1238 if (fsync( file
->unix_handle
) != -1) return TRUE
;
1244 /***********************************************************************
1245 * DeleteFile16 (KERNEL.146)
1247 BOOL16
DeleteFile16( LPCSTR path
)
1249 return DeleteFile32A( path
);
1253 /***********************************************************************
1254 * DeleteFile32A (KERNEL32.71)
1256 BOOL32
DeleteFile32A( LPCSTR path
)
1258 const char *unixName
;
1260 dprintf_file(stddeb
, "DeleteFile: '%s'\n", path
);
1262 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1264 dprintf_file(stddeb
, "DeleteFile: removing device '%s'!\n", unixName
);
1265 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1269 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return FALSE
;
1270 if (unlink( unixName
) == -1)
1279 /***********************************************************************
1280 * DeleteFile32W (KERNEL32.72)
1282 BOOL32
DeleteFile32W( LPCWSTR path
)
1284 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1285 BOOL32 ret
= RemoveDirectory32A( xpath
);
1291 /***********************************************************************
1292 * CreateDirectory16 (KERNEL.144)
1294 BOOL16
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
1296 dprintf_file( stddeb
,"CreateDirectory16(%s,%p)\n", path
, dummy
);
1297 return (BOOL16
)CreateDirectory32A( path
, NULL
);
1301 /***********************************************************************
1302 * CreateDirectory32A (KERNEL32.39)
1304 BOOL32
CreateDirectory32A( LPCSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1306 const char *unixName
;
1308 dprintf_file( stddeb
, "CreateDirectory32A(%s,%p)\n", path
, lpsecattribs
);
1309 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1311 dprintf_file(stddeb
, "CreateDirectory: device '%s'!\n", unixName
);
1312 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
1315 if (!(unixName
= DOSFS_GetUnixFileName( path
, FALSE
))) return 0;
1316 if ((mkdir( unixName
, 0777 ) == -1) && (errno
!= EEXIST
))
1325 /***********************************************************************
1326 * CreateDirectory32W (KERNEL32.42)
1328 BOOL32
CreateDirectory32W( LPCWSTR path
, LPSECURITY_ATTRIBUTES lpsecattribs
)
1330 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1331 BOOL32 ret
= CreateDirectory32A(xpath
,lpsecattribs
);
1337 /***********************************************************************
1338 * RemoveDirectory16 (KERNEL)
1340 BOOL16
RemoveDirectory16( LPCSTR path
)
1342 return (BOOL16
)RemoveDirectory32A( path
);
1346 /***********************************************************************
1347 * RemoveDirectory32A (KERNEL32.437)
1349 BOOL32
RemoveDirectory32A( LPCSTR path
)
1351 const char *unixName
;
1353 dprintf_file(stddeb
, "RemoveDirectory: '%s'\n", path
);
1355 if ((unixName
= DOSFS_IsDevice( path
)) != NULL
)
1357 dprintf_file(stddeb
, "RemoveDirectory: device '%s'!\n", unixName
);
1358 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
1361 if (!(unixName
= DOSFS_GetUnixFileName( path
, TRUE
))) return FALSE
;
1362 if (rmdir( unixName
) == -1)
1371 /***********************************************************************
1372 * RemoveDirectory32W (KERNEL32.438)
1374 BOOL32
RemoveDirectory32W( LPCWSTR path
)
1376 LPSTR xpath
= STRING32_DupUniToAnsi(path
);
1377 BOOL32 ret
= RemoveDirectory32A( xpath
);
1383 /***********************************************************************
1386 BOOL32
FILE_SetFileType( HFILE hFile
, DWORD type
)
1388 DOS_FILE
*file
= FILE_GetFile(hFile
);
1389 if (!file
) return FALSE
;
1395 /***********************************************************************
1396 * GetFileType (KERNEL32.222)
1398 DWORD
GetFileType( HFILE hFile
)
1400 DOS_FILE
*file
= FILE_GetFile(hFile
);
1404 SetLastError( ERROR_INVALID_HANDLE
);
1405 return FILE_TYPE_UNKNOWN
; /* FIXME: correct? */