platform/unix.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 19 Mar 2007 20:15:09 +0000
changeset 840 7f071b3b121e
parent 823 acc29c0049ea
child 844 68272bd212a5
permissions -rw-r--r--
Try to use /proc/self/exe to determine the base dir. This lets systems with a Linux-like /proc filesystem work with PHYSFS_init(NULL). Other cleanups in Unix basedir calculation.
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>
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    26
#include <sys/mount.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    27
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
    28
#ifdef PHYSFS_PLATFORM_MACOSX
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    29
#  include <CoreFoundation/CoreFoundation.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
    30
#  include <CoreServices/CoreServices.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
    31
#  include <IOKit/IOKitLib.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
    32
#  include <IOKit/storage/IOMedia.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
    33
#  include <IOKit/storage/IOCDMedia.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
    34
#  include <IOKit/storage/IODVDMedia.h>
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    35
#endif
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    36
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    37
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    38
#include <pthread.h>
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    39
#endif
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
    40
351
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    41
#ifdef PHYSFS_HAVE_SYS_UCRED_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    42
#  ifdef PHYSFS_HAVE_MNTENT_H
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    43
#    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
    44
#  endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    45
#  include <sys/ucred.h>
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    46
#endif
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    47
bc4fa7896225 Generalized CD-ROM routine selection.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
    48
#ifdef PHYSFS_HAVE_MNTENT_H
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    49
#include <mntent.h>
188
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    50
#endif
41bf2cb44d12 Darwin fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 171
diff changeset
    51
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#include "physfs_internal.h"
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    54
/* Seems to get defined in some system header... */
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    55
#ifdef Free
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    56
#undef Free
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
    57
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
const char *__PHYSFS_platformDirSeparator = "/";
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
    61
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    62
int __PHYSFS_platformInit(void)
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    63
{
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    64
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    65
} /* __PHYSFS_platformInit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    66
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    67
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    68
int __PHYSFS_platformDeinit(void)
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    69
{
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    70
    return(1);  /* always succeed. */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    71
} /* __PHYSFS_platformDeinit */
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    72
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
    73
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
    74
#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
    75
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
/* 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
    77
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
    78
{
b65d27fdc8a9 Can now disable CD-ROM code on Unix systems at build time if need be. The
Ryan C. Gordon <icculus@icculus.org>
parents: 351
diff changeset
    79
} /* __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
    80
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    81
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
    82
#elif (defined PHYSFS_PLATFORM_MACOSX)  /* "Big Nasty." */
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    83
/*
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    84
 * Code based on sample from Apple Developer Connection:
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    85
 *  http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    86
 */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    87
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    88
static int darwinIsWholeMedia(io_service_t service)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    89
{
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    90
    int retval = 0;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    91
    CFTypeRef wholeMedia;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    92
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    93
    if (!IOObjectConformsTo(service, kIOMediaClass))
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    94
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    95
        
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    96
    wholeMedia = IORegistryEntryCreateCFProperty(service,
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    97
                                                 CFSTR(kIOMediaWholeKey),
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    98
                                                 kCFAllocatorDefault, 0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
    99
    if (wholeMedia == NULL)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   100
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   101
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   102
    retval = CFBooleanGetValue(wholeMedia);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   103
    CFRelease(wholeMedia);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   104
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   105
    return retval;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   106
} /* darwinIsWholeMedia */
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
   107
130
2deec3eb7430 Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
   108
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   109
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   110
{
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   111
    int retval = 0;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   112
    CFMutableDictionaryRef matchingDict;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   113
    kern_return_t rc;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   114
    io_iterator_t iter;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   115
    io_service_t service;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   116
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   117
    if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   118
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   119
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   120
    rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   121
    if ((rc != KERN_SUCCESS) || (!iter))
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   122
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   123
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   124
    service = IOIteratorNext(iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   125
    IOObjectRelease(iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   126
    if (!service)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   127
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   128
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   129
    rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   130
             kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   131
    
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   132
    if (!iter)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   133
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   134
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   135
    if (rc != KERN_SUCCESS)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   136
    {
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   137
        IOObjectRelease(iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   138
        return(0);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   139
    } /* if */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   140
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   141
    IOObjectRetain(service);  /* add an extra object reference... */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   142
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   143
    do
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   144
    {
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   145
        if (darwinIsWholeMedia(service))
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   146
        {
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   147
            if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   148
                 (IOObjectConformsTo(service, kIODVDMediaClass)) )
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   149
            {
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   150
                retval = 1;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   151
            } /* if */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   152
        } /* if */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   153
        IOObjectRelease(service);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   154
    } while ((service = IOIteratorNext(iter)) && (!retval));
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   155
                
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   156
    IOObjectRelease(iter);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   157
    IOObjectRelease(service);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   158
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   159
    return(retval);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   160
} /* darwinIsMountedDisc */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   161
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   162
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   163
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   164
{
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   165
    const char *devPrefix = "/dev/";
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   166
    int prefixLen = strlen(devPrefix);
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   167
    mach_port_t masterPort = 0;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   168
    struct statfs *mntbufp;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   169
    int i, mounts;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   170
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   171
    if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS)
677
c8e2615d6173 Fixed unix.c to compile again on MacOS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   172
        BAIL_MACRO(ERR_OS_ERROR, /*return void*/);
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   173
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   174
    mounts = getmntinfo(&mntbufp, MNT_WAIT);  /* NOT THREAD SAFE! */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   175
    for (i = 0; i < mounts; i++)
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   176
    {
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   177
        char *dev = mntbufp[i].f_mntfromname;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   178
        char *mnt = mntbufp[i].f_mntonname;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   179
        if (strncmp(dev, devPrefix, prefixLen) != 0)  /* a virtual device? */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   180
            continue;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   181
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   182
        dev += prefixLen;
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   183
        if (darwinIsMountedDisc(dev, masterPort))
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   184
            cb(data, mnt);
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   185
    } /* for */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   186
} /* __PHYSFS_platformDetectAvailableCDs */
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   187
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   188
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   189
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   190
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
   191
{
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   192
    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
   193
    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
   194
    int mounts = getmntinfo(&mntbufp, MNT_WAIT);
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   195
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
   196
    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
   197
    {
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   198
        int add_it = 0;
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   199
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
   200
        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
   201
            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
   202
        else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
191
0b95b9fd25e3 Darwin patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 188
diff changeset
   203
            add_it = 1;
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   204
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   205
        /* add other mount types here */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   206
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   207
        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
   208
            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
   209
    } /* for */
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   210
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   211
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   212
#elif (defined PHYSFS_HAVE_MNTENT_H)
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   213
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 570
diff changeset
   214
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
{
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   216
    FILE *mounts = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   217
    struct mntent *ent = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   218
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   219
    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
   220
    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
   221
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   222
    while ( (ent = getmntent(mounts)) != NULL )
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   223
    {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   224
        int add_it = 0;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   225
        if (strcmp(ent->mnt_type, "iso9660") == 0)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   226
            add_it = 1;
225
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
        /* 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
   229
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   230
        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
   231
            cb(data, ent->mnt_dir);
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   232
    } /* while */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   233
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   234
    endmntent(mounts);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
   235
117
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   236
} /* __PHYSFS_platformDetectAvailableCDs */
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   237
55ae10d7f2a9 Darwin support by Patrick Stein.
Ryan C. Gordon <icculus@icculus.org>
parents: 115
diff changeset
   238
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
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
   241
/* 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
   242
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
   243
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   244
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   245
/*
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   246
 * See where program (bin) resides in the $PATH specified by (envr).
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   247
 *  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
   248
 *  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
   249
 *  called if we have a problem.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   250
 *
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   251
 * (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
   252
 *  return value when you're done with it.
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   253
 */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   254
static char *findBinaryInPath(const char *bin, char *envr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   255
{
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   256
    size_t alloc_size = 0;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   257
    char *exe = NULL;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   258
    char *start = envr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   259
    char *ptr;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   260
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   261
    BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   262
    BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   263
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   264
    do
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   265
    {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   266
        size_t size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   267
        ptr = strchr(start, ':');  /* find next $PATH separator. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   268
        if (ptr)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   269
            *ptr = '\0';
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   270
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   271
        size = strlen(start) + strlen(bin) + 2;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   272
        if (size > alloc_size)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   273
        {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   274
            char *x = (char *) allocator.Realloc(exe, size);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   275
            if (x == NULL)
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   276
            {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   277
                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
   278
                    allocator.Free(exe);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   279
                BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   280
            } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   281
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   282
            alloc_size = size;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   283
            exe = x;
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   284
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   285
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   286
        /* build full binary path... */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   287
        strcpy(exe, start);
505
c7f7f234a156 Minor fix for odd PATH entries.
Ryan C. Gordon <icculus@icculus.org>
parents: 452
diff changeset
   288
        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   289
            strcat(exe, "/");
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   290
        strcat(exe, bin);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   291
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   292
        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
   293
        {
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   294
            strcpy(exe, start);  /* i'm lazy. piss off. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   295
            return(exe);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   296
        } /* if */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   297
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   298
        start = ptr + 1;  /* start points to beginning of next element. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   299
    } while (ptr != NULL);
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   300
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   301
    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
   302
        allocator.Free(exe);
225
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   303
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   304
    return(NULL);  /* doesn't exist in path. */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   305
} /* findBinaryInPath */
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   306
393c0923c1b3 FIXME removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   307
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   308
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
   309
{
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
   310
    const char *PROC_SELF_EXE = "/proc/self/exe";
7f071b3b121e Try to use /proc/self/exe to determine the base dir. This lets systems with a
Ryan C. Gordon <icculus@icculus.org>
parents: 823
diff changeset
   311
    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
   312
    char *envr = 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
   313
    struct stat stbuf;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   314
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
   315
    /* 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
   316
    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
   317
        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
   318
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
   319
    /*
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
   320
     * 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
   321
     *  /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
   322
     *  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
   323
     */
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
   324
    if ((lstat(PROC_SELF_EXE, &stbuf) != -1) && (S_ISLNK(stbuf.st_mode)))
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
   325
    {
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
   326
        const size_t len = stbuf.st_size;
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
   327
        char *buf = (char *) allocator.Malloc(len+1);
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
   328
        if (buf != NULL)  /* if NULL, maybe you'll get lucky later. */
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
   329
        {
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
   330
            if (readlink(PROC_SELF_EXE, buf, len) != len)
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
   331
                allocator.Free(buf);
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
   332
            else
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
   333
            {
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
   334
                buf[len] = '\0';  /* readlink doesn't null-terminate. */
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
   335
                retval = buf;  /* we're good to go. */
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
   336
            } /* else */
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
   337
        } /* 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
   338
    } /* if */
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   339
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
   340
    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
   341
    {
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
   342
        /* 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
   343
        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
   344
        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
   345
        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
   346
        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
   347
    } /* 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
   348
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   349
    return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   350
} /* __PHYSFS_platformCalcBaseDir */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   351
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   352
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   353
/* 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
   354
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
   355
{
115
c0671785777b Changed timeslice implementation to use something more portable (thanks to
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
   356
    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
   357
} /* __PHYSFS_platformTimeslice */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   358
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   359
822
0c874a7198a4 Patched to compile on Mac OS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 818
diff changeset
   360
#ifdef PHYSFS_PLATFORM_MACOSX
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   361
/* 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   362
 * This function is only for OSX. The problem is that Apple's applications
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   363
 * can actually be directory structures with the actual executable nested
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   364
 * several levels down. PhysFS computes the base directory from the Unix
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   365
 * executable, but this may not be the correct directory. Apple tries to
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   366
 * hide everything from the user, so from Finder, the user never sees the
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   367
 * Unix executable, and the directory package (bundle) is considered the
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   368
 * "executable". This means that the correct base directory is at the 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   369
 * level where the directory structure starts.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   370
 * A typical bundle seems to look like this:
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   371
 * MyApp.app/    <-- top level...this is what the user sees in Finder 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   372
 *   Contents/
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   373
 *     MacOS/
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   374
 *       MyApp   <-- the actual executable
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   375
 *       
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   376
 * Since anything below the app folder is considered hidden, most 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   377
 * application files need to be at the top level if you intend to
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   378
 * write portable software. Thus if the application resides in:
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   379
 * /Applications/MyProgram
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   380
 * and the executable is the bundle MyApp.app,
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   381
 * PhysFS computes the following as the base directory:
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   382
 * /Applications/MyProgram/MyApp.app/Contents/MacOS/
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   383
 * We need to strip off the MyApp.app/Contents/MacOS/
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   384
 * 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   385
 * However, there are corner cases. OSX applications can be traditional
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   386
 * Unix executables without the bundle. Also, it is not entirely clear
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   387
 * to me what kinds of permutations bundle structures can have.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   388
 * 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   389
 * For now, this is a temporary hack until a better solution 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   390
 * can be made. This function will try to find a "/Contents/MacOS"
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   391
 * inside the path. If it succeeds, then the path will be truncated
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   392
 * to correct the directory. If it is not found, the path will be 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   393
 * left alone and will presume it is a traditional Unix execuatable.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   394
 * Most programs also include the .app extention in the top level
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   395
 * folder, but it doesn't seem to be a requirement (Acrobat doesn't 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   396
 * have it). MacOS looks like it can also be MacOSClassic. 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   397
 * This function will test for MacOS and hope it captures any
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   398
 * other permutations.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   399
 */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   400
static void stripAppleBundle(char *path)
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   401
{
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   402
    char *sub_str = "/contents/macos";
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   403
    char *found_ptr = NULL;
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   404
    char *tempbuf = 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
   405
    size_t len = strlen(path) + 1;
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   406
    int i;
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   407
    
570
b331437b9ec0 Rewrote MacOSX/Darwin CD detection to use IOKit, for accurate results.
Ryan C. Gordon <icculus@icculus.org>
parents: 520
diff changeset
   408
    /* !!! FIXME: Can we stack-allocate this? --ryan. */
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   409
    tempbuf = (char *) allocator.Malloc(len);
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   410
    if (!tempbuf) return;
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   411
    memset(tempbuf, '\0', len);
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   412
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   413
    /* Unlike other Unix filesystems, HFS is case insensitive
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   414
     * It wouldn be nice to use strcasestr, but it doesn't seem
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   415
     * to be available in the OSX gcc library right now.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   416
     * So we should make a lower case copy of the path to 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   417
     * compare against 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   418
     */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   419
    for(i=0; i<strlen(path); i++)
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   420
    {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   421
        /* convert to lower case */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   422
        tempbuf[i] = tolower(path[i]);
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   423
    }
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   424
    /* See if we can find "/contents/macos" in the path */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   425
    found_ptr = strstr(tempbuf, sub_str);
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   426
    if(NULL == found_ptr)
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   427
    {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   428
        /* It doesn't look like a bundle so we can keep the 
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   429
         * original path. Just return */
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   430
        allocator.Free(tempbuf);
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   431
        return;
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   432
    }
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   433
    /* We have a bundle, so let's backstep character by character
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   434
     * to erase the extra parts of the path. Quit when we hit
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   435
     * the preceding '/' character.
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   436
     */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   437
    for(i=strlen(path)-strlen(found_ptr)-1; i>=0; i--)
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   438
    {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   439
        if('/' == path[i])
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   440
        {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   441
            break;
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   442
        }
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   443
    }
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   444
    /* Safety check */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   445
    if(i<1)
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   446
    {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   447
        /* This probably shouldn't happen. */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   448
        path[0] = '\0';
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   449
    }
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   450
    else
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   451
    {
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   452
        /* Back up one more to remove trailing '/' and set the '\0' */
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   453
        path[i] = '\0';
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   454
    }
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   455
    allocator.Free(tempbuf);
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   456
    return;
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   457
}
823
acc29c0049ea Minor comment removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 822
diff changeset
   458
#endif
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   459
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   460
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   461
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
   462
{
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   463
    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
   464
    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
   465
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   466
    errno = 0;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   467
    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
   468
    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
   469
    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
   470
    strcpy(retval, resolved_path);
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   471
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
   472
/* !!! FIXME: this shouldn't be here. */
822
0c874a7198a4 Patched to compile on Mac OS X.
Ryan C. Gordon <icculus@icculus.org>
parents: 818
diff changeset
   473
#ifdef PHYSFS_PLATFORM_MACOSX
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   474
    stripAppleBundle(retval);
823
acc29c0049ea Minor comment removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 822
diff changeset
   475
#endif
520
a72bbf255074 Added hack for OSX bundles, from Eric Wing.
Ryan C. Gordon <icculus@icculus.org>
parents: 505
diff changeset
   476
    
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   477
    return(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   478
} /* __PHYSFS_platformRealPath */
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   479
69
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   480
396
15f7ccfdd89f Whoops; the pthread-disabling condition was flipped. Fixed.
Ryan C. Gordon <icculus@icculus.org>
parents: 392
diff changeset
   481
#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
   482
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   483
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
   484
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
   485
void __PHYSFS_platformDestroyMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   486
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
   487
void __PHYSFS_platformReleaseMutex(void *mutex) {}
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   488
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   489
#else
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   490
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   491
typedef struct
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   492
{
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   493
    pthread_mutex_t mutex;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   494
    pthread_t owner;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   495
    PHYSFS_uint32 count;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   496
} PthreadMutex;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   497
452
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   498
/* 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
   499
#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
   500
#  define SIZEOF_INT 4
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   501
#endif
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   502
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   503
#if (SIZEOF_INT == 4)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   504
#  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
   505
#elif (SIZEOF_INT == 2)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   506
#  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
   507
#elif (SIZEOF_INT == 1)
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   508
#  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
   509
#else
89578e99ba6e Attempt to fix pthread-to-ui64 cast issue.
Ryan C. Gordon <icculus@icculus.org>
parents: 396
diff changeset
   510
#  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
   511
#endif
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   512
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   513
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   514
{
392
bcc8e4dca868 First step towards getting __PHYSFS_platformGetThreadID() to work universally.
Ryan C. Gordon <icculus@icculus.org>
parents: 390
diff changeset
   515
    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
   516
} /* __PHYSFS_platformGetThreadID */
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   517
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   518
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   519
void *__PHYSFS_platformCreateMutex(void)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   520
{
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   521
    int rc;
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   522
    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   523
    BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   524
    rc = pthread_mutex_init(&m->mutex, NULL);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   525
    if (rc != 0)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   526
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 678
diff changeset
   527
        allocator.Free(m);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   528
        BAIL_MACRO(strerror(rc), NULL);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   529
    } /* if */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   530
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   531
    m->count = 0;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   532
    m->owner = (pthread_t) 0xDEADBEEF;
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   533
    return((void *) m);
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   534
} /* __PHYSFS_platformCreateMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   535
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   536
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   537
void __PHYSFS_platformDestroyMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   538
{
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   539
    PthreadMutex *m = (PthreadMutex *) mutex;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   540
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   541
    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   542
    if ((m->owner == pthread_self()) && (m->count > 0))
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   543
        pthread_mutex_unlock(&m->mutex);
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   544
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   545
    pthread_mutex_destroy(&m->mutex);
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   546
    allocator.Free(m);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   547
} /* __PHYSFS_platformDestroyMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   548
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   549
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   550
int __PHYSFS_platformGrabMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   551
{
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   552
    PthreadMutex *m = (PthreadMutex *) mutex;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   553
    pthread_t tid = pthread_self();
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   554
    if (m->owner != tid)
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   555
    {
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   556
        if (pthread_mutex_lock(&m->mutex) != 0)
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   557
            return(0);
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   558
        m->owner = tid;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   559
    } /* if */
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   560
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   561
    m->count++;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   562
    return(1);
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   563
} /* __PHYSFS_platformGrabMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   564
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   565
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   566
void __PHYSFS_platformReleaseMutex(void *mutex)
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   567
{
757
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   568
    PthreadMutex *m = (PthreadMutex *) mutex;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   569
    if (m->owner == pthread_self())
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   570
    {
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   571
        if (--m->count == 0)
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   572
        {
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   573
            m->owner = (pthread_t) 0xDEADBEEF;
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   574
            pthread_mutex_unlock(&m->mutex);
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   575
        } /* if */
50e6457e78be Made unix mutexes recursive.
Ryan C. Gordon <icculus@icculus.org>
parents: 691
diff changeset
   576
    } /* if */
145
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   577
} /* __PHYSFS_platformReleaseMutex */
d6385584f6c4 First shot at thread-safety.
Ryan C. Gordon <icculus@icculus.org>
parents: 137
diff changeset
   578
390
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   579
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
6649d6caecf2 Stub code for platforms without functional pthreads.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   580
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
   581
#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
   582
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
/* end of unix.c ... */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584