2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* Unix support routines for PhysicsFS.
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2001-07-07 10:24:47 +02:00
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
2007-03-11 23:50:53 +01:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_platforms.h"
|
|
|
|
|
|
|
|
#ifdef PHYSFS_PLATFORM_UNIX
|
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
|
|
|
|
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
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
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_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)
|
|
|
|
{
|
2007-03-19 21:15:09 +01:00
|
|
|
const char *PROC_SELF_EXE = "/proc/self/exe";
|
|
|
|
char *retval = NULL;
|
|
|
|
char *envr = NULL;
|
|
|
|
struct stat stbuf;
|
|
|
|
|
|
|
|
/* fast path: default behaviour can handle this. */
|
|
|
|
if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
|
|
|
|
return(NULL); /* higher level will parse out real path from argv0. */
|
2002-05-21 13:29:00 +02:00
|
|
|
|
2007-03-19 21:15:09 +01:00
|
|
|
/*
|
|
|
|
* Try to avoid using argv0 unless forced to. If there's a Linux-like
|
|
|
|
* /proc filesystem, you can get the full path to the current process from
|
|
|
|
* the /proc/self/exe symlink.
|
|
|
|
*/
|
|
|
|
if ((lstat(PROC_SELF_EXE, &stbuf) != -1) && (S_ISLNK(stbuf.st_mode)))
|
|
|
|
{
|
|
|
|
const size_t len = stbuf.st_size;
|
|
|
|
char *buf = (char *) allocator.Malloc(len+1);
|
|
|
|
if (buf != NULL) /* if NULL, maybe you'll get lucky later. */
|
|
|
|
{
|
|
|
|
if (readlink(PROC_SELF_EXE, buf, len) != len)
|
|
|
|
allocator.Free(buf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[len] = '\0'; /* readlink doesn't null-terminate. */
|
|
|
|
retval = buf; /* we're good to go. */
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
2002-05-21 13:29:00 +02:00
|
|
|
|
2007-03-19 21:15:09 +01:00
|
|
|
if ((retval == NULL) && (argv0 != NULL))
|
|
|
|
{
|
|
|
|
/* If there's no dirsep on argv0, then look through $PATH for it. */
|
|
|
|
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
|
|
|
|
BAIL_IF_MACRO(!envr, NULL, NULL);
|
|
|
|
retval = findBinaryInPath(argv0, envr);
|
|
|
|
allocator.Free(envr);
|
|
|
|
} /* if */
|
2002-05-21 13:29:00 +02:00
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformCalcBaseDir */
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformRealPath */
|
|
|
|
|
2001-08-23 17:23:21 +02:00
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
char *__PHYSFS_platformCurrentDir(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This can't just do platformRealPath("."), since that would eventually
|
|
|
|
* just end up calling back into here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int allocSize = 0;
|
|
|
|
char *retval = NULL;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
allocSize += 100;
|
|
|
|
ptr = (char *) allocator.Realloc(retval, allocSize);
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
if (retval != NULL)
|
|
|
|
allocator.Free(retval);
|
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
retval = ptr;
|
|
|
|
ptr = getcwd(retval, allocSize);
|
|
|
|
} while (ptr == NULL && errno == ERANGE);
|
|
|
|
|
|
|
|
if (ptr == NULL && errno)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* getcwd() failed for some reason, for example current
|
|
|
|
* directory not existing.
|
|
|
|
*/
|
|
|
|
if (retval != NULL)
|
|
|
|
allocator.Free(retval);
|
|
|
|
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformCurrentDir */
|
|
|
|
|
|
|
|
|
2007-03-20 19:33:56 +01:00
|
|
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
|
|
|
{
|
|
|
|
return(0); /* just use malloc() and friends. */
|
|
|
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
|
|
|
|
|
|
|
|
2007-03-21 05:31:13 +01: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)
|
|
|
|
{
|
2007-03-21 05:31:13 +01:00
|
|
|
pthread_mutex_t *m = NULL;
|
|
|
|
pthread_mutexattr_t attr;
|
2002-03-30 17:44:09 +01:00
|
|
|
int rc;
|
2007-03-21 05:31:13 +01:00
|
|
|
|
|
|
|
rc = pthread_mutexattr_init(&attr);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
m = (pthread_mutex_t *) allocator.Malloc(sizeof (pthread_mutex_t));
|
|
|
|
if (m == NULL)
|
|
|
|
rc = ENOMEM;
|
|
|
|
else
|
|
|
|
rc = pthread_mutex_init(m, &attr);
|
|
|
|
} /* if */
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
} /* if */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
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)
|
|
|
|
{
|
2007-03-21 05:31:13 +01:00
|
|
|
pthread_mutex_t *m = (pthread_mutex_t *) mutex;
|
|
|
|
pthread_mutex_destroy(m);
|
2005-09-20 06:01:36 +02:00
|
|
|
allocator.Free(m);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* __PHYSFS_platformDestroyMutex */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformGrabMutex(void *mutex)
|
|
|
|
{
|
2007-03-21 05:31:13 +01:00
|
|
|
pthread_mutex_t *m = (pthread_mutex_t *) mutex;
|
|
|
|
if (pthread_mutex_lock(m) != 0)
|
|
|
|
return(0);
|
2005-09-20 06:01:36 +02:00
|
|
|
return(1);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* __PHYSFS_platformGrabMutex */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformReleaseMutex(void *mutex)
|
|
|
|
{
|
2007-03-21 05:31:13 +01:00
|
|
|
pthread_mutex_t *m = (pthread_mutex_t *) mutex;
|
|
|
|
pthread_mutex_unlock(m);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* __PHYSFS_platformReleaseMutex */
|
|
|
|
|
2002-07-25 19:13:03 +02:00
|
|
|
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
|
|
|
|
|
2007-03-11 23:50:53 +01:00
|
|
|
#endif /* PHYSFS_PLATFORM_UNIX */
|
2002-05-24 11:23:23 +02:00
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of unix.c ... */
|
|
|
|
|