extras/physfsunpack.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Feb 2018 10:26:33 -0500
changeset 1631 1aec60db9326
parent 868 87ea5fcab4e9
permissions -rw-r--r--
Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
868
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include <stdio.h>
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdlib.h>
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <string.h>
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <time.h>
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include "physfs.h"
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
static int failure = 0;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
{
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    const char *str = "unknown modtime";
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
    if (modtime != -1)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
        time_t t = (time_t) modtime;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
        str = ctime(&t);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    strncpy(modstr, str, strsize);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    modstr[strsize-1] = '\0';
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    strsize = strlen(modstr);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    while ((modstr[strsize-1] == '\n') || (modstr[strsize-1] == '\r'))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
        modstr[--strsize] = '\0';
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
} /* modTimeToStr */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
static void fail(const char *what, const char *why)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
{
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    if (why == NULL)
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 868
diff changeset
    31
        why = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
868
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    fprintf(stderr, "%s failed: %s\n", what, why);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
    failure = 1;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
} /* fail */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
static void dumpFile(const char *fname)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
{
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    const int origfailure = failure;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    PHYSFS_File *out = NULL;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    PHYSFS_File *in = NULL;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    failure = 0;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    if ((in = PHYSFS_openRead(fname)) == NULL)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        fail("\nPHYSFS_openRead", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    else if ((out = PHYSFS_openWrite(fname)) == NULL)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
        fail("\nPHYSFS_openWrite", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        char modstr[64];
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        PHYSFS_sint64 size = PHYSFS_fileLength(in);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        printf("(");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
        if (size == -1)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
            printf("?");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
        else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
            printf("%lld", (long long) size);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        printf(" bytes");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        modTimeToStr(PHYSFS_getLastModTime(fname), modstr, sizeof (modstr));
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
        printf(", %s)\n", modstr);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
        while ( (!failure) && (!PHYSFS_eof(in)) )
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
            static char buf[64 * 1024];
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
            PHYSFS_sint64 br = PHYSFS_read(in, buf, 1, sizeof (buf));
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
            if (br == -1)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
                fail("PHYSFS_read", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
            {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
                PHYSFS_sint64 bw = PHYSFS_write(out, buf, 1, br);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
                if (bw != br)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
                    fail("PHYSFS_write", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
                else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
                    size -= bw;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
            } /* else */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        } /* while */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
        if ((!failure) && (size != 0))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
            fail("PHYSFS_eof", "BUG! eof != PHYSFS_fileLength bytes!");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    } /* else */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    if (in != NULL)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        PHYSFS_close(in);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    if (out != NULL)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        if (!PHYSFS_close(out))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
            fail("PHYSFS_close", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    if (failure)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        PHYSFS_delete(fname);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        failure = origfailure;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
} /* dumpFile */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
static void unpackCallback(void *_depth, const char *origdir, const char *str)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
{
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    int depth = *((int *) _depth);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    const int len = strlen(origdir) + strlen(str) + 2;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    char *fname = (char *) malloc(len);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    if (fname == NULL)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        fail("malloc", "Out of memory!");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        if (strcmp(origdir, "/") == 0)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
            origdir = "";
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        snprintf(fname, len, "%s/%s", origdir, str);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        printf("%s ", fname);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        if (PHYSFS_isDirectory(fname))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
            depth++;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
            printf("(directory)\n");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
            if (!PHYSFS_mkdir(fname))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
                fail("PHYSFS_mkdir", NULL);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
            else
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
                PHYSFS_enumerateFilesCallback(fname, unpackCallback, &depth);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
        } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
        else if (PHYSFS_isSymbolicLink(fname))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
            printf("(symlink)\n");
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
            /* !!! FIXME: ?  if (!symlink(fname, */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
        } /* else if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
        else  /* ...file. */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
        {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
            dumpFile(fname);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        } /* else */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
        free(fname);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    } /* else */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
} /* unpackCallback */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
int main(int argc, char **argv)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
{
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    int zero = 0;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    if (argc != 3)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        fprintf(stderr, "USAGE: %s <archive> <unpackDirectory>\n", argv[0]);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        return 1;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    if (!PHYSFS_init(argv[0]))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 868
diff changeset
   153
        fprintf(stderr, "PHYSFS_init() failed: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
868
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
        return 2;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    if (!PHYSFS_setWriteDir(argv[2]))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        fprintf(stderr, "PHYSFS_setWriteDir('%s') failed: %s\n",
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 868
diff changeset
   160
                argv[2], PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
868
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        return 3;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    if (!PHYSFS_mount(argv[1], NULL, 1))
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    {
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        fprintf(stderr, "PHYSFS_mount('%s') failed: %s\n",
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 868
diff changeset
   167
                argv[1], PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
868
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        return 4;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    } /* if */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    PHYSFS_permitSymbolicLinks(1);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    PHYSFS_enumerateFilesCallback("/", unpackCallback, &zero);
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    PHYSFS_deinit();
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    if (failure)
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        return 5;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    return 0;
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
} /* main */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
/* end of physfsunpack.c ... */
87ea5fcab4e9 Added a simple unpacker example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181