physfs/platform/os2.c

781 lines
22 KiB
C
Raw Normal View History

2002-07-27 03:27:19 +02:00
/*
* OS/2 support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if (defined OS2)
#define INCL_DOSSEMAPHORES
#define INCL_DOSDATETIME
#define INCL_DOSFILEMGR
#define INCL_DOSMODULEMGR
#define INCL_DOSERRORS
2002-07-27 08:26:06 +02:00
#define INCL_DOSPROCESS
2002-07-27 03:27:19 +02:00
#define INCL_DOSDEVICES
2002-07-27 08:26:06 +02:00
#define INCL_DOSDEVIOCTL
#define INCL_DOSMISC
2002-07-27 03:27:19 +02:00
#include <os2.h>
2003-11-11 15:08:32 +01:00
#include <stdio.h>
2002-07-27 03:27:19 +02:00
#include <stdlib.h>
#include <errno.h>
#include <string.h>
2002-07-27 08:26:06 +02:00
#include <time.h>
#include <ctype.h>
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = "\\";
2002-07-27 03:27:19 +02:00
static const char *get_os2_error_string(APIRET rc)
2002-07-27 03:27:19 +02:00
{
switch (rc)
2002-07-27 03:27:19 +02:00
{
case NO_ERROR: return(NULL); /* not an error. */
case ERROR_INTERRUPT: return(NULL); /* not an error. */
case ERROR_TIMEOUT: return(NULL); /* not an error. */
case ERROR_NOT_ENOUGH_MEMORY: return(ERR_OUT_OF_MEMORY);
case ERROR_FILE_NOT_FOUND: return(ERR_NO_SUCH_FILE);
case ERROR_PATH_NOT_FOUND: return(ERR_NO_SUCH_PATH);
case ERROR_ACCESS_DENIED: return(ERR_ACCESS_DENIED);
case ERROR_NOT_DOS_DISK: return(ERR_NOT_A_DOS_DISK);
case ERROR_SHARING_VIOLATION: return(ERR_SHARING_VIOLATION);
case ERROR_CANNOT_MAKE: return(ERR_CANNOT_MAKE);
case ERROR_DEVICE_IN_USE: return(ERR_DEV_IN_USE);
case ERROR_OPEN_FAILED: return(ERR_OPEN_FAILED);
case ERROR_DISK_FULL: return(ERR_DISK_FULL);
case ERROR_PIPE_BUSY: return(ERR_PIPE_BUSY);
case ERROR_SHARING_BUFFER_EXCEEDED: return(ERR_SHARING_BUF_EXCEEDED);
case ERROR_FILENAME_EXCED_RANGE: return(ERR_BAD_FILENAME);
case ERROR_META_EXPANSION_TOO_LONG: return(ERR_BAD_FILENAME);
case ERROR_TOO_MANY_HANDLES: return(ERR_TOO_MANY_HANDLES);
case ERROR_TOO_MANY_OPEN_FILES: return(ERR_TOO_MANY_HANDLES);
case ERROR_NO_MORE_SEARCH_HANDLES: return(ERR_TOO_MANY_HANDLES);
case ERROR_SEEK_ON_DEVICE: return(ERR_SEEK_ERROR);
case ERROR_NEGATIVE_SEEK: return(ERR_SEEK_OUT_OF_RANGE);
2003-11-11 15:08:32 +01:00
/*!!! FIXME: Where did this go? case ERROR_DEL_CURRENT_DIRECTORY: return(ERR_DEL_CWD);*/
case ERROR_WRITE_PROTECT: return(ERR_WRITE_PROTECT_ERROR);
case ERROR_WRITE_FAULT: return(ERR_WRITE_FAULT);
case ERROR_LOCK_VIOLATION: return(ERR_LOCK_VIOLATION);
2003-11-11 15:08:32 +01:00
case ERROR_GEN_FAILURE: return(ERR_GEN_FAILURE);
case ERROR_UNCERTAIN_MEDIA: return(ERR_UNCERTAIN_MEDIA);
case ERROR_PROTECTION_VIOLATION: return(ERR_PROT_VIOLATION);
case ERROR_BROKEN_PIPE: return(ERR_BROKEN_PIPE);
2002-07-27 03:27:19 +02:00
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_NAME:
case ERROR_INVALID_DRIVE:
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_FUNCTION:
case ERROR_INVALID_LEVEL:
case ERROR_INVALID_CATEGORY:
case ERROR_DUPLICATE_NAME:
case ERROR_BUFFER_OVERFLOW:
case ERROR_BAD_LENGTH:
case ERROR_BAD_DRIVER_LEVEL:
case ERROR_DIRECT_ACCESS_HANDLE:
case ERROR_NOT_OWNER:
return(ERR_PHYSFS_BAD_OS_CALL);
2002-07-27 03:27:19 +02:00
default: return(ERR_OS2_GENERIC);
} /* switch */
2002-07-27 03:27:19 +02:00
return(NULL);
} /* get_os2_error_string */
2002-07-27 03:27:19 +02:00
static APIRET os2err(APIRET retval)
{
char buf[128];
const char *err = get_os2_error_string(retval);
if (err == ERR_OS2_GENERIC)
{
2003-11-11 15:08:32 +01:00
snprintf(buf, sizeof (buf), ERR_OS2_GENERIC, (int) retval);
err = buf;
} /* if */
2002-07-27 03:27:19 +02:00
if (err != NULL)
__PHYSFS_setError(err);
2003-11-11 15:08:32 +01:00
return(retval);
2002-07-27 03:27:19 +02:00
} /* os2err */
2002-07-28 06:02:06 +02:00
/* (be gentle, this function isn't very robust.) */
static void cvt_path_to_correct_case(char *buf)
{
char *fname = buf + 3; /* point to first element. */
char *ptr = strchr(fname, '\\'); /* find end of first element. */
buf[0] = toupper(buf[0]); /* capitalize drive letter. */
/*
* Go through each path element, and enumerate its parent dir until
* a case-insensitive match is found. If one is (and it SHOULD be)
* then overwrite the original element with the correct case.
* If there's an error, or the path has vanished for some reason, it
* won't hurt to have the original case, so we just keep going.
*/
while (fname != NULL)
{
char spec[CCHMAXPATH];
FILEFINDBUF3 fb;
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
*(fname - 1) = '\0'; /* isolate parent dir string. */
strcpy(spec, buf); /* copy isolated parent dir... */
strcat(spec, "\\*.*"); /* ...and add wildcard search spec. */
if (ptr != NULL) /* isolate element to find (fname is the start). */
*ptr = '\0';
rc = DosFindFirst(spec, &hdir, FILE_DIRECTORY,
&fb, sizeof (fb), &count, FIL_STANDARD);
if (rc == NO_ERROR)
{
while (count == 1) /* while still entries to enumerate... */
{
if (__PHYSFS_platformStricmp(fb.achName, fname) == 0)
{
strcpy(fname, fb.achName);
break; /* there it is. Overwrite and stop searching. */
} /* if */
DosFindNext(hdir, &fb, sizeof (fb), &count);
} /* while */
DosFindClose(hdir);
} /* if */
*(fname - 1) = '\\'; /* unisolate parent dir. */
fname = ptr; /* point to next element. */
if (ptr != NULL)
{
*ptr = '\\'; /* unisolate element. */
ptr = strchr(++fname, '\\'); /* find next element. */
} /* if */
} /* while */
} /* cvt_file_to_correct_case */
2002-07-27 03:27:19 +02:00
static char *baseDir = NULL;
int __PHYSFS_platformInit(void)
{
char buf[CCHMAXPATH];
APIRET rc;
2002-07-27 08:26:06 +02:00
PTIB ptib;
PPIB ppib;
2002-07-27 08:38:28 +02:00
PHYSFS_sint32 len;
2002-07-27 03:27:19 +02:00
assert(baseDir == NULL);
2002-07-27 08:26:06 +02:00
BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, NULL, 0);
rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
2002-07-27 08:38:28 +02:00
/* chop off filename, leave path. */
for (len = strlen(buf) - 1; len >= 0; len--)
{
if (buf[len] == '\\')
{
2002-07-27 08:49:36 +02:00
buf[len] = '\0';
2002-07-27 08:38:28 +02:00
break;
} /* if */
} /* for */
assert(len > 0); /* should have been a "x:\\" on the front on string. */
2002-07-27 08:38:28 +02:00
2002-07-28 06:02:06 +02:00
/* The string is capitalized! Figure out the REAL case... */
cvt_path_to_correct_case(buf);
baseDir = (char *) allocator.Malloc(len + 1);
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(baseDir == NULL, ERR_OUT_OF_MEMORY, 0);
2002-07-27 08:26:06 +02:00
strcpy(baseDir, buf);
2002-07-27 03:27:19 +02:00
return(1); /* success. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
assert(baseDir != NULL);
allocator.Free(baseDir);
2002-07-27 03:27:19 +02:00
baseDir = NULL;
return(1); /* success. */
} /* __PHYSFS_platformDeinit */
static int disc_is_inserted(ULONG drive)
{
int rc;
char buf[20];
DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
return(rc == NO_ERROR);
} /* is_cdrom_inserted */
/* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
#define CD01 0x31304443
2002-07-27 03:27:19 +02:00
static int is_cdrom_drive(ULONG drive)
{
PHYSFS_uint32 param, data;
2002-07-27 03:27:19 +02:00
ULONG ul1, ul2;
APIRET rc;
HFILE hfile = NULLHANDLE;
char drivename[3] = { 'A' + drive, ':', '\0' };
rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
BAIL_IF_MACRO(rc != NO_ERROR, NULL, 0);
data = 0;
param = PHYSFS_swapULE32(CD01);
ul1 = ul2 = sizeof (PHYSFS_uint32);
rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
&param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
DosClose(hfile);
return((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
2002-07-27 03:27:19 +02:00
} /* is_cdrom_drive */
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
2002-07-27 03:27:19 +02:00
{
ULONG dummy = 0;
ULONG drivemap = 0;
2002-07-27 03:27:19 +02:00
ULONG i, bit;
APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
2005-07-21 20:10:17 +02:00
if (os2err(rc) != NO_ERROR)
return;
2002-07-27 03:27:19 +02:00
2002-07-27 08:26:06 +02:00
for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
2002-07-27 03:27:19 +02:00
{
if (drivemap & bit) /* this logical drive exists. */
{
if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
{
char drive[4] = "x:\\";
drive[0] = ('A' + i);
cb(data, drive);
2002-07-27 03:27:19 +02:00
} /* if */
} /* if */
} /* for */
} /* __PHYSFS_platformDetectAvailableCDs */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
char *retval = (char *) allocator.Malloc(strlen(baseDir) + 1);
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, baseDir); /* calculated at init time. */
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
return(NULL); /* (*shrug*) */
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
return(__PHYSFS_platformCalcBaseDir(NULL));
} /* __PHYSFS_platformGetUserDir */
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
int ux, uy;
do
{
ux = toupper((int) *x);
uy = toupper((int) *y);
if (ux > uy)
return(1);
else if (ux < uy)
return(-1);
x++;
y++;
} while ((ux) && (uy));
return(0);
} /* __PHYSFS_platformStricmp */
int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
{
int ux, uy;
if (!len)
return(0);
do
{
ux = toupper((int) *x);
uy = toupper((int) *y);
if (ux > uy)
return(1);
else if (ux < uy)
return(-1);
x++;
y++;
len--;
} while ((ux) && (uy) && (len));
return(0);
} /* __PHYSFS_platformStrnicmp */
2002-07-27 03:27:19 +02:00
int __PHYSFS_platformExists(const char *fname)
{
FILESTATUS3 fs;
APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
return(os2err(rc) == NO_ERROR);
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
return(0); /* no symlinks in OS/2. */
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
FILESTATUS3 fs;
APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0)
return((fs.attrFile & FILE_DIRECTORY) != 0);
} /* __PHYSFS_platformIsDirectory */
/* !!! FIXME: can we lose the malloc here? */
2002-07-27 03:27:19 +02:00
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = allocator.Malloc(len);
2002-07-27 03:27:19 +02:00
char *p;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend)
strcpy(retval, prepend);
else
retval[0] = '\0';
strcat(retval, dirName);
if (append)
strcat(retval, append);
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
return(retval);
} /* __PHYSFS_platformCvtToDependent */
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
PHYSFS_StringCallback callback,
void *callbackdata)
2002-07-27 03:27:19 +02:00
{
char spec[CCHMAXPATH];
2002-07-27 08:26:06 +02:00
FILEFINDBUF3 fb;
2002-07-27 03:27:19 +02:00
HDIR hdir = HDIR_CREATE;
ULONG count = 1;
APIRET rc;
2005-07-21 20:10:17 +02:00
if (strlen(dirname) > sizeof (spec) - 5)
{
__PHYSFS_setError(ERR_BAD_FILENAME);
return;
} /* if */
2002-07-27 08:26:06 +02:00
strcpy(spec, dirname);
strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
2002-07-27 03:27:19 +02:00
rc = DosFindFirst(spec, &hdir,
FILE_DIRECTORY | FILE_ARCHIVED |
FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
2002-07-27 08:26:06 +02:00
&fb, sizeof (fb), &count, FIL_STANDARD);
2005-07-21 20:10:17 +02:00
if (os2err(rc) != NO_ERROR)
return;
2002-07-27 03:27:19 +02:00
while (count == 1)
{
if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
callback(callbackdata, fb.achName);
2002-07-27 03:27:19 +02:00
DosFindNext(hdir, &fb, sizeof (fb), &count);
} /* while */
DosFindClose(hdir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
char *retval;
ULONG currentDisk;
ULONG dummy;
ULONG pathSize = 0;
APIRET rc;
BYTE byte;
rc = DosQueryCurrentDisk(&currentDisk, &dummy);
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
/* The first call just tells us how much space we need for the string. */
rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
pathSize++; /* Add space for null terminator. */
retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/* Actually get the string this time. */
rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
if (os2err(rc) != NO_ERROR)
{
allocator.Free(retval);
2002-07-27 03:27:19 +02:00
return(NULL);
} /* if */
retval[0] = ('A' + (currentDisk - 1));
retval[1] = ':';
retval[2] = '\\';
return(retval);
} /* __PHYSFS_platformCurrentDir */
char *__PHYSFS_platformRealPath(const char *path)
{
char buf[CCHMAXPATH];
char *retval;
2002-07-27 08:26:06 +02:00
APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
retval = (char *) allocator.Malloc(strlen(buf) + 1);
2002-07-27 03:27:19 +02:00
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, buf);
return(retval);
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);
} /* __PHYSFS_platformMkDir */
void *__PHYSFS_platformOpenRead(const char *filename)
{
ULONG actionTaken = 0;
HFILE hfile = NULLHANDLE;
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
2002-07-27 08:26:06 +02:00
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
2002-07-27 03:27:19 +02:00
OPEN_ACCESS_READONLY, NULL));
return((void *) hfile);
} /* __PHYSFS_platformOpenRead */
void *__PHYSFS_platformOpenWrite(const char *filename)
{
ULONG actionTaken = 0;
HFILE hfile = NULLHANDLE;
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
2002-07-27 08:26:06 +02:00
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
2002-07-27 03:27:19 +02:00
OPEN_ACCESS_READWRITE, NULL));
return((void *) hfile);
} /* __PHYSFS_platformOpenWrite */
void *__PHYSFS_platformOpenAppend(const char *filename)
{
ULONG dummy = 0;
HFILE hfile = NULLHANDLE;
2002-07-27 08:26:06 +02:00
APIRET rc;
2002-07-27 03:27:19 +02:00
/*
* File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
* DosQueryFileInfo() will fail if we try to get a file length, etc.
*/
rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
2002-07-27 08:26:06 +02:00
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
2002-07-27 03:27:19 +02:00
OPEN_ACCESS_READWRITE, NULL));
if (rc == NO_ERROR)
{
if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
{
DosClose(hfile);
hfile = NULLHANDLE;
} /* if */
} /* if */
return((void *) hfile);
} /* __PHYSFS_platformOpenAppend */
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HFILE hfile = (HFILE) opaque;
PHYSFS_sint64 retval;
ULONG br;
for (retval = 0; retval < count; retval++)
{
2002-07-27 08:26:06 +02:00
os2err(DosRead(hfile, buffer, size, &br));
2002-07-27 03:27:19 +02:00
if (br < size)
{
2002-07-27 08:26:06 +02:00
DosSetFilePtr(hfile, -br, FILE_CURRENT, &br); /* try to cleanup. */
2002-07-27 03:27:19 +02:00
return(retval);
} /* if */
2002-07-27 08:26:06 +02:00
buffer = (void *) ( ((char *) buffer) + size );
2002-07-27 03:27:19 +02:00
} /* for */
return(retval);
} /* __PHYSFS_platformRead */
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HFILE hfile = (HFILE) opaque;
PHYSFS_sint64 retval;
ULONG bw;
for (retval = 0; retval < count; retval++)
{
2002-07-27 08:26:06 +02:00
os2err(DosWrite(hfile, buffer, size, &bw));
2002-07-27 03:27:19 +02:00
if (bw < size)
{
DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */
return(retval);
} /* if */
2002-07-27 08:26:06 +02:00
buffer = (void *) ( ((char *) buffer) + size );
2002-07-27 03:27:19 +02:00
} /* for */
return(retval);
} /* __PHYSFS_platformWrite */
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
ULONG dummy;
HFILE hfile = (HFILE) opaque;
LONG dist = (LONG) pos;
/* hooray for 32-bit filesystem limits! :) */
BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
} /* __PHYSFS_platformSeek */
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
ULONG pos;
HFILE hfile = (HFILE) opaque;
APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
return((PHYSFS_sint64) pos);
} /* __PHYSFS_platformTell */
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
{
FILESTATUS3 fs;
HFILE hfile = (HFILE) opaque;
APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
return((PHYSFS_sint64) fs.cbFile);
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 len, pos;
len = __PHYSFS_platformFileLength(opaque);
BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
pos = __PHYSFS_platformTell(opaque);
BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
return(pos >= len);
} /* __PHYSFS_platformEOF */
int __PHYSFS_platformFlush(void *opaque)
{
2002-07-27 08:26:06 +02:00
return(os2err(DosResetBuffer((HFILE) opaque) == NO_ERROR));
2002-07-27 03:27:19 +02:00
} /* __PHYSFS_platformFlush */
int __PHYSFS_platformClose(void *opaque)
{
return(os2err(DosClose((HFILE) opaque) == NO_ERROR));
} /* __PHYSFS_platformClose */
int __PHYSFS_platformDelete(const char *path)
{
if (__PHYSFS_platformIsDirectory(path))
return(os2err(DosDeleteDir(path)) == NO_ERROR);
return(os2err(DosDelete(path) == NO_ERROR));
} /* __PHYSFS_platformDelete */
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
{
2002-07-27 08:26:06 +02:00
PHYSFS_sint64 retval;
2002-07-27 03:27:19 +02:00
struct tm tm;
FILESTATUS3 fs;
APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
/* Convert to a format that mktime() can grok... */
tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2;
tm.tm_min = fs.ftimeLastWrite.minutes;
tm.tm_hour = fs.ftimeLastWrite.hours;
tm.tm_mday = fs.fdateLastWrite.day;
tm.tm_mon = fs.fdateLastWrite.month;
tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80;
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
tm.tm_yday = -1;
tm.tm_isdst = -1;
/* Convert to a format PhysicsFS can grok... */
retval = (PHYSFS_sint64) mktime(&tm);
BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
return(retval);
} /* __PHYSFS_platformGetLastModTime */
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
void __PHYSFS_platformTimeslice(void)
{
DosSleep(10);
} /* __PHYSFS_platformTimeslice(void) */
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
2002-07-27 08:26:06 +02:00
PTIB ptib;
PPIB ppib;
2002-07-27 03:27:19 +02:00
/*
* Allegedly, this API never fails, but we'll punt and return a
* default value (zero might as well do) if it does.
*/
2002-07-27 08:26:06 +02:00
BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0);
return((PHYSFS_uint64) ptib->tib_ordinal);
2002-07-27 03:27:19 +02:00
} /* __PHYSFS_platformGetThreadID */
void *__PHYSFS_platformCreateMutex(void)
{
HMTX hmtx = NULLHANDLE;
os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
return((void *) hmtx);
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
DosCloseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
/* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
DosReleaseMutexSem((HMTX) mutex);
} /* __PHYSFS_platformReleaseMutex */
2005-07-24 00:01:03 +02:00
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
void *__PHYSFS_platformAllocatorMalloc(size_t s)
{
#undef malloc
return(malloc(s));
} /* __PHYSFS_platformMalloc */
void *__PHYSFS_platformAllocatorRealloc(void *ptr, size_t s)
{
#undef realloc
return(realloc(ptr, s));
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
2002-07-27 03:27:19 +02:00
#endif /* defined OS2 */
/* end of os2.c ... */