physfs/src/platform_unix.c

335 lines
8.7 KiB
C
Raw Normal View History

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.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_UNIX
2002-06-29 12:23:17 +02:00
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
2002-03-05 20:16:38 +01:00
#if PHYSFS_PLATFORM_LINUX && !defined(PHYSFS_HAVE_MNTENT_H)
#define PHYSFS_HAVE_MNTENT_H 1
#elif PHYSFS_PLATFORM_SOLARIS && !defined(PHYSFS_HAVE_SYS_MNTTAB_H)
#define PHYSFS_HAVE_SYS_MNTTAB_H 1
#elif PHYSFS_PLATFORM_BSD && !defined(PHYSFS_HAVE_SYS_UCRED_H)
#define PHYSFS_HAVE_SYS_UCRED_H 1
#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/mount.h>
2002-07-20 02:19:53 +02:00
# 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
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#endif
2001-07-07 10:24:47 +02:00
#include "physfs_internal.h"
int __PHYSFS_platformInit(void)
{
return 1; /* always succeed. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
return 1; /* always succeed. */
} /* __PHYSFS_platformDeinit */
/* Stub version for platforms without CD-ROM support. */
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
#if (defined PHYSFS_NO_CDROM_SUPPORT)
/* no-op. */
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
int i;
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
2002-03-05 20:16:38 +01:00
for (i = 0; i < mounts; i++)
{
2002-03-05 20:16:38 +01:00
int add_it = 0;
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
2002-03-05 20:16:38 +01:00
add_it = 1;
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)
cb(data, mntbufp[i].f_mntonname);
} /* for */
2002-03-05 20:16:38 +01:00
#elif (defined PHYSFS_HAVE_MNTENT_H)
2001-08-07 05:33:44 +02:00
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*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;
else if (strcmp(ent->mnt_type, "udf") == 0)
add_it = 1;
/* !!! FIXME: these might pick up floppy drives, right? */
else if (strcmp(ent->mnt_type, "auto") == 0)
add_it = 1;
else if (strcmp(ent->mnt_type, "supermount") == 0)
add_it = 1;
2002-05-21 13:29:00 +02:00
2010-03-01 17:49:04 +01:00
/* !!! FIXME: udf? automount? */
2002-05-21 13:29:00 +02:00
/* add other mount types here */
2001-08-07 05:33:44 +02:00
if (add_it)
cb(data, ent->mnt_dir);
2001-08-07 05:33:44 +02:00
} /* while */
endmntent(mounts);
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
FILE *mounts = fopen(MNTTAB, "r");
struct mnttab ent;
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
while (getmntent(mounts, &ent) == 0)
{
int add_it = 0;
if (strcmp(ent.mnt_fstype, "hsfs") == 0)
add_it = 1;
/* add other mount types here */
if (add_it)
cb(data, ent.mnt_mountp);
} /* while */
fclose(mounts);
#else
#error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
2002-03-05 20:16:38 +01:00
#endif
} /* __PHYSFS_platformDetectAvailableCDs */
2001-07-07 10:24:47 +02:00
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
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.
*
* (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)
{
2002-05-21 13:29:00 +02:00
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
char *ptr;
assert(bin != NULL);
assert(envr != NULL);
do
{
2002-05-21 13:29:00 +02:00
size_t size;
ptr = strchr(start, ':'); /* find next $PATH separator. */
if (ptr)
*ptr = '\0';
2002-05-21 13:29:00 +02:00
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
{
char *x = (char *) allocator.Realloc(exe, size);
if (!x)
2002-05-21 13:29:00 +02:00
{
if (exe != NULL)
allocator.Free(exe);
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
2002-05-21 13:29:00 +02:00
} /* if */
alloc_size = size;
exe = x;
} /* if */
2002-05-21 13:29:00 +02:00
/* build full binary path... */
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. */
{
2002-05-21 13:29:00 +02:00
strcpy(exe, start); /* i'm lazy. piss off. */
return exe;
2002-05-21 13:29:00 +02:00
} /* if */
2002-05-21 13:29:00 +02:00
start = ptr + 1; /* start points to beginning of next element. */
} while (ptr != NULL);
2002-05-21 13:29:00 +02:00
if (exe != NULL)
allocator.Free(exe);
2002-05-21 13:29:00 +02:00
return NULL; /* doesn't exist in path. */
2002-05-21 13:29:00 +02:00
} /* findBinaryInPath */
static char *readSymLink(const char *path)
{
ssize_t len = 64;
ssize_t rc = -1;
char *retval = NULL;
while (1)
{
char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
if (ptr == NULL)
break; /* out of memory. */
retval = ptr;
rc = readlink(path, retval, len);
if (rc == -1)
break; /* not a symlink, i/o error, etc. */
else if (rc < len)
{
retval[rc] = '\0'; /* readlink doesn't null-terminate. */
return retval; /* we're good to go. */
} /* else if */
len *= 2; /* grow buffer, try again. */
} /* while */
if (retval != NULL)
allocator.Free(retval);
return NULL;
} /* readSymLink */
2002-05-21 13:29:00 +02:00
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
char *retval = NULL;
char *envr = NULL;
/* 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
/*
* 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.
*/
retval = readSymLink("/proc/self/exe");
if (retval == NULL)
{
/* older kernels don't have /proc/self ... try PID version... */
const unsigned long long pid = (unsigned long long) getpid();
char path[64];
const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
if ( (rc > 0) && (rc < sizeof(path)) )
retval = readSymLink(path);
} /* if */
if (retval != NULL) /* chop off filename. */
{
char *ptr = strrchr(retval, '/');
if (ptr != NULL)
*ptr = '\0';
} /* if */
2002-05-21 13:29:00 +02: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, ERRPASS, NULL);
retval = findBinaryInPath(argv0, envr);
allocator.Free(envr);
} /* if */
2002-05-21 13:29:00 +02:00
if (retval != NULL)
{
/* try to shrink buffer... */
char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr; /* oh well if it failed. */
} /* if */
return retval;
} /* __PHYSFS_platformCalcBaseDir */
2012-03-22 04:30:50 +01:00
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
/*
* We use XDG's base directory spec, even if you're not on Linux.
* This isn't strictly correct, but the results are relatively sane
* in any case.
*
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*/
const char *envr = getenv("XDG_DATA_HOME");
const char *append = "/";
char *retval = NULL;
size_t len = 0;
if (!envr)
{
/* You end up with "$HOME/.local/share/Game Name 2" */
envr = __PHYSFS_getUserDir();
BAIL_IF_MACRO(!envr, ERRPASS, NULL); /* oh well. */
append = ".local/share/";
} /* if */
len = strlen(envr) + strlen(append) + strlen(app) + 2;
2012-03-22 04:30:50 +01:00
retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
snprintf(retval, len, "%s%s%s/", envr, append, app);
2012-03-22 04:30:50 +01:00
return retval;
} /* __PHYSFS_platformCalcPrefDir */
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
return 0; /* just use malloc() and friends. */
} /* __PHYSFS_platformSetDefaultAllocator */
#endif /* PHYSFS_PLATFORM_UNIX */
2001-07-07 10:24:47 +02:00
/* end of unix.c ... */