platform/posix.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Mar 2007 18:33:56 +0000
changeset 845 3f150ffcf50c
parent 832 adef86dfc331
child 847 5e5e6c067413
permissions -rw-r--r--
Since all the platform layers were using the same cut-and-paste of the malloc/realloc/free allocator, I moved it into physfs.c as a default, which is used if the platform layer doesn't offer a platform-specific default allocator, which none do at this point, but will soon.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Posix-esque support routines for PhysicsFS.
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    10
#include "physfs_platforms.h"
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    11
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    12
#ifdef PHYSFS_PLATFORM_POSIX
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 292
diff changeset
    13
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <stdio.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <stdlib.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <string.h>
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    17
#include <unistd.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <ctype.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <sys/types.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <sys/stat.h>
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    21
#include <pwd.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include <dirent.h>
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#include <errno.h>
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    24
#include <fcntl.h>
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
259
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    26
#ifdef PHYSFS_HAVE_LLSEEK
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    27
#include <linux/unistd.h>
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    28
#endif
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    29
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include "physfs_internal.h"
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    const char *envr = getenv(varname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    if (envr != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    40
        retval = (char *) allocator.Malloc(strlen(envr) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
            strcpy(retval, envr);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
} /* __PHYSFS_platformCopyEnvironmentVariable */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
static char *getUserNameByUID(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    uid_t uid = getuid();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    struct passwd *pw;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    pw = getpwuid(uid);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    if ((pw != NULL) && (pw->pw_name != NULL))
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    58
        retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
            strcpy(retval, pw->pw_name);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} /* getUserNameByUID */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
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
static char *getUserDirByUID(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    uid_t uid = getuid();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    struct passwd *pw;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    pw = getpwuid(uid);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    if ((pw != NULL) && (pw->pw_dir != NULL))
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    76
        retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
        if (retval != NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
            strcpy(retval, pw->pw_dir);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    } /* if */
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
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
} /* getUserDirByUID */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
char *__PHYSFS_platformGetUserName(void)
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
    char *retval = getUserNameByUID();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    if (retval == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
} /* __PHYSFS_platformGetUserName */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
char *__PHYSFS_platformGetUserDir(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    if (retval == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        retval = getUserDirByUID();
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
} /* __PHYSFS_platformGetUserDir */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
int __PHYSFS_platformExists(const char *fname)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    struct stat statbuf;
832
adef86dfc331 Removed the strict-ANSI crap.
Ryan C. Gordon <icculus@icculus.org>
parents: 828
diff changeset
   106
    BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 429
diff changeset
   107
    return(1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
} /* __PHYSFS_platformExists */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
int __PHYSFS_platformIsSymLink(const char *fname)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    struct stat statbuf;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 429
diff changeset
   114
    BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 429
diff changeset
   115
    return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 );
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
} /* __PHYSFS_platformIsSymlink */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
int __PHYSFS_platformIsDirectory(const char *fname)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    struct stat statbuf;
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 429
diff changeset
   122
    BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0);
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 429
diff changeset
   123
    return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 );
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
} /* __PHYSFS_platformIsDirectory */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
char *__PHYSFS_platformCvtToDependent(const char *prepend,
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
                                      const char *dirName,
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
                                      const char *append)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    int len = ((prepend) ? strlen(prepend) : 0) +
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
              ((append) ? strlen(append) : 0) +
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
              strlen(dirName) + 1;
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   134
    char *retval = (char *) allocator.Malloc(len);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    /* platform-independent notation is Unix-style already.  :)  */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    if (prepend)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
        strcpy(retval, prepend);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    else
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        retval[0] = '\0';
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    strcat(retval, dirName);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    if (append)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        strcat(retval, append);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
} /* __PHYSFS_platformCvtToDependent */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   155
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
   156
                                     int omitSymLinks,
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   157
                                     PHYSFS_EnumFilesCallback callback,
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   158
                                     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
   159
                                     void *callbackdata)
235
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
    DIR *dir;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    struct dirent *ent;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    int bufsize = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    char *buf = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    int dlen = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 648
diff changeset
   167
    if (omitSymLinks)  /* !!! FIXME: this malloc sucks. */
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
        dlen = strlen(dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        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
   171
        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
   172
        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
   173
            return;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
        strcpy(buf, dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        if (buf[dlen - 1] != '/')
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
            buf[dlen++] = '/';
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
            buf[dlen] = '\0';
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    dir = opendir(dirname);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    if (dir == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        if (buf != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   187
            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
   188
        return;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
362
ac8c846a0a66 Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   191
    while ((ent = readdir(dir)) != NULL)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        if (strcmp(ent->d_name, ".") == 0)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
            continue;
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
        if (strcmp(ent->d_name, "..") == 0)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
            continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        if (omitSymLinks)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
            char *p;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
            int len = strlen(ent->d_name) + dlen + 1;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
            if (len > bufsize)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
            {
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   205
                p = (char *) allocator.Realloc(buf, len);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
                if (p == NULL)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
                    continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
                buf = p;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
                bufsize = len;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
            } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
            strcpy(buf + dlen, ent->d_name);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
            if (__PHYSFS_platformIsSymLink(buf))
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
                continue;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 747
diff changeset
   217
        callback(callbackdata, origdir, ent->d_name);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    } /* while */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    if (buf != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   221
        allocator.Free(buf);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    closedir(dir);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
} /* __PHYSFS_platformEnumerateFiles */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
char *__PHYSFS_platformCurrentDir(void)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    int allocSize = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    char *retval = NULL;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    char *ptr;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    do
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
        allocSize += 100;
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   236
        ptr = (char *) allocator.Realloc(retval, allocSize);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        if (ptr == NULL)
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
            if (retval != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   240
                allocator.Free(retval);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
            BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
        } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        retval = ptr;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        ptr = getcwd(retval, allocSize);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    } while (ptr == NULL && errno == ERANGE);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    if (ptr == NULL && errno)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    {
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
            /*
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
             * getcwd() failed for some reason, for example current
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
             * directory not existing.
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
             */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        if (retval != NULL)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   255
            allocator.Free(retval);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
        BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    } /* if */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
} /* __PHYSFS_platformCurrentDir */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
int __PHYSFS_platformMkDir(const char *path)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    int rc;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    rc = mkdir(path, S_IRWXU);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    return(1);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
} /* __PHYSFS_platformMkDir */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   274
static void *doOpen(const char *filename, int mode)
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   276
    int fd;
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   277
    int *retval;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    errno = 0;
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   280
    fd = open(filename, mode, S_IRUSR | S_IWUSR);
292
99bf6ef5639a Patched a bug where doOpen() reported success when the file couldn't be
Ryan C. Gordon <icculus@icculus.org>
parents: 259
diff changeset
   281
    BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   283
    retval = (int *) allocator.Malloc(sizeof (int));
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   284
    if (retval == NULL)
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   285
    {
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   286
        close(fd);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   287
        BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   288
    } /* if */
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   289
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   290
    *retval = fd;
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    return((void *) retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
} /* doOpen */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
void *__PHYSFS_platformOpenRead(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   297
    return(doOpen(filename, O_RDONLY));
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
} /* __PHYSFS_platformOpenRead */
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
void *__PHYSFS_platformOpenWrite(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   303
    return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
} /* __PHYSFS_platformOpenWrite */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
void *__PHYSFS_platformOpenAppend(const char *filename)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   309
    return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
} /* __PHYSFS_platformOpenAppend */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
                                    PHYSFS_uint32 size, PHYSFS_uint32 count)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   316
    int fd = *((int *) opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   317
    int max = size * count;
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   318
    int rc = read(fd, buffer, max);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   320
    BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   321
    assert(rc <= max);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   322
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   323
    if ((rc < max) && (size > 1))
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   324
        lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   325
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   326
    return(rc / size);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
} /* __PHYSFS_platformRead */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
                                     PHYSFS_uint32 size, PHYSFS_uint32 count)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   333
    int fd = *((int *) opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   334
    int max = size * count;
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   335
    int rc = write(fd, (void *) buffer, max);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   337
    BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   338
    assert(rc <= max);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   339
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   340
    if ((rc < max) && (size > 1))
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   341
        lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   342
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   343
    return(rc / size);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
} /* __PHYSFS_platformWrite */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   349
    int fd = *((int *) opaque);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
259
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   351
    #ifdef PHYSFS_HAVE_LLSEEK
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   352
      unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   353
      unsigned long offset_low = (pos & 0xFFFFFFFF);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   354
      loff_t retoffset;
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   355
      int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   356
      BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   357
    #else
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   358
      BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   359
    #endif
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    return(1);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
} /* __PHYSFS_platformSeek */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   367
    int fd = *((int *) opaque);
259
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   368
    PHYSFS_sint64 retval;
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   369
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   370
    #ifdef PHYSFS_HAVE_LLSEEK
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   371
      loff_t retoffset;
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   372
      int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   373
      BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   374
      retval = (PHYSFS_sint64) retoffset;
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   375
    #else
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   376
      retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   377
      BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   378
    #endif
bd65a0d74e9a 64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   379
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    return(retval);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
} /* __PHYSFS_platformTell */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   386
    int fd = *((int *) opaque);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    struct stat statbuf;
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   388
    BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    return((PHYSFS_sint64) statbuf.st_size);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
} /* __PHYSFS_platformFileLength */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
int __PHYSFS_platformEOF(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   395
    PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   396
    PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   397
    return(pos >= len);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
} /* __PHYSFS_platformEOF */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
int __PHYSFS_platformFlush(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   403
    int fd = *((int *) opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   404
    BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    return(1);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
} /* __PHYSFS_platformFlush */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
int __PHYSFS_platformClose(void *opaque)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
{
251
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   411
    int fd = *((int *) opaque);
0f4615263e32 Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
   412
    BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   413
    allocator.Free(opaque);
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
    return(1);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
} /* __PHYSFS_platformClose */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
int __PHYSFS_platformDelete(const char *path)
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
{
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
    BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    return(1);
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
} /* __PHYSFS_platformDelete */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
240
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   424
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   425
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   426
{
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   427
    struct stat statbuf;
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   428
    BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   429
    return statbuf.st_mtime;
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   430
} /* __PHYSFS_platformGetLastModTime */
052041af9001 Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   431
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
   432
#endif  /* PHYSFS_PLATFORM_POSIX */
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 292
diff changeset
   433
235
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
/* end of posix.c ... */
4ad12914f652 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435