platform/unix.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 22 Mar 2010 11:16:12 -0400
branchstable-2.0
changeset 1078 da660d3cc67c
parent 1061 8b05539d6660
child 1294 184697ee7a77
permissions -rw-r--r--
Backported extra CD/DVD filesystem checks to stable-2.0.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Unix support routines for PhysicsFS.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    10
#include "physfs_platforms.h"
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    11
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    12
#ifdef PHYSFS_PLATFORM_UNIX
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 273
diff changeset
    13
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <stdio.h>
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <stdlib.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    16
#include <string.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    17
#include <ctype.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    18
#include <unistd.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    19
#include <sys/types.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    20
#include <pwd.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    21
#include <sys/stat.h>
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
    22
#include <sys/param.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    23
#include <dirent.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    24
#include <time.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    25
#include <errno.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    26
1048
22b96d39d1a2 Catch case where we use the Unix code on a non-Unix system that lacks pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
    27
#if (!defined PHYSFS_NO_THREAD_SUPPORT)
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    28
#include <pthread.h>
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    29
#endif
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    30
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    31
#ifdef PHYSFS_HAVE_SYS_UCRED_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    32
#  ifdef PHYSFS_HAVE_MNTENT_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    33
#    undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    34
#  endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    35
#  include <sys/ucred.h>
1061
8b05539d6660 Fix for the Hurd, from the debian project.
Ryan C. Gordon <icculus@icculus.org>
parents: 1048
diff changeset
    36
#  include <sys/mount.h>
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    37
#endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    38
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    39
#ifdef PHYSFS_HAVE_MNTENT_H
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    40
#include <mntent.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    41
#endif
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    42
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#include "physfs_internal.h"
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
1061
8b05539d6660 Fix for the Hurd, from the debian project.
Ryan C. Gordon <icculus@icculus.org>
parents: 1048
diff changeset
    45
#ifndef MAXPATHLEN
8b05539d6660 Fix for the Hurd, from the debian project.
Ryan C. Gordon <icculus@icculus.org>
parents: 1048
diff changeset
    46
#define MAXPATHLEN 1024
8b05539d6660 Fix for the Hurd, from the debian project.
Ryan C. Gordon <icculus@icculus.org>
parents: 1048
diff changeset
    47
#endif
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    48
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    49
int __PHYSFS_platformInit(void)
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    50
{
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    51
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    52
} /* __PHYSFS_platformInit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    53
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    54
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    55
int __PHYSFS_platformDeinit(void)
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    56
{
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    57
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    58
} /* __PHYSFS_platformDeinit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    59
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    60
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    61
#ifdef PHYSFS_NO_CDROM_SUPPORT
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    62
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    63
/* Stub version for platforms without CD-ROM support. */
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    64
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    65
{
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    66
} /* __PHYSFS_platformDetectAvailableCDs */
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    67
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    68
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    69
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    70
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    71
{
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    72
    int i;
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    73
    struct statfs *mntbufp = NULL;
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    74
    int mounts = getmntinfo(&mntbufp, MNT_WAIT);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    75
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    76
    for (i = 0; i < mounts; i++)
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    77
    {
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    78
        int add_it = 0;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    79
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    80
        if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    81
            add_it = 1;
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    82
        else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
191
0b95b9fd25e3 Darwin patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 188
diff changeset
    83
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
    84
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
    85
        /* add other mount types here */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    86
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    87
        if (add_it)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    88
            cb(data, mntbufp[i].f_mntonname);
381
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    89
    } /* for */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    90
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    91
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    92
#elif (defined PHYSFS_HAVE_MNTENT_H)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    93
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
    94
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    96
    FILE *mounts = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    97
    struct mntent *ent = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    98
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    99
    mounts = setmntent("/etc/mtab", "r");
678
73a2641375a0 Cleaned up some minor bloat with my new evil GOTO_*_MACRO macros.
Ryan C. Gordon <icculus@icculus.org>
parents: 677
diff changeset
   100
    BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   101
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   102
    while ( (ent = getmntent(mounts)) != NULL )
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   103
    {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   104
        int add_it = 0;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   105
        if (strcmp(ent->mnt_type, "iso9660") == 0)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   106
            add_it = 1;
1078
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   107
        else if (strcmp(ent->mnt_type, "udf") == 0)
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   108
            add_it = 1;
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   109
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   110
        /* !!! FIXME: these might pick up floppy drives, right? */
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   111
        else if (strcmp(ent->mnt_type, "auto") == 0)
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   112
            add_it = 1;
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   113
        else if (strcmp(ent->mnt_type, "supermount") == 0)
da660d3cc67c Backported extra CD/DVD filesystem checks to stable-2.0.
Ryan C. Gordon <icculus@icculus.org>
parents: 1061
diff changeset
   114
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   115
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   116
        /* add other mount types here */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   117
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   118
        if (add_it)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   119
            cb(data, ent->mnt_dir);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   120
    } /* while */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   121
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   122
    endmntent(mounts);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   123
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   124
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   125
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   126
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
233
1b74a7df1ca6 Split some of the unix.c code out to posix.c for BeOS to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 225
diff changeset
   129
/* this is in posix.c ... */
1b74a7df1ca6 Split some of the unix.c code out to posix.c for BeOS to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 225
diff changeset
   130
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   131
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   132
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   133
/*
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   134
 * See where program (bin) resides in the $PATH specified by (envr).
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   135
 *  returns a copy of the first element in envr that contains it, or NULL
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   136
 *  if it doesn't exist or there were other problems. PHYSFS_SetError() is
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   137
 *  called if we have a problem.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   138
 *
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   139
 * (envr) will be scribbled over, and you are expected to allocator.Free() the
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   140
 *  return value when you're done with it.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   141
 */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   142
static char *findBinaryInPath(const char *bin, char *envr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   143
{
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   144
    size_t alloc_size = 0;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   145
    char *exe = NULL;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   146
    char *start = envr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   147
    char *ptr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   148
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   149
    BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   150
    BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   151
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   152
    do
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   153
    {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   154
        size_t size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   155
        ptr = strchr(start, ':');  /* find next $PATH separator. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   156
        if (ptr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   157
            *ptr = '\0';
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   158
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   159
        size = strlen(start) + strlen(bin) + 2;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   160
        if (size > alloc_size)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   161
        {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   162
            char *x = (char *) allocator.Realloc(exe, size);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   163
            if (x == NULL)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   164
            {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   165
                if (exe != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   166
                    allocator.Free(exe);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   167
                BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   168
            } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   169
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   170
            alloc_size = size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   171
            exe = x;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   172
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   173
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   174
        /* build full binary path... */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   175
        strcpy(exe, start);
505
c7f7f234a156 Minor fix for odd PATH entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 452
diff changeset
   176
        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   177
            strcat(exe, "/");
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   178
        strcat(exe, bin);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   179
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   180
        if (access(exe, X_OK) == 0)  /* Exists as executable? We're done. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   181
        {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   182
            strcpy(exe, start);  /* i'm lazy. piss off. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   183
            return(exe);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   184
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   185
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   186
        start = ptr + 1;  /* start points to beginning of next element. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   187
    } while (ptr != NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   188
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   189
    if (exe != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   190
        allocator.Free(exe);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   191
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   192
    return(NULL);  /* doesn't exist in path. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   193
} /* findBinaryInPath */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   194
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   195
1004
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   196
static char *readSymLink(const char *path)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   197
{
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   198
    ssize_t len = 64;
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   199
    ssize_t rc = -1;
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   200
    char *retval = NULL;
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   201
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   202
    while (1)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   203
    {
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   204
         char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   205
         if (ptr == NULL)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   206
             break;   /* out of memory. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   207
         retval = ptr;
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   208
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   209
         rc = readlink(path, retval, len);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   210
         if (rc == -1)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   211
             break;  /* not a symlink, i/o error, etc. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   212
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   213
         else if (rc < len)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   214
         {
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   215
             retval[rc] = '\0';  /* readlink doesn't null-terminate. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   216
             return retval;  /* we're good to go. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   217
         } /* else if */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   218
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   219
         len *= 2;  /* grow buffer, try again. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   220
    } /* while */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   221
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   222
    if (retval != NULL)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   223
        allocator.Free(retval);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   224
    return NULL;
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   225
} /* readSymLink */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   226
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   227
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   228
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   229
{
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   230
    char *retval = NULL;
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   231
    char *envr = NULL;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   232
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   233
    /* fast path: default behaviour can handle this. */
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   234
    if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   235
        return(NULL);  /* higher level will parse out real path from argv0. */
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   236
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   237
    /*
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   238
     * Try to avoid using argv0 unless forced to. If there's a Linux-like
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   239
     *  /proc filesystem, you can get the full path to the current process from
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   240
     *  the /proc/self/exe symlink.
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   241
     */
1004
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   242
    retval = readSymLink("/proc/self/exe");
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   243
    if (retval == NULL)
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   244
    {
1004
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   245
        /* older kernels don't have /proc/self ... try PID version... */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   246
        const unsigned long long pid = (unsigned long long) getpid();
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   247
        char path[64];
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   248
        const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   249
        if ( (rc > 0) && (rc < sizeof(path)) )
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   250
            retval = readSymLink(path);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   251
    } /* if */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   252
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   253
    if (retval != NULL)  /* chop off filename. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   254
    {
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   255
        char *ptr = strrchr(retval, '/');
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   256
        if (ptr != NULL)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   257
            *ptr = '\0';
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   258
    } /* if */
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   259
840
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   260
    if ((retval == NULL) && (argv0 != NULL))
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   261
    {
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   262
        /* If there's no dirsep on argv0, then look through $PATH for it. */
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   263
        envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   264
        BAIL_IF_MACRO(!envr, NULL, NULL);
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   265
        retval = findBinaryInPath(argv0, envr);
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   266
        allocator.Free(envr);
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   267
    } /* if */
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   268
1004
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   269
    if (retval != NULL)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   270
    {
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   271
        /* try to shrink buffer... */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   272
        char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   273
        if (ptr != NULL)
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   274
            retval = ptr;  /* oh well if it failed. */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   275
    } /* if */
cd500847dbc9 Merged 1001:b0c6f2f4f361 through 1003:a28d30d275e2 from default branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 889
diff changeset
   276
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   277
    return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   278
} /* __PHYSFS_platformCalcBaseDir */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   279
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   280
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   281
char *__PHYSFS_platformRealPath(const char *path)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   282
{
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   283
    char resolved_path[MAXPATHLEN];
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   284
    char *retval = NULL;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   285
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   286
    errno = 0;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   287
    BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   288
    retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   289
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   290
    strcpy(retval, resolved_path);
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   291
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   292
    return(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   293
} /* __PHYSFS_platformRealPath */
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   294
69
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   295
847
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   296
char *__PHYSFS_platformCurrentDir(void)
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   297
{
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   298
    /*
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   299
     * This can't just do platformRealPath("."), since that would eventually
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   300
     *  just end up calling back into here.
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   301
     */
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   302
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   303
    int allocSize = 0;
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   304
    char *retval = NULL;
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   305
    char *ptr;
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   306
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   307
    do
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   308
    {
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   309
        allocSize += 100;
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   310
        ptr = (char *) allocator.Realloc(retval, allocSize);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   311
        if (ptr == NULL)
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   312
        {
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   313
            if (retval != NULL)
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   314
                allocator.Free(retval);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   315
            BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   316
        } /* if */
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   317
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   318
        retval = ptr;
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   319
        ptr = getcwd(retval, allocSize);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   320
    } while (ptr == NULL && errno == ERANGE);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   321
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   322
    if (ptr == NULL && errno)
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   323
    {
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   324
            /*
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   325
             * getcwd() failed for some reason, for example current
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   326
             * directory not existing.
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   327
             */
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   328
        if (retval != NULL)
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   329
            allocator.Free(retval);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   330
        BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   331
    } /* if */
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   332
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   333
    return(retval);
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   334
} /* __PHYSFS_platformCurrentDir */
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   335
5e5e6c067413 Split out Mac OS X code from unix.c and added some Carbon-specific code...
Ryan C. Gordon <icculus@icculus.org>
parents: 846
diff changeset
   336
845
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   337
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   338
{
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   339
    return(0);  /* just use malloc() and friends. */
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   340
} /* __PHYSFS_platformSetDefaultAllocator */
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   341
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   342
1048
22b96d39d1a2 Catch case where we use the Unix code on a non-Unix system that lacks pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   343
#if (defined PHYSFS_NO_THREAD_SUPPORT)
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   344
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   345
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   346
void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   347
void __PHYSFS_platformDestroyMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   348
int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   349
void __PHYSFS_platformReleaseMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   350
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   351
#else
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   352
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   353
typedef struct
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   354
{
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   355
    pthread_mutex_t mutex;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   356
    pthread_t owner;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   357
    PHYSFS_uint32 count;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   358
} PthreadMutex;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   359
452
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   360
/* Just in case; this is a panic value. */
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   361
#if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   362
#  define SIZEOF_INT 4
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   363
#endif
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   364
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   365
#if (SIZEOF_INT == 4)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   366
#  define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   367
#elif (SIZEOF_INT == 2)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   368
#  define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   369
#elif (SIZEOF_INT == 1)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   370
#  define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   371
#else
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   372
#  define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   373
#endif
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   374
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   375
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   376
{
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   377
    return(PHTREAD_TO_UI64(pthread_self()));
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   378
} /* __PHYSFS_platformGetThreadID */
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   379
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   380
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   381
void *__PHYSFS_platformCreateMutex(void)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   382
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   383
    int rc;
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   384
    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   385
    BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   386
    rc = pthread_mutex_init(&m->mutex, NULL);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   387
    if (rc != 0)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   388
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   389
        allocator.Free(m);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   390
        BAIL_MACRO(strerror(rc), NULL);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   391
    } /* if */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   392
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   393
    m->count = 0;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   394
    m->owner = (pthread_t) 0xDEADBEEF;
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   395
    return((void *) m);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   396
} /* __PHYSFS_platformCreateMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   397
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   398
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   399
void __PHYSFS_platformDestroyMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   400
{
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   401
    PthreadMutex *m = (PthreadMutex *) mutex;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   402
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   403
    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   404
    if ((m->owner == pthread_self()) && (m->count > 0))
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   405
        pthread_mutex_unlock(&m->mutex);
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   406
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   407
    pthread_mutex_destroy(&m->mutex);
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   408
    allocator.Free(m);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   409
} /* __PHYSFS_platformDestroyMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   410
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   411
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   412
int __PHYSFS_platformGrabMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   413
{
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   414
    PthreadMutex *m = (PthreadMutex *) mutex;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   415
    pthread_t tid = pthread_self();
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   416
    if (m->owner != tid)
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   417
    {
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   418
        if (pthread_mutex_lock(&m->mutex) != 0)
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   419
            return(0);
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   420
        m->owner = tid;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   421
    } /* if */
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   422
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   423
    m->count++;
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   424
    return(1);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   425
} /* __PHYSFS_platformGrabMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   426
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   427
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   428
void __PHYSFS_platformReleaseMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   429
{
889
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   430
    PthreadMutex *m = (PthreadMutex *) mutex;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   431
    if (m->owner == pthread_self())
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   432
    {
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   433
        if (--m->count == 0)
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   434
        {
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   435
            m->owner = (pthread_t) 0xDEADBEEF;
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   436
            pthread_mutex_unlock(&m->mutex);
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   437
        } /* if */
0364fa566f29 Reverted change r857:858...recursive mutex APIs sort of suck for portability.
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   438
    } /* if */
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   439
} /* __PHYSFS_platformReleaseMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   440
1048
22b96d39d1a2 Catch case where we use the Unix code on a non-Unix system that lacks pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 1004
diff changeset
   441
#endif /* !PHYSFS_NO_THREAD_SUPPORT */
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   442
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   443
#endif /* PHYSFS_PLATFORM_UNIX */
233
1b74a7df1ca6 Split some of the unix.c code out to posix.c for BeOS to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 225
diff changeset
   444
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
/* end of unix.c ... */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446