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 = STRING32_DupUniToAnsi(path);
634 prefixa = STRING32_DupUniToAnsi(prefix);
635 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
636 STRING32_AnsiToUni( buffer, buffera );
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)
925 LPCWSTR path,LPCWSTR fn,LPCWSTR ext,DWORD buflen,LPWSTR buf,
928 LPSTR pathA = path?STRING32_DupUniToAnsi(path):NULL;
929 LPSTR fnA = STRING32_DupUniToAnsi(fn);
930 LPSTR extA = ext?STRING32_DupUniToAnsi(fn):NULL;
932 LPSTR bufA = (char*)xmalloc(buflen+1);
935 ret=SearchPath32A(pathA,fnA,extA,buflen,bufA,&lastpartA);
936 lstrcpynAtoW(buf,bufA,buflen);
939 *lastpart = buf+(lastpartA-bufA);
945 if (pathA) free(pathA);
946 if (extA) free(extA);
950 /***********************************************************************
951 * _lclose (KERNEL.81) (KERNEL32.592)
953 HFILE _lclose( HFILE hFile )
957 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
958 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
960 FILE_FreeTaskHandle( hFile );
965 /***********************************************************************
968 LONG WIN16_hread( HFILE hFile, SEGPTR buffer, LONG count )
972 dprintf_file( stddeb, "_hread16: %d %08lx %ld\n",
973 hFile, (DWORD)buffer, count );
975 /* Some programs pass a count larger than the allocated buffer */
976 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
977 if (count > maxlen) count = maxlen;
978 return _lread32( hFile, PTR_SEG_TO_LIN(buffer), count );
982 /***********************************************************************
985 UINT16 WIN16_lread( HFILE hFile, SEGPTR buffer, UINT16 count )
987 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
991 /***********************************************************************
992 * _lread32 (KERNEL32.596)
994 UINT32 _lread32( HFILE hFile, LPVOID buffer, UINT32 count )
999 dprintf_file( stddeb, "_lread32: %d %p %d\n", hFile, buffer, count );
1000 if (!(file = FILE_GetFile( hFile ))) return -1;
1001 if (!count) return 0;
1002 if ((result = read( file->unix_handle, buffer, count )) == -1)
1008 /***********************************************************************
1009 * _lread16 (KERNEL.82)
1011 UINT16 _lread16( HFILE hFile, LPVOID buffer, UINT16 count )
1013 return (UINT16)_lread32( hFile, buffer, (LONG)count );
1017 /***********************************************************************
1018 * _lcreat (KERNEL.83) (KERNEL32.593)
1020 HFILE _lcreat( LPCSTR path, INT32 attr )
1026 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1027 mode = (attr & 1) ? 0444 : 0666;
1028 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
1029 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1035 /***********************************************************************
1036 * _lcreat_uniq (Not a Windows API)
1038 HFILE _lcreat_uniq( LPCSTR path, INT32 attr )
1044 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
1045 mode = (attr & 1) ? 0444 : 0666;
1046 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
1047 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1053 /***********************************************************************
1054 * _llseek (KERNEL.84) (KERNEL32.594)
1056 LONG _llseek( HFILE hFile, LONG lOffset, INT32 nOrigin )
1061 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
1062 hFile, lOffset, nOrigin);
1064 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1067 case 1: origin = SEEK_CUR; break;
1068 case 2: origin = SEEK_END; break;
1069 default: origin = SEEK_SET; break;
1072 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
1078 /***********************************************************************
1079 * _lopen (KERNEL.85) (KERNEL32.595)
1081 HFILE _lopen( LPCSTR path, INT32 mode )
1085 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
1090 unixMode = O_WRONLY | O_TRUNC;
1097 unixMode = O_RDONLY;
1100 return FILE_Open( path, unixMode );
1104 /***********************************************************************
1105 * _lwrite16 (KERNEL.86)
1107 UINT16 _lwrite16( HFILE hFile, LPCSTR buffer, UINT16 count )
1109 return (UINT16)_hwrite( hFile, buffer, (LONG)count );
1112 /***********************************************************************
1113 * _lwrite32 (KERNEL.86)
1115 UINT32 _lwrite32( HFILE hFile, LPCSTR buffer, UINT32 count )
1117 return (UINT32)_hwrite( hFile, buffer, (LONG)count );
1121 /***********************************************************************
1122 * _hread (KERNEL.349)
1124 LONG _hread( HFILE hFile, LPVOID buffer, LONG count)
1126 return _lread32( hFile, buffer, count );
1130 /***********************************************************************
1131 * _hwrite (KERNEL.350)
1133 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
1138 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
1140 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1142 if (count == 0) /* Expand or truncate at current position */
1143 result = ftruncate( file->unix_handle,
1144 lseek( file->unix_handle, 0, SEEK_CUR ) );
1146 result = write( file->unix_handle, buffer, count );
1148 if (result == -1) FILE_SetDosError();
1153 /***********************************************************************
1154 * SetHandleCount16 (KERNEL.199)
1156 UINT16 SetHandleCount16( UINT16 count )
1158 HGLOBAL16 hPDB = GetCurrentPDB();
1159 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1160 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1163 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
1165 if (count < 20) count = 20; /* No point in going below 20 */
1166 else if (count > 254) count = 254;
1168 /* If shrinking the table, make sure all extra file handles are closed */
1169 if (count < pdb->nbFiles)
1171 for (i = count; i < pdb->nbFiles; i++)
1172 if (files[i] != 0xff) /* File open */
1174 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
1175 SA_Abort, EL_Disk );
1176 return pdb->nbFiles;
1182 if (pdb->nbFiles > 20)
1184 memcpy( pdb->fileHandles, files, 20 );
1186 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1187 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1189 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1190 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1191 GlobalHandleToSel( hPDB ) );
1196 else /* More than 20, need a new file handles table */
1200 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1202 HGLOBAL16 newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1205 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1206 return pdb->nbFiles;
1208 newfiles = (BYTE *)GlobalLock16( newhandle );
1209 #endif /* WINELIB */
1210 if (count > pdb->nbFiles)
1212 memcpy( newfiles, files, pdb->nbFiles );
1213 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1215 else memcpy( newfiles, files, count );
1217 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1218 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1220 if (pdb->nbFiles > 20)
1221 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1222 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1223 #endif /* WINELIB */
1224 pdb->nbFiles = count;
1226 return pdb->nbFiles;
1230 /***********************************************************************
1231 * FlushFileBuffers (KERNEL32.133)
1233 BOOL32 FlushFileBuffers( HFILE hFile )
1237 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1238 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1239 if (fsync( file->unix_handle ) != -1) return TRUE;
1245 /***********************************************************************
1246 * DeleteFile16 (KERNEL.146)
1248 BOOL16 DeleteFile16( LPCSTR path )
1250 return DeleteFile32A( path );
1254 /***********************************************************************
1255 * DeleteFile32A (KERNEL32.71)
1257 BOOL32 DeleteFile32A( LPCSTR path )
1259 const char *unixName;
1261 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1263 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1265 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1266 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1270 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1271 if (unlink( unixName ) == -1)
1280 /***********************************************************************
1281 * DeleteFile32W (KERNEL32.72)
1283 BOOL32 DeleteFile32W( LPCWSTR path )
1285 LPSTR xpath = STRING32_DupUniToAnsi(path);
1286 BOOL32 ret = RemoveDirectory32A( xpath );
1292 /***********************************************************************
1293 * CreateDirectory16 (KERNEL.144)
1295 BOOL16 CreateDirectory16( LPCSTR path, LPVOID dummy )
1297 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
1298 return (BOOL16)CreateDirectory32A( path, NULL );
1302 /***********************************************************************
1303 * CreateDirectory32A (KERNEL32.39)
1305 BOOL32 CreateDirectory32A( LPCSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1307 const char *unixName;
1309 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
1310 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1312 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
1313 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1316 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
1317 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
1326 /***********************************************************************
1327 * CreateDirectory32W (KERNEL32.42)
1329 BOOL32 CreateDirectory32W( LPCWSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1331 LPSTR xpath = STRING32_DupUniToAnsi(path);
1332 BOOL32 ret = CreateDirectory32A(xpath,lpsecattribs);
1337 /***********************************************************************
1338 * CreateDirectoryEx32A (KERNEL32.40)
1340 BOOL32 CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
1341 LPSECURITY_ATTRIBUTES lpsecattribs)
1343 return CreateDirectory32A(path,lpsecattribs);
1346 /***********************************************************************
1347 * CreateDirectoryEx32W (KERNEL32.41)
1349 BOOL32 CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
1350 LPSECURITY_ATTRIBUTES lpsecattribs)
1352 return CreateDirectory32W(path,lpsecattribs);
1355 /***********************************************************************
1356 * RemoveDirectory16 (KERNEL)
1358 BOOL16 RemoveDirectory16( LPCSTR path )
1360 return (BOOL16)RemoveDirectory32A( path );
1364 /***********************************************************************
1365 * RemoveDirectory32A (KERNEL32.437)
1367 BOOL32 RemoveDirectory32A( LPCSTR path )
1369 const char *unixName;
1371 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
1373 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1375 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
1376 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1379 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1380 if (rmdir( unixName ) == -1)
1389 /***********************************************************************
1390 * RemoveDirectory32W (KERNEL32.438)
1392 BOOL32 RemoveDirectory32W( LPCWSTR path )
1394 LPSTR xpath = STRING32_DupUniToAnsi(path);
1395 BOOL32 ret = RemoveDirectory32A( xpath );
1401 /***********************************************************************
1404 BOOL32 FILE_SetFileType( HFILE hFile, DWORD type )
1406 DOS_FILE *file = FILE_GetFile(hFile);
1407 if (!file) return FALSE;
1413 /***********************************************************************
1414 * GetFileType (KERNEL32.222)
1416 DWORD GetFileType( HFILE hFile )
1418 DOS_FILE *file = FILE_GetFile(hFile);
1422 SetLastError( ERROR_INVALID_HANDLE );
1423 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1429 /***********************************************************************
1430 * GetFileTime (KERNEL32.221)
1432 BOOL32 GetFileTime( HFILE hFile, FILETIME *lpCreationTime,
1433 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1435 DOS_FILE *file = FILE_GetFile(hFile);
1439 SetLastError( ERROR_INVALID_HANDLE );
1440 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1442 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1448 if (-1==fstat(file->unix_handle,&stbuf)) {
1452 if (lpLastWriteTime)
1453 DOSFS_UnixTimeToFileTime(stbuf.st_mtime,lpLastWriteTime);
1455 DOSFS_UnixTimeToFileTime(stbuf.st_ctime,lpCreationTime);
1456 if (lpLastAccessTime)
1457 DOSFS_UnixTimeToFileTime(stbuf.st_atime,lpLastAccessTime);
1462 /***********************************************************************
1463 * SetFileTime (KERNEL32.493)
1465 BOOL32 SetFileTime( HFILE hFile, FILETIME *lpCreationTime,
1466 FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime )
1468 DOS_FILE *file = FILE_GetFile(hFile);
1469 struct utimbuf utimbuf;
1472 SetLastError( ERROR_INVALID_HANDLE );
1473 return FILE_TYPE_UNKNOWN; /* FIXME: correct? */
1475 dprintf_file(stddeb,"SetFileTime(%s,%p,%p,%p)\n",
1481 if (lpLastAccessTime)
1482 utimbuf.actime = DOSFS_FileTimeToUnixTime(lpLastAccessTime);
1484 utimbuf.actime = 0; /* FIXME */
1485 if (lpLastWriteTime)
1486 utimbuf.modtime = DOSFS_FileTimeToUnixTime(lpLastWriteTime);
1488 utimbuf.modtime = 0; /* FIXME */
1489 if (-1==utime(file->unix_name,&utimbuf)) {