2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* Unix support routines for PhysicsFS.
|
|
|
|
*
|
|
|
|
* Please see the file LICENSE in the source's root directory.
|
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-10 23:14:33 +02:00
|
|
|
/* BeOS uses beos.cpp and posix.c ... Cygwin and such use win32.c ... */
|
|
|
|
#if ((!defined __BEOS__) && (!defined WIN32))
|
2002-06-29 12:23:17 +02:00
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-07-08 05:25:12 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/stat.h>
|
2001-07-16 16:36:02 +02:00
|
|
|
#include <sys/param.h>
|
2001-07-08 05:25:12 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2002-07-20 02:19:53 +02:00
|
|
|
#include <sys/mount.h>
|
2002-03-05 20:16:38 +01:00
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
#ifndef PHYSFS_DARWIN
|
|
|
|
# if defined(__MACH__) && defined(__APPLE__)
|
|
|
|
# define PHYSFS_DARWIN 1
|
|
|
|
# include <CoreFoundation/CoreFoundation.h>
|
|
|
|
# include <CoreServices/CoreServices.h>
|
|
|
|
# include <IOKit/IOKitLib.h>
|
|
|
|
# include <IOKit/storage/IOMedia.h>
|
|
|
|
# include <IOKit/storage/IOCDMedia.h>
|
|
|
|
# include <IOKit/storage/IODVDMedia.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2002-07-25 19:13:03 +02:00
|
|
|
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-20 02:19:53 +02:00
|
|
|
#ifdef PHYSFS_HAVE_SYS_UCRED_H
|
|
|
|
# ifdef PHYSFS_HAVE_MNTENT_H
|
|
|
|
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
|
|
|
|
# endif
|
|
|
|
# include <sys/ucred.h>
|
2002-03-05 20:16:38 +01:00
|
|
|
#endif
|
2001-07-07 10:24:47 +02:00
|
|
|
|
2002-07-20 02:19:53 +02:00
|
|
|
#ifdef PHYSFS_HAVE_MNTENT_H
|
|
|
|
#include <mntent.h>
|
|
|
|
#endif
|
2002-04-06 23:05:14 +02:00
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
/* Seems to get defined in some system header... */
|
|
|
|
#ifdef Free
|
|
|
|
#undef Free
|
|
|
|
#endif
|
2001-07-07 10:24:47 +02:00
|
|
|
|
|
|
|
const char *__PHYSFS_platformDirSeparator = "/";
|
|
|
|
|
2002-03-05 20:16:38 +01:00
|
|
|
|
2002-03-24 07:36:48 +01:00
|
|
|
int __PHYSFS_platformInit(void)
|
|
|
|
{
|
|
|
|
return(1); /* always succeed. */
|
|
|
|
} /* __PHYSFS_platformInit */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformDeinit(void)
|
|
|
|
{
|
|
|
|
return(1); /* always succeed. */
|
|
|
|
} /* __PHYSFS_platformDeinit */
|
|
|
|
|
|
|
|
|
2002-07-25 07:21:00 +02:00
|
|
|
#ifdef PHYSFS_NO_CDROM_SUPPORT
|
|
|
|
|
|
|
|
/* Stub version for platforms without CD-ROM support. */
|
2004-09-29 08:09:29 +02:00
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
2002-07-25 07:21:00 +02:00
|
|
|
{
|
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
#elif (defined PHYSFS_DARWIN) /* "Big Nasty." */
|
|
|
|
/*
|
|
|
|
* Code based on sample from Apple Developer Connection:
|
|
|
|
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int darwinIsWholeMedia(io_service_t service)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
CFTypeRef wholeMedia;
|
2002-03-24 07:36:48 +01:00
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
if (!IOObjectConformsTo(service, kIOMediaClass))
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
wholeMedia = IORegistryEntryCreateCFProperty(service,
|
|
|
|
CFSTR(kIOMediaWholeKey),
|
|
|
|
kCFAllocatorDefault, 0);
|
|
|
|
if (wholeMedia == NULL)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
retval = CFBooleanGetValue(wholeMedia);
|
|
|
|
CFRelease(wholeMedia);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
} /* darwinIsWholeMedia */
|
|
|
|
|
|
|
|
|
|
|
|
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
CFMutableDictionaryRef matchingDict;
|
|
|
|
kern_return_t rc;
|
|
|
|
io_iterator_t iter;
|
|
|
|
io_service_t service;
|
|
|
|
|
|
|
|
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
|
|
|
|
if ((rc != KERN_SUCCESS) || (!iter))
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
service = IOIteratorNext(iter);
|
|
|
|
IOObjectRelease(iter);
|
|
|
|
if (!service)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
|
|
|
|
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
|
|
|
|
|
|
|
|
if (!iter)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
if (rc != KERN_SUCCESS)
|
|
|
|
{
|
|
|
|
IOObjectRelease(iter);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
IOObjectRetain(service); /* add an extra object reference... */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (darwinIsWholeMedia(service))
|
|
|
|
{
|
|
|
|
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
|
|
|
|
(IOObjectConformsTo(service, kIODVDMediaClass)) )
|
|
|
|
{
|
|
|
|
retval = 1;
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
IOObjectRelease(service);
|
|
|
|
} while ((service = IOIteratorNext(iter)) && (!retval));
|
|
|
|
|
|
|
|
IOObjectRelease(iter);
|
|
|
|
IOObjectRelease(service);
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* darwinIsMountedDisc */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
2003-05-24 05:36:36 +02:00
|
|
|
{
|
|
|
|
const char *devPrefix = "/dev/";
|
|
|
|
int prefixLen = strlen(devPrefix);
|
|
|
|
mach_port_t masterPort = 0;
|
|
|
|
struct statfs *mntbufp;
|
|
|
|
int i, mounts;
|
|
|
|
|
|
|
|
if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS)
|
2005-03-13 04:18:18 +01:00
|
|
|
BAIL_MACRO(ERR_OS_ERROR, /*return void*/);
|
2003-05-24 05:36:36 +02:00
|
|
|
|
|
|
|
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */
|
|
|
|
for (i = 0; i < mounts; i++)
|
|
|
|
{
|
|
|
|
char *dev = mntbufp[i].f_mntfromname;
|
|
|
|
char *mnt = mntbufp[i].f_mntonname;
|
|
|
|
if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dev += prefixLen;
|
|
|
|
if (darwinIsMountedDisc(dev, masterPort))
|
2004-09-29 08:09:29 +02:00
|
|
|
cb(data, mnt);
|
2003-05-24 05:36:36 +02:00
|
|
|
} /* for */
|
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
|
|
|
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
|
2002-03-05 20:16:38 +01:00
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
2002-03-05 20:16:38 +01:00
|
|
|
{
|
2002-07-25 07:21:00 +02:00
|
|
|
int i;
|
2004-09-29 08:09:29 +02:00
|
|
|
struct statfs *mntbufp = NULL;
|
|
|
|
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
|
2002-03-05 20:16:38 +01:00
|
|
|
|
2002-07-25 07:21:00 +02:00
|
|
|
for (i = 0; i < mounts; i++)
|
|
|
|
{
|
2002-03-05 20:16:38 +01:00
|
|
|
int add_it = 0;
|
|
|
|
|
2002-07-25 07:21:00 +02:00
|
|
|
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
|
2002-03-05 20:16:38 +01:00
|
|
|
add_it = 1;
|
2002-07-25 07:21:00 +02:00
|
|
|
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
|
2002-04-07 01:49:17 +02:00
|
|
|
add_it = 1;
|
2002-05-21 13:29:00 +02:00
|
|
|
|
|
|
|
/* add other mount types here */
|
2002-03-05 20:16:38 +01:00
|
|
|
|
|
|
|
if (add_it)
|
2004-09-29 08:09:29 +02:00
|
|
|
cb(data, mntbufp[i].f_mntonname);
|
2002-07-25 07:21:00 +02:00
|
|
|
} /* for */
|
2002-03-05 20:16:38 +01:00
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
#elif (defined PHYSFS_HAVE_MNTENT_H)
|
2002-03-05 20:16:38 +01:00
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
2001-07-07 10:24:47 +02:00
|
|
|
{
|
2001-08-07 05:33:44 +02:00
|
|
|
FILE *mounts = NULL;
|
|
|
|
struct mntent *ent = NULL;
|
|
|
|
|
|
|
|
mounts = setmntent("/etc/mtab", "r");
|
2005-03-13 04:33:11 +01:00
|
|
|
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
|
2001-08-07 05:33:44 +02:00
|
|
|
|
|
|
|
while ( (ent = getmntent(mounts)) != NULL )
|
|
|
|
{
|
|
|
|
int add_it = 0;
|
|
|
|
if (strcmp(ent->mnt_type, "iso9660") == 0)
|
|
|
|
add_it = 1;
|
2002-05-21 13:29:00 +02:00
|
|
|
|
|
|
|
/* add other mount types here */
|
2001-08-07 05:33:44 +02:00
|
|
|
|
|
|
|
if (add_it)
|
2004-09-29 08:09:29 +02:00
|
|
|
cb(data, ent->mnt_dir);
|
2001-08-07 05:33:44 +02:00
|
|
|
} /* while */
|
|
|
|
|
|
|
|
endmntent(mounts);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2002-03-05 20:16:38 +01:00
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
|
|
|
#endif
|
2001-07-07 10:24:47 +02:00
|
|
|
|
|
|
|
|
2002-05-24 11:23:23 +02:00
|
|
|
/* this is in posix.c ... */
|
|
|
|
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
/*
|
|
|
|
* See where program (bin) resides in the $PATH specified by (envr).
|
|
|
|
* returns a copy of the first element in envr that contains it, or NULL
|
|
|
|
* if it doesn't exist or there were other problems. PHYSFS_SetError() is
|
|
|
|
* called if we have a problem.
|
|
|
|
*
|
2005-03-14 12:49:30 +01:00
|
|
|
* (envr) will be scribbled over, and you are expected to allocator.Free() the
|
2002-05-21 13:29:00 +02:00
|
|
|
* return value when you're done with it.
|
|
|
|
*/
|
|
|
|
static char *findBinaryInPath(const char *bin, char *envr)
|
2001-07-08 15:57:28 +02:00
|
|
|
{
|
2002-05-21 13:29:00 +02:00
|
|
|
size_t alloc_size = 0;
|
|
|
|
char *exe = NULL;
|
|
|
|
char *start = envr;
|
2001-07-08 15:57:28 +02:00
|
|
|
char *ptr;
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
|
2001-07-08 15:57:28 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2002-05-21 13:29:00 +02:00
|
|
|
size_t size;
|
|
|
|
ptr = strchr(start, ':'); /* find next $PATH separator. */
|
2001-07-08 15:57:28 +02:00
|
|
|
if (ptr)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
size = strlen(start) + strlen(bin) + 2;
|
|
|
|
if (size > alloc_size)
|
2001-07-08 15:57:28 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
char *x = (char *) allocator.Realloc(exe, size);
|
2002-05-21 13:29:00 +02:00
|
|
|
if (x == NULL)
|
|
|
|
{
|
|
|
|
if (exe != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(exe);
|
2002-05-21 13:29:00 +02:00
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
alloc_size = size;
|
|
|
|
exe = x;
|
2001-07-08 15:57:28 +02:00
|
|
|
} /* if */
|
2002-05-21 13:29:00 +02:00
|
|
|
|
|
|
|
/* build full binary path... */
|
2001-07-08 15:57:28 +02:00
|
|
|
strcpy(exe, start);
|
2002-11-30 05:50:49 +01:00
|
|
|
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
|
2001-08-29 05:31:43 +02:00
|
|
|
strcat(exe, "/");
|
2002-05-21 13:29:00 +02:00
|
|
|
strcat(exe, bin);
|
|
|
|
|
|
|
|
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
|
2001-07-08 15:57:28 +02:00
|
|
|
{
|
2002-05-21 13:29:00 +02:00
|
|
|
strcpy(exe, start); /* i'm lazy. piss off. */
|
|
|
|
return(exe);
|
|
|
|
} /* if */
|
2001-07-08 15:57:28 +02:00
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
start = ptr + 1; /* start points to beginning of next element. */
|
2001-07-08 15:57:28 +02:00
|
|
|
} while (ptr != NULL);
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
if (exe != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(exe);
|
2002-05-21 13:29:00 +02:00
|
|
|
|
|
|
|
return(NULL); /* doesn't exist in path. */
|
|
|
|
} /* findBinaryInPath */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
|
|
|
|
{
|
|
|
|
/* If there isn't a path on argv0, then look through the $PATH for it. */
|
|
|
|
|
|
|
|
char *retval;
|
|
|
|
char *envr;
|
|
|
|
|
|
|
|
if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
|
|
|
|
return(NULL);
|
|
|
|
|
2002-05-24 11:23:23 +02:00
|
|
|
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
|
2002-05-21 13:29:00 +02:00
|
|
|
BAIL_IF_MACRO(!envr, NULL, NULL);
|
|
|
|
retval = findBinaryInPath(argv0, envr);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(envr);
|
2001-07-08 15:57:28 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformCalcBaseDir */
|
|
|
|
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
|
|
|
|
void __PHYSFS_platformTimeslice(void)
|
|
|
|
{
|
2002-03-05 02:35:54 +01:00
|
|
|
usleep( 10 * 1000 ); /* don't care if it fails. */
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* __PHYSFS_platformTimeslice */
|
|
|
|
|
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
#if PHYSFS_DARWIN
|
2003-01-05 00:24:50 +01:00
|
|
|
/*
|
|
|
|
* This function is only for OSX. The problem is that Apple's applications
|
|
|
|
* can actually be directory structures with the actual executable nested
|
|
|
|
* several levels down. PhysFS computes the base directory from the Unix
|
|
|
|
* executable, but this may not be the correct directory. Apple tries to
|
|
|
|
* hide everything from the user, so from Finder, the user never sees the
|
|
|
|
* Unix executable, and the directory package (bundle) is considered the
|
|
|
|
* "executable". This means that the correct base directory is at the
|
|
|
|
* level where the directory structure starts.
|
|
|
|
* A typical bundle seems to look like this:
|
|
|
|
* MyApp.app/ <-- top level...this is what the user sees in Finder
|
|
|
|
* Contents/
|
|
|
|
* MacOS/
|
|
|
|
* MyApp <-- the actual executable
|
|
|
|
*
|
|
|
|
* Since anything below the app folder is considered hidden, most
|
|
|
|
* application files need to be at the top level if you intend to
|
|
|
|
* write portable software. Thus if the application resides in:
|
|
|
|
* /Applications/MyProgram
|
|
|
|
* and the executable is the bundle MyApp.app,
|
|
|
|
* PhysFS computes the following as the base directory:
|
|
|
|
* /Applications/MyProgram/MyApp.app/Contents/MacOS/
|
|
|
|
* We need to strip off the MyApp.app/Contents/MacOS/
|
|
|
|
*
|
|
|
|
* However, there are corner cases. OSX applications can be traditional
|
|
|
|
* Unix executables without the bundle. Also, it is not entirely clear
|
|
|
|
* to me what kinds of permutations bundle structures can have.
|
|
|
|
*
|
|
|
|
* For now, this is a temporary hack until a better solution
|
|
|
|
* can be made. This function will try to find a "/Contents/MacOS"
|
|
|
|
* inside the path. If it succeeds, then the path will be truncated
|
|
|
|
* to correct the directory. If it is not found, the path will be
|
|
|
|
* left alone and will presume it is a traditional Unix execuatable.
|
|
|
|
* Most programs also include the .app extention in the top level
|
|
|
|
* folder, but it doesn't seem to be a requirement (Acrobat doesn't
|
|
|
|
* have it). MacOS looks like it can also be MacOSClassic.
|
|
|
|
* This function will test for MacOS and hope it captures any
|
|
|
|
* other permutations.
|
|
|
|
*/
|
|
|
|
static void stripAppleBundle(char *path)
|
|
|
|
{
|
|
|
|
char *sub_str = "/contents/macos";
|
|
|
|
char *found_ptr = NULL;
|
|
|
|
char *tempbuf = NULL;
|
2005-03-14 12:49:30 +01:00
|
|
|
size_t len = strlen(path) + 1;
|
2003-01-05 00:24:50 +01:00
|
|
|
int i;
|
|
|
|
|
2003-05-24 05:36:36 +02:00
|
|
|
/* !!! FIXME: Can we stack-allocate this? --ryan. */
|
2005-03-14 12:49:30 +01:00
|
|
|
tempbuf = (char *) allocator.Malloc(len);
|
|
|
|
if (!tempbuf) return;
|
|
|
|
memset(tempbuf, '\0', len);
|
|
|
|
|
2003-01-05 00:24:50 +01:00
|
|
|
/* Unlike other Unix filesystems, HFS is case insensitive
|
|
|
|
* It wouldn be nice to use strcasestr, but it doesn't seem
|
|
|
|
* to be available in the OSX gcc library right now.
|
|
|
|
* So we should make a lower case copy of the path to
|
|
|
|
* compare against
|
|
|
|
*/
|
|
|
|
for(i=0; i<strlen(path); i++)
|
|
|
|
{
|
|
|
|
/* convert to lower case */
|
|
|
|
tempbuf[i] = tolower(path[i]);
|
|
|
|
}
|
|
|
|
/* See if we can find "/contents/macos" in the path */
|
|
|
|
found_ptr = strstr(tempbuf, sub_str);
|
|
|
|
if(NULL == found_ptr)
|
|
|
|
{
|
|
|
|
/* It doesn't look like a bundle so we can keep the
|
|
|
|
* original path. Just return */
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(tempbuf);
|
2003-01-05 00:24:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* We have a bundle, so let's backstep character by character
|
|
|
|
* to erase the extra parts of the path. Quit when we hit
|
|
|
|
* the preceding '/' character.
|
|
|
|
*/
|
|
|
|
for(i=strlen(path)-strlen(found_ptr)-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
if('/' == path[i])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Safety check */
|
|
|
|
if(i<1)
|
|
|
|
{
|
|
|
|
/* This probably shouldn't happen. */
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Back up one more to remove trailing '/' and set the '\0' */
|
|
|
|
path[i] = '\0';
|
|
|
|
}
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(tempbuf);
|
2003-01-05 00:24:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* defined __MACH__ && defined __APPLE__ */
|
|
|
|
|
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
char *__PHYSFS_platformRealPath(const char *path)
|
|
|
|
{
|
|
|
|
char resolved_path[MAXPATHLEN];
|
|
|
|
char *retval = NULL;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
|
2001-07-16 16:36:02 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
strcpy(retval, resolved_path);
|
2003-01-05 00:24:50 +01:00
|
|
|
|
|
|
|
#if defined(__MACH__) && defined(__APPLE__)
|
|
|
|
stripAppleBundle(retval);
|
|
|
|
#endif /* defined __MACH__ && defined __APPLE__ */
|
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformRealPath */
|
|
|
|
|
2001-08-23 17:23:21 +02:00
|
|
|
|
2002-07-26 01:22:29 +02:00
|
|
|
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
|
2002-07-25 19:13:03 +02:00
|
|
|
|
|
|
|
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
|
|
|
|
void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
|
|
|
|
void __PHYSFS_platformDestroyMutex(void *mutex) {}
|
|
|
|
int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
|
|
|
|
void __PHYSFS_platformReleaseMutex(void *mutex) {}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-08-09 21:47:06 +02:00
|
|
|
/* Just in case; this is a panic value. */
|
|
|
|
#if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
|
|
|
|
# define SIZEOF_INT 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SIZEOF_INT == 4)
|
|
|
|
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
|
|
|
|
#elif (SIZEOF_INT == 2)
|
|
|
|
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
|
|
|
|
#elif (SIZEOF_INT == 1)
|
|
|
|
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
|
|
|
|
#else
|
|
|
|
# define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
|
|
|
|
#endif
|
2002-07-25 19:42:21 +02:00
|
|
|
|
2002-07-25 19:13:03 +02:00
|
|
|
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
|
|
|
|
{
|
2002-07-25 19:42:21 +02:00
|
|
|
return(PHTREAD_TO_UI64(pthread_self()));
|
2002-07-25 19:13:03 +02:00
|
|
|
} /* __PHYSFS_platformGetThreadID */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
void *__PHYSFS_platformCreateMutex(void)
|
|
|
|
{
|
|
|
|
int rc;
|
2005-03-14 12:49:30 +01:00
|
|
|
pthread_mutex_t *m;
|
|
|
|
m = (pthread_mutex_t *) allocator.Malloc(sizeof (pthread_mutex_t));
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
rc = pthread_mutex_init(m, NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(m);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO(strerror(rc), NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return((void *) m);
|
|
|
|
} /* __PHYSFS_platformCreateMutex */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformDestroyMutex(void *mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy((pthread_mutex_t *) mutex);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(mutex);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* __PHYSFS_platformDestroyMutex */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformGrabMutex(void *mutex)
|
|
|
|
{
|
|
|
|
return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
|
|
|
|
} /* __PHYSFS_platformGrabMutex */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformReleaseMutex(void *mutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock((pthread_mutex_t *) mutex);
|
|
|
|
} /* __PHYSFS_platformReleaseMutex */
|
|
|
|
|
2002-07-25 19:13:03 +02:00
|
|
|
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
|
|
|
|
|
|
|
|
|
2002-07-11 10:44:27 +02:00
|
|
|
#endif /* !defined __BEOS__ && !defined WIN32 */
|
2002-05-24 11:23:23 +02:00
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of unix.c ... */
|
|
|
|
|