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 /* check for filename, don't check for last entry if creating */
370 if (!(unixName = DOSFS_GetUnixFileName( path, !(mode & O_CREAT) )))
373 if (!(file = FILE_OpenUnixFile( unixName, mode ))) return HFILE_ERROR;
374 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
380 /***********************************************************************
383 static DOS_FILE *FILE_Create( LPCSTR path, int mode, int unique )
386 const char *unixName;
388 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
390 if ((unixName = DOSFS_IsDevice( path )) != NULL)
392 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
393 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
397 if (!(file = FILE_Alloc())) return NULL;
399 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE )))
404 if ((file->unix_handle = open( unixName,
405 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
413 /* File created OK, now fill the DOS_FILE */
415 file->unix_name = xstrdup( unixName );
416 DOSFS_ToDosDateTime( time(NULL), &file->filedate, &file->filetime );
421 /***********************************************************************
424 * Stat a Unix path name. Return 1 if OK.
426 int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
427 WORD *pdate, WORD *ptime )
431 if (stat( unixName, &st ) == -1)
436 if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
437 if (psize) *psize = S_ISDIR(st.st_mode) ? 0 : st.st_size;
438 DOSFS_ToDosDateTime( st.st_mtime, pdate, ptime );
443 /***********************************************************************
446 * Get the date and time of a file.
448 int FILE_GetDateTime( HFILE hFile, WORD *pdate, WORD *ptime, BOOL32 refresh )
452 if (!(file = FILE_GetFile( hFile ))) return 0;
456 if (fstat( file->unix_handle, &st ) == -1)
461 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
463 *pdate = file->filedate;
464 *ptime = file->filetime;
469 /***********************************************************************
472 * Set the date and time of a file.
474 int FILE_SetDateTime( HFILE hFile, WORD date, WORD time )
478 struct utimbuf filetime;
480 if (!(file = FILE_GetFile( hFile ))) return 0;
481 newtm.tm_sec = (time & 0x1f) * 2;
482 newtm.tm_min = (time >> 5) & 0x3f;
483 newtm.tm_hour = (time >> 11);
484 newtm.tm_mday = (date & 0x1f);
485 newtm.tm_mon = ((date >> 5) & 0x0f) - 1;
486 newtm.tm_year = (date >> 9) + 80;
488 filetime.actime = filetime.modtime = mktime( &newtm );
489 if (utime( file->unix_name, &filetime ) != -1) return 1;
495 /***********************************************************************
498 * dup() function for DOS handles.
500 HFILE FILE_Dup( HFILE hFile )
505 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
506 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
507 if ((handle = FILE_AllocTaskHandle( file )) != HFILE_ERROR) file->count++;
508 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
513 /***********************************************************************
516 * dup2() function for DOS handles.
518 HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
521 PDB *pdb = (PDB *)GlobalLock16( GetCurrentPDB() );
522 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
524 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
525 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR;
527 if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
529 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
532 if (files[hFile2] < MAX_OPEN_FILES)
534 dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
536 FILE_Close( &DOSFiles[files[hFile2]] );
538 files[hFile2] = (BYTE)(file - DOSFiles);
540 dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n", hFile2 );
545 /***********************************************************************
546 * GetTempFileName16 (KERNEL.97)
548 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
553 if ((drive & TF_FORCEDRIVE) &&
554 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
556 drive &= ~TF_FORCEDRIVE;
557 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
561 if (drive & TF_FORCEDRIVE)
562 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
565 GetTempPath32A( 132, temppath );
566 strcat( temppath, "\\" );
568 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
572 /***********************************************************************
573 * GetTempFileName32A (KERNEL32.290)
575 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
580 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
583 strcpy( buffer, path );
584 p = buffer + strlen(buffer);
586 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
587 sprintf( p, "%04x.tmp", num );
591 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
592 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
593 if (-1==access(DOSFS_GetUnixFileName(buffer,TRUE),W_OK))
594 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);
598 /* Now try to create it */
603 if ((file = FILE_Create( buffer, 0666, TRUE )) != NULL)
604 { /* We created it */
605 dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
609 if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
611 sprintf( p, "%04x.tmp", num );
612 } while (num != (unique & 0xffff));
614 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
615 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
616 if (-1==access(DOSFS_GetUnixFileName(buffer,TRUE),W_OK))
617 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);
622 /***********************************************************************
623 * GetTempFileName32W (KERNEL32.291)
625 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
633 patha = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
634 prefixa = HEAP_strdupWtoA( GetProcessHeap(), 0, prefix );
635 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
636 lstrcpyAtoW( buffer, buffera );
637 HeapFree( GetProcessHeap(), 0, patha );
638 HeapFree( GetProcessHeap(), 0, prefixa );
643 /***********************************************************************
644 * OpenFile (KERNEL.74) (KERNEL32.396)
646 HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
650 WORD filedatetime[2];
651 const char *unixName, *dosName;
653 int len, i, unixMode;
655 ofs->cBytes = sizeof(OFSTRUCT);
657 if (mode & OF_REOPEN) name = ofs->szPathName;
658 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
660 /* First allocate a task handle */
662 if ((hFileRet = FILE_AllocTaskHandle( NULL )) == HFILE_ERROR)
664 ofs->nErrCode = DOS_ExtendedError;
665 dprintf_file( stddeb, "OpenFile: no more task handles.\n" );
669 /* OF_PARSE simply fills the structure */
673 if (!(dosName = DOSFS_GetDosTrueName( name, FALSE ))) goto error;
674 lstrcpyn32A( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
675 ofs->fFixedDisk = (GetDriveType16( dosName[0]-'A' ) != DRIVE_REMOVABLE);
676 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
677 name, ofs->szPathName, hFileRet );
678 /* Return the handle, but close it first */
679 FILE_FreeTaskHandle( hFileRet );
680 /* return hFileRet; */
681 return 0; /* Progman seems to like this better */
684 /* OF_CREATE is completely different from all other options, so
687 if (mode & OF_CREATE)
689 if (!(file = FILE_Create( name, 0666, FALSE ))) goto error;
690 lstrcpyn32A( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
691 sizeof(ofs->szPathName) );
695 /* Now look for the file */
697 /* First try the current directory */
699 lstrcpyn32A( ofs->szPathName, name, sizeof(ofs->szPathName) );
700 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
703 /* Now try some different paths if none was specified */
705 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
707 if (name[1] == ':') name += 2;
708 if ((p = strrchr( name, '\\' ))) name = p + 1;
709 if ((p = strrchr( name, '/' ))) name = p + 1;
710 if (!name[0]) goto not_found;
714 if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
718 if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
720 /* Try the Windows directory */
722 GetWindowsDirectory32A( ofs->szPathName, len );
723 strcat( ofs->szPathName, "\\" );
724 strcat( ofs->szPathName, name );
725 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
728 /* Try the Windows system directory */
730 GetSystemDirectory32A( ofs->szPathName, len );
731 strcat( ofs->szPathName, "\\" );
732 strcat( ofs->szPathName, name );
733 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
736 /* Try the path of the current executable */
738 if (GetCurrentTask())
740 GetModuleFileName16( GetCurrentTask(), ofs->szPathName, len );
741 if ((p = strrchr( ofs->szPathName, '\\' )))
743 strcpy( p + 1, name );
744 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
749 /* Try all directories in path */
753 if (!DIR_GetDosPath( i, ofs->szPathName, len )) goto not_found;
754 strcat( ofs->szPathName, "\\" );
755 strcat( ofs->szPathName, name );
756 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
761 dprintf_file( stddeb, "OpenFile: found '%s'\n", unixName );
762 lstrcpyn32A(ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE),
763 sizeof(ofs->szPathName) );
765 if (mode & OF_DELETE)
767 if (unlink( unixName ) == -1) goto not_found;
768 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
769 /* Return the handle, but close it first */
770 FILE_FreeTaskHandle( hFileRet );
777 unixMode = O_WRONLY; break;
779 unixMode = O_RDWR; break;
782 unixMode = O_RDONLY; break;
785 if (!(file = FILE_OpenUnixFile( unixName, unixMode ))) goto not_found;
786 filedatetime[0] = file->filedate;
787 filedatetime[1] = file->filetime;
788 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
790 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
793 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
794 /* FIXME: what error here? */
795 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
799 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
804 /* Return the handle, but close it first */
805 FILE_FreeTaskHandle( hFileRet );
809 success: /* We get here if the open was successful */
810 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
811 FILE_SetTaskHandle( hFileRet, file );
814 not_found: /* We get here if the file does not exist */
815 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
816 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
819 error: /* We get here if there was an error opening the file */
820 ofs->nErrCode = DOS_ExtendedError;
821 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR\n", name );
822 FILE_FreeTaskHandle( hFileRet );
826 /***********************************************************************
827 * SearchPath32A (KERNEL32.447)
828 * Code borrowed from OpenFile above.
831 LPCSTR path,LPCSTR fn,LPCSTR ext,DWORD buflen,LPSTR buf,LPSTR *lastpart
835 char testpath[1000]; /* should be enough for now */
841 name=(char*)xmalloc(strlen(fn)+strlen(ext)+1);
845 dprintf_file(stddeb,"SearchPath32A(%s,%s,%s,%ld,%p,%p)\n",
846 path,fn,ext,buflen,buf,lastpart
849 strcpy(testpath,path);
850 strcat(testpath,"\\");
851 strcat(testpath,name);
852 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL) {
855 strcpy(testpath,name);
856 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
861 if ((len=sizeof(testpath)-strlen(name)-1)<0)
864 /* Try the path of the current executable */
865 if (GetCurrentTask()) {
866 GetModuleFileName16(GetCurrentTask(),testpath,len);
867 if ((p=strrchr(testpath,'\\'))) {
869 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE)))
874 /* Try the current directory */
875 lstrcpyn32A(testpath,name,sizeof(testpath) );
876 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
879 /* Try the Windows directory */
880 GetWindowsDirectory32A(testpath,len);
881 strcat(testpath,"\\");
882 strcat(testpath,name);
883 if ((unixName = DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
886 /* Try the Windows system directory */
887 GetSystemDirectory32A(testpath,len);
888 strcat(testpath,"\\");
889 strcat(testpath,name);
890 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
893 /* Try all directories in path */
897 if (!DIR_GetDosPath(i,testpath,len))
899 strcat(testpath,"\\");
900 strcat(testpath,name);
901 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
906 strncpy(buf,testpath,buflen);
907 if (NULL!=(p=strrchr(testpath,'\\')))
912 if (p-testpath<buflen)
913 *lastpart=(p-testpath)+buf;
917 dprintf_file(stddeb," -> found %s,last part is %s\n",testpath,p);
918 return strlen(testpath);
921 /***********************************************************************
922 * SearchPath32W (KERNEL32.448)
924 DWORD SearchPath32W( LPCWSTR path, LPCWSTR fn, LPCWSTR ext, DWORD buflen,
925 LPWSTR buf, LPWSTR *lastpart )
927 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
928 LPSTR fnA = HEAP_strdupWtoA( GetProcessHeap(), 0, fn );
929 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
931 LPSTR bufA = HeapAlloc( GetProcessHeap(), 0, buflen + 1 );
934 ret = SearchPath32A(pathA,fnA,extA,buflen,bufA,&lastpartA);
935 lstrcpyAtoW( buf, bufA );
938 if (lastpartA) *lastpart = buf+(lastpartA-bufA);
939 else *lastpart = NULL;
941 HeapFree( GetProcessHeap(), 0, bufA );
942 HeapFree( GetProcessHeap(), 0, fnA );
943 HeapFree( GetProcessHeap(), 0, pathA );
944 HeapFree( GetProcessHeap(), 0, extA );
948 /***********************************************************************
949 * _lclose (KERNEL.81) (KERNEL32.592)
951 HFILE _lclose( HFILE hFile )
955 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
956 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
958 FILE_FreeTaskHandle( hFile );
963 /***********************************************************************
966 LONG WIN16_hread( HFILE hFile, SEGPTR buffer, LONG count )
970 dprintf_file( stddeb, "_hread16: %d %08lx %ld\n",
971 hFile, (DWORD)buffer, count );
973 /* Some programs pass a count larger than the allocated buffer */
974 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
975 if (count > maxlen) count = maxlen;
976 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
980 /***********************************************************************
983 UINT16 WIN16_lread( HFILE hFile, SEGPTR buffer, UINT16 count )
985 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
989 /***********************************************************************
990 * _lread32 (KERNEL32.596)
992 UINT32 _lread32( HFILE hFile, LPVOID buffer, UINT32 count )
997 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
998 if (!(file = FILE_GetFile( hFile ))) return -1;
999 if (!count) return 0;
1000 if ((result = read( file->unix_handle, buffer, count )) == -1)
1006 /***********************************************************************
1007 * _lread16 (KERNEL.82)
1009 UINT16 _lread16( HFILE hFile, LPVOID buffer, UINT16 count )
1011 return (UINT16)_lread32( hFile, buffer, (LONG)count );
1015 /***********************************************************************
1016 * _lcreat (KERNEL.83) (KERNEL32.593)
1018 HFILE _lcreat( LPCSTR path, INT32 attr )
1024 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1025 mode = (attr & 1) ? 0444 : 0666;
1026 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
1027 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1033 /***********************************************************************
1034 * _lcreat_uniq (Not a Windows API)
1036 HFILE _lcreat_uniq( LPCSTR path, INT32 attr )
1042 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1043 mode = (attr & 1) ? 0444 : 0666;
1044 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
1045 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1051 /***********************************************************************
1052 * _llseek (KERNEL.84) (KERNEL32.594)
1054 LONG _llseek( HFILE hFile, LONG lOffset, INT32 nOrigin )
1059 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
1060 hFile, lOffset, nOrigin);
1062 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1065 case 1: origin = SEEK_CUR; break;
1066 case 2: origin = SEEK_END; break;
1067 default: origin = SEEK_SET; break;
1070 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
1076 /***********************************************************************
1077 * _lopen (KERNEL.85) (KERNEL32.595)
1079 HFILE _lopen( LPCSTR path, INT32 mode )
1083 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
1088 unixMode = O_WRONLY | O_TRUNC;
1095 unixMode = O_RDONLY;
1098 return FILE_Open( path, unixMode );
1102 /***********************************************************************
1103 * _lwrite16 (KERNEL.86)
1105 UINT16 _lwrite16( HFILE hFile, LPCSTR buffer, UINT16 count )
1107 return (UINT16)_hwrite( hFile, buffer, (LONG)count );
1110 /***********************************************************************
1111 * _lwrite32 (KERNEL.86)
1113 UINT32 _lwrite32( HFILE hFile, LPCSTR buffer, UINT32 count )
1115 return (UINT32)_hwrite( hFile, buffer, (LONG)count );
1119 /***********************************************************************
1120 * _hread (KERNEL.349)
1122 LONG _hread( HFILE hFile, LPVOID buffer, LONG count)
1124 return _lread32( hFile, buffer, count );
1128 /***********************************************************************
1129 * _hwrite (KERNEL.350)
1131 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
1136 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
1138 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1140 if (count == 0) /* Expand or truncate at current position */
1141 result = ftruncate( file->unix_handle,
1142 lseek( file->unix_handle, 0, SEEK_CUR ) );
1144 result = write( file->unix_handle, buffer, count );
1146 if (result == -1) FILE_SetDosError();
1151 /***********************************************************************
1152 * SetHandleCount16 (KERNEL.199)
1154 UINT16 SetHandleCount16( UINT16 count )
1156 HGLOBAL16 hPDB = GetCurrentPDB();
1157 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1158 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1161 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
1163 if (count < 20) count = 20; /* No point in going below 20 */
1164 else if (count > 254) count = 254;
1166 /* If shrinking the table, make sure all extra file handles are closed */
1167 if (count < pdb->nbFiles)
1169 for (i = count; i < pdb->nbFiles; i++)
1170 if (files[i] != 0xff) /* File open */
1172 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
1173 SA_Abort, EL_Disk );
1174 return pdb->nbFiles;
1180 if (pdb->nbFiles > 20)
1182 memcpy( pdb->fileHandles, files, 20 );
1184 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1185 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1187 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1188 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1189 GlobalHandleToSel( hPDB ) );
1194 else /* More than 20, need a new file handles table */
1198 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1200 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1203 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1204 return pdb->nbFiles;
1206 newfiles = (BYTE *)GlobalLock16( newhandle );
1207 #endif /* WINELIB */
1208 if (count > pdb->nbFiles)
1210 memcpy( newfiles, files, pdb->nbFiles );
1211 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1213 else memcpy( newfiles, files, count );
1215 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1216 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1218 if (pdb->nbFiles > 20)
1219 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1220 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1221 #endif /* WINELIB */
1222 pdb->nbFiles = count;
1224 return pdb->nbFiles;
1228 /***********************************************************************
1229 * FlushFileBuffers (KERNEL32.133)
1231 BOOL32 FlushFileBuffers( HFILE hFile )
1235 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1236 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1237 if (fsync( file->unix_handle ) != -1) return TRUE;
1243 /***********************************************************************
1244 * DeleteFile16 (KERNEL.146)
1246 BOOL16 DeleteFile16( LPCSTR path )
1248 return DeleteFile32A( path );
1252 /***********************************************************************
1253 * DeleteFile32A (KERNEL32.71)
1255 BOOL32 DeleteFile32A( LPCSTR path )
1257 const char *unixName;
1259 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1261 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1263 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1264 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1268 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1269 if (unlink( unixName ) == -1)
1278 /***********************************************************************
1279 * DeleteFile32W (KERNEL32.72)
1281 BOOL32 DeleteFile32W( LPCWSTR path )
1283 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1284 BOOL32 ret = RemoveDirectory32A( xpath );
1285 HeapFree( GetProcessHeap(), 0, xpath );
1290 /***********************************************************************
1291 * CreateDirectory16 (KERNEL.144)
1293 BOOL16 CreateDirectory16( LPCSTR path, LPVOID dummy )
1295 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
1296 return (BOOL16)CreateDirectory32A( path, NULL );
1300 /***********************************************************************
1301 * CreateDirectory32A (KERNEL32.39)
1303 BOOL32 CreateDirectory32A( LPCSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1305 const char *unixName;
1307 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
1308 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1310 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
1311 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1314 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
1315 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
1324 /***********************************************************************
1325 * CreateDirectory32W (KERNEL32.42)
1327 BOOL32 CreateDirectory32W( LPCWSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1329 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1330 BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
1331 HeapFree( GetProcessHeap(), 0, xpath );
1335 /***********************************************************************
1336 * CreateDirectoryEx32A (KERNEL32.40)
1338 BOOL32 CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
1339 LPSECURITY_ATTRIBUTES lpsecattribs)
1341 return CreateDirectory32A(path,lpsecattribs);
1344 /***********************************************************************
1345 * CreateDirectoryEx32W (KERNEL32.41)
1347 BOOL32 CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
1348 LPSECURITY_ATTRIBUTES lpsecattribs)
1350 return CreateDirectory32W(path,lpsecattribs);
1353 /***********************************************************************
1354 * RemoveDirectory16 (KERNEL)
1356 BOOL16 RemoveDirectory16( LPCSTR path )
1358 return (BOOL16)RemoveDirectory32A( path );
1362 /***********************************************************************
1363 * RemoveDirectory32A (KERNEL32.437)
1365 BOOL32 RemoveDirectory32A( LPCSTR path )
1367 const char *unixName;
1369 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
1371 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1373 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
1374 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1377 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1378 if (rmdir( unixName ) == -1)
1387 /***********************************************************************
1388 * RemoveDirectory32W (KERNEL32.438)
1390 BOOL32 RemoveDirectory32W( LPCWSTR path )
1392 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1393 BOOL32 ret = RemoveDirectory32A( xpath );
1394 HeapFree( GetProcessHeap(), 0, xpath );
1399 /***********************************************************************
1402 BOOL32 FILE_SetFileType( HFILE hFile, DWORD type )
1404 DOS_FILE *file = FILE_GetFile(hFile);
1405 if (!file) return FALSE;
1411 /***********************************************************************
1412 * GetFileType (KERNEL32.222)
1414 DWORD GetFileType( HFILE hFile )
1416 DOS_FILE *file = FILE_GetFile(hFile);
1420 SetLastError( ERROR_INVALID_HANDLE );
1421 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1427 /***********************************************************************
1428 * GetFileTime (KERNEL32.221)
1430 BOOL32 GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
1431 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1433 DOS_FILE *file = FILE_GetFile(hFile);
1437 SetLastError( ERROR_INVALID_HANDLE );
1438 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1440 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1446 if (-1==fstat(file->unix_handle,&stbuf)) {
1450 if (lpLastWriteTime)
1451 DOSFS_UnixTimeToFileTime(stbuf.st_mtime,lpLastWriteTime);
1453 DOSFS_UnixTimeToFileTime(stbuf.st_ctime,lpCreationTime);
1454 if (lpLastAccessTime)
1455 DOSFS_UnixTimeToFileTime(stbuf.st_atime,lpLastAccessTime);
1460 /***********************************************************************
1461 * SetFileTime (KERNEL32.493)
1463 BOOL32 SetFileTime( HFILE hFile, FILETIME *lpCreationTime,
1464 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1466 DOS_FILE *file = FILE_GetFile(hFile);
1467 struct utimbuf utimbuf;
1470 SetLastError( ERROR_INVALID_HANDLE );
1471 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1473 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1479 if (lpLastAccessTime)
1480 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime);
1482 utimbuf.actime = 0; /* FIXME */
1483 if (lpLastWriteTime)
1484 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime);
1486 utimbuf.modtime = 0; /* FIXME */
1487 if (-1==utime(file->unix_name,&utimbuf)) {