src/platform_posix.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Mar 2012 12:31:29 -0400
changeset 1228 7c0e9de2ba33
parent 1226 3ea51c799aba
child 1238 97d4522cdf02
permissions -rw-r--r--
Removed llseek(). Use Linux's off64_t support instead.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Posix-esque support routines for PhysicsFS.
4ad12914f652 Initial add.
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.
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
4ad12914f652 Initial add.
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_POSIX
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 292
diff changeset
    13
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    14
#include <unistd.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <ctype.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <sys/types.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <sys/stat.h>
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    18
#include <pwd.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <dirent.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <errno.h>
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    21
#include <fcntl.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
1173
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
    23
#if ((!defined PHYSFS_NO_THREAD_SUPPORT) && (!defined PHYSFS_PLATFORM_BEOS))
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
    24
#include <pthread.h>
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
    25
#endif
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
    26
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#include "physfs_internal.h"
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    const char *envr = getenv(varname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    if (envr != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    36
        retval = (char *) allocator.Malloc(strlen(envr) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
            strcpy(retval, envr);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    41
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
} /* __PHYSFS_platformCopyEnvironmentVariable */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
static char *getUserNameByUID(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    uid_t uid = getuid();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    struct passwd *pw;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    pw = getpwuid(uid);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    if ((pw != NULL) && (pw->pw_name != NULL))
4ad12914f652 Initial add.
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: 657
diff changeset
    54
        retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
            strcpy(retval, pw->pw_name);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    59
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
} /* getUserNameByUID */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
static char *getUserDirByUID(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    uid_t uid = getuid();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    struct passwd *pw;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    pw = getpwuid(uid);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    if ((pw != NULL) && (pw->pw_dir != NULL))
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    72
        retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
            strcpy(retval, pw->pw_dir);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    77
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
} /* getUserDirByUID */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
char *__PHYSFS_platformGetUserName(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    char *retval = getUserNameByUID();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    if (retval == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
    86
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
} /* __PHYSFS_platformGetUserName */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
char *__PHYSFS_platformGetUserDir(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
1073
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    93
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    94
    /* if the environment variable was set, make sure it's really a dir. */
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    95
    if (retval != NULL)
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    96
    {
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    97
        struct stat statbuf;
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    98
        if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0))
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    99
        {
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   100
            allocator.Free(retval);
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   101
            retval = NULL;
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   102
        } /* if */
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   103
    } /* if */
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   104
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    if (retval == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        retval = getUserDirByUID();
1073
b45193ab2033 See if $HOME is bogus, and if so, use getpwuid() instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   107
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   108
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
} /* __PHYSFS_platformGetUserDir */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   112
void __PHYSFS_platformEnumerateFiles(const char *dirname,
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   113
                                     int omitSymLinks,
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   114
                                     PHYSFS_EnumFilesCallback callback,
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   115
                                     const char *origdir,
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   116
                                     void *callbackdata)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    DIR *dir;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    struct dirent *ent;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    int bufsize = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    char *buf = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    int dlen = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   124
    if (omitSymLinks)  /* !!! FIXME: this malloc sucks. */
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        dlen = strlen(dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
        bufsize = dlen + 256;
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   128
        buf = (char *) allocator.Malloc(bufsize);
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   129
        if (buf == NULL)
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   130
            return;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
        strcpy(buf, dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
        if (buf[dlen - 1] != '/')
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
            buf[dlen++] = '/';
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
            buf[dlen] = '\0';
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
        } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    dir = opendir(dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    if (dir == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    {
852
9467e96abdf1 Replaced some Malloc and all the alloca() calls with __PHYSFS_smallAlloc(),
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   143
        allocator.Free(buf);
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   144
        return;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
362
ac8c846a0a66 Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   147
    while ((ent = readdir(dir)) != NULL)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
        if (strcmp(ent->d_name, ".") == 0)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
            continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        if (strcmp(ent->d_name, "..") == 0)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
            continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
        if (omitSymLinks)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        {
1125
bcff76dbd9fd Removed isDirectory, isSymLink and exists methods from internal code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1118
diff changeset
   157
            PHYSFS_Stat statbuf;
bcff76dbd9fd Removed isDirectory, isSymLink and exists methods from internal code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1118
diff changeset
   158
            int exists = 0;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
            char *p;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
            int len = strlen(ent->d_name) + dlen + 1;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
            if (len > bufsize)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
            {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   163
                p = (char *) allocator.Realloc(buf, len);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
                if (p == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
                    continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
                buf = p;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
                bufsize = len;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
            } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
            strcpy(buf + dlen, ent->d_name);
1125
bcff76dbd9fd Removed isDirectory, isSymLink and exists methods from internal code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1118
diff changeset
   171
1199
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   172
            if (!__PHYSFS_platformStat(buf, &exists, &statbuf))
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   173
                continue;
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   174
            else if (!exists)
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   175
                continue;  /* probably can't happen, but just in case. */
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   176
            else if (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK)
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   177
                continue;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   180
        callback(callbackdata, origdir, ent->d_name);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    } /* while */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
852
9467e96abdf1 Replaced some Malloc and all the alloca() calls with __PHYSFS_smallAlloc(),
Ryan C. Gordon <icculus@icculus.org>
parents: 847
diff changeset
   183
    allocator.Free(buf);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    closedir(dir);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
} /* __PHYSFS_platformEnumerateFiles */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
int __PHYSFS_platformMkDir(const char *path)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    int rc;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    rc = mkdir(path, S_IRWXU);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   194
    return 1;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
} /* __PHYSFS_platformMkDir */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   198
static void *doOpen(const char *filename, int mode)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
{
939
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   200
    const int appending = (mode & O_APPEND);
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   201
    int fd;
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   202
    int *retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
939
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   205
    /* O_APPEND doesn't actually behave as we'd like. */
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   206
    mode &= ~O_APPEND;
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   207
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   208
    fd = open(filename, mode, S_IRUSR | S_IWUSR);
292
99bf6ef5639a Patched a bug where doOpen() reported success when the file couldn't be
Ryan C. Gordon <icculus@icculus.org>
parents: 259
diff changeset
   209
    BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
939
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   211
    if (appending)
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   212
    {
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   213
        if (lseek(fd, 0, SEEK_END) < 0)
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   214
        {
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   215
            close(fd);
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   216
            BAIL_MACRO(strerror(errno), NULL);
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   217
        } /* if */
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   218
    } /* if */
684c583cb586 Fixed PHYSFS_openAppend() on Unix.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   219
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   220
    retval = (int *) allocator.Malloc(sizeof (int));
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   221
    if (retval == NULL)
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   222
    {
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   223
        close(fd);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   224
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   225
    } /* if */
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   226
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   227
    *retval = fd;
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   228
    return ((void *) retval);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
} /* doOpen */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
void *__PHYSFS_platformOpenRead(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   234
    return doOpen(filename, O_RDONLY);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
} /* __PHYSFS_platformOpenRead */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
void *__PHYSFS_platformOpenWrite(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   240
    return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
} /* __PHYSFS_platformOpenWrite */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
void *__PHYSFS_platformOpenAppend(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   246
    return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
} /* __PHYSFS_platformOpenAppend */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   251
                                    PHYSFS_uint64 len)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   253
    const int fd = *((int *) opaque);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   254
    ssize_t rc = 0;
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   255
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   256
    BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   258
    rc = read(fd, buffer, (size_t) len);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   259
    BAIL_IF_MACRO(rc == -1, strerror(errno), (PHYSFS_sint64) rc);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   260
    assert(rc >= 0);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   261
    assert(rc <= len);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   262
    return (PHYSFS_sint64) rc;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
} /* __PHYSFS_platformRead */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   267
                                     PHYSFS_uint64 len)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   269
    const int fd = *((int *) opaque);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   270
    ssize_t rc = 0;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   272
    BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   273
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   274
    rc = write(fd, (void *) buffer, (size_t) len);
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   275
    BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   276
    assert(rc >= 0);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   277
    assert(rc <= len);
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   278
    return (PHYSFS_sint64) rc;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
} /* __PHYSFS_platformWrite */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   284
    const int fd = *((int *) opaque);
1228
7c0e9de2ba33 Removed llseek().
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
   285
    BAIL_IF_MACRO(lseek(fd, (off_t) pos, SEEK_SET) == -1, strerror(errno), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   286
    return 1;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
} /* __PHYSFS_platformSeek */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   292
    const int fd = *((int *) opaque);
259
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   293
    PHYSFS_sint64 retval;
1228
7c0e9de2ba33 Removed llseek().
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
   294
    retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
7c0e9de2ba33 Removed llseek().
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
   295
    BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   296
    return retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
} /* __PHYSFS_platformTell */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   302
    const int fd = *((int *) opaque);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    struct stat statbuf;
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   304
    BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   305
    return ((PHYSFS_sint64) statbuf.st_size);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
} /* __PHYSFS_platformFileLength */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
int __PHYSFS_platformFlush(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
{
1117
968e1567f4f4 Constification.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   311
    const int fd = *((int *) opaque);
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   312
    BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   313
    return 1;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
} /* __PHYSFS_platformFlush */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1117
diff changeset
   317
void __PHYSFS_platformClose(void *opaque)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1117
diff changeset
   319
    const int fd = *((int *) opaque);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1117
diff changeset
   320
    (void) close(fd);  /* we don't check this. You should have used flush! */
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   321
    allocator.Free(opaque);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
} /* __PHYSFS_platformClose */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
int __PHYSFS_platformDelete(const char *path)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   328
    return 1;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
} /* __PHYSFS_platformDelete */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
240
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   331
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   332
int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   333
{
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   334
    struct stat statbuf;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   335
1199
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   336
    if (lstat(filename, &statbuf) == -1)
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   337
    {
1199
fc5cec340d26 Cleaned up some __PHYSFS_platformStat() details.
Ryan C. Gordon <icculus@icculus.org>
parents: 1176
diff changeset
   338
        *exists = (errno == ENOENT);
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1098
diff changeset
   339
        BAIL_MACRO(strerror(errno), 0);
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   340
    } /* if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   341
1167
9d84f8652839 POSIX version of __PHYSFS_platformStat() forgot to set *exists properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   342
    *exists = 1;
9d84f8652839 POSIX version of __PHYSFS_platformStat() forgot to set *exists properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   343
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   344
    if (S_ISREG(statbuf.st_mode))
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   345
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   346
        st->filetype = PHYSFS_FILETYPE_REGULAR;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   347
        st->filesize = statbuf.st_size;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   348
    } /* if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   349
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   350
    else if(S_ISDIR(statbuf.st_mode))
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   351
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   352
        st->filetype = PHYSFS_FILETYPE_DIRECTORY;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   353
        st->filesize = 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   354
    } /* else if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   355
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   356
    else
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   357
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   358
        st->filetype = PHYSFS_FILETYPE_OTHER;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   359
        st->filesize = statbuf.st_size;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   360
    } /* else */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   361
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   362
    st->modtime = statbuf.st_mtime;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   363
    st->createtime = statbuf.st_ctime;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   364
    st->accesstime = statbuf.st_atime;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   365
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   366
    /* !!! FIXME: maybe we should just report full permissions? */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   367
    st->readonly = access(filename, W_OK);
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1098
diff changeset
   368
    return 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   369
} /* __PHYSFS_platformStat */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   370
1173
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   371
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   372
#ifndef PHYSFS_PLATFORM_BEOS  /* BeOS has its own code in platform_beos.cpp */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   373
#if (defined PHYSFS_NO_THREAD_SUPPORT)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   374
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   375
void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   376
void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   377
void __PHYSFS_platformDestroyMutex(void *mutex) {}
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   378
int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   379
void __PHYSFS_platformReleaseMutex(void *mutex) {}
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   380
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   381
#else
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   382
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   383
typedef struct
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   384
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   385
    pthread_mutex_t mutex;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   386
    pthread_t owner;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   387
    PHYSFS_uint32 count;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   388
} PthreadMutex;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   389
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   390
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   391
void *__PHYSFS_platformGetThreadID(void)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   392
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   393
    return ( (void *) ((size_t) pthread_self()) );
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   394
} /* __PHYSFS_platformGetThreadID */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   395
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   396
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   397
void *__PHYSFS_platformCreateMutex(void)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   398
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   399
    int rc;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   400
    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   401
    BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   402
    rc = pthread_mutex_init(&m->mutex, NULL);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   403
    if (rc != 0)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   404
    {
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   405
        allocator.Free(m);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   406
        BAIL_MACRO(strerror(rc), NULL);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   407
    } /* if */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   408
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   409
    m->count = 0;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   410
    m->owner = (pthread_t) 0xDEADBEEF;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   411
    return ((void *) m);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   412
} /* __PHYSFS_platformCreateMutex */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   413
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   414
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   415
void __PHYSFS_platformDestroyMutex(void *mutex)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   416
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   417
    PthreadMutex *m = (PthreadMutex *) mutex;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   418
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   419
    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   420
    if ((m->owner == pthread_self()) && (m->count > 0))
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   421
        pthread_mutex_unlock(&m->mutex);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   422
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   423
    pthread_mutex_destroy(&m->mutex);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   424
    allocator.Free(m);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   425
} /* __PHYSFS_platformDestroyMutex */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   426
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   427
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   428
int __PHYSFS_platformGrabMutex(void *mutex)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   429
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   430
    PthreadMutex *m = (PthreadMutex *) mutex;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   431
    pthread_t tid = pthread_self();
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   432
    if (m->owner != tid)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   433
    {
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   434
        if (pthread_mutex_lock(&m->mutex) != 0)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   435
            return 0;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   436
        m->owner = tid;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   437
    } /* if */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   438
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   439
    m->count++;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   440
    return 1;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   441
} /* __PHYSFS_platformGrabMutex */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   442
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   443
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   444
void __PHYSFS_platformReleaseMutex(void *mutex)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   445
{
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   446
    PthreadMutex *m = (PthreadMutex *) mutex;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   447
    if (m->owner == pthread_self())
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   448
    {
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   449
        if (--m->count == 0)
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   450
        {
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   451
            m->owner = (pthread_t) 0xDEADBEEF;
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   452
            pthread_mutex_unlock(&m->mutex);
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   453
        } /* if */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   454
    } /* if */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   455
} /* __PHYSFS_platformReleaseMutex */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   456
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   457
#endif /* !PHYSFS_NO_THREAD_SUPPORT */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   458
#endif /* !PHYSFS_PLATFORM_BEOS */
77b2df665bd1 Removed deprecated Mac OS X APIs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   459
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
   460
#endif  /* PHYSFS_PLATFORM_POSIX */
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 292
diff changeset
   461
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
/* end of posix.c ... */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463