platform/unix.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 14 Sep 2001 22:59:53 +0000
changeset 100 d2f04ab4b412
parent 79 5dbb03944ef0
child 115 c0671785777b
permissions -rw-r--r--
Patches from David Hedbor to handle missing current working directories. "The attached patch makes PhysFS handle the case when the current directory (as returned by getcwd()) has been deleted. Fix 1, platform/unix.c: Only retry getcwd if the error is ERANGE. Before it retried till the malloc failed in the case of the directory not existing. Fix 2: physfs.c: If __PHYSFS_platformCurrentDir() return's NULL, use the root as the base dir. Bad solution but the best I can think of (alternative : use /tmp or Windows TEMP dir?)."
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Unix support routines for PhysicsFS.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * Please see the file LICENSE in the source's root directory.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
     9
#if (defined __STRICT_ANSI__)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    10
#define __PHYSFS_DOING_STRICT_ANSI__
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    11
#endif
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    12
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    13
/*
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    14
 * We cheat a little: I want the symlink version of stat() (lstat), and
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    15
 *  GCC/Linux will not declare it if compiled with the -ansi flag.
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    16
 *  If you are really lacking symlink support on your platform,
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    17
 *  you should #define __PHYSFS_NO_SYMLINKS__ before compiling this
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    18
 *  file. That will open a security hole, though, if you really DO have
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    19
 *  symlinks on your platform; it renders PHYSFS_permitSymbolicLinks(0)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    20
 *  useless, since every symlink will be reported as a regular file/dir.
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    21
 */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    22
#if (defined __PHYSFS_DOING_STRICT_ANSI__)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    23
#undef __STRICT_ANSI__
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    24
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#include <stdio.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    26
#if (defined __PHYSFS_DOING_STRICT_ANSI__)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    27
#define __STRICT_ANSI__
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    28
#endif
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    29
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include <stdlib.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    31
#include <string.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    32
#include <ctype.h>
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include <pthread.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    34
#include <unistd.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    35
#include <sys/types.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    36
#include <pwd.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    37
#include <sys/stat.h>
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
    38
#include <sys/param.h>
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    39
#include <dirent.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    40
#include <time.h>
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    41
#include <errno.h>
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    42
#include <mntent.h>
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#define __PHYSICSFS_INTERNAL__
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#include "physfs_internal.h"
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
const char *__PHYSFS_platformDirSeparator = "/";
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
char **__PHYSFS_platformDetectAvailableCDs(void)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
{
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    52
    char **retval = (char **) malloc(sizeof (char *));
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    53
    int cd_count = 1;  /* We count the NULL entry. */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    54
    FILE *mounts = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    55
    struct mntent *ent = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    56
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    57
    *retval = NULL;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    58
    mounts = setmntent("/etc/mtab", "r");
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    59
    BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    60
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    61
    while ( (ent = getmntent(mounts)) != NULL )
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    62
    {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    63
        int add_it = 0;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    64
        if (strcmp(ent->mnt_type, "iso9660") == 0)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    65
            add_it = 1;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    66
        /* !!! other mount types? */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    67
63
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    68
        if (add_it)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    69
        {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    70
            char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    71
            if (tmp)
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    72
            {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    73
                retval = tmp;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    74
                retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    75
                if (retval[cd_count-1])
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    76
                {
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    77
                    strcpy(retval[cd_count-1], ent->mnt_dir);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    78
                    cd_count++;
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    79
                } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    80
            } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    81
        } /* if */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    82
    } /* while */
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    83
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    84
    endmntent(mounts);
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    85
cb9a4714b844 Added CD-ROM detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 41
diff changeset
    86
    retval[cd_count - 1] = NULL;
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    87
    return(retval);
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
} /* __PHYSFS_detectAvailableCDs */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    91
static char *copyEnvironmentVariable(const char *varname)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    92
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    93
    const char *envr = getenv(varname);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    94
    char *retval = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    95
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    96
    if (envr != NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    97
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    98
        retval = malloc(strlen(envr) + 1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
    99
        if (retval != NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   100
            strcpy(retval, envr);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   101
    } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   102
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   103
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   104
} /* copyEnvironmentVariable */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   105
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   106
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   107
/* !!! this is ugly. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   108
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   109
{
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   110
    /* If there isn't a path on argv0, then look through the $PATH for it. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   111
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   112
    char *retval = NULL;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   113
    char *envr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   114
    char *start;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   115
    char *ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   116
    char *exe;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   117
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   118
    if (strchr(argv0, '/') != NULL)   /* default behaviour can handle this. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   119
        return(NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   120
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   121
    envr = copyEnvironmentVariable("PATH");
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   122
    BAIL_IF_MACRO(!envr, NULL, NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   123
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   124
    start = envr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   125
    do
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   126
    {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   127
        ptr = strchr(start, ':');
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   128
        if (ptr)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   129
            *ptr = '\0';
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   130
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   131
        exe = (char *) malloc(strlen(start) + strlen(argv0) + 2);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   132
        if (!exe)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   133
        {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   134
            free(envr);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   135
            BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   136
        } /* if */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   137
        strcpy(exe, start);
79
5dbb03944ef0 Fixed base directory determination.
Ryan C. Gordon <icculus@icculus.org>
parents: 70
diff changeset
   138
        if (exe[strlen(exe) - 1] != '/')
5dbb03944ef0 Fixed base directory determination.
Ryan C. Gordon <icculus@icculus.org>
parents: 70
diff changeset
   139
            strcat(exe, "/");
5dbb03944ef0 Fixed base directory determination.
Ryan C. Gordon <icculus@icculus.org>
parents: 70
diff changeset
   140
        strcat(exe, argv0);
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   141
        if (access(exe, X_OK) != 0)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   142
            free(exe);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   143
        else
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   144
        {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   145
            retval = exe;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   146
            strcpy(retval, start);  /* i'm lazy. piss off. */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   147
            break;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   148
        } /* else */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   149
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   150
        start = ptr + 1;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   151
    } while (ptr != NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   152
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   153
    free(envr);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   154
    return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   155
} /* __PHYSFS_platformCalcBaseDir */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   156
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
   157
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   158
static char *getUserNameByUID(void)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   159
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   160
    uid_t uid = getuid();
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   161
    struct passwd *pw;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   162
    char *retval = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   163
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   164
    pw = getpwuid(uid);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   165
    if ((pw != NULL) && (pw->pw_name != NULL))
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   166
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   167
        retval = malloc(strlen(pw->pw_name) + 1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   168
        if (retval != NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   169
            strcpy(retval, pw->pw_name);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   170
    } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   171
    
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   172
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   173
} /* getUserNameByUID */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   174
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   175
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   176
static char *getUserDirByUID(void)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   177
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   178
    uid_t uid = getuid();
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   179
    struct passwd *pw;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   180
    char *retval = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   181
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   182
    pw = getpwuid(uid);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   183
    if ((pw != NULL) && (pw->pw_dir != NULL))
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   184
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   185
        retval = malloc(strlen(pw->pw_dir) + 1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   186
        if (retval != NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   187
            strcpy(retval, pw->pw_dir);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   188
    } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   189
    
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   190
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   191
} /* getUserDirByUID */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   192
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   193
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
char *__PHYSFS_platformGetUserName(void)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
{
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   196
    char *retval = getUserNameByUID();
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   197
    if (retval == NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   198
        retval = copyEnvironmentVariable("USER");
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   199
    return(retval);
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
} /* __PHYSFS_platformGetUserName */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
char *__PHYSFS_platformGetUserDir(void)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
{
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   205
    char *retval = copyEnvironmentVariable("HOME");
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   206
    if (retval == NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   207
        retval = getUserDirByUID();
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   208
    return(retval);
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
} /* __PHYSFS_platformGetUserDir */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
int __PHYSFS_platformGetThreadID(void)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
{
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    return((int) pthread_self());
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
} /* __PHYSFS_platformGetThreadID */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   218
/* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   219
int __PHYSFS_platformStricmp(const char *x, const char *y)
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
{
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   221
    int ux, uy;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   222
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   223
    do
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   224
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   225
        ux = toupper((int) *x);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   226
        uy = toupper((int) *y);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   227
        if (ux > uy)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   228
            return(1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   229
        else if (ux < uy)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   230
            return(-1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   231
        x++;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   232
        y++;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   233
    } while ((ux) && (uy));
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   234
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   235
    return(0);
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
} /* __PHYSFS_platformStricmp */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   239
int __PHYSFS_platformExists(const char *fname)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   240
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   241
    struct stat statbuf;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   242
    return(stat(fname, &statbuf) == 0);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   243
} /* __PHYSFS_platformExists */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   244
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   245
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   246
int __PHYSFS_platformIsSymLink(const char *fname)
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
{
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   248
#if (defined __PHYSFS_NO_SYMLINKS__)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   249
    return(0);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   250
#else
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   251
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   252
    struct stat statbuf;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   253
    int retval = 0;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   254
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   255
    if (lstat(fname, &statbuf) == 0)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   256
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   257
        if (S_ISLNK(statbuf.st_mode))
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   258
            retval = 1;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   259
    } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   260
    
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   261
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   262
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   263
#endif
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
} /* __PHYSFS_platformIsSymlink */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
int __PHYSFS_platformIsDirectory(const char *fname)
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
{
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   269
    struct stat statbuf;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   270
    int retval = 0;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   271
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   272
    if (stat(fname, &statbuf) == 0)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   273
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   274
        if (S_ISDIR(statbuf.st_mode))
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   275
            retval = 1;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   276
    } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   277
    
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   278
    return(retval);
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
} /* __PHYSFS_platformIsDirectory */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   282
char *__PHYSFS_platformCvtToDependent(const char *prepend,
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   283
                                      const char *dirName,
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   284
                                      const char *append)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   285
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   286
    int len = ((prepend) ? strlen(prepend) : 0) +
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   287
              ((append) ? strlen(append) : 0) +
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   288
              strlen(dirName) + 1;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   289
    char *retval = malloc(len);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   290
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   291
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   292
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   293
    /* platform-independent notation is Unix-style already.  :)  */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   294
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   295
    if (prepend)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   296
        strcpy(retval, prepend);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   297
    else
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   298
        retval[0] = '\0';
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   299
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   300
    strcat(retval, dirName);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   301
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   302
    if (append)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   303
        strcat(retval, append);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   304
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   305
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   306
} /* __PHYSFS_platformCvtToDependent */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   307
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   308
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   309
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   310
void __PHYSFS_platformTimeslice(void)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   311
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   312
    struct timespec napTime;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   313
    napTime.tv_sec = 0;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   314
    napTime.tv_nsec = 10 * 1000 * 1000;  /* specified in nanoseconds. */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   315
    nanosleep(&napTime, NULL);           /* don't care if it fails. */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   316
} /* __PHYSFS_platformTimeslice */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   317
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   318
41
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   319
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   320
                                                  int omitSymLinks)
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   321
{
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   322
    LinkedStringList *retval = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   323
    LinkedStringList *l = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   324
    LinkedStringList *prev = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   325
    DIR *dir;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   326
    struct dirent *ent;
41
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   327
    int bufsize = 0;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   328
    char *buf = NULL;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   329
    int dlen = 0;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   330
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   331
    if (omitSymLinks)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   332
    {
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   333
        dlen = strlen(dirname);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   334
        bufsize = dlen + 256;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   335
        buf = malloc(bufsize);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   336
        BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   337
        strcpy(buf, dirname);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   338
        if (buf[dlen - 1] != '/')
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   339
        {
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   340
            buf[dlen++] = '/';
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   341
            buf[dlen] = '\0';
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   342
        } /* if */
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   343
    } /* if */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   344
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   345
    errno = 0;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   346
    dir = opendir(dirname);
41
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   347
    if (dir == NULL)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   348
    {
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   349
        if (buf != NULL)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   350
            free(buf);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   351
        BAIL_IF_MACRO(1, strerror(errno), NULL);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   352
    } /* if */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   353
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   354
    while (1)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   355
    {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   356
        ent = readdir(dir);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   357
        if (ent == NULL)   /* we're done. */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   358
            break;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   359
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   360
        if (strcmp(ent->d_name, ".") == 0)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   361
            continue;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   362
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   363
        if (strcmp(ent->d_name, "..") == 0)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   364
            continue;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   365
41
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   366
        if (omitSymLinks)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   367
        {
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   368
            char *p;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   369
            int len = strlen(ent->d_name) + dlen + 1;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   370
            if (len > bufsize)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   371
            {
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   372
                p = realloc(buf, len);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   373
                if (p == NULL)
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   374
                    continue;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   375
                buf = p;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   376
                bufsize = len;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   377
            } /* if */
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   378
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   379
            strcpy(buf + dlen, ent->d_name);
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   380
            if (__PHYSFS_platformIsSymLink(buf))
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   381
                continue;
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   382
        } /* if */
ff60d39d0862 Architecture adjustment for enumerating files with regards to whether
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   383
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   384
        l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   385
        if (l == NULL)
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   386
            break;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   387
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   388
        l->str = (char *) malloc(strlen(ent->d_name) + 1);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   389
        if (l->str == NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   390
        {
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   391
            free(l);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   392
            break;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   393
        } /* if */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   394
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   395
        strcpy(l->str, ent->d_name);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   396
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   397
        if (retval == NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   398
            retval = l;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   399
        else
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   400
            prev->next = l;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   401
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   402
        prev = l;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   403
        l->next = NULL;
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   404
    } /* while */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   405
67
5000499d4ee2 Fixed a potential free()ing of a NULL pointer in
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
   406
    if (buf != NULL)
5000499d4ee2 Fixed a potential free()ing of a NULL pointer in
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
   407
        free(buf);
5000499d4ee2 Fixed a potential free()ing of a NULL pointer in
Ryan C. Gordon <icculus@icculus.org>
parents: 63
diff changeset
   408
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   409
    closedir(dir);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   410
    return(retval);
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   411
} /* __PHYSFS_platformEnumerateFiles */
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   412
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   413
28
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   414
int __PHYSFS_platformFileLength(FILE *handle)
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   415
{
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   416
    struct stat statbuf;
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   417
    errno = 0;
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   418
    BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   419
    return(statbuf.st_size);
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   420
} /* __PHYSFS_platformFileLength */
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   421
529214f57d1b Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   422
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   423
char *__PHYSFS_platformCurrentDir(void)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   424
{
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   425
    int allocSize = 0;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   426
    char *retval = NULL;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   427
    char *ptr;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   428
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   429
    do
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   430
    {
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   431
        allocSize += 100;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   432
        ptr = (char *) realloc(retval, allocSize);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   433
        if (ptr == NULL)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   434
        {
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   435
            if (retval != NULL)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   436
                free(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   437
            BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   438
        } /* if */
100
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   439
	
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   440
        retval = ptr;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   441
        ptr = getcwd(retval, allocSize);
100
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   442
    } while (ptr == NULL && errno == ERANGE);
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   443
    if(ptr == NULL && errno) {
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   444
	/* getcwd() failed for some reason, for example current
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   445
	 * directory not existing.
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   446
	 */
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   447
	if (retval != NULL)
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   448
	    free(retval);
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   449
	BAIL_IF_MACRO(1, ERR_NO_SUCH_FILE, NULL);
d2f04ab4b412 Patches from David Hedbor to handle missing current working directories.
Ryan C. Gordon <icculus@icculus.org>
parents: 79
diff changeset
   450
    }
39
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   451
    return(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   452
} /* __PHYSFS_platformCurrentDir */
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   453
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   454
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   455
char *__PHYSFS_platformRealPath(const char *path)
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   456
{
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   457
    char resolved_path[MAXPATHLEN];
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   458
    char *retval = NULL;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   459
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   460
    errno = 0;
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   461
    BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   462
    retval = malloc(strlen(resolved_path) + 1);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   463
    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   464
    strcpy(retval, resolved_path);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   465
    return(retval);
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   466
} /* __PHYSFS_platformRealPath */
bc29e1ee7ef6 Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   467
69
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   468
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   469
int __PHYSFS_platformMkDir(const char *path)
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   470
{
70
203efbe2532e Whoops. Forgot to declare a variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 69
diff changeset
   471
    int rc;
69
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   472
    errno = 0;
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   473
    rc = mkdir(path, S_IRWXU);
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   474
    BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   475
    return(1);
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   476
} /* __PHYSFS_platformMkDir */
546a95cc5591 Updates, corrections and enhancements to get this ported to win32.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
   477
17
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
/* end of unix.c ... */
7337737f5120 Moved from root source dir.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479