physfs/src/platform_unix.c

370 lines
9.8 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 <stdlib.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>
#include <limits.h>
2002-03-05 20:16:38 +01:00
#if PHYSFS_NO_CDROM_SUPPORT
#elif PHYSFS_PLATFORM_LINUX
# define PHYSFS_HAVE_MNTENT_H 1
#elif defined __CYGWIN__
# define PHYSFS_HAVE_MNTENT_H 1
#elif PHYSFS_PLATFORM_SOLARIS
# define PHYSFS_HAVE_SYS_MNTTAB_H 1
#elif PHYSFS_PLATFORM_BSD
# define PHYSFS_HAVE_SYS_UCRED_H 1
#else
# warning No CD-ROM support included. Either define your platform here,
# warning or define PHYSFS_NO_CDROM_SUPPORT=1 to confirm this is intentional.
#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
2017-07-09 23:31:27 +02:00
#if PHYSFS_PLATFORM_FREEBSD
#include <sys/sysctl.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(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(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);
2002-03-05 20:16:38 +01:00
#endif
} /* __PHYSFS_platformDetectAvailableCDs */
2001-07-07 10:24:47 +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.
*
* (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;
size_t binlen;
2002-05-21 13:29:00 +02:00
ptr = strchr(start, ':'); /* find next $PATH separator. */
if (ptr)
*ptr = '\0';
binlen = strlen(bin);
size = strlen(start) + binlen + 2;
2014-08-19 16:55:56 +02:00
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(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. */
{
exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */
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;
2012-03-24 05:26:04 +01:00
const char *envr = NULL;
/* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
#if PHYSFS_PLATFORM_FREEBSD
{
char fullpath[PATH_MAX];
size_t buflen = sizeof (fullpath);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1)
retval = __PHYSFS_strdup(fullpath);
}
#elif PHYSFS_PLATFORM_SOLARIS
{
const char *path = getexecname();
if ((path != NULL) && (path[0] == '/')) /* must be absolute path... */
retval = __PHYSFS_strdup(path);
}
#endif
/* If there's a Linux-like /proc filesystem, you can get the full path to
* the current process from a symlink in there.
*/
if (!retval && (access("/proc", F_OK) == 0))
{
retval = readSymLink("/proc/self/exe");
if (!retval) retval = readSymLink("/proc/curproc/file");
if (!retval) retval = readSymLink("/proc/curproc/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 */
if (retval != NULL) /* chop off filename. */
{
char *ptr = strrchr(retval, '/');
if (ptr != NULL)
*(ptr+1) = '\0';
2012-03-24 05:26:04 +01:00
else /* shouldn't happen, but just in case... */
{
allocator.Free(retval);
retval = NULL;
} /* else */
} /* if */
2002-05-21 13:29:00 +02:00
/* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
if ((retval == NULL) && (argv0 != NULL))
{
/* fast path: default behaviour can handle this. */
if (strchr(argv0, '/') != NULL)
return NULL; /* higher level parses out real path from argv0. */
/* If there's no dirsep on argv0, then look through $PATH for it. */
2012-03-24 05:26:04 +01:00
envr = getenv("PATH");
if (envr != NULL)
{
char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
2012-03-24 05:26:04 +01:00
strcpy(path, envr);
retval = findBinaryInPath(argv0, path);
__PHYSFS_smallFree(path);
} /* if */
} /* 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.
*
2016-02-25 08:51:28 +01:00
* https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
2012-03-22 04:30:50 +01:00
*/
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_ERRPASS(!envr, NULL); /* oh well. */
2012-03-22 04:30:50 +01:00
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(!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 */
#endif /* PHYSFS_PLATFORM_UNIX */
2001-07-07 10:24:47 +02:00
/* end of unix.c ... */