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