misc/libphysfs/platform_posix.c
author Wuzzy <almikes@aol.com>
Sat, 15 Apr 2017 05:01:40 +0200
changeset 12259 9d501bd1169c
parent 12218 bb5522e88ab2
permissions -rw-r--r--
Replace initial health icon in game scheme editor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * Posix-esque support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
/* !!! FIXME: check for EINTR? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
#include "physfs_platforms.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
#ifdef PHYSFS_PLATFORM_POSIX
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
#include <unistd.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
#include <ctype.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
#include <sys/types.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
#include <sys/stat.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
#include <pwd.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
#include <dirent.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
#include <errno.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
#include <fcntl.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
#if ((!defined PHYSFS_NO_THREAD_SUPPORT) && (!defined PHYSFS_PLATFORM_BEOS))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
#include <pthread.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
static PHYSFS_ErrorCode errcodeFromErrnoError(const int err)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
    switch (err)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
        case 0: return PHYSFS_ERR_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
        case EACCES: return PHYSFS_ERR_PERMISSION;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
        case EPERM: return PHYSFS_ERR_PERMISSION;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
        case EDQUOT: return PHYSFS_ERR_NO_SPACE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
        case EIO: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
        case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
        case EMLINK: return PHYSFS_ERR_NO_SPACE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
        case ENAMETOOLONG: return PHYSFS_ERR_BAD_FILENAME;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    44
        case ENOENT: return PHYSFS_ERR_NOT_FOUND;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
        case ENOSPC: return PHYSFS_ERR_NO_SPACE;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    46
        case ENOTDIR: return PHYSFS_ERR_NOT_FOUND;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
        case EISDIR: return PHYSFS_ERR_NOT_A_FILE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
        case EROFS: return PHYSFS_ERR_READ_ONLY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
        case ETXTBSY: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
        case EBUSY: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
        case ENOMEM: return PHYSFS_ERR_OUT_OF_MEMORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
        case ENOTEMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
        default: return PHYSFS_ERR_OS_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
    } /* switch */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
} /* errcodeFromErrnoError */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
static inline PHYSFS_ErrorCode errcodeFromErrno(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
    return errcodeFromErrnoError(errno);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
} /* errcodeFromErrno */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
static char *getUserDirByUID(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
    uid_t uid = getuid();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
    struct passwd *pw;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    pw = getpwuid(uid);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    if ((pw != NULL) && (pw->pw_dir != NULL) && (*pw->pw_dir != '\0'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
        const size_t dlen = strlen(pw->pw_dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
        const size_t add_dirsep = (pw->pw_dir[dlen-1] != '/') ? 1 : 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
        retval = (char *) allocator.Malloc(dlen + 1 + add_dirsep);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
        if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
            strcpy(retval, pw->pw_dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
            if (add_dirsep)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
                retval[dlen] = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
                retval[dlen+1] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
    } /* if */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    86
    
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
} /* getUserDirByUID */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
char *__PHYSFS_platformCalcUserDir(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
    char *envr = getenv("HOME");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    /* if the environment variable was set, make sure it's really a dir. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
    if (envr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
        struct stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
        if ((stat(envr, &statbuf) != -1) && (S_ISDIR(statbuf.st_mode)))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
            const size_t envrlen = strlen(envr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
            const size_t add_dirsep = (envr[envrlen-1] != '/') ? 1 : 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
            retval = allocator.Malloc(envrlen + 1 + add_dirsep);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
            if (retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
                strcpy(retval, envr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
                if (add_dirsep)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
                    retval[envrlen] = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
                    retval[envrlen+1] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
    if (retval == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
        retval = getUserDirByUID();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
} /* __PHYSFS_platformCalcUserDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
void __PHYSFS_platformEnumerateFiles(const char *dirname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
                                     PHYSFS_EnumFilesCallback callback,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
                                     const char *origdir,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
                                     void *callbackdata)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
    DIR *dir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
    struct dirent *ent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
    char *buf = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
    errno = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
    dir = opendir(dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
    if (dir == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
        allocator.Free(buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
    while ((ent = readdir(dir)) != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
        if (strcmp(ent->d_name, ".") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
            continue;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   145
        else if (strcmp(ent->d_name, "..") == 0)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
        callback(callbackdata, origdir, ent->d_name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
    allocator.Free(buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
    closedir(dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
} /* __PHYSFS_platformEnumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
int __PHYSFS_platformMkDir(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
    const int rc = mkdir(path, S_IRWXU);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
} /* __PHYSFS_platformMkDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
static void *doOpen(const char *filename, int mode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
    const int appending = (mode & O_APPEND);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
    int fd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
    int *retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
    errno = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
    /* O_APPEND doesn't actually behave as we'd like. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
    mode &= ~O_APPEND;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
    fd = open(filename, mode, S_IRUSR | S_IWUSR);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
    BAIL_IF_MACRO(fd < 0, errcodeFromErrno(), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
    if (appending)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
        if (lseek(fd, 0, SEEK_END) < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
            const int err = errno;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
            close(fd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
            BAIL_MACRO(errcodeFromErrnoError(err), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
    retval = (int *) allocator.Malloc(sizeof (int));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
        close(fd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
    *retval = fd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
    return ((void *) retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
} /* doOpen */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
void *__PHYSFS_platformOpenRead(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
    return doOpen(filename, O_RDONLY);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
} /* __PHYSFS_platformOpenRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
void *__PHYSFS_platformOpenWrite(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
    return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
} /* __PHYSFS_platformOpenWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
void *__PHYSFS_platformOpenAppend(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
    return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
} /* __PHYSFS_platformOpenAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
    ssize_t rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
    rc = read(fd, buffer, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
    assert(rc >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    assert(rc <= len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
    return (PHYSFS_sint64) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
} /* __PHYSFS_platformRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
                                     PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
    ssize_t rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
    rc = write(fd, (void *) buffer, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), rc);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
    assert(rc >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
    assert(rc <= len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
    return (PHYSFS_sint64) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
} /* __PHYSFS_platformWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
    const int rc = lseek(fd, (off_t) pos, SEEK_SET);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
} /* __PHYSFS_platformSeek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
    PHYSFS_sint64 retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
    retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
    BAIL_IF_MACRO(retval == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
} /* __PHYSFS_platformTell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
    struct stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
    BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
    return ((PHYSFS_sint64) statbuf.st_size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
} /* __PHYSFS_platformFileLength */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
int __PHYSFS_platformFlush(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
    const int fd = *((int *) opaque);
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   282
    if ((fcntl(fd, F_GETFL) & O_ACCMODE) != O_RDONLY)
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   283
        BAIL_IF_MACRO(fsync(fd) == -1, errcodeFromErrno(), 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
} /* __PHYSFS_platformFlush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
void __PHYSFS_platformClose(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
    (void) close(fd);  /* we don't check this. You should have used flush! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
    allocator.Free(opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
} /* __PHYSFS_platformClose */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
int __PHYSFS_platformDelete(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
    BAIL_IF_MACRO(remove(path) == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
} /* __PHYSFS_platformDelete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   303
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
    struct stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   307
    BAIL_IF_MACRO(lstat(filename, &statbuf) == -1, errcodeFromErrno(), 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
    if (S_ISREG(statbuf.st_mode))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
        st->filetype = PHYSFS_FILETYPE_REGULAR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
        st->filesize = statbuf.st_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
    else if(S_ISDIR(statbuf.st_mode))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
        st->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
        st->filesize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
    } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   321
    else if(S_ISLNK(statbuf.st_mode))
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   322
    {
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   323
        st->filetype = PHYSFS_FILETYPE_SYMLINK;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   324
        st->filesize = 0;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   325
    } /* else if */
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   326
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
        st->filetype = PHYSFS_FILETYPE_OTHER;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
        st->filesize = statbuf.st_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
    st->modtime = statbuf.st_mtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    st->createtime = statbuf.st_ctime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    st->accesstime = statbuf.st_atime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
    /* !!! FIXME: maybe we should just report full permissions? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
    st->readonly = access(filename, W_OK);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
} /* __PHYSFS_platformStat */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
#ifndef PHYSFS_PLATFORM_BEOS  /* BeOS has its own code in platform_beos.cpp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
#if (defined PHYSFS_NO_THREAD_SUPPORT)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
void __PHYSFS_platformDestroyMutex(void *mutex) {}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
void __PHYSFS_platformReleaseMutex(void *mutex) {}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    pthread_mutex_t mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
    pthread_t owner;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
    PHYSFS_uint32 count;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
} PthreadMutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
void *__PHYSFS_platformGetThreadID(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
    return ( (void *) ((size_t) pthread_self()) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
} /* __PHYSFS_platformGetThreadID */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
void *__PHYSFS_platformCreateMutex(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
    BAIL_IF_MACRO(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
    rc = pthread_mutex_init(&m->mutex, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
    if (rc != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
        allocator.Free(m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
        BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
    m->count = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
    m->owner = (pthread_t) 0xDEADBEEF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
    return ((void *) m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
} /* __PHYSFS_platformCreateMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
void __PHYSFS_platformDestroyMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
    if ((m->owner == pthread_self()) && (m->count > 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
        pthread_mutex_unlock(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
    pthread_mutex_destroy(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
    allocator.Free(m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
} /* __PHYSFS_platformDestroyMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
int __PHYSFS_platformGrabMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
    pthread_t tid = pthread_self();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
    if (m->owner != tid)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
        if (pthread_mutex_lock(&m->mutex) != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
            return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
        m->owner = tid;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
    m->count++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
} /* __PHYSFS_platformGrabMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
void __PHYSFS_platformReleaseMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
    assert(m->owner == pthread_self());  /* catch programming errors. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
    assert(m->count > 0);  /* catch programming errors. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
    if (m->owner == pthread_self())
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
        if (--m->count == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
            m->owner = (pthread_t) 0xDEADBEEF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
            pthread_mutex_unlock(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
} /* __PHYSFS_platformReleaseMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
#endif /* !PHYSFS_NO_THREAD_SUPPORT */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
#endif /* !PHYSFS_PLATFORM_BEOS */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
#endif  /* PHYSFS_PLATFORM_POSIX */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
/* end of posix.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436