9 static int failure = 0;
11 static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
13 const char *str = "unknown modtime";
16 time_t t = (time_t) modtime;
20 strncpy(modstr, str, strsize);
21 modstr[strsize-1] = '\0';
22 strsize = strlen(modstr);
23 while ((modstr[strsize-1] == '\n') || (modstr[strsize-1] == '\r'))
24 modstr[--strsize] = '\0';
28 static void fail(const char *what, const char *why)
31 why = PHYSFS_getLastError();
32 fprintf(stderr, "%s failed: %s\n", what, why);
37 static void dumpFile(const char *fname)
39 const int origfailure = failure;
40 PHYSFS_File *out = NULL;
41 PHYSFS_File *in = NULL;
45 if ((in = PHYSFS_openRead(fname)) == NULL)
46 fail("\nPHYSFS_openRead", NULL);
47 else if ((out = PHYSFS_openWrite(fname)) == NULL)
48 fail("\nPHYSFS_openWrite", NULL);
52 PHYSFS_sint64 size = PHYSFS_fileLength(in);
58 printf("%lld", (long long) size);
61 modTimeToStr(PHYSFS_getLastModTime(fname), modstr, sizeof (modstr));
62 printf(", %s)\n", modstr);
64 while ( (!failure) && (!PHYSFS_eof(in)) )
66 static char buf[64 * 1024];
67 PHYSFS_sint64 br = PHYSFS_read(in, buf, 1, sizeof (buf));
69 fail("PHYSFS_read", NULL);
72 PHYSFS_sint64 bw = PHYSFS_write(out, buf, 1, br);
74 fail("PHYSFS_write", NULL);
80 if ((!failure) && (size != 0))
81 fail("PHYSFS_eof", "BUG! eof != PHYSFS_fileLength bytes!");
89 if (!PHYSFS_close(out))
90 fail("PHYSFS_close", NULL);
96 failure = origfailure;
100 static void unpackCallback(void *_depth, const char *origdir, const char *str)
102 int depth = *((int *) _depth);
103 const int len = strlen(origdir) + strlen(str) + 2;
104 char *fname = (char *) malloc(len);
106 fail("malloc", "Out of memory!");
109 if (strcmp(origdir, "/") == 0)
112 snprintf(fname, len, "%s/%s", origdir, str);
114 printf("%s ", fname);
115 if (PHYSFS_isDirectory(fname))
118 printf("(directory)\n");
119 if (!PHYSFS_mkdir(fname))
120 fail("PHYSFS_mkdir", NULL);
122 PHYSFS_enumerateFilesCallback(fname, unpackCallback, &depth);
125 else if (PHYSFS_isSymbolicLink(fname))
127 printf("(symlink)\n");
128 /* !!! FIXME: ? if (!symlink(fname, */
138 } /* unpackCallback */
141 int main(int argc, char **argv)
147 fprintf(stderr, "USAGE: %s <archive> <unpackDirectory>\n", argv[0]);
151 if (!PHYSFS_init(argv[0]))
153 fprintf(stderr, "PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
157 if (!PHYSFS_setWriteDir(argv[2]))
159 fprintf(stderr, "PHYSFS_setWriteDir('%s') failed: %s\n",
160 argv[2], PHYSFS_getLastError());
164 if (!PHYSFS_mount(argv[1], NULL, 1))
166 fprintf(stderr, "PHYSFS_mount('%s') failed: %s\n",
167 argv[1], PHYSFS_getLastError());
171 PHYSFS_permitSymbolicLinks(1);
172 PHYSFS_enumerateFilesCallback("/", unpackCallback, &zero);
180 /* end of physfsunpack.c ... */