platform/unix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 09 Aug 2002 19:47:06 +0000
changeset 452 89578e99ba6e
parent 396 15f7ccfdd89f
child 505 c7f7f234a156
permissions -rw-r--r--
Attempt to fix pthread-to-ui64 cast issue.
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
 *
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * Please see the file LICENSE in the source's root directory.
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
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 191
diff changeset
     9
#if HAVE_CONFIG_H
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 191
diff changeset
    10
#  include <config.h>
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 191
diff changeset
    11
#endif
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 191
diff changeset
    12
327
76e207c6b4a7 Build system corrections for BeOS and Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 316
diff changeset
    13
/* BeOS uses beos.cpp and posix.c ... Cygwin and such use win32.c ... */
76e207c6b4a7 Build system corrections for BeOS and Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 316
diff changeset
    14
#if ((!defined __BEOS__) && (!defined WIN32))
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 273
diff changeset
    15
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <stdio.h>
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#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
    18
#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
    19
#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
    20
#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
    21
#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
    22
#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
    23
#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
    24
#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
    25
#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
    26
#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
    27
#include <errno.h>
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    28
#include <sys/mount.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    29
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    30
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    31
#include <pthread.h>
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    32
#endif
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    33
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    34
#ifdef PHYSFS_HAVE_SYS_UCRED_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    35
#  ifdef PHYSFS_HAVE_MNTENT_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    36
#    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
    37
#  endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    38
#  include <sys/ucred.h>
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    39
#endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    40
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    41
#ifdef PHYSFS_HAVE_MNTENT_H
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    42
#include <mntent.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    43
#endif
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    44
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#define __PHYSICSFS_INTERNAL__
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#include "physfs_internal.h"
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
const char *__PHYSFS_platformDirSeparator = "/";
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    51
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    52
int __PHYSFS_platformInit(void)
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
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    55
} /* __PHYSFS_platformInit */
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
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    58
int __PHYSFS_platformDeinit(void)
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
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    61
} /* __PHYSFS_platformDeinit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    62
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    63
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
    64
#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
    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
/* Stub version for platforms without CD-ROM 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
    67
char **__PHYSFS_platformDetectAvailableCDs(void)
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
    68
{
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
    69
    char **retval = (char **) malloc(sizeof (char *));
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
    70
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
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
    71
    *retval = NULL;
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
    72
    return(retval);
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
} /* __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
    74
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
    75
#else
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
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    77
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    78
#ifdef PHYSFS_HAVE_SYS_UCRED_H
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    79
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    80
char **__PHYSFS_platformDetectAvailableCDs(void)
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    81
{
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    82
    char **retval = (char **) malloc(sizeof (char *));
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    83
    int cd_count = 1;  /* We count the NULL entry. */
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
    84
    struct statfs *mntbufp = NULL;
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    85
    int mounts;
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
    86
    int i;
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    87
171
0d92d6358410 Added some malloc() failure checks in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
    88
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
0d92d6358410 Added some malloc() failure checks in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
    89
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
    90
    mounts = getmntinfo(&mntbufp, MNT_WAIT);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    91
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
    92
    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
    93
    {
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    94
        int add_it = 0;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    95
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
    96
        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
    97
            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
    98
        else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
191
0b95b9fd25e3 Darwin patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 188
diff changeset
    99
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   100
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   101
        /* add other mount types here */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   102
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   103
        if (add_it)
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   104
        {
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   105
            char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   106
            if (tmp)
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   107
            {
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   108
                retval = tmp;
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   109
                retval[cd_count - 1] = (char *)
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
   110
                                malloc(strlen(mntbufp[i].f_mntonname) + 1);
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   111
                if (retval[cd_count - 1])
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   112
                {
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
   113
                    strcpy(retval[cd_count - 1], mntbufp[i].f_mntonname);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   114
                    cd_count++;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   115
                } /* if */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   116
            } /* if */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   117
        } /* if */
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
   118
    } /* for */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   119
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   120
    retval[cd_count - 1] = NULL;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   121
    return(retval);
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   122
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   123
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
   124
#endif
117
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
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
   127
#ifdef PHYSFS_HAVE_MNTENT_H
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   128
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
char **__PHYSFS_platformDetectAvailableCDs(void)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
{
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   131
    char **retval = (char **) malloc(sizeof (char *));
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   132
    int cd_count = 1;  /* We count the NULL entry. */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   133
    FILE *mounts = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   134
    struct mntent *ent = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   135
171
0d92d6358410 Added some malloc() failure checks in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   136
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
0d92d6358410 Added some malloc() failure checks in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   137
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   138
    *retval = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   139
    mounts = setmntent("/etc/mtab", "r");
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   140
    BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   141
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   142
    while ( (ent = getmntent(mounts)) != NULL )
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   143
    {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   144
        int add_it = 0;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   145
        if (strcmp(ent->mnt_type, "iso9660") == 0)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   146
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   147
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   148
        /* 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
   149
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   150
        if (add_it)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   151
        {
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   152
            char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   153
            if (tmp)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   154
            {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   155
                retval = tmp;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   156
                retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   157
                if (retval[cd_count - 1])
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   158
                {
316
29a4d12d1dc6 Fixed buffer overflow in CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   159
                    strcpy(retval[cd_count - 1], ent->mnt_dir);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   160
                    cd_count++;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   161
                } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   162
            } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   163
        } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   164
    } /* while */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   165
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   166
    endmntent(mounts);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   167
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   168
    retval[cd_count - 1] = NULL;
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   169
    return(retval);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   170
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   171
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   172
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
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
   174
#endif  /* !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
   175
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
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
   177
/* 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
   178
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
   179
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   180
225
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
 * See where program (bin) resides in the $PATH specified by (envr).
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   183
 *  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
   184
 *  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
   185
 *  called if we have a problem.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   186
 *
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   187
 * (envr) will be scribbled over, and you are expected to free() the
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   188
 *  return value when you're done with it.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   189
 */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   190
static char *findBinaryInPath(const char *bin, char *envr)
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
    size_t alloc_size = 0;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   193
    char *exe = NULL;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   194
    char *start = envr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   195
    char *ptr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   196
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   197
    BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   198
    BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   199
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   200
    do
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   201
    {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   202
        size_t size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   203
        ptr = strchr(start, ':');  /* find next $PATH separator. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   204
        if (ptr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   205
            *ptr = '\0';
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   206
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   207
        size = strlen(start) + strlen(bin) + 2;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   208
        if (size > alloc_size)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   209
        {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   210
            char *x = (char *) realloc(exe, size);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   211
            if (x == NULL)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   212
            {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   213
                if (exe != NULL)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   214
                    free(exe);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   215
                BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   216
            } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   217
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   218
            alloc_size = size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   219
            exe = x;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   220
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   221
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   222
        /* build full binary path... */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   223
        strcpy(exe, start);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   224
        if (exe[strlen(exe) - 1] != '/')
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   225
            strcat(exe, "/");
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   226
        strcat(exe, bin);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   227
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   228
        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
   229
        {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   230
            strcpy(exe, start);  /* i'm lazy. piss off. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   231
            return(exe);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   232
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   233
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   234
        start = ptr + 1;  /* start points to beginning of next element. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   235
    } while (ptr != NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   236
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   237
    if (exe != NULL)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   238
        free(exe);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   239
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   240
    return(NULL);  /* doesn't exist in path. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   241
} /* findBinaryInPath */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   242
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   243
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   244
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
   245
{
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   246
    /* If there isn't a path on argv0, then look through the $PATH for it. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   247
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   248
    char *retval;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   249
    char *envr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   250
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   251
    if (strchr(argv0, '/') != NULL)   /* default behaviour can handle this. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   252
        return(NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   253
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
   254
    envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   255
    BAIL_IF_MACRO(!envr, NULL, NULL);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   256
    retval = findBinaryInPath(argv0, envr);
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   257
    free(envr);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   258
    return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   259
} /* __PHYSFS_platformCalcBaseDir */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   260
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   261
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   262
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   263
void __PHYSFS_platformTimeslice(void)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   264
{
115
c0671785777b Changed timeslice implementation to use something more portable (thanks to
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
   265
    usleep( 10 * 1000 );           /* don't care if it fails. */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   266
} /* __PHYSFS_platformTimeslice */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   267
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   268
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   269
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
   270
{
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   271
    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
   272
    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
   273
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   274
    errno = 0;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   275
    BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
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
   276
    retval = (char *) 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
   277
    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
   278
    strcpy(retval, resolved_path);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   279
    return(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   280
} /* __PHYSFS_platformRealPath */
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   281
69
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   282
396
15f7ccfdd89f Whoops; the pthread-disabling condition was flipped. Fixed.
Ryan C. Gordon <icculus@icculus.org>
parents: 392
diff changeset
   283
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   284
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   285
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
   286
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
   287
void __PHYSFS_platformDestroyMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   288
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
   289
void __PHYSFS_platformReleaseMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   290
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   291
#else
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   292
452
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   293
/* 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
   294
#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
   295
#  define SIZEOF_INT 4
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   296
#endif
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   297
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   298
#if (SIZEOF_INT == 4)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   299
#  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
   300
#elif (SIZEOF_INT == 2)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   301
#  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
   302
#elif (SIZEOF_INT == 1)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   303
#  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
   304
#else
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   305
#  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
   306
#endif
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   307
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   308
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   309
{
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   310
    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
   311
} /* __PHYSFS_platformGetThreadID */
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   312
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   313
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   314
void *__PHYSFS_platformCreateMutex(void)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   315
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   316
    int rc;
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   317
    pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   318
    BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   319
    rc = pthread_mutex_init(m, NULL);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   320
    if (rc != 0)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   321
    {
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   322
        free(m);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   323
        BAIL_MACRO(strerror(rc), NULL);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   324
    } /* if */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   325
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   326
    return((void *) m);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   327
} /* __PHYSFS_platformCreateMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   328
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   329
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   330
void __PHYSFS_platformDestroyMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   331
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   332
    pthread_mutex_destroy((pthread_mutex_t *) mutex);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   333
    free(mutex);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   334
} /* __PHYSFS_platformDestroyMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   335
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   336
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   337
int __PHYSFS_platformGrabMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   338
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   339
    return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);    
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   340
} /* __PHYSFS_platformGrabMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   341
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   342
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   343
void __PHYSFS_platformReleaseMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   344
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   345
    pthread_mutex_unlock((pthread_mutex_t *) mutex);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   346
} /* __PHYSFS_platformReleaseMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   347
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   348
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   349
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   350
330
5ca5066cf5ff Removed extra #endif
Ryan C. Gordon <icculus@icculus.org>
parents: 327
diff changeset
   351
#endif /* !defined __BEOS__ && !defined WIN32 */
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
   352
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
/* end of unix.c ... */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354