physfs.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 09 Jul 2001 04:15:35 +0000
changeset 28 529214f57d1b
parent 27 e1d3292769b2
child 39 bc29e1ee7ef6
permissions -rw-r--r--
Added PHYSFS_fileLength(). Bleh.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * PhysicsFS; a portable, flexible file i/o abstraction.
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * Documentation is in physfs.h. It's verbose, honest.  :)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * Please see the file LICENSE in the source's root directory.
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  This file written by Ryan C. Gordon.
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdio.h>
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <stdlib.h>
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <string.h>
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
    14
#include <unistd.h>
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <assert.h>
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include "physfs.h"
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    18
#define __PHYSICSFS_INTERNAL__
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    19
#include "physfs_internal.h"
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    20
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
typedef struct __PHYSFS_ERRMSGTYPE__
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    int tid;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    int errorAvailable;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
    char errorString[80];
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    struct __PHYSFS_ERRMSGTYPE__ *next;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
} ErrMsg;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    29
typedef struct __PHYSFS_DIRINFO__
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
    char *dirName;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    32
    DirHandle *dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    33
    struct __PHYSFS_DIRINFO__ *next;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    34
} DirInfo;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    36
typedef struct __PHYSFS_FILEHANDLELIST__
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    37
{
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
    38
    PHYSFS_file handle;
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    39
    struct __PHYSFS_FILEHANDLELIST__ *next;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    40
} FileHandleList;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    42
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    43
/* The various i/o drivers... */
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    45
#if (defined PHYSFS_SUPPORTS_ZIP)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    46
extern const PHYSFS_ArchiveInfo   __PHYSFS_ArchiveInfo_ZIP;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    47
extern const DirFunctions         __PHYSFS_DirFunctions_ZIP;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    48
#endif
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    49
22
49f6101707b4 More tweaks; GRP completely implemented. Everything builds clean.
Ryan C. Gordon <icculus@icculus.org>
parents: 21
diff changeset
    50
#if (defined PHYSFS_SUPPORTS_GRP)
49f6101707b4 More tweaks; GRP completely implemented. Everything builds clean.
Ryan C. Gordon <icculus@icculus.org>
parents: 21
diff changeset
    51
extern const PHYSFS_ArchiveInfo   __PHYSFS_ArchiveInfo_GRP;
49f6101707b4 More tweaks; GRP completely implemented. Everything builds clean.
Ryan C. Gordon <icculus@icculus.org>
parents: 21
diff changeset
    52
extern const DirFunctions         __PHYSFS_DirFunctions_GRP;
49f6101707b4 More tweaks; GRP completely implemented. Everything builds clean.
Ryan C. Gordon <icculus@icculus.org>
parents: 21
diff changeset
    53
#endif
49f6101707b4 More tweaks; GRP completely implemented. Everything builds clean.
Ryan C. Gordon <icculus@icculus.org>
parents: 21
diff changeset
    54
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    55
extern const DirFunctions  __PHYSFS_DirFunctions_DIR;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    56
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    57
static const PHYSFS_ArchiveInfo *supported_types[] =
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
#if (defined PHYSFS_SUPPORTS_ZIP)
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    60
    &__PHYSFS_ArchiveInfo_ZIP,
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
#endif
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
21
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    63
#if (defined PHYSFS_SUPPORTS_GRP)
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    64
    &__PHYSFS_ArchiveInfo_GRP,
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    65
#endif
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    66
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    NULL
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
};
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    70
static const DirFunctions *dirFunctions[] =
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    71
{
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    72
#if (defined PHYSFS_SUPPORTS_ZIP)
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    73
    &__PHYSFS_DirFunctions_ZIP,
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    74
#endif
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    75
21
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    76
#if (defined PHYSFS_SUPPORTS_GRP)
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    77
    &__PHYSFS_DirFunctions_GRP,
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    78
#endif
b1ea58d70a56 Archive implementation (Build Groupfiles), other tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 20
diff changeset
    79
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    80
    &__PHYSFS_DirFunctions_DIR,
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    81
    NULL
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    82
};
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    83
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    86
/* General PhysicsFS state ... */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    87
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    88
static int initialized = 0;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    89
static ErrMsg *errorMessages = NULL;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    90
static DirInfo *searchPath = NULL;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
    91
static DirInfo *writeDir = NULL;
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    92
static FileHandleList *openWriteList = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    93
static FileHandleList *openReadList = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    94
static char *baseDir = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    95
static char *userDir = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    96
static int allowSymLinks = 0;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    97
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    98
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    99
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   100
/* functions ... */
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
static ErrMsg *findErrorForCurrentThread(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    ErrMsg *i;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    int tid;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    if (errorMessages != NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        tid = __PHYSFS_platformGetThreadID();
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        for (i = errorMessages; i != NULL; i = i->next)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
            if (i->tid == tid)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
                return(i);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        } /* for */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    return(NULL);   /* no error available. */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
} /* findErrorForCurrentThread */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   122
void __PHYSFS_setError(const char *str)
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   124
    ErrMsg *err;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   125
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   126
    if (str == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   127
        return;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   128
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   129
    err = findErrorForCurrentThread();
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    if (err == NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        err = (ErrMsg *) malloc(sizeof (ErrMsg));
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        if (err == NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
            return;   /* uhh...? */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   137
        err->tid = __PHYSFS_platformGetThreadID();
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        err->next = errorMessages;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   139
        errorMessages = err;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    err->errorAvailable = 1;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    strncpy(err->errorString, str, sizeof (err->errorString));
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    err->errorString[sizeof (err->errorString) - 1] = '\0';
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   145
} /* __PHYSFS_setError */
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   148
static void freeErrorMessages(void)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   149
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   150
    ErrMsg *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   151
    ErrMsg *next;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   152
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   153
    for (i = errorMessages; i != NULL; i = next)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   154
    {
25
96b32c0fd7f3 Bugfix for infinite loop/touching free()'d memory during PHYSFS_deinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   155
        next = i->next;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   156
        free(i);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   157
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   158
} /* freeErrorMessages */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   159
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   160
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
const char *PHYSFS_getLastError(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    ErrMsg *err = findErrorForCurrentThread();
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    if ((err == NULL) || (!err->errorAvailable))
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        return(NULL);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    err->errorAvailable = 0;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    return(err->errorString);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
} /* PHYSFS_getLastError */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    if (ver != NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        ver->major = PHYSFS_VER_MAJOR;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        ver->minor = PHYSFS_VER_MINOR;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        ver->patch = PHYSFS_VER_PATCH;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
} /* PHYSFS_getLinkedVersion */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   184
static DirHandle *openDirectory(const char *d, int forWriting)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   185
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   186
    const DirFunctions **i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   187
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   188
    for (i = dirFunctions; *i != NULL; i++)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   189
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   190
        if ((*i)->isArchive(d, forWriting))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   191
            return( (*i)->openArchive(d, forWriting) );
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   192
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   193
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   194
    __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   195
    return(NULL);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   196
} /* openDirectory */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   197
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   198
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   199
static DirInfo *buildDirInfo(const char *newDir, int forWriting)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   200
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   201
    DirHandle *dirHandle = NULL;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   202
    DirInfo *di = NULL;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   203
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   204
    BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   205
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   206
    dirHandle = openDirectory(newDir, forWriting);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   207
    BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   208
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   209
    di = (DirInfo *) malloc(sizeof (DirInfo));
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   210
    if (di == NULL)
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   211
        dirHandle->funcs->dirClose(dirHandle);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   212
    BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   213
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   214
    di->dirName = (char *) malloc(strlen(newDir) + 1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   215
    if (di->dirName == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   216
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   217
        free(di);
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   218
        dirHandle->funcs->dirClose(dirHandle);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   219
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   220
        return(0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   221
    } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   222
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   223
    di->next = NULL;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   224
    di->dirHandle = dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   225
    strcpy(di->dirName, newDir);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   226
    return(di);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   227
} /* buildDirInfo */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   228
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   229
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   230
static int freeDirInfo(DirInfo *di, FileHandleList *openList)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   231
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   232
    FileHandleList *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   233
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   234
    if (di == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   235
        return(1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   236
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   237
    for (i = openList; i != NULL; i = i->next)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   238
    {
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
   239
        const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   240
        BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   241
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   242
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   243
    di->dirHandle->funcs->dirClose(di->dirHandle);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   244
    free(di->dirName);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   245
    free(di);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   246
    return(1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   247
} /* freeDirInfo */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   248
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   249
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   250
static char *calculateUserDir(void)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   251
{
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   252
    char *retval = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   253
    const char *str = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   254
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   255
    str = __PHYSFS_platformGetUserDir();
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   256
    if (str != NULL)
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   257
        retval = (char *) str;
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   258
    else
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   259
    {
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   260
        const char *dirsep = PHYSFS_getDirSeparator();
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   261
        const char *uname = __PHYSFS_platformGetUserName();
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   262
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   263
        str = (uname != NULL) ? uname : "default";
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   264
        retval = (char *) malloc(strlen(baseDir) + strlen(str) +
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   265
                                 (strlen(dirsep) * 2) + 6);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   266
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   267
        if (retval == NULL)
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   268
            __PHYSFS_setError(ERR_OUT_OF_MEMORY);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   269
        else
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   270
            sprintf(retval, "%s%susers%s%s", baseDir, dirsep, dirsep, str);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   271
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   272
        if (uname != NULL)
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   273
            free((void *) uname);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   274
    } /* else */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   275
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   276
    return(retval);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   277
} /* calculateUserDir */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   278
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   279
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   280
static int appendDirSep(char **dir)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   281
{
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   282
    const char *dirsep = PHYSFS_getDirSeparator();
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   283
    char *ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   284
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   285
    if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   286
        return(1);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   287
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   288
    ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   289
    if (!ptr)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   290
    {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   291
        free(*dir);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   292
        return(0);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   293
    } /* if */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   294
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   295
    strcat(ptr, dirsep);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   296
    *dir = ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   297
    return(1);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   298
} /* appendDirSep */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   299
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   300
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   301
static char *calculateBaseDir(const char *argv0)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   302
{
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   303
    const char *dirsep = PHYSFS_getDirSeparator();
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   304
    char *retval;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   305
    char *ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   306
    int allocSize = 0;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   307
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   308
    /*
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   309
     * See if the platform driver wants to handle this for us...
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   310
     */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   311
    retval = __PHYSFS_platformCalcBaseDir(argv0);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   312
    if (retval != NULL)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   313
        return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   314
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   315
    /*
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   316
     * Determine if there's a path on argv0. If there is, that's the base dir.
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   317
     */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   318
    ptr = strstr(argv0, dirsep);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   319
    if (ptr != NULL)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   320
    {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   321
        char *p = ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   322
        size_t size;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   323
        while (p != NULL)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   324
        {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   325
            ptr = p;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   326
            p = strstr(p + 1, dirsep);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   327
        } /* while */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   328
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   329
        size = (size_t) (ptr - argv0);  /* !!! is this portable? */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   330
        retval = (char *) malloc(size + 1);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   331
        BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   332
        memcpy(retval, argv0, size);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   333
        retval[size] = '\0';
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   334
        return(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   335
    } /* if */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   336
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   337
    /*
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   338
     * Last ditch effort: it's the current working directory. (*shrug*)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   339
     */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   340
    do
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   341
    {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   342
        allocSize += 100;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   343
        ptr = (char *) realloc(retval, allocSize);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   344
        if (ptr == NULL)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   345
        {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   346
            if (retval != NULL)
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   347
                free(retval);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   348
            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: 22
diff changeset
   349
        } /* if */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   350
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   351
        retval = ptr;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   352
        ptr = getcwd(retval, allocSize);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   353
    } while (ptr == NULL);
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   354
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   355
    return(retval);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   356
} /* calculateBaseDir */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   357
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   358
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
int PHYSFS_init(const char *argv0)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   364
    baseDir = calculateBaseDir(argv0);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   365
    BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   366
    BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   367
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   368
    userDir = calculateUserDir();
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   369
    if ((userDir == NULL) || (!appendDirSep(&userDir)))
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   370
    {
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   371
        free(baseDir);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   372
        baseDir = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   373
        return(0);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   374
    } /* if */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   375
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    initialized = 1;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    return(1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
} /* PHYSFS_init */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   381
static int closeFileHandleList(FileHandleList **list)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   382
{
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   383
    FileHandleList *i;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   384
    FileHandleList *next = NULL;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   385
    FileHandle *h;
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   386
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   387
    for (i = *list; i != NULL; i = next)
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   388
    {
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   389
        next = i->next;
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
   390
        h = (FileHandle *) (i->handle.opaque);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
   391
        if (!h->funcs->fileClose(h))
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   392
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   393
            *list = i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   394
            return(0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   395
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   396
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   397
        free(i);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   398
    } /* for */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   399
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   400
    *list = NULL;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   401
    return(1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   402
} /* closeFileHandleList */
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   403
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   404
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
static void freeSearchPath(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   407
    DirInfo *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   408
    DirInfo *next = NULL;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   410
    closeFileHandleList(&openReadList);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   411
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    if (searchPath != NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
        for (i = searchPath; i != NULL; i = next)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
        {
25
96b32c0fd7f3 Bugfix for infinite loop/touching free()'d memory during PHYSFS_deinit().
Ryan C. Gordon <icculus@icculus.org>
parents: 23
diff changeset
   416
            next = i->next;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   417
            freeDirInfo(i, openReadList);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
        } /* for */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
        searchPath = NULL;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
    } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
} /* freeSearchPath */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   424
int PHYSFS_deinit(void)
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   428
    closeFileHandleList(&openWriteList);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   429
    BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   430
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    freeSearchPath();
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    freeErrorMessages();
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   434
    if (baseDir != NULL)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   435
    {
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   436
        free(baseDir);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   437
        baseDir = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   438
    } /* if */
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   439
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   440
    if (userDir != NULL)
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   441
    {
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   442
        free(userDir);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   443
        userDir = NULL;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   444
    } /* if */
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   445
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   446
    allowSymLinks = 0;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
    initialized = 0;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    return(1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
} /* PHYSFS_deinit */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    return(supported_types);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
} /* PHYSFS_supportedArchiveTypes */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
void PHYSFS_freeList(void *list)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    void **i;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    for (i = (void **) list; *i != NULL; i++)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
        free(*i);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
    free(list);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
} /* PHYSFS_freeList */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
const char *PHYSFS_getDirSeparator(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   470
    return(__PHYSFS_platformDirSeparator);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
} /* PHYSFS_getDirSeparator */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
char **PHYSFS_getCdRomDirs(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    return(__PHYSFS_platformDetectAvailableCDs());
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
} /* PHYSFS_getCdRomDirs */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
const char *PHYSFS_getBaseDir(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
    return(baseDir);   /* this is calculated in PHYSFS_init()... */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
} /* PHYSFS_getBaseDir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
const char *PHYSFS_getUserDir(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
{
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   488
    return(userDir);   /* this is calculated in PHYSFS_init()... */
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
} /* PHYSFS_getUserDir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
const char *PHYSFS_getWriteDir(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   494
    if (writeDir == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   495
        return(NULL);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   496
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   497
    return(writeDir->dirName);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
} /* PHYSFS_getWriteDir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
int PHYSFS_setWriteDir(const char *newDir)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
    if (writeDir != NULL)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
    {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   505
        BAIL_IF_MACRO(!freeDirInfo(writeDir, openWriteList), NULL, 0);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        writeDir = NULL;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   509
    if (newDir != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   510
    {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   511
        writeDir = buildDirInfo(newDir, 1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   512
        return(writeDir != NULL);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   513
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   514
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    return(1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
} /* PHYSFS_setWriteDir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   521
    DirInfo *di = buildDirInfo(newDir, 0);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   523
    BAIL_IF_MACRO(di == NULL, NULL, 0);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    if (appendToPath)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   527
        DirInfo *i = searchPath;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   528
        DirInfo *prev = NULL;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   530
        di->next = NULL;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
        while (i != NULL)
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   532
        {
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
            prev = i;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   534
            i = i->next;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   535
        } /* while */
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
        if (prev == NULL)
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   538
            searchPath = di;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
        else
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   540
            prev->next = di;
23
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   541
    } /* if */
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   542
    else
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   543
    {
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   544
        di->next = searchPath;
bd6ba9c8717c Initial debugging: dropped PhysicsFS routines into the Build engine,
Ryan C. Gordon <icculus@icculus.org>
parents: 22
diff changeset
   545
        searchPath = di;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    } /* else */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    return(1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
} /* PHYSFS_addToSearchPath */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
int PHYSFS_removeFromSearchPath(const char *oldDir)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   554
    DirInfo *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   555
    DirInfo *prev = NULL;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   556
    DirInfo *next = NULL;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
    BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
    for (i = searchPath; i != NULL; i = i->next)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
        if (strcmp(i->dirName, oldDir) == 0)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
        {
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   564
            next = i->next;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   565
            BAIL_IF_MACRO(!freeDirInfo(i, openReadList), NULL, 0);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   566
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
            if (prev == NULL)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   568
                searchPath = next;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
            else
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   570
                prev->next = next;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   571
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
            return(1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
        } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
        prev = i;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
    } /* for */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   577
    __PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    return(0);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
} /* PHYSFS_removeFromSearchPath */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
char **PHYSFS_getSearchPath(void)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
{
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    int count = 1;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
    int x;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   586
    DirInfo *i;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    char **retval;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
    for (i = searchPath; i != NULL; i = i->next)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
        count++;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
    retval = (char **) malloc(sizeof (char *) * count);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
    count--;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
    retval[count] = NULL;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
    for (i = searchPath, x = 0; x < count; i = i->next, x++)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
    {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
        retval[x] = (char *) malloc(strlen(i->dirName) + 1);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
        if (retval[x] == NULL)  /* this is friggin' ugly. */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
        {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
            while (x > 0)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
            {
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
                x--;
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
                free(retval[x]);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
            } /* while */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
            free(retval);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   609
            __PHYSFS_setError(ERR_OUT_OF_MEMORY);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
            return(NULL);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
        } /* if */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
        strcpy(retval[x], i->dirName);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
    } /* for */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
    return(retval);
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
} /* PHYSFS_getSearchPath */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   620
int PHYSFS_setSaneConfig(const char *appName, const char *archiveExt,
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
                         int includeCdRoms, int archivesFirst)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
{
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   623
    const char *basedir = PHYSFS_getBaseDir();
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   624
    const char *userdir = PHYSFS_getUserDir();
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   625
    const char *dirsep = PHYSFS_getDirSeparator();
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   626
    char *str;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   627
    int rc;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   628
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   629
        /* set write dir... */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   630
    str = malloc(strlen(userdir) + (strlen(appName) * 2) +
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   631
                 (strlen(dirsep) * 2) + 2);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   632
    BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   633
    sprintf(str, "%s%s.%s", userdir, dirsep, appName);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   634
    rc = PHYSFS_setWriteDir(str);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   635
    BAIL_IF_MACRO(!rc, NULL, 0);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   636
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   637
        /* Put write dir related dirs on search path... */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   638
    PHYSFS_addToSearchPath(str, 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   639
    PHYSFS_mkdir(appName); /* don't care if this fails. */
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   640
    strcat(str, dirsep);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   641
    strcat(str, appName);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   642
    PHYSFS_addToSearchPath(str, 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   643
    free(str);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   644
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   645
        /* Put base path stuff on search path... */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   646
    PHYSFS_addToSearchPath(basedir, 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   647
    str = malloc(strlen(basedir) + (strlen(appName) * 2) +
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   648
                 (strlen(dirsep) * 2) + 2);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   649
    if (str != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   650
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   651
        sprintf(str, "%s%s.%s", basedir, dirsep, appName);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   652
        PHYSFS_addToSearchPath(str, 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   653
        free(str);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   654
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   655
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   656
        /* handle CD-ROMs... */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   657
    if (includeCdRoms)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   658
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   659
        char **cds = PHYSFS_getCdRomDirs();
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   660
        char **i;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   661
        for (i = cds; *i != NULL; i++)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   662
        {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   663
            PHYSFS_addToSearchPath(*i, 1);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   664
            str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   665
            if (str != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   666
            {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   667
                sprintf(str, "%s%s%s", *i, dirsep, appName);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   668
                PHYSFS_addToSearchPath(str, 1);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   669
                free(str);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   670
            } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   671
        } /* for */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   672
        PHYSFS_freeList(cds);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   673
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   674
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   675
        /* Root out archives, and add them to search path... */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   676
    if (archiveExt != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   677
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   678
        char **rc = PHYSFS_enumerateFiles("");
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   679
        char **i;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   680
        int extlen = strlen(archiveExt);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   681
        char *ext;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   682
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   683
        for (i = rc; *i != NULL; i++)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   684
        {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   685
            int l = strlen(*i);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   686
            if ((l > extlen) && ((*i)[l - extlen - 1] == '.'));
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   687
            {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   688
                ext = (*i) + (l - extlen);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   689
                if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   690
                {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   691
                    const char *d = PHYSFS_getRealDir(*i);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   692
                    str = malloc(strlen(d) + strlen(dirsep) + l + 1);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   693
                    if (str != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   694
                    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   695
                        sprintf(str, "%s%s%s", d, dirsep, *i);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   696
                        PHYSFS_addToSearchPath(str, archivesFirst == 0);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   697
                        free(str);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   698
                    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   699
                } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   700
            } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   701
        } /* for */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   702
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   703
        PHYSFS_freeList(rc);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   704
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   705
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   706
    return(1);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
} /* PHYSFS_setSaneConfig */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   710
void PHYSFS_permitSymbolicLinks(int allow)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   711
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   712
    allowSymLinks = allow;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   713
} /* PHYSFS_permitSymbolicLinks */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   714
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   715
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   716
/* string manipulation in C makes my ass itch. */
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   717
char *__PHYSFS_convertToDependent(const char *prepend,
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   718
                                  const char *dirName,
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   719
                                  const char *append)
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   720
{
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   721
    const char *dirsep = PHYSFS_getDirSeparator();
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   722
    int sepsize = strlen(dirsep);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   723
    char *str;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   724
    char *i1;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   725
    char *i2;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   726
    size_t allocSize;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   727
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   728
    allocSize = strlen(dirName) + 1;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   729
    if (prepend != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   730
        allocSize += strlen(prepend) + sepsize;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   731
    if (append != NULL)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   732
        allocSize += strlen(append) + sepsize;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   733
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   734
        /* make sure there's enough space if the dir separator is bigger. */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   735
    if (sepsize > 1)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   736
    {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   737
        str = (char *) dirName;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   738
        do
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   739
        {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   740
            str = strchr(str, '/');
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   741
            if (str != NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   742
            {
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   743
                allocSize += (sepsize - 1);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   744
                str++;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   745
            } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   746
        } while (str != NULL);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   747
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   748
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   749
    str = (char *) malloc(allocSize);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   750
    BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   751
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   752
    if (prepend == NULL)
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   753
        *str = '\0';
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   754
    else
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   755
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   756
        strcpy(str, prepend);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   757
        strcat(str, dirsep);
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   758
    } /* else */
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   759
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   760
    for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   761
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   762
        if (*i1 == '/')
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   763
        {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   764
            strcpy(i2, dirsep);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   765
            i2 += sepsize;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   766
        } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   767
        else
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   768
        {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   769
            *i2 = *i1;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   770
        } /* else */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   771
    } /* for */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   772
    *i2 = '\0';
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   773
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   774
    if (append)
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   775
    {
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   776
        strcat(str, dirsep);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   777
        strcpy(str, append);
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   778
    } /* if */
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   779
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   780
    return(str);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   781
} /* __PHYSFS_convertToDependentNotation */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   782
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   783
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   784
int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   785
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   786
    int retval = 1;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   787
    char *start;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   788
    char *end;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   789
    char *str;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   790
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   791
    start = str = malloc(strlen(fname) + 1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   792
    BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   793
    strcpy(str, fname);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   794
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   795
    while (1)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   796
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   797
        end = strchr(start, '/');
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   798
        if (end != NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   799
            *end = '\0';
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   800
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   801
        if ( (strcmp(start, ".") == 0) ||
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   802
             (strcmp(start, "..") == 0) ||
20
efdde0d21521 Implementation compiles and links with no actual archive support. No test
Ryan C. Gordon <icculus@icculus.org>
parents: 19
diff changeset
   803
             (strchr(start, '\\') != NULL) ||
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   804
             (strchr(start, ':') != NULL) )
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   805
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   806
            __PHYSFS_setError(ERR_INSECURE_FNAME);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   807
            retval = 0;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   808
            break;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   809
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   810
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   811
        if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   812
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   813
            __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   814
            retval = 0;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   815
            break;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   816
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   817
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   818
        if (end == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   819
            break;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   820
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   821
        *end = '/';
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   822
        start = end + 1;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   823
    } /* while */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   824
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   825
    free(str);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   826
    return(retval);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   827
} /* __PHYSFS_verifySecurity */
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   828
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   829
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
int PHYSFS_mkdir(const char *dirName)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   832
    DirHandle *h;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   833
    char *str;
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   834
    char *start;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   835
    char *end;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   836
    int retval = 0;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   837
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   838
    BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   839
    h = writeDir->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   840
    BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   841
    BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   842
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   843
    start = str = malloc(strlen(dirName) + 1);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   844
    BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   845
    strcpy(str, dirName);
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   846
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   847
    while (1)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   848
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   849
        end = strchr(start, '/');
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   850
        if (end != NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   851
            *end = '\0';
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   852
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   853
        retval = h->funcs->mkdir(h, str);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   854
        if (!retval)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   855
            break;
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   856
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   857
        if (end == NULL)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   858
            break;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   859
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   860
        *end = '/';
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   861
        start = end + 1;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   862
    } /* while */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   863
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   864
    free(str);
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   865
    return(retval);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
} /* PHYSFS_mkdir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   867
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   868
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   869
int PHYSFS_delete(const char *fname)
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   871
    DirHandle *h;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   872
    BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   873
    h = writeDir->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   874
    BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   875
    BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   876
    return(h->funcs->remove(h, fname));
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   877
} /* PHYSFS_delete */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   879
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   880
const char *PHYSFS_getRealDir(const char *filename)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   881
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   882
    DirInfo *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   883
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   884
    for (i = searchPath; i != NULL; i = i->next)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   885
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   886
        DirHandle *h = i->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   887
        if (__PHYSFS_verifySecurity(h, filename))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   888
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   889
            if (h->funcs->exists(h, filename))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   890
                return(i->dirName);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   891
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   892
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   893
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   894
    return(NULL);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   895
} /* PHYSFS_getRealDir */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   897
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   898
static int countList(LinkedStringList *list)
12
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   899
{
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   900
    int retval = 0;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   901
    LinkedStringList *i;
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   902
12
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   903
    assert(list != NULL);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   904
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   905
    for (i = list; i != NULL; i = i->next)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   906
        retval++;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   907
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   908
    return(retval);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   909
} /* countList */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   910
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   911
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   912
static char **convertStringListToPhysFSList(LinkedStringList *finalList)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   913
{
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   914
    int i;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   915
    LinkedStringList *next = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   916
    int len = countList(finalList);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   917
    char **retval = (char **) malloc((len + 1) * sizeof (char *));
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   918
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   919
    if (retval == NULL)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   920
        __PHYSFS_setError(ERR_OUT_OF_MEMORY);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   921
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   922
    for (i = 0; i < len; i++)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   923
    {
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   924
        next = finalList->next;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   925
        if (retval == NULL)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   926
            free(finalList->str);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   927
        else
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   928
            retval[i] = finalList->str;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   929
        free(finalList);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   930
        finalList = next;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   931
    } /* for */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   932
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   933
    if (retval != NULL);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   934
        retval[i] = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   935
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   936
    return(retval);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   937
} /* convertStringListToPhysFSList */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   938
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   939
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   940
static void insertStringListItem(LinkedStringList **final,
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   941
                                 LinkedStringList *item)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   942
{
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   943
    LinkedStringList *i;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   944
    LinkedStringList *prev = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   945
    int rc;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   946
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   947
    for (i = *final; i != NULL; i = i->next)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   948
    {
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   949
        rc = strcmp(i->str, item->str);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   950
        if (rc == 0)      /* already in list. */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   951
        {
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   952
            free(item->str);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   953
            free(item);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   954
            return;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   955
        } /* if */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   956
        else if (rc > 0)  /* insertion point. */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   957
        {
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   958
            if (prev == NULL)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   959
                *final = item;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   960
            else
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   961
                prev->next = item;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   962
            item->next = i;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   963
            return;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   964
        } /* else if */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   965
        prev = i;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   966
    } /* for */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   967
} /* insertStringListItem */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   968
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   969
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   970
/* if we run out of memory anywhere in here, we give back what we can. */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   971
static void interpolateStringLists(LinkedStringList **final,
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   972
                                    LinkedStringList *newList)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   973
{
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   974
    LinkedStringList *next = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   975
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   976
    while (newList != NULL)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   977
    {
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   978
        next = newList->next;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   979
        insertStringListItem(final, newList);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   980
        newList = next;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   981
    } /* while */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   982
} /* interpolateStringLists */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   983
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   984
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
char **PHYSFS_enumerateFiles(const char *path)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
{
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   987
    DirInfo *i;
12
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   988
    char **retval = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   989
    LinkedStringList *rc;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   990
    LinkedStringList *finalList = NULL;
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   991
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   992
    for (i = searchPath; i != NULL; i = i->next)
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   993
    {
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   994
        DirHandle *h = i->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   995
        if (__PHYSFS_verifySecurity(h, path))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   996
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   997
            rc = h->funcs->enumerateFiles(h, path);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   998
            interpolateStringLists(&finalList, rc);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   999
        } /* if */
12
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
  1000
    } /* for */
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
  1001
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
  1002
    retval = convertStringListToPhysFSList(finalList);
a4041c91d715 Redesign of enumerateFiles code. More efficient and clean, less memory
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
  1003
    return(retval);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
} /* PHYSFS_enumerateFiles */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
15
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1007
int PHYSFS_exists(const char *fname)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1008
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1009
    return(PHYSFS_getRealDir(fname) != NULL);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1010
} /* PHYSFS_exists */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1011
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1012
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1013
int PHYSFS_isDirectory(const char *fname)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1014
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1015
    DirInfo *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1016
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1017
    for (i = searchPath; i != NULL; i = i->next)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1018
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1019
        DirHandle *h = i->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1020
        if (__PHYSFS_verifySecurity(h, fname))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1021
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1022
            if (h->funcs->exists(h, fname))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1023
                return(h->funcs->isDirectory(h, fname));
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1024
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1025
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1026
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1027
    return(0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1028
} /* PHYSFS_isDirectory */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1029
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1030
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1031
int PHYSFS_isSymbolicLink(const char *fname)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1032
{
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1033
    DirInfo *i;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1034
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1035
    if (!allowSymLinks)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1036
        return(0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1037
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1038
    for (i = searchPath; i != NULL; i = i->next)
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1039
    {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1040
        DirHandle *h = i->dirHandle;
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1041
        if (__PHYSFS_verifySecurity(h, fname))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1042
        {
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1043
            if (h->funcs->exists(h, fname))
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1044
                return(h->funcs->isSymLink(h, fname));
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1045
        } /* if */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1046
    } /* for */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1047
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1048
    return(0);
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1049
} /* PHYSFS_isSymbolicLink */
418eacc97ac8 Tons of updates. Mostly implemented. Mostly compiling.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
  1050
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1051
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1052
static PHYSFS_file *doOpenWrite(const char *fname, int appending)
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1053
{
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1054
    PHYSFS_file *retval = NULL;
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1055
    FileHandle *rc = NULL;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1056
    DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1057
    const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1058
    FileHandleList *list;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1059
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1060
    BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1061
    BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1062
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1063
    list = (FileHandleList *) malloc(sizeof (FileHandleList));
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1064
    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1065
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1066
    rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1067
    if (rc == NULL)
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1068
        free(list);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1069
    else
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1070
    {
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1071
        list->handle.opaque = (void *) rc;
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1072
        list->next = openWriteList;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1073
        openWriteList = list;
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1074
        retval = &(list->handle);
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1075
    } /* else */
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1076
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1077
    return(retval);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1078
} /* doOpenWrite */
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1079
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1080
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1081
PHYSFS_file *PHYSFS_openWrite(const char *filename)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1082
{
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1083
    return(doOpenWrite(filename, 0));
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1084
} /* PHYSFS_openWrite */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1085
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1086
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1087
PHYSFS_file *PHYSFS_openAppend(const char *filename)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1088
{
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1089
    return(doOpenWrite(filename, 1));
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1090
} /* PHYSFS_openAppend */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1091
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1092
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1093
PHYSFS_file *PHYSFS_openRead(const char *fname)
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1094
{
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1095
    PHYSFS_file *retval = NULL;
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1096
    FileHandle *rc = NULL;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1097
    FileHandleList *list;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1098
    DirInfo *i;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1099
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1100
    list = (FileHandleList *) malloc(sizeof (FileHandleList));
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1101
    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1102
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1103
    for (i = searchPath; i != NULL; i = i->next)
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1104
    {
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1105
        DirHandle *h = i->dirHandle;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1106
        if (__PHYSFS_verifySecurity(h, fname))
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1107
        {
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1108
            rc = h->funcs->openRead(h, fname);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1109
            if (rc != NULL)
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1110
                break;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1111
        } /* if */
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1112
    } /* for */
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1113
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1114
    if (rc == NULL)
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1115
        free(list);
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1116
    else
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1117
    {
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1118
        list->handle.opaque = (void *) rc;
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1119
        list->next = openReadList;
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1120
        openReadList = list;
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1121
        retval = &(list->handle);
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1122
    } /* else */
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1123
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1124
    return(retval);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1125
} /* PHYSFS_openRead */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1126
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1127
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1128
static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1129
{
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
  1130
    FileHandle *h = (FileHandle *) handle->opaque;
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1131
    FileHandleList *prev = NULL;
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1132
    FileHandleList *i;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1133
    int rc;
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1134
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1135
    for (i = *list; i != NULL; i = i->next)
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1136
    {
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1137
        if (&i->handle == handle)  /* handle is in this list? */
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1138
        {
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1139
            rc = h->funcs->fileClose(h);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1140
            if (!rc)
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1141
                return(-1);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1142
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1143
            if (prev == NULL)
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1144
                *list = i->next;
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1145
            else
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1146
                prev->next = i->next;
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1147
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1148
            free(i);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1149
            return(1);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1150
        } /* if */
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1151
        prev = i;
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1152
    } /* for */
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1153
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1154
    return(0);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1155
} /* closeHandleInOpenList */
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1156
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1157
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1158
int PHYSFS_close(PHYSFS_file *handle)
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1159
{
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1160
    int rc;
19
a0279b57398c Base implementation is now complete. Now to fill in archive/platform drivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
  1161
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1162
    /* -1 == close failure. 0 == not found. 1 == success. */
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1163
    rc = closeHandleInOpenList(&openReadList, handle);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1164
    BAIL_IF_MACRO(rc == -1, NULL, 0);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1165
    if (!rc)
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1166
    {
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1167
        rc = closeHandleInOpenList(&openWriteList, handle);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1168
        BAIL_IF_MACRO(rc == -1, NULL, 0);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1169
    } /* if */
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1170
27
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1171
    if (!rc)
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1172
        __PHYSFS_setError(ERR_NOT_A_HANDLE);
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1173
e1d3292769b2 Rewrote the file closing routines to not be so gay. Cleaned up the opening
Ryan C. Gordon <icculus@icculus.org>
parents: 25
diff changeset
  1174
    return(rc);
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1175
} /* PHYSFS_close */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1176
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1177
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1178
int PHYSFS_read(PHYSFS_file *handle, void *buffer,
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1179
                unsigned int objSize, unsigned int objCount)
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1180
{
7
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
  1181
    FileHandle *h = (FileHandle *) handle->opaque;
07d5e6e8259d More work. Getting better.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
  1182
    assert(h != NULL);
11
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1183
    assert(h->funcs != NULL);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1184
    BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
677e01f5109e Progress toward complete implementation continues...
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
  1185
    return(h->funcs->read(h, buffer, objSize, objCount));
5
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1186
} /* PHYSFS_read */
055ddaaabf85 Initial add; not yet completely implemented.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1187