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