Skip to content

Latest commit

 

History

History
578 lines (455 loc) · 13.6 KB

test_physfs.c

File metadata and controls

578 lines (455 loc) · 13.6 KB
 
Jul 16, 2001
Jul 16, 2001
1
2
3
4
5
6
7
8
/**
* Test program for PhysicsFS. May only work on Unix.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
9
#include <stdio.h>
Jul 16, 2001
Jul 16, 2001
10
#include <stdlib.h>
Jul 16, 2001
Jul 16, 2001
11
12
#include <errno.h>
#include <unistd.h>
Jul 16, 2001
Jul 16, 2001
13
14
#include <readline.h>
#include <history.h>
15
16
17
18
19
20
#include "physfs.h"
#define TEST_VERSION_MAJOR 0
#define TEST_VERSION_MINOR 1
#define TEST_VERSION_PATCH 0
Jul 16, 2001
Jul 16, 2001
21
22
static FILE *history_file = NULL;
Jul 16, 2001
Jul 16, 2001
23
static void output_versions(void)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
PHYSFS_Version compiled;
PHYSFS_Version linked;
PHYSFS_VERSION(&compiled);
PHYSFS_getLinkedVersion(&linked);
printf("test_physfs version %d.%d.%d.\n"
" Compiled against PhysicsFS version %d.%d.%d,\n"
" and linked against %d.%d.%d.\n\n",
TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
compiled.major, compiled.minor, compiled.patch,
linked.major, linked.minor, linked.patch);
} /* output_versions */
Jul 16, 2001
Jul 16, 2001
40
static void output_archivers(void)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
const PHYSFS_ArchiveInfo **i;
printf("Supported archive types:\n");
if (*rc == NULL)
printf(" * Apparently, NONE!\n");
else
{
for (i = rc; *i != NULL; i++)
{
printf(" * %s: %s\n Written by %s.\n %s\n",
(*i)->extension, (*i)->description,
(*i)->author, (*i)->url);
} /* for */
} /* else */
Jul 16, 2001
Jul 16, 2001
57
58
printf("\n");
59
60
61
} /* output_archivers */
Jul 16, 2001
Jul 16, 2001
62
63
64
65
66
67
68
static int cmd_quit(char *args)
{
return(0);
} /* cmd_quit */
static int cmd_init(char *args)
Jul 16, 2001
Jul 16, 2001
69
{
Jul 16, 2001
Jul 16, 2001
70
71
72
73
74
if (PHYSFS_init(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
Jul 16, 2001
Jul 16, 2001
75
return(1);
Jul 16, 2001
Jul 16, 2001
76
} /* cmd_init */
Jul 16, 2001
Jul 16, 2001
77
78
Jul 16, 2001
Jul 16, 2001
79
static int cmd_deinit(char *args)
Jul 16, 2001
Jul 16, 2001
80
{
Jul 16, 2001
Jul 16, 2001
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
if (PHYSFS_deinit())
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_deinit */
static int cmd_addarchive(char *args)
{
char *ptr = strchr(args, ' ');
int appending = atoi(ptr + 1);
*ptr = '\0';
if (PHYSFS_addToSearchPath(args, appending))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_addarchive */
static int cmd_removearchive(char *args)
{
if (PHYSFS_removeFromSearchPath(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_removearchive */
static int cmd_enumerate(char *args)
{
char **rc = PHYSFS_enumerateFiles(args);
if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
else
{
int file_count;
char **i;
for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
printf("%s\n", *i);
printf("\n total (%d) files.\n", file_count);
PHYSFS_freeList(rc);
} /* else */
return(1);
} /* cmd_enumerate */
static int cmd_getdirsep(char *args)
{
printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
return(1);
} /* cmd_getdirsep */
static int cmd_getlasterror(char *args)
{
printf("last error is [%s].\n", PHYSFS_getLastError());
return(1);
} /* cmd_getlasterror */
static int cmd_getcdromdirs(char *args)
{
char **rc = PHYSFS_getCdRomDirs();
if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
else
{
int dir_count;
char **i;
for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
printf("%s\n", *i);
printf("\n total (%d) drives.\n", dir_count);
PHYSFS_freeList(rc);
} /* else */
return(1);
} /* cmd_getcdromdirs */
static int cmd_getsearchpath(char *args)
{
char **rc = PHYSFS_getSearchPath();
if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
else
{
int dir_count;
char **i;
for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
printf("%s\n", *i);
printf("\n total (%d) directories.\n", dir_count);
PHYSFS_freeList(rc);
} /* else */
return(1);
} /* cmd_getcdromdirs */
static int cmd_getbasedir(char *args)
{
printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
return(1);
} /* cmd_getbasedir */
static int cmd_getuserdir(char *args)
{
printf("User dir is [%s].\n", PHYSFS_getUserDir());
return(1);
} /* cmd_getuserdir */
static int cmd_getwritedir(char *args)
{
printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
return(1);
} /* cmd_getwritedir */
static int cmd_setwritedir(char *args)
{
if (PHYSFS_setWriteDir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_setwritedir */
static int cmd_permitsyms(char *args)
{
int num = atoi(args);
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
return(1);
} /* cmd_permitsyms */
static int cmd_setsaneconfig(char *args)
{
char *appName;
char *arcExt;
int inclCD;
int arcsFirst;
char *ptr = args;
/* ugly. */
appName = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
arcsFirst = atoi(ptr);
if (strcmp(appName, "!") == 0)
appName = NULL;
if (strcmp(arcExt, "!") == 0)
arcExt = NULL;
if (PHYSFS_setSaneConfig(appName, arcExt, inclCD, arcsFirst))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_setsaneconfig */
Jul 16, 2001
Jul 16, 2001
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
static int cmd_mkdir(char *args)
{
if (PHYSFS_mkdir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_mkdir */
static int cmd_delete(char *args)
{
if (PHYSFS_delete(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_delete */
static int cmd_getrealdir(char *args)
{
const char *rc = PHYSFS_getRealDir(args);
if (rc)
printf("Found at [%s].\n", rc);
else
printf("Not found.\n");
return(1);
} /* cmd_getrealdir */
static int cmd_exists(char *args)
{
int rc = PHYSFS_exists(args);
printf("File %sexists.\n", rc ? "" : "does not ");
return(1);
} /* cmd_exists */
static int cmd_isdir(char *args)
{
int rc = PHYSFS_isDirectory(args);
printf("File %s a directory.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_isdir */
static int cmd_issymlink(char *args)
{
int rc = PHYSFS_isSymbolicLink(args);
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_issymlink */
Jul 16, 2001
Jul 16, 2001
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* must have spaces trimmed prior to this call. */
static int count_args(const char *str)
{
int retval = 0;
if (str != NULL)
{
for (; *str != '\0'; str++)
{
if (*str == ' ')
retval++;
} /* for */
retval++;
} /* if */
return(retval);
} /* count_args */
Jul 16, 2001
Jul 16, 2001
338
339
Jul 16, 2001
Jul 16, 2001
340
341
static int cmd_help(char *args);
Jul 16, 2001
Jul 16, 2001
342
343
344
typedef struct
{
const char *cmd;
Jul 16, 2001
Jul 16, 2001
345
346
347
int (*func)(char *args);
int argcount;
const char *usage;
Jul 16, 2001
Jul 16, 2001
348
349
} command_info;
Jul 16, 2001
Jul 16, 2001
350
static const command_info commands[] =
Jul 16, 2001
Jul 16, 2001
351
{
Jul 16, 2001
Jul 16, 2001
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{ "quit", cmd_quit, 0, NULL },
{ "q", cmd_quit, 0, NULL },
{ "help", cmd_help, 0, NULL },
{ "init", cmd_init, 1, "<argv0>" },
{ "deinit", cmd_deinit, 0, NULL },
{ "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
{ "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
{ "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
{ "getlasterror", cmd_getlasterror, 0, NULL },
{ "getdirsep", cmd_getdirsep, 0, NULL },
{ "getcdromdirs", cmd_getcdromdirs, 0, NULL },
{ "getsearchpath", cmd_getsearchpath, 0, NULL },
{ "getbasedir", cmd_getbasedir, 0, NULL },
{ "getuserdir", cmd_getuserdir, 0, NULL },
{ "getwritedir", cmd_getwritedir, 0, NULL },
{ "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
{ "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
{ "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
Jul 16, 2001
Jul 16, 2001
370
371
372
373
374
375
{ "mkdir", cmd_mkdir, 1, "<dirToMk>" },
{ "delete", cmd_delete, 1, "<dirToDelete>" },
{ "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
{ "exists", cmd_exists, 1, "<fileToCheck>" },
{ "isdir", cmd_isdir, 1, "<fileToCheck>" },
{ "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
Jul 16, 2001
Jul 16, 2001
376
{ NULL, NULL, -1, NULL }
Jul 16, 2001
Jul 16, 2001
377
378
};
Jul 16, 2001
Jul 16, 2001
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
static void output_usage(const char *intro, const command_info *cmdinfo)
{
if (cmdinfo->argcount == 0)
printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
else
printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
} /* output_usage */
static int cmd_help(char *args)
{
const command_info *i;
printf("Commands:\n");
for (i = commands; i->cmd != NULL; i++)
output_usage(" -", i);
return(1);
} /* output_cmd_help */
static void trim_command(const char *orig, char *copy)
{
const char *i;
char *writeptr = copy;
int spacecount = 0;
int have_first = 0;
for (i = orig; *i != '\0'; i++)
{
if (*i == ' ')
{
if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
{
if ((have_first) && (!spacecount))
{
spacecount++;
*writeptr = ' ';
writeptr++;
} /* if */
} /* if */
} /* if */
else
{
have_first = 1;
spacecount = 0;
*writeptr = *i;
writeptr++;
} /* else */
} /* for */
*writeptr = '\0';
/*
printf("\n command is [%s].\n", copy);
*/
} /* trim_command */
Jul 16, 2001
Jul 16, 2001
437
438
439
static int process_command(char *complete_cmd)
{
Jul 16, 2001
Jul 16, 2001
440
441
442
const command_info *i;
char *cmd_copy = malloc(strlen(complete_cmd) + 1);
char *args;
Jul 16, 2001
Jul 16, 2001
443
444
int rc = 1;
Jul 16, 2001
Jul 16, 2001
445
if (cmd_copy == NULL)
Jul 16, 2001
Jul 16, 2001
446
{
Jul 16, 2001
Jul 16, 2001
447
448
printf("\n\n\nOUT OF MEMORY!\n\n\n");
return(0);
Jul 16, 2001
Jul 16, 2001
449
} /* if */
Jul 16, 2001
Jul 16, 2001
450
451
452
453
trim_command(complete_cmd, cmd_copy);
args = strchr(cmd_copy, ' ');
if (args != NULL)
Jul 16, 2001
Jul 16, 2001
454
{
Jul 16, 2001
Jul 16, 2001
455
456
*args = '\0';
args++;
Jul 16, 2001
Jul 16, 2001
457
458
} /* else */
Jul 16, 2001
Jul 16, 2001
459
if (cmd_copy[0] != '\0')
Jul 16, 2001
Jul 16, 2001
460
{
Jul 16, 2001
Jul 16, 2001
461
for (i = commands; i->cmd != NULL; i++)
Jul 16, 2001
Jul 16, 2001
462
{
Jul 16, 2001
Jul 16, 2001
463
464
465
466
467
468
469
470
471
if (strcmp(i->cmd, cmd_copy) == 0)
{
if ((i->argcount >= 0) && (count_args(args) != i->argcount))
output_usage("usage:", i);
else
rc = i->func(args);
break;
} /* if */
} /* for */
Jul 16, 2001
Jul 16, 2001
472
Jul 16, 2001
Jul 16, 2001
473
474
if (i->cmd == NULL)
printf("Unknown command. Enter \"help\" for instructions.\n");
Jul 16, 2001
Jul 16, 2001
475
Jul 16, 2001
Jul 16, 2001
476
477
478
479
480
481
482
483
484
add_history(complete_cmd);
if (history_file)
{
fprintf(history_file, "%s\n", complete_cmd);
fflush(history_file);
} /* if */
} /* if */
free(cmd_copy);
Jul 16, 2001
Jul 16, 2001
485
486
487
488
return(rc);
} /* process_command */
Jul 16, 2001
Jul 16, 2001
489
490
static void open_history_file(void)
{
Jul 16, 2001
Jul 16, 2001
491
#if 0
Jul 16, 2001
Jul 16, 2001
492
493
494
const char *envr = getenv("TESTPHYSFS_HISTORY");
if (!envr)
return;
Jul 16, 2001
Jul 16, 2001
495
496
497
498
499
#else
char envr[256];
strcpy(envr, PHYSFS_getUserDir());
strcat(envr, ".testphys_history");
#endif
Jul 16, 2001
Jul 16, 2001
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
if (access(envr, F_OK) == 0)
{
char buf[512];
FILE *f = fopen(envr, "r");
if (!f)
{
printf("\n\n"
"Could not open history file [%s] for reading!\n"
" Will not have past history available.\n\n",
envr);
return;
} /* if */
do
{
fgets(buf, sizeof (buf), f);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
add_history(buf);
} while (!feof(f));
fclose(f);
} /* if */
history_file = fopen(envr, "ab");
if (!history_file)
{
printf("\n\n"
"Could not open history file [%s] for appending!\n"
" Will not be able to record this session's history.\n\n",
envr);
} /* if */
} /* open_history_file */
536
537
int main(int argc, char **argv)
{
Jul 16, 2001
Jul 16, 2001
538
539
540
541
542
char *buf = NULL;
int rc = 0;
printf("\n");
543
544
if (!PHYSFS_init(argv[0]))
{
Jul 16, 2001
Jul 16, 2001
545
printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
546
547
548
549
550
551
return(1);
} /* if */
output_versions();
output_archivers();
Jul 16, 2001
Jul 16, 2001
552
553
open_history_file();
Jul 16, 2001
Jul 16, 2001
554
555
556
557
558
559
560
561
562
printf("Enter commands. Enter \"help\" for instructions.\n");
do
{
buf = readline("> ");
rc = process_command(buf);
free(buf);
} while (rc);
Jul 16, 2001
Jul 16, 2001
563
564
565
566
567
568
569
570
571
572
573
if (!PHYSFS_deinit())
printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
if (history_file)
fclose(history_file);
/*
printf("\n\ntest_physfs written by ryan c. gordon.\n");
printf(" it makes you shoot teh railgun bettar.\n");
*/
574
575
576
577
return(0);
} /* main */
/* end of test_physfs.c ... */