1
0
Fork 0
mirror of synced 2025-03-07 03:53:26 +01:00
wine/files/file.c
Alexandre Julliard 7e56f6843b Release 960131
Wed Jan 31 10:58:00 1996  Alexandre Julliard  <julliard@sunsite.unc.edu>

	* [configure.in]
	Added --with-dll option to build libwine.so.

	* [controls/listbox.c]
	Fixed ListBoxDirectory(), DlgDirSelect() and
	DlgDirList(). Hopefully their behavior is correct now.

	* [controls/menu.c]
	Use SEGPTRs in ChangeMenu(), InsertMenu(), AppendMenu() and
 	ModifyMenu() for the item data, to avoid corrupting the pointer
 	for owner-drawn items.

	* [controls/static.c]
	Attempt to load OEM icons for SS_ICON controls. Probably not
	entirely correct.
	Don't clip the text output.

	* [files/directory.c]
	Add temp dir and Windows dir to environment.

	* [files/dos_fs.c]
	Fixed a few path handling bugs in DOSFS_GetUnixFileName().
	Cache last used directory in DOSFS_FindNext() to avoid quadratic
	search time.

	* [files/drive.c]
	New format for drives configuration in wine.conf; allows
	specifying the type, label and serial number of a drive.

	* [files/file.c]
	New function FILE_OpenUnixFile to make sure we don't open a
	directory instead of a file.
	Fixed DOSFS_GetUnixFileName() check_last flag in FILE_MakeDir().

	* [files/profile.c]
	Rewrote profile handling. Should be closer to Windows behavior now.
	New function PROFILE_GetWineIniString() to get a string from wine.conf.
	Support environment variables in wine.conf.

	* [loader/task.c]
	Fixed the order of deletion in TASK_DeleteTask() to avoid memory
	corruption.

	* [memory/global.c]
	Create a discarded block on GlobalAlloc() if the size is 0; thanks
	to John Harvey for noticing this.

	* [memory/local.c]
	LOCAL_GetHeap: make sure the pointer is valid before checking
	magic number.

	* [misc/main.c]
	Moved profile and registry saving to ExitWindows(), so we don't
	try to save them in case of a crash.

	* [miscemu/int21.c]
	INT21_GetFreeDiskSpace: try to compute the cluster size from the
	filesystem size instead of hard-coding it to 64.
	Fixed functions 0x3f and 0x40 to use _hread and _hwrite to allow
	reading or writing 65535 bytes (thanks to Bruce Milner for this	one).

	* [windows/message.c]
	Fixed bug in linked-list handling in MSG_DeleteQueue().
	Simplified SetMessageQueue().

	* [wine.ini] [wine.man]
	Updated for new drives configuration format.

Tue Jan 30 11:24:46 1996  William Magro  <wmagro@tc.cornell.edu>

	* [controls/edit.c]
	Implemented ES_PASSWORD style, EM_SETPASSWORDCHAR and
 	EM_GETPASSWORDCHAR messages.

	* [controls/widgets.c]
	Adjusted class creation flags to better match values Windows uses.

	* [include/windows.h]
	Fixed ES_NOHIDESEL typo.

	* [loader/ne_image.c]
	Added detection for zero offset in RADDR fixups. Quicken
	was in an infinite loop here.

Mon Jan 29 20:12:22 1996  Albrecht Kleine  <kleine@ak.sax.de>

	* [files/dos_fs.c]
	Bugfix: range error in month value (0..11 set to 1..12).

	* [windows/caret.c]
	Changed ROP2-mode to R2_NOTXORPEN in CARET_Callback for pulsed
 	appearance of the caret.

	* [windows/mdi.c] [include/mdi.h]
	Changed MDITile(): added a new parameter WORD wParam for
 	WM_MDITILE second tiling method (MDITILE_HORIZONTAL in wParam) as
 	used in Win3.1

Sun Jan 28 14:20:00 1996  Cameron Heide  <heide@ee.ualberta.ca>

	* [miscemu/int2f.c]
	Added a small bit of MSCDEX emulation.

	* [windows/alias.c]
	ALIAS_RegisterAlias was returning the hash value when it should
 	have been returning the record number.
	
Sat Jan 27 10:53:51 1996  Jim Peterson <jspeter@birch.ee.vt.edu>

	* [include/shell.h] [include/wintypes.h]
	Moved definition of HKEY and LPHKEY types to include/wintypes.h.
	Declared FONTENUMPROC in wintypes.h.

	* [include/windows.h]
	Added definition of KERNINGPAIR and LPKERNINGPAIR types.  Added
	declarations for CopyCursor(), CopyIcon(), EnumFontFamilies(),
	ExtractIcon(), FatalAppExit(), FindExecutable(), GetClipCursor(),
	GetKerningPairs(), GetQueueStatus(), GetRasterizerCaps(),
	IsGDIObject(), IsMenu(), IsTask(), RegCloseKey(), RegCreateKey(),
	RegDeleteKey(), RegEnumKey(), RegOpenKey(), RegQueryValue(),
	RegSetValue(), ResetDC(), ShellExecute(), SystemParametersInfo(),
	and wsprintf().

	* [tools/makehtml.pl] [documentation/apiw.index]
	New files that scan windows.h, commdlg.h, and toolhelp.h and output
	an HTML sorted list with optional links to www.willows.com and a
	tally of unimplemented APIW functions.

	* [objects/cursoricon.c]
	Added Win32 versions of CopyIcon() and CopyCursor() for use in
	libwine.

	* [win32/resource.c] [win32/winprocs.c]
	Added '#include "libres.h"' and explicit declarations of windows
	procs in order to avoid warnings.

	* [windows/utility.c]
	Added Win32 version of MulDiv() for libwine.

	* [*/*] [include/windows.h]
	Changed several function declarations to comply more strictly to
	the windows API (without, hopefully, altering their functionality).

	* [controls/menu.c]
	Made the return value of CheckMenuItem be the previous state of
 	the menu item if it was found, otherwise -1 as specified in the
 	SDK. This conflicts with the APIW specification, which says it
 	should return TRUE if successful, otherwise FALSE.

	* [include/windows.h]
	Added obsolete WM_SIZE message wParam names for compatibility.
  	Added WinHelp() command constants, even though they are not yet
 	supported.

	* [rc/winerc.c]
	Tidied up transform_binary_file().  In argument checking, flattened
	any invalid characters specified with the prefix argument.

	* [library/libres.c]
	Made FindResource() case-insensitive when parameter 'name' is a string.

Sat Jan 27 02:30 1996  Uwe Bonnes <bon@elektron.ikp.physik.th-darmstadt.de

	* [files/drive.c]
	If root "/" is given in wine.conf, use it as last resort.

	* [files/file.c]
	Report ER_AccessDenied it disk ist not writable
	More Debug Output

	* [miscemu/int21.c]
	Squeezed some bugs in ExtendedOpenCreateFile
	
	* [windows/winpos.c]
	Some windows may not be moved or resized. We are missing some
	structures to be exact, but the approach should help in some cases
	and make things worse in much fewer.

Fri Jan 26 10:24:00 1996  Thomas Sandford <t.d.g.sandford@prds-grn.demon.co.uk>

	* [loader/pe_image.c]
	fixup_imports: Find builtins for Borland style entries, too

Fri Jan 26 10:24:00 1996  Martin von Loewis <loewis@informatik.hu-berlin.de>

	* [controls/menu.c]
	LoadMenu: branch to Win32 for PE modules

	* [if1632/gdi.spec][if1632/kernel32.spec][if1632/user32.spec]
	DeleteObject, GetPixel, SetPixel,WritePrivateProfileStringA,
	WriteProfileStringA, EmptyClipboard, EnableMenuItem, EnableScrollBar,
	EnableWindow, InvalidateRect, SetWindowTextA, WinHelpA: new relays
	DrawTextA, MoveToEx, GetClientRect, InvalidateRect, LoadBitmapA/W,
	LoadAcceleratorsA/W, LoadMenu[Indirect]A/W, LoadStringA/W: changed
	to convert parameters or naming convention

	* [include/kernel32.h][include/wintypes.h]
	moved WCHAR, defined LPWSTR

	* [include/string32.h][win32/string32.c][include/struct32.h]
	New files

	* [loader/module.h]
	LoadModule: exit after returning from PE_LoadModule

	* [loader/pe_image.c]
	my_wcstombs: isascii does not work on Linux for Unicode
	PE_LoadImage: Handle directories

	* [misc/user32.c]
	USER32_RECT32to16, USER32_RECT16to32: new functions
	implemented new user32 relays

	* [misc/newfns.c]
	WIN32_WinHelpA: new function

	* [win32/param32.c]
	New file

	* [win32/resource.c]
	GetResDirEntry: added support for named entries
	WIN32_LoadAcceleratorsW: invoke *32 resource functions
	WIN32_LoadBitmapA: convert name to unicode if appropriate
	WIN32_ParseMenu: new function
	implemented new resource functions from user32.spec

Wed Jan 24 18:09:00 1996 Alex Korobka <alex@phm30.pharm.sunysb.edu>

	* [objects/cursoricon.c]
	GetIconId() and LoadIconHandler() functions.

	* [windows/mdi.c]
	Better maximization support, TranslateMDISysAccel() function, 
	misc improvements.

	* [windows/defwnd.c]
	Fix for WM_WINDOWPOSCHANGED message handler.

	* [windows/winpos.c]
	Rewrote WindowFromPoint() function.

Sun Jan 21 1996 17:05:09  Marcus Meissner <msmeissn@faui01.informatik.uni-erlangen.de>

	* [include/toolhelp.h] [misc/toolhelp.c]
	Added Notify(Un)Register, but no callbacks yet.

Fri Jan 19 01:43:37 1996 Victor Schneider <root@tailor.roman.org>

	* [Makefile.in]
	Added target for libwine.so.1.0.

	* [library/winmain.c]
	For WINELIBDLL, _WinMain just returns hInstance instead of calling
	WinMain().

	* [misc/main.c]
	For WINELIBDLL, renamed main() to _wine_main() for calling from the
	stub main function.

	* [library/winestub.c] (new file)
	Provides a stub main() function for using libwine.so.

Tue Jan 16 11:04:34 1996  Anand Kumria <akumria@ozemail.com.au>

	* [winsocket.c]
	Fix EPERM problem.

	* [global.c]
	Attempt to do some sanity checking in MemManInfo().

	* [Changelog]
	Fix changelog oversight for previous entry.
1996-01-31 19:02:28 +00:00

951 lines
26 KiB
C

/*
* File handling functions
*
* Copyright 1993 John Burton
* Copyright 1996 Alexandre Julliard
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "windows.h"
#include "directory.h"
#include "dos_fs.h"
#include "drive.h"
#include "global.h"
#include "msdos.h"
#include "options.h"
#include "ldt.h"
#include "task.h"
#include "stddebug.h"
#include "debug.h"
/***********************************************************************
* FILE_SetDosError
*
* Set the DOS error code from errno.
*/
void FILE_SetDosError(void)
{
dprintf_file(stddeb, "FILE_SetDosError: errno = %d\n", errno );
switch (errno)
{
case EAGAIN:
DOS_ERROR( ER_ShareViolation, EC_Temporary, SA_Retry, EL_Disk );
break;
case EBADF:
DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
break;
case ENOSPC:
DOS_ERROR( ER_DiskFull, EC_MediaError, SA_Abort, EL_Disk );
break;
case EACCES:
case EPERM:
case EROFS:
DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
break;
case EBUSY:
DOS_ERROR( ER_LockViolation, EC_AccessDenied, SA_Abort, EL_Disk );
break;
case ENOENT:
DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
break;
case EISDIR:
DOS_ERROR( ER_CanNotMakeDir, EC_AccessDenied, SA_Abort, EL_Unknown );
break;
case ENFILE:
case EMFILE:
DOS_ERROR( ER_NoMoreFiles, EC_MediaError, SA_Abort, EL_Unknown );
break;
case EEXIST:
DOS_ERROR( ER_FileExists, EC_Exists, SA_Abort, EL_Disk );
break;
default:
perror( "int21: unknown errno" );
DOS_ERROR( ER_GeneralFailure, EC_SystemFailure, SA_Abort, EL_Unknown );
break;
}
}
/***********************************************************************
* FILE_AllocTaskHandle
*
* Allocate a DOS file handle for the current task.
*/
static HFILE FILE_AllocTaskHandle( int handle )
{
PDB *pdb = (PDB *)GlobalLock( GetCurrentPDB() );
BYTE *files, *fp;
WORD i;
if (!pdb)
{
fprintf(stderr,"FILE_MakeTaskHandle: internal error, no current PDB.\n");
exit(1);
}
fp = files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
for (i = pdb->nbFiles; (i > 0) && (*fp != 0xff); i--, fp++);
if (!i || (handle >= 0xff)) /* No more handles */
{
DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
return -1;
}
*fp = (BYTE)handle;
dprintf_file(stddeb,
"FILE_AllocTaskHandle: returning %d for handle %d file %d of %d \n",
(fp - files),handle,pdb->nbFiles - i, pdb->nbFiles );
return (HFILE)(fp - files);
}
/***********************************************************************
* FILE_FreeTaskHandle
*
* Free a DOS file handle for the current task.
*/
static void FILE_FreeTaskHandle( HFILE handle )
{
PDB *pdb = (PDB *)GlobalLock( GetCurrentPDB() );
BYTE *files;
if (!pdb)
{
fprintf(stderr,"FILE_FreeTaskHandle: internal error, no current PDB.\n");
exit(1);
}
files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
dprintf_file( stddeb,"FILE_FreeTaskHandle: dos=%d unix=%d\n",
handle, files[handle]);
if ((handle<0) || (handle >= (INT)pdb->nbFiles) || (files[handle] == 0xff))
{
fprintf( stderr, "FILE_FreeTaskHandle: invalid file handle %d\n",
handle );
return;
}
files[handle] = 0xff;
}
/***********************************************************************
* FILE_GetUnixHandle
*
* Return the Unix file handle associated to a DOS file handle.
*/
int FILE_GetUnixHandle( HFILE handle )
{
PDB *pdb = (PDB *)GlobalLock( GetCurrentPDB() );
BYTE *files;
if (!pdb)
{
fprintf(stderr,"FILE_GetUnixHandle: internal error, no current PDB.\n");
exit(1);
}
files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
if ((handle<0) || (handle >= (INT)pdb->nbFiles) || (files[handle] == 0xff))
{
DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
return -1;
}
return (int)files[handle];
}
/***********************************************************************
* FILE_CloseAllFiles
*
* Close all open files of a given PDB. Used on task termination.
*/
void FILE_CloseAllFiles( HANDLE hPDB )
{
BYTE *files;
WORD count;
PDB *pdb = (PDB *)GlobalLock( hPDB );
if (!pdb) return;
files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
fprintf(stderr,"FILE_CloseAllFiles: closing %d files\n",pdb->nbFiles);
for (count = pdb->nbFiles; count > 0; count--, files++)
{
if (*files != 0xff)
{
close( (int)*files );
*files = 0xff;
}
}
}
/***********************************************************************
* FILE_OpenUnixFile
*/
static int FILE_OpenUnixFile( const char *name, int mode )
{
int handle;
struct stat st;
if ((handle = open( name, mode )) == -1)
{
if (Options.allowReadOnly && (mode == O_RDWR))
{
if ((handle = open( name, O_RDONLY )) != -1)
fprintf( stderr, "Warning: could not open %s for writing, opening read-only.\n", name );
}
}
if (handle != -1) /* Make sure it's not a directory */
{
if ((fstat( handle, &st ) == -1))
{
FILE_SetDosError();
close( handle );
handle = -1;
}
else if (S_ISDIR(st.st_mode))
{
DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
close( handle );
handle = -1;
}
}
return handle;
}
/***********************************************************************
* FILE_Open
*/
int FILE_Open( LPCSTR path, int mode )
{
const char *unixName;
dprintf_file(stddeb, "FILE_Open: '%s' %04x\n", path, mode );
if ((unixName = DOSFS_IsDevice( path )) != NULL)
{
dprintf_file( stddeb, "FILE_Open: opening device '%s'\n", unixName );
if (!unixName[0]) /* Non-existing device */
{
dprintf_file(stddeb, "FILE_Open: Non-existing device\n");
DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
return -1;
}
}
else if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return -1;
return FILE_OpenUnixFile( unixName, mode );
}
/***********************************************************************
* FILE_Create
*/
int FILE_Create( LPCSTR path, int mode, int unique )
{
const char *unixName;
int handle;
dprintf_file(stddeb, "FILE_Create: '%s' %04x %d\n", path, mode, unique );
if ((unixName = DOSFS_IsDevice( path )) != NULL)
{
dprintf_file(stddeb, "FILE_Create: creating device '%s'!\n", unixName);
DOS_ERROR( ER_AccessDenied, EC_NotFound, SA_Abort, EL_Disk );
return -1;
}
if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return -1;
if ((handle = open( unixName,
O_CREAT | O_TRUNC | O_RDWR | (unique ? O_EXCL : 0),
mode )) == -1)
FILE_SetDosError();
return handle;
}
/***********************************************************************
* FILE_Unlink
*/
int FILE_Unlink( LPCSTR path )
{
const char *unixName;
dprintf_file(stddeb, "FILE_Unlink: '%s'\n", path );
if ((unixName = DOSFS_IsDevice( path )) != NULL)
{
dprintf_file(stddeb, "FILE_Unlink: removing device '%s'!\n", unixName);
DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
return 0;
}
if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return 0;
if (unlink( unixName ) == -1)
{
FILE_SetDosError();
return 0;
}
return 1;
}
/***********************************************************************
* FILE_Stat
*
* Stat a Unix path name. Return 1 if OK.
*/
int FILE_Stat( LPCSTR unixName, BYTE *pattr, DWORD *psize,
WORD *pdate, WORD *ptime )
{
struct stat st;
if (stat( unixName, &st ) == -1)
{
FILE_SetDosError();
return 0;
}
if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
if (psize) *psize = st.st_size;
DOSFS_ToDosDateTime( &st.st_mtime, pdate, ptime );
return 1;
}
/***********************************************************************
* FILE_Fstat
*
* Stat a DOS handle. Return 1 if OK.
*/
int FILE_Fstat( HFILE hFile, BYTE *pattr, DWORD *psize,
WORD *pdate, WORD *ptime )
{
struct stat st;
int handle;
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return 0;
if (fstat( handle, &st ) == -1)
{
FILE_SetDosError();
return 0;
}
if (pattr) *pattr = FA_ARCHIVE | (S_ISDIR(st.st_mode) ? FA_DIRECTORY : 0);
if (psize) *psize = st.st_size;
DOSFS_ToDosDateTime( &st.st_mtime, pdate, ptime );
return 1;
}
/***********************************************************************
* FILE_MakeDir
*/
int FILE_MakeDir( LPCSTR path )
{
const char *unixName;
dprintf_file(stddeb, "FILE_MakeDir: '%s'\n", path );
if ((unixName = DOSFS_IsDevice( path )) != NULL)
{
dprintf_file(stddeb, "FILE_MakeDir: device '%s'!\n", unixName);
DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
return 0;
}
if (!(unixName = DOSFS_GetUnixFileName( path, FALSE ))) return 0;
if ((mkdir( unixName, 0777 ) == -1) && (errno != EEXIST))
{
FILE_SetDosError();
return 0;
}
return 1;
}
/***********************************************************************
* FILE_RemoveDir
*/
int FILE_RemoveDir( LPCSTR path )
{
const char *unixName;
dprintf_file(stddeb, "FILE_RemoveDir: '%s'\n", path );
if ((unixName = DOSFS_IsDevice( path )) != NULL)
{
dprintf_file(stddeb, "FILE_RemoveDir: device '%s'!\n", unixName);
DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
return 0;
}
if (!(unixName = DOSFS_GetUnixFileName( path, TRUE ))) return 0;
if (rmdir( unixName ) == -1)
{
FILE_SetDosError();
return 0;
}
return 1;
}
/***********************************************************************
* FILE_Dup
*
* dup() function for DOS handles.
*/
HFILE FILE_Dup( HFILE hFile )
{
int handle, newhandle;
HFILE dosHandle;
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return HFILE_ERROR;
dprintf_file( stddeb, "FILE_Dup for handle %d\n",handle);
if ((newhandle = dup(handle)) == -1)
{
FILE_SetDosError();
return HFILE_ERROR;
}
if ((dosHandle = FILE_AllocTaskHandle( newhandle )) == HFILE_ERROR)
close( newhandle );
dprintf_file( stddeb, "FILE_Dup return handle %d\n",dosHandle);
return dosHandle;
}
/***********************************************************************
* FILE_Dup2
*
* dup2() function for DOS handles.
*/
HFILE FILE_Dup2( HFILE hFile1, HFILE hFile2 )
{
PDB *pdb = (PDB *)GlobalLock( GetCurrentPDB() );
BYTE *files;
int handle, newhandle;
if ((handle = FILE_GetUnixHandle( hFile1 )) == -1) return HFILE_ERROR;
dprintf_file( stddeb, "FILE_Dup2 for handle %d\n",handle);
if ((hFile2 < 0) || (hFile2 >= (INT)pdb->nbFiles))
{
DOS_ERROR( ER_InvalidHandle, EC_ProgramError, SA_Abort, EL_Disk );
return HFILE_ERROR;
}
if ((newhandle = dup(handle)) == -1)
{
FILE_SetDosError();
return HFILE_ERROR;
}
if (newhandle >= 0xff)
{
DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError, SA_Abort, EL_Disk );
close( newhandle );
return HFILE_ERROR;
}
files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
if (files[hFile2] != 0xff)
{
dprintf_file( stddeb, "FILE_Dup2 closing old handle2 %d\n",
files[hFile2]);
close( files[hFile2] );
}
files[hFile2] = (BYTE)newhandle;
dprintf_file( stddeb, "FILE_Dup2 return handle2 %d\n",newhandle);
return hFile2;
}
/***********************************************************************
* FILE_OpenFile
*
* Implementation of API function OpenFile(). Returns a Unix file handle.
*/
int FILE_OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
{
const char *unixName, *dosName;
char *p;
int handle, len, i, unixMode;
struct stat st;
ofs->cBytes = sizeof(OFSTRUCT);
ofs->nErrCode = 0;
if (mode & OF_REOPEN) name = ofs->szPathName;
dprintf_file( stddeb, "FILE_Openfile: %s %04x\n", name, mode );
/* OF_PARSE simply fills the structure */
if (mode & OF_PARSE)
{
if (!(dosName = DOSFS_GetDosTrueName( name, FALSE )))
{
ofs->nErrCode = DOS_ExtendedError;
dprintf_file( stddeb, "FILE_Openfile: %s return = -1\n", name);
return -1;
}
lstrcpyn( ofs->szPathName, dosName, sizeof(ofs->szPathName) );
ofs->fFixedDisk = (GetDriveType( dosName[0]-'A' ) != DRIVE_REMOVABLE);
dprintf_file( stddeb, "FILE_Openfile: %s return = 0\n", name);
return 0;
}
/* OF_CREATE is completely different from all other options, so
handle it first */
if (mode & OF_CREATE)
{
if ((unixName = DOSFS_GetUnixFileName( name, FALSE )) == NULL)
{
ofs->nErrCode = DOS_ExtendedError;
dprintf_file( stddeb, "FILE_Openfile: %s return = -1\n", name);
return -1;
}
dprintf_file( stddeb, "FILE_OpenFile: creating '%s'\n", unixName );
handle = open( unixName, O_TRUNC | O_RDWR | O_CREAT, 0666 );
if (handle == -1)
{
FILE_SetDosError();
ofs->nErrCode = DOS_ExtendedError;
dprintf_file( stddeb, "FILE_Openfile: %s return = -1\n", name);
return -1;
}
lstrcpyn( ofs->szPathName, DOSFS_GetDosTrueName( name, FALSE ),
sizeof(ofs->szPathName) );
dprintf_file( stddeb, "FILE_Openfile: %s return = %d \n", name, handle);
return handle;
}
/* Now look for the file */
/* First try the current directory */
lstrcpyn( ofs->szPathName, name, sizeof(ofs->szPathName) );
if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
goto found;
/* Now try some different paths if none was specified */
if ((mode & OF_SEARCH) && !(mode & OF_REOPEN))
{
if (name[1] == ':') name += 2;
if ((p = strrchr( name, '\\' ))) name = p + 1;
if ((p = strrchr( name, '/' ))) name = p + 1;
if (!name[0]) goto not_found;
}
else
{
if ((name[1] == ':') || strchr( name, '/' ) || strchr( name, '\\' ))
goto not_found;
}
if ((len = sizeof(ofs->szPathName) - strlen(name) - 1) < 0) goto not_found;
/* Try the Windows directory */
GetWindowsDirectory( ofs->szPathName, len );
strcat( ofs->szPathName, "\\" );
strcat( ofs->szPathName, name );
if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
goto found;
/* Try the Windows system directory */
GetSystemDirectory( ofs->szPathName, len );
strcat( ofs->szPathName, "\\" );
strcat( ofs->szPathName, name );
if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )) != NULL)
goto found;
/* Try the path of the current executable */
if (GetCurrentTask())
{
GetModuleFileName( GetCurrentTask(), ofs->szPathName, len );
if ((p = strrchr( ofs->szPathName, '\\' )))
{
strcpy( p + 1, name );
if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE )))
goto found;
}
}
/* Try all directories in path */
for (i = 0; ; i++)
{
if (!DIR_GetDosPath( i, ofs->szPathName, len )) break;
strcat( ofs->szPathName, "\\" );
strcat( ofs->szPathName, name );
if ((unixName = DOSFS_GetUnixFileName( ofs->szPathName, TRUE)) != NULL)
goto found;
}
not_found:
dprintf_file( stddeb, "FILE_OpenFile: '%s' not found\n", name );
DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
ofs->nErrCode = ER_FileNotFound;
dprintf_file( stddeb, "FILE_Openfile: %s return =-1\n", name);
return -1;
found:
dprintf_file( stddeb, "FILE_OpenFile: found '%s'\n", unixName );
lstrcpyn( ofs->szPathName, DOSFS_GetDosTrueName( ofs->szPathName, FALSE ),
sizeof(ofs->szPathName) );
if (mode & OF_PARSE)
{
dprintf_file( stddeb, "FILE_Openfile: %s return = 0\n", name);
return 0;
}
if (mode & OF_DELETE)
{
if (unlink( unixName ) == -1) goto not_found;
dprintf_file( stddeb, "FILE_Openfile: %s return = 0\n", name);
return 0;
}
switch(mode & 3)
{
case OF_WRITE:
unixMode = O_WRONLY; break;
case OF_READWRITE:
unixMode = O_RDWR; break;
case OF_READ:
default:
unixMode = O_RDONLY; break;
}
if ((handle = FILE_OpenUnixFile( unixName, unixMode )) == -1)
goto not_found;
if (fstat( handle, &st ) != -1)
{
if ((mode & OF_VERIFY) && (mode & OF_REOPEN))
{
if (memcmp( ofs->reserved, &st.st_mtime, sizeof(ofs->reserved) ))
{
dprintf_file( stddeb, "FILE_Openfile: %s return = -1\n", name);
return -1;
}
}
memcpy( ofs->reserved, &st.st_mtime, sizeof(ofs->reserved) );
}
if (mode & OF_EXIST) close( handle );
dprintf_file( stddeb, "FILE_Openfile: %s return = %d\n", name,handle);
return handle;
}
/***********************************************************************
* GetTempFileName (KERNEL.97)
*/
INT GetTempFileName( BYTE drive, LPCSTR prefix, UINT unique, LPSTR buffer )
{
int i, handle;
UINT num = unique ? (unique & 0xffff) : time(NULL) & 0xffff;
char *p;
if (drive & TF_FORCEDRIVE)
{
sprintf( buffer, "%c:", drive & ~TF_FORCEDRIVE );
}
else
{
DIR_GetTempDosDir( buffer, 132 ); /* buffer must be at least 144 */
strcat( buffer, "\\" );
}
p = buffer + strlen(buffer);
for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
sprintf( p, "%04x.tmp", num );
if (unique)
{
lstrcpyn( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
return unique;
}
/* Now try to create it */
do
{
if ((handle = FILE_Create( buffer, 0666, TRUE )) != -1)
{ /* We created it */
dprintf_file( stddeb, "GetTempFileName: created %s\n", buffer );
close( handle );
break;
}
if (DOS_ExtendedError != ER_FileExists) break; /* No need to go on */
num++;
sprintf( p, "%04x.tmp", num );
} while (num != (unique & 0xffff));
lstrcpyn( buffer, DOSFS_GetDosTrueName( buffer, FALSE ), 144 );
dprintf_file( stddeb, "GetTempFileName: returning %s\n", buffer );
return num;
}
/***********************************************************************
* OpenFile (KERNEL.74)
*/
HFILE OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
{
int unixHandle;
HFILE handle;
dprintf_file( stddeb, "OpenFile %s \n",name);
if ((unixHandle = FILE_OpenFile( name, ofs, mode )) == -1)
return HFILE_ERROR;
if ((handle = FILE_AllocTaskHandle( unixHandle )) == HFILE_ERROR)
{
ofs->nErrCode = DOS_ExtendedError;
if (unixHandle) close( unixHandle );
}
if (!unixHandle) FILE_FreeTaskHandle( handle );
return handle;
}
/***********************************************************************
* _lclose (KERNEL.81)
*/
HFILE _lclose( HFILE hFile )
{
int handle;
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return HFILE_ERROR;
dprintf_file( stddeb, "_lclose: doshandle %d unixhandle %d\n", hFile,handle );
if (handle <= 2)
{
fprintf( stderr, "_lclose: internal error: closing handle %d\n", handle );
exit(1);
}
FILE_FreeTaskHandle( hFile );
close( handle );
return 0;
}
/***********************************************************************
* _lread (KERNEL.82)
*/
INT _lread( HFILE hFile, LPSTR buffer, WORD count )
{
return (INT)_hread( hFile, buffer, (LONG)count );
}
/***********************************************************************
* _lcreat (KERNEL.83)
*/
INT _lcreat( LPCSTR path, INT attr )
{
int unixHandle, mode;
HFILE handle;
dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
mode = (attr & 1) ? 0444 : 0666;
if ((unixHandle = FILE_Create( path, mode, FALSE )) == -1)
return HFILE_ERROR;
if ((handle = FILE_AllocTaskHandle( unixHandle )) == HFILE_ERROR)
close( unixHandle );
return handle;
}
/***********************************************************************
* _lcreat_uniq (Not a Windows API)
*/
INT _lcreat_uniq( LPCSTR path, INT attr )
{
int unixHandle, mode;
HFILE handle;
dprintf_file( stddeb, "_lcreat: %s %02x\n", path, attr );
mode = (attr & 1) ? 0444 : 0666;
if ((unixHandle = FILE_Create( path, mode, TRUE )) == -1)
return HFILE_ERROR;
if ((handle = FILE_AllocTaskHandle( unixHandle )) == HFILE_ERROR)
close( unixHandle );
return handle;
}
/***********************************************************************
* _llseek (KERNEL.84)
*/
LONG _llseek( HFILE hFile, LONG lOffset, INT nOrigin )
{
int handle, origin, result;
dprintf_file( stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
hFile, lOffset, nOrigin);
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return HFILE_ERROR;
switch(nOrigin)
{
case 1: origin = SEEK_CUR; break;
case 2: origin = SEEK_END; break;
default: origin = SEEK_SET; break;
}
if ((result = lseek( handle, lOffset, origin )) == -1) FILE_SetDosError();
return (result == -1) ? HFILE_ERROR : result;
}
/***********************************************************************
* _lopen (KERNEL.85)
*/
HFILE _lopen( LPCSTR path, INT mode )
{
int unixHandle;
int unixMode;
HFILE handle;
dprintf_file(stddeb, "_lopen('%s',%04x)\n", path, mode );
switch(mode & 3)
{
case OF_WRITE:
unixMode = O_WRONLY | O_TRUNC;
break;
case OF_READWRITE:
unixMode = O_RDWR;
break;
case OF_READ:
default:
unixMode = O_RDONLY;
break;
}
if ((unixHandle = FILE_Open( path, unixMode )) == -1) return HFILE_ERROR;
if ((handle = FILE_AllocTaskHandle( unixHandle )) == HFILE_ERROR)
close( unixHandle );
return handle;
}
/***********************************************************************
* _lwrite (KERNEL.86)
*/
INT _lwrite( HFILE hFile, LPCSTR buffer, WORD count )
{
return (INT)_hwrite( hFile, buffer, (LONG)count );
}
/***********************************************************************
* _hread (KERNEL.349)
*/
LONG _hread( HFILE hFile, LPSTR buffer, LONG count )
{
int handle;
LONG result;
dprintf_file( stddeb, "_hread: %d %p %ld\n", hFile, buffer, count );
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return -1;
if (!count) return 0;
if ((result = read( handle, buffer, count )) == -1) FILE_SetDosError();
return result;
}
/***********************************************************************
* _hwrite (KERNEL.350)
*/
LONG _hwrite( HFILE hFile, LPCSTR buffer, LONG count )
{
int handle;
LONG result;
dprintf_file( stddeb, "_hwrite: %d %p %ld\n", hFile, buffer, count );
if ((handle = FILE_GetUnixHandle( hFile )) == -1) return HFILE_ERROR;
if (count == 0) /* Expand or truncate at current position */
result = ftruncate( handle, lseek( handle, 0, SEEK_CUR ) );
else
result = write( handle, buffer, count );
if (result == -1) FILE_SetDosError();
return (result == -1) ? HFILE_ERROR : result;
}
/***********************************************************************
* SetHandleCount (KERNEL.199)
*/
WORD SetHandleCount( WORD count )
{
HANDLE hPDB = GetCurrentPDB();
PDB *pdb = (PDB *)GlobalLock( hPDB );
BYTE *files = PTR_SEG_TO_LIN( pdb->fileHandlesPtr );
WORD i;
dprintf_file( stddeb, "SetHandleCount(%d)\n", count );
if (count < 20) count = 20; /* No point in going below 20 */
else if (count > 254) count = 254;
/* If shrinking the table, make sure all extra file handles are closed */
if (count < pdb->nbFiles)
{
for (i = count; i < pdb->nbFiles; i++)
if (files[i] != 0xff) /* File open */
{
DOS_ERROR( ER_TooManyOpenFiles, EC_ProgramError,
SA_Abort, EL_Disk );
return pdb->nbFiles;
}
}
if (count == 20)
{
if (pdb->nbFiles > 20)
{
memcpy( pdb->fileHandles, files, 20 );
#ifdef WINELIB
GlobalFree( pdb->fileHandlesPtr );
pdb->fileHandlesPtr = pdb->fileHandles;
#else
GlobalFree( GlobalHandle( SELECTOROF(pdb->fileHandlesPtr) ));
pdb->fileHandlesPtr = (SEGPTR)MAKELONG( 0x18,
GlobalHandleToSel( hPDB ) );
#endif
pdb->nbFiles = 20;
}
}
else /* More than 20, need a new file handles table */
{
BYTE *newfiles;
HANDLE newhandle = GlobalAlloc( GMEM_MOVEABLE, count );
if (!newhandle)
{
DOS_ERROR( ER_OutOfMemory, EC_OutOfResource, SA_Abort, EL_Memory );
return pdb->nbFiles;
}
newfiles = (BYTE *)GlobalLock( newhandle );
if (count > pdb->nbFiles)
{
memcpy( newfiles, files, pdb->nbFiles );
memset( newfiles + pdb->nbFiles, 0xff, count - pdb->nbFiles );
}
else memcpy( newfiles, files, count );
#ifdef WINELIB
if (pdb->nbFiles > 20) GlobalFree( pdb->fileHandlesPtr );
#else
if (pdb->nbFiles > 20)
GlobalFree( GlobalHandle( SELECTOROF(pdb->fileHandlesPtr) ));
#endif
pdb->fileHandlesPtr = WIN16_GlobalLock( newhandle );
pdb->nbFiles = count;
}
return pdb->nbFiles;
}