#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_UNIX
#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>
#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
#ifdef PHYSFS_HAVE_SYS_UCRED_H
# ifdef PHYSFS_HAVE_MNTENT_H
# undef PHYSFS_HAVE_MNTENT_H
# endif
# include <sys/mount.h>
# include <sys/ucred.h>
#endif
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#endif
#ifdef PHYSFS_PLATFORM_FREEBSD
#include <sys/sysctl.h>
#endif
#include "physfs_internal.h"
int __PHYSFS_platformInit(void)
{
return 1;
}
void __PHYSFS_platformDeinit(void)
{
}
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
#if (defined PHYSFS_NO_CDROM_SUPPORT)
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
int i;
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_NOWAIT);
for (i = 0; i < mounts; i++)
{
int add_it = 0;
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
add_it = 1;
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
add_it = 1;
if (add_it)
cb(data, mntbufp[i].f_mntonname);
}
#elif (defined PHYSFS_HAVE_MNTENT_H)
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, );
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;
else if (strcmp(ent->mnt_type, "auto") == 0)
add_it = 1;
else if (strcmp(ent->mnt_type, "supermount") == 0)
add_it = 1;
if (add_it)
cb(data, ent->mnt_dir);
}
endmntent(mounts);
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
FILE *mounts = fopen(MNTTAB, "r");
struct mnttab ent;
BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, );
while (getmntent(mounts, &ent) == 0)
{
int add_it = 0;
if (strcmp(ent.mnt_fstype, "hsfs") == 0)
add_it = 1;
if (add_it)
cb(data, ent.mnt_mountp);
}
fclose(mounts);
#endif
}
static char *findBinaryInPath(const char *bin, char *envr)
{
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
char *ptr;
assert(bin != NULL);
assert(envr != NULL);
do
{
size_t size;
size_t binlen;
ptr = strchr(start, ':');
if (ptr)
*ptr = '\0';
binlen = strlen(bin);
size = strlen(start) + binlen + 2;
if (size >= alloc_size)
{
char *x = (char *) allocator.Realloc(exe, size);
if (!x)
{
if (exe != NULL)
allocator.Free(exe);
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
}
alloc_size = size;
exe = x;
}
strcpy(exe, start);
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
strcat(exe, "/");
strcat(exe, bin);
if (access(exe, X_OK) == 0)
{
exe[(size - binlen) - 1] = '\0';
return exe;
}
start = ptr + 1;
} while (ptr != NULL);
if (exe != NULL)
allocator.Free(exe);
return NULL;
}
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;
retval = ptr;
rc = readlink(path, retval, len);
if (rc == -1)
break;
else if (rc < len)
{
retval[rc] = '\0';
return retval;
}
len *= 2;
}
if (retval != NULL)
allocator.Free(retval);
return NULL;
}
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
char *retval = NULL;
const char *envr = NULL;
#if defined(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 defined(PHYSFS_PLATFORM_SOLARIS)
{
const char *path = getexecname();
if ((path != NULL) && (path[0] == '/'))
retval = __PHYSFS_strdup(path);
}
#endif
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)
{
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 (retval != NULL)
{
char *ptr = strrchr(retval, '/');
if (ptr != NULL)
*(ptr+1) = '\0';
else
{
allocator.Free(retval);
retval = NULL;
}
}
if ((retval == NULL) && (argv0 != NULL))
{
if (strchr(argv0, '/') != NULL)
return NULL;
envr = getenv("PATH");
if (envr != NULL)
{
char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
strcpy(path, envr);
retval = findBinaryInPath(argv0, path);
__PHYSFS_smallFree(path);
}
}
if (retval != NULL)
{
char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr;
}
return retval;
}
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
const char *envr = getenv("XDG_DATA_HOME");
const char *append = "/";
char *retval = NULL;
size_t len = 0;
if (!envr)
{
envr = __PHYSFS_getUserDir();
BAIL_IF_ERRPASS(!envr, NULL);
append = ".local/share/";
}
len = strlen(envr) + strlen(append) + strlen(app) + 2;
retval = (char *) allocator.Malloc(len);
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
snprintf(retval, len, "%s%s%s/", envr, append, app);
return retval;
}
#endif