extras/selfextract.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Mar 2007 10:10:28 +0000
changeset 809 116b8fe30371
parent 767 db29bf06d171
child 1016 957c97389257
permissions -rw-r--r--
Renamed LICENSE to LICENSE.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
633
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * This code shows how to read a zipfile included in an app's binary.
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * License: this code is public domain. I make no warranty that it is useful,
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *  correct, harmless, or environmentally safe.
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * This particular file may be used however you like, including copying it
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  verbatim into a closed-source project, exploiting it commercially, and
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *  removing any trace of my name from the source (although I hope you won't
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *  do that). I welcome enhancements and corrections to this file, but I do
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *  not require you to send me patches if you make changes. This code has
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *  NO WARRANTY.
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
    15
 *  Please see LICENSE.txt in the root of the source tree.
633
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *
767
db29bf06d171 Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 633
diff changeset
    17
 *  This file was written by Ryan C. Gordon. (icculus@icculus.org).
633
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
/*
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 * Compile this program and then attach a .zip file to the end of the
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *  compiled binary.
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 * On Linux, something like this will build the final binary:
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *   gcc -o selfextract.tmp selfextract.c -lphysfs && \
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *   cat selfextract.tmp myzipfile.zip >> selfextract && \
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *   chmod a+x selfextract && \
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *   rm -f selfextract.tmp
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 * This may not work on all platforms, and it probably only works with
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 *  .zip files, since they are designed to be appended to another file.
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include <stdio.h>
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include "physfs.h"
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
int main(int argc, char **argv)
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
{
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    int rc = 0;
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    if (!PHYSFS_init(argv[0]))
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    {
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
        printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        return(42);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    } /* if */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    rc = PHYSFS_addToSearchPath(argv[0], 0);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    if (!rc)
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    {
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        printf("Couldn't find self-extract data: %s\n", PHYSFS_getLastError());
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        printf("This might mean you didn't append a zipfile to the binary.\n");
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        return(42);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    } /* if */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    char **files = PHYSFS_enumerateFiles("/");
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    char **i;
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    for (i = files; *i != NULL; i++)
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    {
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        const char *dirorfile = PHYSFS_isDirectory(*i) ? "Directory" : "File";
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        printf(" * %s '%s' is in root of attached data.\n", dirorfile, *i);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    } /* for */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    PHYSFS_freeList(files);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    return(0);
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
} /* main */
0178d6f71e15 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66