misc/libphysfs/platform_unix.c
author unc0rr
Sun, 14 Jan 2018 23:30:42 +0100
branchqmlfrontend
changeset 12897 0088bcccd19a
parent 12218 bb5522e88ab2
permissions -rw-r--r--
Fix for freebsd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * Unix support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
#include "physfs_platforms.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
#ifdef PHYSFS_PLATFORM_UNIX
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
#include <ctype.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
#include <unistd.h>
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
    16
#include <stdlib.h>
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
#include <sys/types.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
#include <pwd.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
#include <sys/stat.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
#include <sys/param.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
#include <dirent.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
#include <time.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
#include <errno.h>
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
    24
#include <limits.h>
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
12897
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
    26
#include <sys/types.h>
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
    27
#include <sys/sysctl.h>
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
    28
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
    29
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
#if PHYSFS_PLATFORM_LINUX && !defined(PHYSFS_HAVE_MNTENT_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
#define PHYSFS_HAVE_MNTENT_H 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
#elif PHYSFS_PLATFORM_SOLARIS && !defined(PHYSFS_HAVE_SYS_MNTTAB_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
#define PHYSFS_HAVE_SYS_MNTTAB_H 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
#elif PHYSFS_PLATFORM_BSD && !defined(PHYSFS_HAVE_SYS_UCRED_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
#define PHYSFS_HAVE_SYS_UCRED_H 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
#ifdef PHYSFS_HAVE_SYS_UCRED_H
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
#  ifdef PHYSFS_HAVE_MNTENT_H
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
#    undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
#  endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
#  include <sys/mount.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
#  include <sys/ucred.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
#ifdef PHYSFS_HAVE_MNTENT_H
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
#include <mntent.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
#include <sys/mnttab.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
int __PHYSFS_platformInit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
    return 1;  /* always succeed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
} /* __PHYSFS_platformInit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
int __PHYSFS_platformDeinit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
    return 1;  /* always succeed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
} /* __PHYSFS_platformDeinit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
/* Stub version for platforms without CD-ROM support. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
#if (defined PHYSFS_NO_CDROM_SUPPORT)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    /* no-op. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
    int i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
    struct statfs *mntbufp = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
    int mounts = getmntinfo(&mntbufp, MNT_WAIT);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
    for (i = 0; i < mounts; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
        int add_it = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
        if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
        else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
        /* add other mount types here */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
        if (add_it)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
            cb(data, mntbufp[i].f_mntonname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
#elif (defined PHYSFS_HAVE_MNTENT_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
    FILE *mounts = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    struct mntent *ent = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
    mounts = setmntent("/etc/mtab", "r");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
    BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
    while ( (ent = getmntent(mounts)) != NULL )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
        int add_it = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
        if (strcmp(ent->mnt_type, "iso9660") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
        else if (strcmp(ent->mnt_type, "udf") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
        /* !!! FIXME: these might pick up floppy drives, right? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
        else if (strcmp(ent->mnt_type, "auto") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
        else if (strcmp(ent->mnt_type, "supermount") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
        /* !!! FIXME: udf? automount? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
        /* add other mount types here */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
        if (add_it)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
            cb(data, ent->mnt_dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
    endmntent(mounts);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
    FILE *mounts = fopen(MNTTAB, "r");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
    struct mnttab ent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
    BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
    while (getmntent(mounts, &ent) == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
        int add_it = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
        if (strcmp(ent.mnt_fstype, "hsfs") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
            add_it = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
        /* add other mount types here */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
        if (add_it)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
            cb(data, ent.mnt_mountp);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
    fclose(mounts);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
#error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
} /* __PHYSFS_platformDetectAvailableCDs */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
 * See where program (bin) resides in the $PATH specified by (envr).
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
 *  returns a copy of the first element in envr that contains it, or NULL
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
 *  if it doesn't exist or there were other problems. PHYSFS_SetError() is
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
 *  called if we have a problem.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
 * (envr) will be scribbled over, and you are expected to allocator.Free() the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
 *  return value when you're done with it.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
static char *findBinaryInPath(const char *bin, char *envr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
    size_t alloc_size = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
    char *exe = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
    char *start = envr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
    char *ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
    assert(bin != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
    assert(envr != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
        size_t size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
        size_t binlen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
        ptr = strchr(start, ':');  /* find next $PATH separator. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
        if (ptr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
            *ptr = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
        binlen = strlen(bin);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
        size = strlen(start) + binlen + 2;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   180
        if (size >= alloc_size)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
            char *x = (char *) allocator.Realloc(exe, size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
            if (!x)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
                if (exe != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
                    allocator.Free(exe);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
                BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
            alloc_size = size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
            exe = x;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
        /* build full binary path... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
        strcpy(exe, start);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
            strcat(exe, "/");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
        strcat(exe, bin);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
        if (access(exe, X_OK) == 0)  /* Exists as executable? We're done. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
        {
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   202
            exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
            return exe;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
        start = ptr + 1;  /* start points to beginning of next element. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
    } while (ptr != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
    if (exe != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
        allocator.Free(exe);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
    return NULL;  /* doesn't exist in path. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
} /* findBinaryInPath */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
static char *readSymLink(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
    ssize_t len = 64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
    ssize_t rc = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
    while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
         char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
         if (ptr == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
             break;   /* out of memory. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
         retval = ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
         rc = readlink(path, retval, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
         if (rc == -1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
             break;  /* not a symlink, i/o error, etc. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
         else if (rc < len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
         {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
             retval[rc] = '\0';  /* readlink doesn't null-terminate. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
             return retval;  /* we're good to go. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
         } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
         len *= 2;  /* grow buffer, try again. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
        allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
} /* readSymLink */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
    const char *envr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   253
    /* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   254
    
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   255
    #if PHYSFS_PLATFORM_FREEBSD
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   256
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   257
        char fullpath[PATH_MAX];
12897
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   258
        size_t buflen = sizeof (fullpath) - 1;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   259
        int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
12897
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   260
        if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1) {
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   261
            int pathlen = strlen(fullpath);
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   262
            fullpath[pathlen] = '/';
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   263
            fullpath[pathlen + 1] = 0;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   264
            retval = __PHYSFS_strdup(fullpath);
12897
0088bcccd19a Fix for freebsd
unc0rr
parents: 12218
diff changeset
   265
        }
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   266
    }
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   267
    #elif PHYSFS_PLATFORM_SOLARIS
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   268
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   269
        const char *path = getexecname();
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   270
        if ((path != NULL) && (path[0] == '/'))  /* must be absolute path... */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   271
            retval = __PHYSFS_strdup(path);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   272
    }
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   273
    #endif
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   274
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   275
    if (retval)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   276
        return retval;   /* already got it. */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   277
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   278
    /* If there's a Linux-like /proc filesystem, you can get the full path to
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   279
     *  the current process from a symlink in there.
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
     */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   281
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   282
    if (access("/proc", F_OK) == 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
    {
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   284
        retval = readSymLink("/proc/self/exe");
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   285
        if (!retval) retval = readSymLink("/proc/curproc/file");
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   286
        if (!retval) retval = readSymLink("/proc/curproc/exe");
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   287
        if (retval == NULL)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   288
        {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   289
            /* older kernels don't have /proc/self ... try PID version... */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   290
            const unsigned long long pid = (unsigned long long) getpid();
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   291
            char path[64];
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   292
            const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   293
            if ( (rc > 0) && (rc < sizeof(path)) )
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   294
                retval = readSymLink(path);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   295
        } /* if */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
    if (retval != NULL)  /* chop off filename. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
        char *ptr = strrchr(retval, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
        if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
            *(ptr+1) = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
        else  /* shouldn't happen, but just in case... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
            allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
            retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   310
    /* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
    if ((retval == NULL) && (argv0 != NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
        /* fast path: default behaviour can handle this. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
        if (strchr(argv0, '/') != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
            return NULL;  /* higher level parses out real path from argv0. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
        /* If there's no dirsep on argv0, then look through $PATH for it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
        envr = getenv("PATH");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
        if (envr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
            char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
            BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
            strcpy(path, envr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
            retval = findBinaryInPath(argv0, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
            __PHYSFS_smallFree(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
        /* try to shrink buffer... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
        char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
        if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
            retval = ptr;  /* oh well if it failed. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
} /* __PHYSFS_platformCalcBaseDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
     * We use XDG's base directory spec, even if you're not on Linux.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
     *  This isn't strictly correct, but the results are relatively sane
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
     *  in any case.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
     *
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 9799
diff changeset
   348
     * https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
    const char *envr = getenv("XDG_DATA_HOME");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
    const char *append = "/";
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
    size_t len = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
    if (!envr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
        /* You end up with "$HOME/.local/share/Game Name 2" */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
        envr = __PHYSFS_getUserDir();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
        BAIL_IF_MACRO(!envr, ERRPASS, NULL);  /* oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
        append = ".local/share/";
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
    len = strlen(envr) + strlen(append) + strlen(app) + 2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
    retval = (char *) allocator.Malloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
    BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
    snprintf(retval, len, "%s%s%s/", envr, append, app);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
} /* __PHYSFS_platformCalcPrefDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
    return 0;  /* just use malloc() and friends. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
} /* __PHYSFS_platformSetDefaultAllocator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
#endif /* PHYSFS_PLATFORM_UNIX */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
/* end of unix.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379