2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
14 #include <sys/errno.h>
21 #include "directory.h"
34 #define MAX_OPEN_FILES 64 /* Max. open files for all tasks; must be <255 */
36 typedef struct tagDOS_FILE
38 struct tagDOS_FILE *next;
39 int count; /* Usage count (0 if free) */
47 /* Global files array */
48 static DOS_FILE DOSFiles[MAX_OPEN_FILES];
50 static DOS_FILE *FILE_First = DOSFiles;
51 static DOS_FILE *FILE_LastUsed = DOSFiles;
53 /* Small file handles array for boot-up, before the first PDB is created */
54 #define MAX_BOOT_HANDLES 4
55 static BYTE bootFileHandles[MAX_BOOT_HANDLES] = { 0xff, 0xff, 0xff, 0xff };
57 /***********************************************************************
60 * Allocate a DOS file.
62 static DOS_FILE *FILE_Alloc(void)
64 DOS_FILE *file = FILE_First;
65 if (file) FILE_First = file->next;
66 else if (FILE_LastUsed >= &DOSFiles[MAX_OPEN_FILES-1])
68 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
71 else file = ++FILE_LastUsed;
73 file->unix_handle = -1;
74 file->unix_name = NULL;
79 /***********************************************************************
84 static BOOL FILE_Close( DOS_FILE *file )
86 if (!file->count) return FALSE;
87 if (--file->count > 0) return TRUE;
88 /* Now really close the file */
89 if (file->unix_handle != -1) close( file->unix_handle );
90 if (file->unix_name) free( file->unix_name );
91 file->next = FILE_First;
97 /***********************************************************************
100 * Return a pointer to the current PDB files array.
102 static void FILE_GetPDBFiles( BYTE **files, WORD *nbFiles )
106 if ((pdb = (PDB *)GlobalLock16( GetCurrentPDB() )) != NULL)
108 *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
109 *nbFiles = pdb->nbFiles;
113 *files = bootFileHandles;
114 *nbFiles = MAX_BOOT_HANDLES;
119 /***********************************************************************
120 * FILE_AllocTaskHandle
122 * Allocate a task file handle for a DOS file.
124 static HFILE FILE_AllocTaskHandle( DOS_FILE *dos_file )
129 FILE_GetPDBFiles( &files, &nbFiles );
130 fp = files + 1; /* Don't use handle 0, as some programs don't like it */
131 for (i = nbFiles - 1; (i > 0) && (*fp != 0xff); i--, fp++);
133 { /* No more handles or files */
134 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
137 *fp = dos_file ? (BYTE)(dos_file - DOSFiles) : 0;
139 "FILE_AllocTaskHandle: returning task handle %d, dos_file %d, file %d of %d \n",
140 (fp - files), *fp, nbFiles - i, nbFiles );
141 return (HFILE)(fp - files);
145 /***********************************************************************
146 * FILE_FreeTaskHandle
148 * Free a per-task file handle.
150 static void FILE_FreeTaskHandle( HFILE handle )
155 FILE_GetPDBFiles( &files, &nbFiles );
156 dprintf_file( stddeb,"FILE_FreeTaskHandle: dos=%d file=%d\n",
157 handle, files[handle] );
158 if ((handle < 0) || (handle >= (INT)nbFiles))
160 fprintf( stderr, "FILE_FreeTaskHandle: invalid file handle %d\n",
164 files[handle] = 0xff;
168 /***********************************************************************
171 * Set the value of a task handle (no error checking).
173 static void FILE_SetTaskHandle( HFILE handle, DOS_FILE *file )
178 FILE_GetPDBFiles( &files, &nbFiles );
179 files[handle] = (BYTE)(file - DOSFiles);
183 /***********************************************************************
186 * Return the DOS file associated to a task file handle.
188 static DOS_FILE *FILE_GetFile( HFILE handle )
194 FILE_GetPDBFiles( &files, &nbFiles );
195 if ((handle < 0) || (handle >= (INT)nbFiles) ||
196 (files[handle] >= MAX_OPEN_FILES) ||
197 !(file = &DOSFiles[files[handle]])->count)
199 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
206 int FILE_GetUnixHandle( HFILE hFile )
210 if (!(file = FILE_GetFile( hFile ))) return -1;
211 return file->unix_handle;
215 /***********************************************************************
218 * Close all open files of a given PDB. Used on task termination.
220 void FILE_CloseAllFiles( HANDLE hPDB )
224 PDB *pdb = (PDB *)GlobalLock16( hPDB );
227 files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
228 dprintf_file(stddeb,"FILE_CloseAllFiles: closing %d files\n",pdb->nbFiles);
229 for (count = pdb->nbFiles; count > 0; count--, files++)
231 if (*files < MAX_OPEN_FILES) FILE_Close( &DOSFiles[*files] );
237 /***********************************************************************
240 * Set the DOS error code from errno.
242 void FILE_SetDosError(void)
244 dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
248 DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
251 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
254 DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
259 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
262 DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
265 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
268 DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
272 DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
275 DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
278 perror( "int21: unknown errno" );
279 DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
285 /***********************************************************************
288 * Duplicate a Unix handle into a task handle.
290 HFILE FILE_DupUnixHandle( int fd )
295 if (!(file = FILE_Alloc())) return HFILE_ERROR;
296 if ((file->unix_handle = dup(fd)) == -1)
302 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
308 /***********************************************************************
311 static DOS_FILE *FILE_OpenUnixFile( const char *name, int mode )
316 if (!(file = FILE_Alloc())) return NULL;
317 if ((file->unix_handle = open( name, mode )) == -1)
319 if (Options.allowReadOnly && (mode == O_RDWR))
321 if ((file->unix_handle = open( name, O_RDONLY )) != -1)
322 fprintf( stderr, "Warning: could not open %s for writing, opening read-only.\n", name );
325 if ((file->unix_handle == -1) || (fstat( file->unix_handle, &st ) == -1))
331 if (S_ISDIR(st.st_mode))
333 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
338 /* File opened OK, now fill the DOS_FILE */
340 file->unix_name = xstrdup( name );
341 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
346 /***********************************************************************
349 static DOS_FILE *FILE_Open( LPCSTR path, int mode )
351 const char *unixName;
353 dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
354 if ((unixName = DOSFS_IsDevice( path )) != NULL)
356 dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
357 if (!unixName[0]) /* Non-existing device */
359 dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
360 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
364 else if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return NULL;
365 return FILE_OpenUnixFile( unixName, mode );
369 /***********************************************************************
372 static DOS_FILE *FILE_Create( LPCSTR path, int mode, int unique )
375 const char *unixName;
377 dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
379 if ((unixName = DOSFS_IsDevice( path )) != NULL)
381 dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
382 DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
386 if (!(file = FILE_Alloc())) return NULL;
388 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE )))
393 if ((file->unix_handle = open( unixName,
394 O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
402 /* File created OK, now fill the DOS_FILE */
404 file->unix_name = xstrdup( unixName );
405 DOSFS_ToDosDateTime( time(NULL), &file->filedate, &file->filetime );
410 /***********************************************************************
413 * Stat a Unix path name. Return 1 if OK.
415 int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
416 WORD *pdate, WORD *ptime )
420 if (stat( unixName, &st ) == -1)
425 if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
426 if (psize) *psize = S_ISDIR(st.st_mode) ? 0 : st.st_size;
427 DOSFS_ToDosDateTime( st.st_mtime, pdate, ptime );
432 /***********************************************************************
435 * Get the date and time of a file.
437 int FILE_GetDateTime( HFILE hFile, WORD *pdate, WORD *ptime, BOOL refresh )
441 if (!(file = FILE_GetFile( hFile ))) return 0;
445 if (fstat( file->unix_handle, &st ) == -1)
450 DOSFS_ToDosDateTime( st.st_mtime, &file->filedate, &file->filetime );
452 *pdate = file->filedate;
453 *ptime = file->filetime;
458 /***********************************************************************
461 * Set the date and time of a file.
463 int FILE_SetDateTime( HFILE hFile, WORD date, WORD time )
467 struct utimbuf filetime;
469 if (!(file = FILE_GetFile( hFile ))) return 0;
470 newtm.tm_sec = (time & 0x1f) * 2;
471 newtm.tm_min = (time >> 5) & 0x3f;
472 newtm.tm_hour = (time >> 11);
473 newtm.tm_mday = (date & 0x1f);
474 newtm.tm_mon = ((date >> 5) & 0x0f) - 1;
475 newtm.tm_year = (date >> 9) + 80;
477 filetime.actime = filetime.modtime = mktime( &newtm );
478 if (utime( file->unix_name, &filetime ) != -1) return 1;
484 /***********************************************************************
487 * dup() function for DOS handles.
489 HFILE FILE_Dup( HFILE hFile )
494 dprintf_file( stddeb, "FILE_Dup for handle %d\n", hFile );
495 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
496 if ((handle = FILE_AllocTaskHandle( file )) != HFILE_ERROR) file->count++;
497 dprintf_file( stddeb, "FILE_Dup return handle %d\n", handle );
502 /***********************************************************************
505 * dup2() function for DOS handles.
507 HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
510 PDB *pdb = (PDB *)GlobalLock16( GetCurrentPDB() );
511 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
513 dprintf_file( stddeb, "FILE_Dup2 for handle %d\n", hFile1 );
514 if (!(file = FILE_GetFile( hFile1 ))) return HFILE_ERROR;
516 if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
518 DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
521 if (files[hFile2] < MAX_OPEN_FILES)
523 dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
525 FILE_Close( &DOSFiles[files[hFile2]] );
527 files[hFile2] = (BYTE)(file - DOSFiles);
529 dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n", hFile2 );
534 /***********************************************************************
537 INT32 FILE_Read( HFILE hFile, LPVOID buffer, UINT32 count )
542 dprintf_file( stddeb, "FILE_Read: %d %p %d\n", hFile, buffer, count );
543 if (!(file = FILE_GetFile( hFile ))) return -1;
544 if (!count) return 0;
545 if ((result = read( file->unix_handle, buffer, count )) == -1)
551 /***********************************************************************
552 * GetTempFileName16 (KERNEL.97)
554 UINT16 GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
559 if ((drive & TF_FORCEDRIVE) &&
560 !DRIVE_IsValid( toupper(drive & ~TF_FORCEDRIVE) - 'A' ))
562 drive &= ~TF_FORCEDRIVE;
563 fprintf( stderr, "Warning: GetTempFileName: invalid drive %d specified\n",
567 if (drive & TF_FORCEDRIVE)
568 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
571 GetTempPath32A( 132, temppath );
572 strcat( temppath, "\\" );
574 return (UINT16)GetTempFileName32A( temppath, prefix, unique, buffer );
578 /***********************************************************************
579 * GetTempFileName32A (KERNEL32.290)
581 UINT32 GetTempFileName32A( LPCSTR path, LPCSTR prefix, UINT32 unique,
586 UINT32 num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
589 strcpy( buffer, path );
590 p = buffer + strlen(buffer);
592 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
593 sprintf( p, "%04x.tmp", num );
597 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
598 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
602 /* Now try to create it */
607 if ((file = FILE_Create( buffer, 0666, TRUE )) != NULL)
608 { /* We created it */
609 dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
613 if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
615 sprintf( p, "%04x.tmp", num );
616 } while (num != (unique & 0xffff));
618 lstrcpyn32A( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
619 dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
624 /***********************************************************************
625 * GetTempFileName32W (KERNEL32.291)
627 UINT32 GetTempFileName32W( LPCWSTR path, LPCWSTR prefix, UINT32 unique,
635 patha = STRING32_DupUniToAnsi(path);
636 prefixa = STRING32_DupUniToAnsi(prefix);
637 ret = GetTempFileName32A( patha, prefixa, unique, buffera );
638 STRING32_AnsiToUni( buffer, buffera );
645 /***********************************************************************
646 * OpenFile (KERNEL.74) (KERNEL32.396)
648 HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT32 mode )
652 WORD filedatetime[2];
653 const char *unixName, *dosName;
655 int len, i, unixMode;
657 ofs->cBytes = sizeof(OFSTRUCT);
659 if (mode & OF_REOPEN) name = ofs->szPathName;
660 dprintf_file( stddeb, "OpenFile: %s %04x\n", name, mode );
662 /* First allocate a task handle */
664 if ((hFileRet = FILE_AllocTaskHandle( NULL )) == HFILE_ERROR)
666 ofs->nErrCode = DOS_ExtendedError;
667 dprintf_file( stddeb, "OpenFile: no more task handles.\n" );
671 /* OF_PARSE simply fills the structure */
675 if (!(dosName = DOSFS_GetDosTrueName( name, FALSE ))) goto error;
676 lstrcpyn32A( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
677 ofs->fFixedDisk = (GetDriveType16( dosName[0]-'A' ) != DRIVE_REMOVABLE);
678 dprintf_file( stddeb, "OpenFile(%s): OF_PARSE, res = '%s', %d\n",
679 name, ofs->szPathName, hFileRet );
680 /* Return the handle, but close it first */
681 FILE_FreeTaskHandle( hFileRet );
682 /* return hFileRet; */
683 return 0; /* Progman seems to like this better */
686 /* OF_CREATE is completely different from all other options, so
689 if (mode & OF_CREATE)
691 if (!(file = FILE_Create( name, 0666, FALSE ))) goto error;
692 lstrcpyn32A( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
693 sizeof(ofs->szPathName) );
697 /* Now look for the file */
699 /* First try the current directory */
701 lstrcpyn32A( ofs->szPathName, name, sizeof(ofs->szPathName) );
702 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
705 /* Now try some different paths if none was specified */
707 if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
709 if (name[1] == ':') name += 2;
710 if ((p = strrchr( name, '\\' ))) name = p + 1;
711 if ((p = strrchr( name, '/' ))) name = p + 1;
712 if (!name[0]) goto not_found;
716 if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
720 if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
722 /* Try the Windows directory */
724 GetWindowsDirectory( ofs->szPathName, len );
725 strcat( ofs->szPathName, "\\" );
726 strcat( ofs->szPathName, name );
727 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
730 /* Try the Windows system directory */
732 GetSystemDirectory32A( ofs->szPathName, len );
733 strcat( ofs->szPathName, "\\" );
734 strcat( ofs->szPathName, name );
735 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
738 /* Try the path of the current executable */
740 if (GetCurrentTask())
742 GetModuleFileName( GetCurrentTask(), ofs->szPathName, len );
743 if ((p = strrchr( ofs->szPathName, '\\' )))
745 strcpy( p + 1, name );
746 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
751 /* Try all directories in path */
755 if (!DIR_GetDosPath( i, ofs->szPathName, len )) goto not_found;
756 strcat( ofs->szPathName, "\\" );
757 strcat( ofs->szPathName, name );
758 if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
763 dprintf_file( stddeb, "OpenFile: found '%s'\n", unixName );
764 lstrcpyn32A(ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE),
765 sizeof(ofs->szPathName) );
767 if (mode & OF_DELETE)
769 if (unlink( unixName ) == -1) goto not_found;
770 dprintf_file( stddeb, "OpenFile(%s): OF_DELETE return = OK\n", name);
771 /* Return the handle, but close it first */
772 FILE_FreeTaskHandle( hFileRet );
779 unixMode = O_WRONLY; break;
781 unixMode = O_RDWR; break;
784 unixMode = O_RDONLY; break;
787 if (!(file = FILE_OpenUnixFile( unixName, unixMode ))) goto not_found;
788 filedatetime[0] = file->filedate;
789 filedatetime[1] = file->filetime;
790 if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
792 if (memcmp( ofs->reserved, filedatetime, sizeof(ofs->reserved) ))
795 dprintf_file( stddeb, "OpenFile(%s): OF_VERIFY failed\n", name );
796 /* FIXME: what error here? */
797 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
801 memcpy( ofs->reserved, filedatetime, sizeof(ofs->reserved) );
806 /* Return the handle, but close it first */
807 FILE_FreeTaskHandle( hFileRet );
811 success: /* We get here if the open was successful */
812 dprintf_file( stddeb, "OpenFile(%s): OK, return = %d\n", name, hFileRet );
813 FILE_SetTaskHandle( hFileRet, file );
816 not_found: /* We get here if the file does not exist */
817 dprintf_file( stddeb, "OpenFile: '%s' not found\n", name );
818 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
821 error: /* We get here if there was an error opening the file */
822 ofs->nErrCode = DOS_ExtendedError;
823 dprintf_file( stddeb, "OpenFile(%s): return = HFILE_ERROR\n", name );
824 FILE_FreeTaskHandle( hFileRet );
828 /***********************************************************************
829 * SearchPath32A (KERNEL32.447)
830 * Code borrowed from OpenFile above.
833 LPCSTR path,LPCSTR fn,LPCSTR ext,DWORD buflen,LPSTR buf,LPSTR *lastpart
837 char testpath[1000]; /* should be enough for now */
843 name=(char*)xmalloc(strlen(fn)+strlen(ext)+1);
847 dprintf_file(stddeb,"SearchPath32A(%s,%s,%s,%ld,%p,%p)\n",
848 path,fn,ext,buflen,buf,lastpart
851 strcpy(testpath,path);
852 strcat(testpath,"\\");
853 strcat(testpath,name);
854 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL) {
859 if ((len=sizeof(testpath)-strlen(name)-1)<0)
862 /* Try the path of the current executable */
863 if (GetCurrentTask()) {
864 GetModuleFileName(GetCurrentTask(),testpath,len);
865 if ((p=strrchr(testpath,'\\'))) {
867 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE)))
872 /* Try the current directory */
873 lstrcpyn32A(testpath,name,sizeof(testpath) );
874 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
877 /* Try the Windows directory */
878 GetWindowsDirectory(testpath,len);
879 strcat(testpath,"\\");
880 strcat(testpath,name);
881 if ((unixName = DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
884 /* Try the Windows system directory */
885 GetSystemDirectory32A(testpath,len);
886 strcat(testpath,"\\");
887 strcat(testpath,name);
888 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
891 /* Try all directories in path */
895 if (!DIR_GetDosPath(i,testpath,len))
897 strcat(testpath,"\\");
898 strcat(testpath,name);
899 if ((unixName=DOSFS_GetUnixFileName((LPCSTR)testpath,TRUE))!=NULL)
904 strncpy(buf,testpath,buflen);
905 if (NULL!=(p=strrchr(testpath,'\\')))
909 if (p-testpath<buflen)
910 *lastpart=(p-testpath)+buf;
913 dprintf_file(stddeb," -> found %s,last part is %s\n",testpath,p);
914 return strlen(testpath);
917 /***********************************************************************
918 * SearchPath32W (KERNEL32.448)
921 LPCWSTR path,LPCWSTR fn,LPCWSTR ext,DWORD buflen,LPWSTR buf,
924 LPSTR pathA = path?STRING32_DupUniToAnsi(path):NULL;
925 LPSTR fnA = STRING32_DupUniToAnsi(fn);
926 LPSTR extA = ext?STRING32_DupUniToAnsi(fn):NULL;
928 LPSTR bufA = (char*)xmalloc(buflen+1);
931 ret=SearchPath32A(pathA,fnA,extA,buflen,bufA,&lastpartA);
932 lstrcpynAtoW(buf,bufA,buflen);
934 *lastpart = buf+(lastpartA-bufA);
939 if (pathA) free(pathA);
940 if (extA) free(extA);
944 /***********************************************************************
945 * _lclose (KERNEL.81) (KERNEL32.592)
947 HFILE _lclose( HFILE hFile )
951 dprintf_file( stddeb, "_lclose: handle %d\n", hFile );
952 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
954 FILE_FreeTaskHandle( hFile );
959 /***********************************************************************
962 INT _lread( HFILE hFile, SEGPTR buffer, WORD count )
964 return (INT)_hread( hFile, buffer, (LONG)count );
968 /***********************************************************************
969 * _lcreat (KERNEL.83) (KERNEL32.593)
971 HFILE _lcreat( LPCSTR path, INT32 attr )
977 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
978 mode = (attr & 1) ? 0444 : 0666;
979 if (!(file = FILE_Create( path, mode, FALSE ))) return HFILE_ERROR;
980 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
986 /***********************************************************************
987 * _lcreat_uniq (Not a Windows API)
989 HFILE _lcreat_uniq( LPCSTR path, INT32 attr )
995 dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
996 mode = (attr & 1) ? 0444 : 0666;
997 if (!(file = FILE_Create( path, mode, TRUE ))) return HFILE_ERROR;
998 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1004 /***********************************************************************
1005 * _llseek (KERNEL.84)
1007 LONG _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
1012 dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
1013 hFile, lOffset, nOrigin);
1015 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1018 case 1: origin = SEEK_CUR; break;
1019 case 2: origin = SEEK_END; break;
1020 default: origin = SEEK_SET; break;
1023 if ((result = lseek( file->unix_handle, lOffset, origin )) == -1)
1029 /***********************************************************************
1030 * _lopen (KERNEL.85) (KERNEL32.595)
1032 HFILE _lopen( LPCSTR path, INT32 mode )
1038 dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
1043 unixMode = O_WRONLY | O_TRUNC;
1050 unixMode = O_RDONLY;
1053 if (!(file = FILE_Open( path, unixMode ))) return HFILE_ERROR;
1054 if ((handle = FILE_AllocTaskHandle( file )) == HFILE_ERROR)
1060 /***********************************************************************
1061 * _lwrite (KERNEL.86)
1063 INT _lwrite( HFILE hFile, LPCSTR buffer, WORD count )
1065 return (INT)_hwrite( hFile, buffer, (LONG)count );
1069 /***********************************************************************
1070 * _hread (KERNEL.349)
1072 LONG _hread( HFILE hFile, SEGPTR buffer, LONG count )
1077 dprintf_file( stddeb, "_hread: %d %08lx %ld\n",
1078 hFile, (DWORD)buffer, count );
1080 /* Some programs pass a count larger than the allocated buffer */
1081 maxlen = GetSelectorLimit( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
1082 if (count > maxlen) count = maxlen;
1084 return FILE_Read( hFile, PTR_SEG_TO_LIN(buffer), count );
1088 /***********************************************************************
1089 * _hwrite (KERNEL.350)
1091 LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
1096 dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
1098 if (!(file = FILE_GetFile( hFile ))) return HFILE_ERROR;
1100 if (count == 0) /* Expand or truncate at current position */
1101 result = ftruncate( file->unix_handle,
1102 lseek( file->unix_handle, 0, SEEK_CUR ) );
1104 result = write( file->unix_handle, buffer, count );
1106 if (result == -1) FILE_SetDosError();
1111 /***********************************************************************
1112 * SetHandleCount (KERNEL.199)
1114 WORD SetHandleCount( WORD count )
1116 HANDLE hPDB = GetCurrentPDB();
1117 PDB *pdb = (PDB *)GlobalLock16( hPDB );
1118 BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
1121 dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
1123 if (count < 20) count = 20; /* No point in going below 20 */
1124 else if (count > 254) count = 254;
1126 /* If shrinking the table, make sure all extra file handles are closed */
1127 if (count < pdb->nbFiles)
1129 for (i = count; i < pdb->nbFiles; i++)
1130 if (files[i] != 0xff) /* File open */
1132 DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
1133 SA_Abort, EL_Disk );
1134 return pdb->nbFiles;
1140 if (pdb->nbFiles > 20)
1142 memcpy( pdb->fileHandles, files, 20 );
1144 GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1145 pdb->fileHandlesPtr = (SEGPTR)pdb->fileHandles;
1147 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1148 pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
1149 GlobalHandleToSel( hPDB ) );
1154 else /* More than 20, need a new file handles table */
1158 newfiles = (BYTE *)GlobalAlloc32( GMEM_FIXED, count );
1160 HANDLE newhandle = GlobalAlloc16( GMEM_MOVEABLE, count );
1163 DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
1164 return pdb->nbFiles;
1166 newfiles = (BYTE *)GlobalLock16( newhandle );
1167 #endif /* WINELIB */
1168 if (count > pdb->nbFiles)
1170 memcpy( newfiles, files, pdb->nbFiles );
1171 memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
1173 else memcpy( newfiles, files, count );
1175 if (pdb->nbFiles > 20) GlobalFree32( (HGLOBAL32)pdb->fileHandlesPtr );
1176 pdb->fileHandlesPtr = (SEGPTR)newfiles;
1178 if (pdb->nbFiles > 20)
1179 GlobalFree16( GlobalHandle16( SELECTOROF(pdb->fileHandlesPtr) ));
1180 pdb->fileHandlesPtr = WIN16_GlobalLock16( newhandle );
1181 #endif /* WINELIB */
1182 pdb->nbFiles = count;
1184 return pdb->nbFiles;
1188 /***********************************************************************
1189 * FlushFileBuffers (KERNEL32.133)
1191 BOOL32 FlushFileBuffers( HFILE hFile )
1195 dprintf_file( stddeb, "FlushFileBuffers(%d)\n", hFile );
1196 if (!(file = FILE_GetFile( hFile ))) return FALSE;
1197 if (fsync( file->unix_handle ) != -1) return TRUE;
1203 /***********************************************************************
1204 * DeleteFile16 (KERNEL.146)
1206 BOOL16 DeleteFile16( LPCSTR path )
1208 return DeleteFile32A( path );
1212 /***********************************************************************
1213 * DeleteFile32A (KERNEL32.71)
1215 BOOL32 DeleteFile32A( LPCSTR path )
1217 const char *unixName;
1219 dprintf_file(stddeb, "DeleteFile: '%s'\n", path );
1221 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1223 dprintf_file(stddeb, "DeleteFile: removing device '%s'!\n", unixName);
1224 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1228 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1229 if (unlink( unixName ) == -1)
1238 /***********************************************************************
1239 * DeleteFile32W (KERNEL32.72)
1241 BOOL32 DeleteFile32W( LPCWSTR path )
1243 LPSTR xpath = STRING32_DupUniToAnsi(path);
1244 BOOL32 ret = RemoveDirectory32A( xpath );
1250 /***********************************************************************
1251 * CreateDirectory16 (KERNEL.144)
1253 BOOL16 CreateDirectory16( LPCSTR path, LPVOID dummy )
1255 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
1256 return (BOOL16)CreateDirectory32A( path, NULL );
1260 /***********************************************************************
1261 * CreateDirectory32A (KERNEL32.39)
1263 BOOL32 CreateDirectory32A( LPCSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1265 const char *unixName;
1267 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
1268 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1270 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
1271 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
1274 if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
1275 if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
1284 /***********************************************************************
1285 * CreateDirectory32W (KERNEL32.42)
1287 BOOL32 CreateDirectory32W( LPCWSTR path, LPSECURITY_ATTRIBUTES lpsecattribs )
1289 LPSTR xpath = STRING32_DupUniToAnsi(path);
1290 BOOL32 ret = CreateDirectory32A(xpath,lpsecattribs);
1296 /***********************************************************************
1297 * RemoveDirectory16 (KERNEL)
1299 BOOL16 RemoveDirectory16( LPCSTR path )
1301 return (BOOL16)RemoveDirectory32A( path );
1305 /***********************************************************************
1306 * RemoveDirectory32A (KERNEL32.437)
1308 BOOL32 RemoveDirectory32A( LPCSTR path )
1310 const char *unixName;
1312 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
1314 if ((unixName = DOSFS_IsDevice( path )) != NULL)
1316 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
1317 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
1320 if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return FALSE;
1321 if (rmdir( unixName ) == -1)
1330 /***********************************************************************
1331 * RemoveDirectory32W (KERNEL32.438)
1333 BOOL32 RemoveDirectory32W( LPCWSTR path )
1335 LPSTR xpath = STRING32_DupUniToAnsi(path);
1336 BOOL32 ret = RemoveDirectory32A( xpath );