src/physfs_platform_unix.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 17 May 2020 01:41:52 -0400
changeset 1691 c66c9326b05d
parent 1589 e4f75163b60d
permissions -rw-r--r--
7z: Make error handling a little more robust.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Unix support routines for PhysicsFS.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    10
#include "physfs_platforms.h"
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    11
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    12
#ifdef PHYSFS_PLATFORM_UNIX
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 273
diff changeset
    13
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    14
#include <ctype.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    15
#include <unistd.h>
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
    16
#include <stdlib.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    17
#include <sys/types.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    18
#include <pwd.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    19
#include <sys/stat.h>
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
    20
#include <sys/param.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    21
#include <dirent.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    22
#include <time.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    23
#include <errno.h>
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
    24
#include <limits.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    25
1440
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    26
#if PHYSFS_NO_CDROM_SUPPORT
1448
e1e5ed1f54a1 Cygwin has mntent.h, and no need to check for CMake tests now.
Ryan C. Gordon <icculus@icculus.org>
parents: 1446
diff changeset
    27
#elif PHYSFS_PLATFORM_LINUX
e1e5ed1f54a1 Cygwin has mntent.h, and no need to check for CMake tests now.
Ryan C. Gordon <icculus@icculus.org>
parents: 1446
diff changeset
    28
#  define PHYSFS_HAVE_MNTENT_H 1
e1e5ed1f54a1 Cygwin has mntent.h, and no need to check for CMake tests now.
Ryan C. Gordon <icculus@icculus.org>
parents: 1446
diff changeset
    29
#elif defined __CYGWIN__
1440
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    30
#  define PHYSFS_HAVE_MNTENT_H 1
1448
e1e5ed1f54a1 Cygwin has mntent.h, and no need to check for CMake tests now.
Ryan C. Gordon <icculus@icculus.org>
parents: 1446
diff changeset
    31
#elif PHYSFS_PLATFORM_SOLARIS
1440
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    32
#  define PHYSFS_HAVE_SYS_MNTTAB_H 1
1448
e1e5ed1f54a1 Cygwin has mntent.h, and no need to check for CMake tests now.
Ryan C. Gordon <icculus@icculus.org>
parents: 1446
diff changeset
    33
#elif PHYSFS_PLATFORM_BSD
1440
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    34
#  define PHYSFS_HAVE_SYS_UCRED_H 1
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    35
#else
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    36
#  warning No CD-ROM support included. Either define your platform here,
874043e29d55 Simplify CMakeLists.txt, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 1427
diff changeset
    37
#  warning  or define PHYSFS_NO_CDROM_SUPPORT=1 to confirm this is intentional.
1236
ad779490c5b6 Try to make a sane default for CD detection path by platform.
Ryan C. Gordon <icculus@icculus.org>
parents: 1230
diff changeset
    38
#endif
ad779490c5b6 Try to make a sane default for CD detection path by platform.
Ryan C. Gordon <icculus@icculus.org>
parents: 1230
diff changeset
    39
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    40
#ifdef PHYSFS_HAVE_SYS_UCRED_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    41
#  ifdef PHYSFS_HAVE_MNTENT_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    42
#    undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    43
#  endif
995
de210c7e2b09 I think sys/mount.h is only needed in the ucred.h path.
Ryan C. Gordon <icculus@icculus.org>
parents: 990
diff changeset
    44
#  include <sys/mount.h>
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    45
#  include <sys/ucred.h>
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    46
#endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    47
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    48
#ifdef PHYSFS_HAVE_MNTENT_H
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    49
#include <mntent.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    50
#endif
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    51
990
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
    52
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
    53
#include <sys/mnttab.h>
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
    54
#endif
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
    55
1516
ec5acd7392a2 Some minor preprocessor nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1515
diff changeset
    56
#ifdef PHYSFS_PLATFORM_FREEBSD
1427
7986f0591f70 Fixed compiler warnings on FreeBSD.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    57
#include <sys/sysctl.h>
7986f0591f70 Fixed compiler warnings on FreeBSD.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    58
#endif
7986f0591f70 Fixed compiler warnings on FreeBSD.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    59
1589
e4f75163b60d Cleaned up some header inclusion politics.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
    60
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
#include "physfs_internal.h"
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    63
int __PHYSFS_platformInit(void)
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    64
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
    65
    return 1;  /* always succeed. */
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    66
} /* __PHYSFS_platformInit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    67
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    68
1536
c791c9aaa2a2 __PHYSFS_platformDeinit() should return void.
Ryan C. Gordon <icculus@icculus.org>
parents: 1516
diff changeset
    69
void __PHYSFS_platformDeinit(void)
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    70
{
1536
c791c9aaa2a2 __PHYSFS_platformDeinit() should return void.
Ryan C. Gordon <icculus@icculus.org>
parents: 1516
diff changeset
    71
    /* no-op */
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    72
} /* __PHYSFS_platformDeinit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    73
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    74
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    75
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    76
{
987
ae7b616e7d0e Merge all the Unix __PHYSFS_platformDetectAvailableCDs() funcs into one.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    77
#if (defined PHYSFS_NO_CDROM_SUPPORT)
ae7b616e7d0e Merge all the Unix __PHYSFS_platformDetectAvailableCDs() funcs into one.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    78
    /* no-op. */
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    79
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    80
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    81
    int i;
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    82
    struct statfs *mntbufp = NULL;
1515
6b999f011725 unix: getmntinfo() should proabably be set to MNT_NOWAIT to avoid blocking.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    83
    int mounts = getmntinfo(&mntbufp, MNT_NOWAIT);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    84
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    85
    for (i = 0; i < mounts; i++)
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    86
    {
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    87
        int add_it = 0;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    88
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    89
        if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    90
            add_it = 1;
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    91
        else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
191
0b95b9fd25e3 Darwin patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 188
diff changeset
    92
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
    93
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
    94
        /* add other mount types here */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    95
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    96
        if (add_it)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    97
            cb(data, mntbufp[i].f_mntonname);
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    98
    } /* for */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    99
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   100
#elif (defined PHYSFS_HAVE_MNTENT_H)
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   101
    FILE *mounts = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   102
    struct mntent *ent = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   103
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   104
    mounts = setmntent("/etc/mtab", "r");
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   105
    BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   106
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   107
    while ( (ent = getmntent(mounts)) != NULL )
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   108
    {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   109
        int add_it = 0;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   110
        if (strcmp(ent->mnt_type, "iso9660") == 0)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   111
            add_it = 1;
1077
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   112
        else if (strcmp(ent->mnt_type, "udf") == 0)
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   113
            add_it = 1;
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   114
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   115
        /* !!! FIXME: these might pick up floppy drives, right? */
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   116
        else if (strcmp(ent->mnt_type, "auto") == 0)
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   117
            add_it = 1;
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   118
        else if (strcmp(ent->mnt_type, "supermount") == 0)
21166622a4d4 Add some more filesystem types that might be CDs or DVDs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1062
diff changeset
   119
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   120
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   121
        /* add other mount types here */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   122
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   123
        if (add_it)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   124
            cb(data, ent->mnt_dir);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   125
    } /* while */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   126
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   127
    endmntent(mounts);
990
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   128
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   129
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   130
    FILE *mounts = fopen(MNTTAB, "r");
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   131
    struct mnttab ent;
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   132
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   133
    BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
990
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   134
    while (getmntent(mounts, &ent) == 0)
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   135
    {
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   136
        int add_it = 0;
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   137
        if (strcmp(ent.mnt_fstype, "hsfs") == 0)
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   138
            add_it = 1;
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   139
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   140
        /* add other mount types here */
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   141
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   142
        if (add_it)
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   143
            cb(data, ent.mnt_mountp);
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   144
    } /* while */
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   145
c25717b5c165 Added sys/mnttab.h CD-ROM detection. Fixes missing CD-ROM support on Solaris.
Ryan C. Gordon <icculus@icculus.org>
parents: 987
diff changeset
   146
    fclose(mounts);
987
ae7b616e7d0e Merge all the Unix __PHYSFS_platformDetectAvailableCDs() funcs into one.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   147
#endif
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   148
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   149
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   151
/*
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   152
 * See where program (bin) resides in the $PATH specified by (envr).
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   153
 *  returns a copy of the first element in envr that contains it, or NULL
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   154
 *  if it doesn't exist or there were other problems. PHYSFS_SetError() is
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   155
 *  called if we have a problem.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   156
 *
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   157
 * (envr) will be scribbled over, and you are expected to allocator.Free() the
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   158
 *  return value when you're done with it.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   159
 */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   160
static char *findBinaryInPath(const char *bin, char *envr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   161
{
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   162
    size_t alloc_size = 0;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   163
    char *exe = NULL;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   164
    char *start = envr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   165
    char *ptr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   166
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1236
diff changeset
   167
    assert(bin != NULL);
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1236
diff changeset
   168
    assert(envr != NULL);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   169
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   170
    do
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   171
    {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   172
        size_t size;
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   173
        size_t binlen;
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   174
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   175
        ptr = strchr(start, ':');  /* find next $PATH separator. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   176
        if (ptr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   177
            *ptr = '\0';
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   178
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   179
        binlen = strlen(bin);
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   180
        size = strlen(start) + binlen + 2;
1354
3e80839670da Make static analysis happy.
Ryan C. Gordon <icculus@icculus.org>
parents: 1337
diff changeset
   181
        if (size >= alloc_size)
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   182
        {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   183
            char *x = (char *) allocator.Realloc(exe, size);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1236
diff changeset
   184
            if (!x)
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   185
            {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   186
                if (exe != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   187
                    allocator.Free(exe);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   188
                BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   189
            } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   190
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   191
            alloc_size = size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   192
            exe = x;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   193
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   194
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   195
        /* build full binary path... */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   196
        strcpy(exe, start);
505
c7f7f234a156 Minor fix for odd PATH entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 452
diff changeset
   197
        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   198
            strcat(exe, "/");
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   199
        strcat(exe, bin);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   200
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   201
        if (access(exe, X_OK) == 0)  /* Exists as executable? We're done. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   202
        {
1333
baecd71ae0e7 Fixed off-by-one error in findBinaryInPath() (thanks, Dmitry!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1266
diff changeset
   203
            exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   204
            return exe;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   205
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   206
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   207
        start = ptr + 1;  /* start points to beginning of next element. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   208
    } while (ptr != NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   209
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   210
    if (exe != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   211
        allocator.Free(exe);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   212
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   213
    return NULL;  /* doesn't exist in path. */
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   214
} /* findBinaryInPath */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   215
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   216
1001
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   217
static char *readSymLink(const char *path)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   218
{
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   219
    ssize_t len = 64;
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   220
    ssize_t rc = -1;
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   221
    char *retval = NULL;
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   222
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   223
    while (1)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   224
    {
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   225
         char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   226
         if (ptr == NULL)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   227
             break;   /* out of memory. */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   228
         retval = ptr;
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   229
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   230
         rc = readlink(path, retval, len);
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   231
         if (rc == -1)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   232
             break;  /* not a symlink, i/o error, etc. */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   233
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   234
         else if (rc < len)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   235
         {
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   236
             retval[rc] = '\0';  /* readlink doesn't null-terminate. */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   237
             return retval;  /* we're good to go. */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   238
         } /* else if */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   239
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   240
         len *= 2;  /* grow buffer, try again. */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   241
    } /* while */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   242
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   243
    if (retval != NULL)
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   244
        allocator.Free(retval);
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   245
    return NULL;
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   246
} /* readSymLink */
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   247
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   248
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   249
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   250
{
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   251
    char *retval = NULL;
1266
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   252
    const char *envr = NULL;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   253
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   254
    /* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
1516
ec5acd7392a2 Some minor preprocessor nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1515
diff changeset
   255
ec5acd7392a2 Some minor preprocessor nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1515
diff changeset
   256
    #if defined(PHYSFS_PLATFORM_FREEBSD)
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   257
    {
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   258
        char fullpath[PATH_MAX];
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   259
        size_t buflen = sizeof (fullpath);
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   260
        int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   261
        if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1)
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   262
            retval = __PHYSFS_strdup(fullpath);
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   263
    }
1516
ec5acd7392a2 Some minor preprocessor nitpicks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1515
diff changeset
   264
    #elif defined(PHYSFS_PLATFORM_SOLARIS)
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   265
    {
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   266
        const char *path = getexecname();
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   267
        if ((path != NULL) && (path[0] == '/'))  /* must be absolute path... */
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   268
            retval = __PHYSFS_strdup(path);
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   269
    }
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   270
    #endif
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   271
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   272
    /* If there's a Linux-like /proc filesystem, you can get the full path to
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   273
     *  the current process from a symlink in there.
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   274
     */
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   275
1452
9f32f6773924 unix: OS-specific paths should also chop filename off base path.
Ryan C. Gordon <icculus@icculus.org>
parents: 1448
diff changeset
   276
    if (!retval && (access("/proc", F_OK) == 0))
1003
a28d30d275e2 Try /proc/$PID/exe if /proc/self/exe doesn't work out.
Ryan C. Gordon <icculus@icculus.org>
parents: 1002
diff changeset
   277
    {
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   278
        retval = readSymLink("/proc/self/exe");
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   279
        if (!retval) retval = readSymLink("/proc/curproc/file");
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   280
        if (!retval) retval = readSymLink("/proc/curproc/exe");
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   281
        if (retval == NULL)
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   282
        {
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   283
            /* older kernels don't have /proc/self ... try PID version... */
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   284
            const unsigned long long pid = (unsigned long long) getpid();
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   285
            char path[64];
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   286
            const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   287
            if ( (rc > 0) && (rc < sizeof(path)) )
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   288
                retval = readSymLink(path);
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   289
        } /* if */
1003
a28d30d275e2 Try /proc/$PID/exe if /proc/self/exe doesn't work out.
Ryan C. Gordon <icculus@icculus.org>
parents: 1002
diff changeset
   290
    } /* if */
a28d30d275e2 Try /proc/$PID/exe if /proc/self/exe doesn't work out.
Ryan C. Gordon <icculus@icculus.org>
parents: 1002
diff changeset
   291
1001
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   292
    if (retval != NULL)  /* chop off filename. */
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   293
    {
1001
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   294
        char *ptr = strrchr(retval, '/');
b0c6f2f4f361 Fixed wrong readlink() usage (lstat() doesn't report dest link size!).
Ryan C. Gordon <icculus@icculus.org>
parents: 995
diff changeset
   295
        if (ptr != NULL)
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   296
            *(ptr+1) = '\0';
1266
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   297
        else  /* shouldn't happen, but just in case... */
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   298
        {
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   299
            allocator.Free(retval);
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   300
            retval = NULL;
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   301
        } /* else */
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   302
    } /* if */
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   303
1337
3b2e649c044c Better basedir detection for various Unix platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1333
diff changeset
   304
    /* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   305
    if ((retval == NULL) && (argv0 != NULL))
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   306
    {
1265
d4098b3aa37d Favor /proc/self/exe over argv[0] on Linux, to match other platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   307
        /* fast path: default behaviour can handle this. */
d4098b3aa37d Favor /proc/self/exe over argv[0] on Linux, to match other platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   308
        if (strchr(argv0, '/') != NULL)
d4098b3aa37d Favor /proc/self/exe over argv[0] on Linux, to match other platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   309
            return NULL;  /* higher level parses out real path from argv0. */
d4098b3aa37d Favor /proc/self/exe over argv[0] on Linux, to match other platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   310
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   311
        /* If there's no dirsep on argv0, then look through $PATH for it. */
1266
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   312
        envr = getenv("PATH");
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   313
        if (envr != NULL)
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   314
        {
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   315
            char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   316
            BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1266
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   317
            strcpy(path, envr);
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   318
            retval = findBinaryInPath(argv0, path);
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   319
            __PHYSFS_smallFree(path);
135cb99a06a0 More FIXME cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1265
diff changeset
   320
        } /* if */
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   321
    } /* if */
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   322
1002
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   323
    if (retval != NULL)
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   324
    {
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   325
        /* try to shrink buffer... */
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   326
        char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   327
        if (ptr != NULL)
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   328
            retval = ptr;  /* oh well if it failed. */
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   329
    } /* if */
001380a457a0 Try to shrink returned buffer's allocation.
Ryan C. Gordon <icculus@icculus.org>
parents: 1001
diff changeset
   330
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   331
    return retval;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   332
} /* __PHYSFS_platformCalcBaseDir */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   333
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   334
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   335
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   336
{
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   337
    /*
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   338
     * We use XDG's base directory spec, even if you're not on Linux.
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   339
     *  This isn't strictly correct, but the results are relatively sane
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   340
     *  in any case.
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   341
     *
1373
527ef3c6a2d6 HTTPS all the things.
Ryan C. Gordon <icculus@icculus.org>
parents: 1354
diff changeset
   342
     * https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   343
     */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   344
    const char *envr = getenv("XDG_DATA_HOME");
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   345
    const char *append = "/";
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   346
    char *retval = NULL;
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   347
    size_t len = 0;
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   348
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   349
    if (!envr)
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   350
    {
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   351
        /* You end up with "$HOME/.local/share/Game Name 2" */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   352
        envr = __PHYSFS_getUserDir();
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   353
        BAIL_IF_ERRPASS(!envr, NULL);  /* oh well. */
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   354
        append = ".local/share/";
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   355
    } /* if */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   356
1246
4fa9fe3e0b52 Make sure the prefdir has a final dirsep on it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1242
diff changeset
   357
    len = strlen(envr) + strlen(append) + strlen(app) + 2;
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   358
    retval = (char *) allocator.Malloc(len);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
   359
    BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1246
4fa9fe3e0b52 Make sure the prefdir has a final dirsep on it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1242
diff changeset
   360
    snprintf(retval, len, "%s%s%s/", envr, append, app);
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   361
    return retval;
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   362
} /* __PHYSFS_platformCalcPrefDir */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   363
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   364
#endif /* PHYSFS_PLATFORM_UNIX */
233
1b74a7df1ca6 Split some of the unix.c code out to posix.c for BeOS to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 225
diff changeset
   365
1507
8953b8a171e7 Update filename comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1506
diff changeset
   366
/* end of physfs_platform_unix.c ... */
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367