misc/libphysfs/platform_posix.c
author sheepluva
Tue, 10 Jun 2014 01:15:48 +0200
changeset 10263 6ef900411158
parent 10017 de822cd3df3a
child 12213 bb5522e88ab2
permissions -rw-r--r--
here a personal experimental mockup of how I imagine wrapping world edges should look like. bouncing one could be similar just with mirrored stuff. please test and share opinion. note: you might wanna disable auto-cam
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;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
        case ENOENT: return PHYSFS_ERR_NO_SUCH_PATH;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
        case ENOSPC: return PHYSFS_ERR_NO_SPACE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
        case ENOTDIR: return PHYSFS_ERR_NO_SUCH_PATH;
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 */
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8524
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
                                     int omitSymLinks,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
                                     PHYSFS_EnumFilesCallback callback,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
                                     const char *origdir,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
                                     void *callbackdata)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
    DIR *dir;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
    struct dirent *ent;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
    int bufsize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
    char *buf = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
    int dlen = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
    if (omitSymLinks)  /* !!! FIXME: this malloc sucks. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
        dlen = strlen(dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
        bufsize = dlen + 256;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
        buf = (char *) allocator.Malloc(bufsize);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
        if (buf == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
            return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
        strcpy(buf, dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
        if (buf[dlen - 1] != '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
            buf[dlen++] = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
            buf[dlen] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
    errno = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
    dir = opendir(dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
    if (dir == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
        allocator.Free(buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
    while ((ent = readdir(dir)) != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
        if (strcmp(ent->d_name, ".") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
        if (strcmp(ent->d_name, "..") == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
        if (omitSymLinks)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
            PHYSFS_Stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
            int exists = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
            char *p;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
            int len = strlen(ent->d_name) + dlen + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
            if (len > bufsize)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
                p = (char *) allocator.Realloc(buf, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
                if (p == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
                    continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
                buf = p;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
                bufsize = len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
            strcpy(buf + dlen, ent->d_name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
            if (!__PHYSFS_platformStat(buf, &exists, &statbuf))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
                continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
            else if (!exists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
                continue;  /* probably can't happen, but just in case. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
            else if (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
                continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
        callback(callbackdata, origdir, ent->d_name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
    allocator.Free(buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
    closedir(dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
} /* __PHYSFS_platformEnumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
int __PHYSFS_platformMkDir(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
    const int rc = mkdir(path, S_IRWXU);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
} /* __PHYSFS_platformMkDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
static void *doOpen(const char *filename, int mode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
    const int appending = (mode & O_APPEND);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
    int fd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
    int *retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
    errno = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
    /* O_APPEND doesn't actually behave as we'd like. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
    mode &= ~O_APPEND;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
    fd = open(filename, mode, S_IRUSR | S_IWUSR);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
    BAIL_IF_MACRO(fd < 0, errcodeFromErrno(), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
    if (appending)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
        if (lseek(fd, 0, SEEK_END) < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
            const int err = errno;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
            close(fd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
            BAIL_MACRO(errcodeFromErrnoError(err), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
    retval = (int *) allocator.Malloc(sizeof (int));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
        close(fd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
    *retval = fd;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
    return ((void *) retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
} /* doOpen */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
void *__PHYSFS_platformOpenRead(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
    return doOpen(filename, O_RDONLY);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
} /* __PHYSFS_platformOpenRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
void *__PHYSFS_platformOpenWrite(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
    return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
} /* __PHYSFS_platformOpenWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
void *__PHYSFS_platformOpenAppend(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
    return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
} /* __PHYSFS_platformOpenAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
                                    PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
    ssize_t rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
    rc = read(fd, buffer, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
    assert(rc >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
    assert(rc <= len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
    return (PHYSFS_sint64) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
} /* __PHYSFS_platformRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
                                     PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
    ssize_t rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
    rc = write(fd, (void *) buffer, (size_t) len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), rc);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
    assert(rc >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
    assert(rc <= len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
    return (PHYSFS_sint64) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
} /* __PHYSFS_platformWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
    const int rc = lseek(fd, (off_t) pos, SEEK_SET);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
    BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
} /* __PHYSFS_platformSeek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
    PHYSFS_sint64 retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
    retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
    BAIL_IF_MACRO(retval == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
} /* __PHYSFS_platformTell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
    struct stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
    BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
    return ((PHYSFS_sint64) statbuf.st_size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
} /* __PHYSFS_platformFileLength */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
int __PHYSFS_platformFlush(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
    BAIL_IF_MACRO(fsync(fd) == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
} /* __PHYSFS_platformFlush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
void __PHYSFS_platformClose(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
    const int fd = *((int *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    (void) close(fd);  /* we don't check this. You should have used flush! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    allocator.Free(opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
} /* __PHYSFS_platformClose */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
int __PHYSFS_platformDelete(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
    BAIL_IF_MACRO(remove(path) == -1, errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
} /* __PHYSFS_platformDelete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
    struct stat statbuf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
    if (lstat(filename, &statbuf) == -1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
        *exists = (errno != ENOENT);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
        BAIL_MACRO(errcodeFromErrno(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    *exists = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
    if (S_ISREG(statbuf.st_mode))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
        st->filetype = PHYSFS_FILETYPE_REGULAR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
        st->filesize = statbuf.st_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
    else if(S_ISDIR(statbuf.st_mode))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
        st->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
        st->filesize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
    } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
        st->filetype = PHYSFS_FILETYPE_OTHER;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
        st->filesize = statbuf.st_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
    st->modtime = statbuf.st_mtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
    st->createtime = statbuf.st_ctime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
    st->accesstime = statbuf.st_atime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
    /* !!! FIXME: maybe we should just report full permissions? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
    st->readonly = access(filename, W_OK);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
} /* __PHYSFS_platformStat */
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
#ifndef PHYSFS_PLATFORM_BEOS  /* BeOS has its own code in platform_beos.cpp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
#if (defined PHYSFS_NO_THREAD_SUPPORT)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
void __PHYSFS_platformDestroyMutex(void *mutex) {}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
void __PHYSFS_platformReleaseMutex(void *mutex) {}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
    pthread_mutex_t mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
    pthread_t owner;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
    PHYSFS_uint32 count;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
} PthreadMutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
void *__PHYSFS_platformGetThreadID(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
    return ( (void *) ((size_t) pthread_self()) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
} /* __PHYSFS_platformGetThreadID */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
void *__PHYSFS_platformCreateMutex(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
    BAIL_IF_MACRO(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
    rc = pthread_mutex_init(&m->mutex, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
    if (rc != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
        allocator.Free(m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
        BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
    m->count = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
    m->owner = (pthread_t) 0xDEADBEEF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
    return ((void *) m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
} /* __PHYSFS_platformCreateMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
void __PHYSFS_platformDestroyMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
    if ((m->owner == pthread_self()) && (m->count > 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
        pthread_mutex_unlock(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
    pthread_mutex_destroy(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
    allocator.Free(m);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
} /* __PHYSFS_platformDestroyMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
int __PHYSFS_platformGrabMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
    pthread_t tid = pthread_self();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
    if (m->owner != tid)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   448
        if (pthread_mutex_lock(&m->mutex) != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
            return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
        m->owner = tid;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
    m->count++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
} /* __PHYSFS_platformGrabMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
void __PHYSFS_platformReleaseMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
    PthreadMutex *m = (PthreadMutex *) mutex;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
    assert(m->owner == pthread_self());  /* catch programming errors. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
    assert(m->count > 0);  /* catch programming errors. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
    if (m->owner == pthread_self())
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
        if (--m->count == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
            m->owner = (pthread_t) 0xDEADBEEF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
            pthread_mutex_unlock(&m->mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
} /* __PHYSFS_platformReleaseMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
#endif /* !PHYSFS_NO_THREAD_SUPPORT */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
#endif /* !PHYSFS_PLATFORM_BEOS */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
#endif  /* PHYSFS_PLATFORM_POSIX */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
/* end of posix.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479