Skip to content

Latest commit

 

History

History
874 lines (699 loc) · 19.8 KB

test_physfs.c

File metadata and controls

874 lines (699 loc) · 19.8 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
#include <errno.h>
Aug 23, 2001
Aug 23, 2001
12
13
#include <string.h>
Apr 3, 2002
Apr 3, 2002
14
15
16
17
#if (defined __MWERKS__)
#include <SIOUX.h>
#endif
Apr 6, 2002
Apr 6, 2002
18
19
20
21
22
23
24
#if ((defined __APPLE__) && (defined __MACH__))
# if (!defined __DARWIN__)
# define __DARWIN__
# endif
#endif
#if ((!defined WIN32) && (!defined __MACOS__) && (!defined __DARWIN__))
Aug 23, 2001
Aug 23, 2001
25
26
27
28
#define HAVE_READLINE
#endif
#if (defined HAVE_READLINE)
Jul 16, 2001
Jul 16, 2001
29
#include <unistd.h>
Jul 16, 2001
Jul 16, 2001
30
31
#include <readline.h>
#include <history.h>
Aug 23, 2001
Aug 23, 2001
32
33
#endif
34
35
36
37
#include "physfs.h"
#define TEST_VERSION_MAJOR 0
#define TEST_VERSION_MINOR 1
Apr 5, 2002
Apr 5, 2002
38
#define TEST_VERSION_PATCH 1
39
Jul 16, 2001
Jul 16, 2001
40
41
static FILE *history_file = NULL;
Jul 16, 2001
Jul 16, 2001
42
static void output_versions(void)
43
44
45
46
47
48
49
50
51
52
53
{
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,
Mar 24, 2002
Mar 24, 2002
54
55
(int) compiled.major, (int) compiled.minor, (int) compiled.patch,
(int) linked.major, (int) linked.minor, (int) linked.patch);
56
57
58
} /* output_versions */
Jul 16, 2001
Jul 16, 2001
59
static void output_archivers(void)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
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
76
77
printf("\n");
78
79
80
} /* output_archivers */
Jul 16, 2001
Jul 16, 2001
81
82
83
84
85
86
87
static int cmd_quit(char *args)
{
return(0);
} /* cmd_quit */
static int cmd_init(char *args)
Jul 16, 2001
Jul 16, 2001
88
{
Apr 5, 2002
Apr 5, 2002
89
90
91
92
93
94
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
Jul 16, 2001
Jul 16, 2001
95
96
97
98
99
if (PHYSFS_init(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
Jul 16, 2001
Jul 16, 2001
100
return(1);
Jul 16, 2001
Jul 16, 2001
101
} /* cmd_init */
Jul 16, 2001
Jul 16, 2001
102
103
Jul 16, 2001
Jul 16, 2001
104
static int cmd_deinit(char *args)
Jul 16, 2001
Jul 16, 2001
105
{
Jul 16, 2001
Jul 16, 2001
106
107
108
109
110
111
112
113
114
115
116
if (PHYSFS_deinit())
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_deinit */
static int cmd_addarchive(char *args)
{
Apr 4, 2002
Apr 4, 2002
117
char *ptr = strrchr(args, ' ');
Jul 16, 2001
Jul 16, 2001
118
119
120
int appending = atoi(ptr + 1);
*ptr = '\0';
Apr 4, 2002
Apr 4, 2002
121
122
123
124
if (*args == '\"')
{
args++;
*(ptr - 1) = '\0';
Apr 5, 2002
Apr 5, 2002
125
} /* if */
Apr 4, 2002
Apr 4, 2002
126
127
128
/*printf("[%s], [%d]\n", args, appending);*/
Jul 16, 2001
Jul 16, 2001
129
130
131
132
133
134
135
136
137
138
139
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)
{
Apr 5, 2002
Apr 5, 2002
140
141
142
143
144
145
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
Jul 16, 2001
Jul 16, 2001
146
147
148
149
150
151
152
153
154
155
156
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)
{
Apr 5, 2002
Apr 5, 2002
157
158
159
160
161
162
163
164
165
char **rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_enumerateFiles(args);
Jul 16, 2001
Jul 16, 2001
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
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)
Apr 5, 2002
Apr 5, 2002
203
printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
Jul 16, 2001
Jul 16, 2001
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
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)
{
Apr 5, 2002
Apr 5, 2002
263
264
265
266
267
268
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
Jul 16, 2001
Jul 16, 2001
269
270
271
272
273
274
275
276
277
278
279
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)
{
Apr 5, 2002
Apr 5, 2002
280
281
282
283
284
285
286
287
288
int num;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
num = atoi(args);
Jul 16, 2001
Jul 16, 2001
289
290
291
292
293
294
295
296
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
return(1);
} /* cmd_permitsyms */
static int cmd_setsaneconfig(char *args)
{
Sep 26, 2001
Sep 26, 2001
297
char *org;
Jul 16, 2001
Jul 16, 2001
298
299
300
301
302
303
304
char *appName;
char *arcExt;
int inclCD;
int arcsFirst;
char *ptr = args;
/* ugly. */
Sep 26, 2001
Sep 26, 2001
305
306
org = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
Jul 16, 2001
Jul 16, 2001
307
308
309
310
311
312
313
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
arcsFirst = atoi(ptr);
if (strcmp(arcExt, "!") == 0)
arcExt = NULL;
Sep 26, 2001
Sep 26, 2001
314
if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
Jul 16, 2001
Jul 16, 2001
315
316
317
318
319
320
321
322
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_setsaneconfig */
Jul 16, 2001
Jul 16, 2001
323
324
static int cmd_mkdir(char *args)
{
Apr 5, 2002
Apr 5, 2002
325
326
327
328
329
330
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
Jul 16, 2001
Jul 16, 2001
331
332
333
334
335
336
337
338
339
340
341
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)
{
Apr 5, 2002
Apr 5, 2002
342
343
344
345
346
347
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
Jul 16, 2001
Jul 16, 2001
348
349
350
351
352
353
354
355
356
357
358
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)
{
Apr 5, 2002
Apr 5, 2002
359
360
361
362
363
364
365
366
367
const char *rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_getRealDir(args);
Jul 16, 2001
Jul 16, 2001
368
369
370
371
372
373
374
375
376
377
378
if (rc)
printf("Found at [%s].\n", rc);
else
printf("Not found.\n");
return(1);
} /* cmd_getrealdir */
static int cmd_exists(char *args)
{
Apr 5, 2002
Apr 5, 2002
379
380
381
382
383
384
385
386
387
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_exists(args);
Jul 16, 2001
Jul 16, 2001
388
389
390
391
392
393
394
printf("File %sexists.\n", rc ? "" : "does not ");
return(1);
} /* cmd_exists */
static int cmd_isdir(char *args)
{
Apr 5, 2002
Apr 5, 2002
395
396
397
398
399
400
401
402
403
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_isDirectory(args);
Jul 16, 2001
Jul 16, 2001
404
405
406
407
408
409
410
printf("File %s a directory.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_isdir */
static int cmd_issymlink(char *args)
{
Apr 5, 2002
Apr 5, 2002
411
412
413
414
415
416
417
418
419
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_isSymbolicLink(args);
Jul 16, 2001
Jul 16, 2001
420
421
422
423
424
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_issymlink */
Oct 9, 2001
Oct 9, 2001
425
426
static int cmd_cat(char *args)
{
Apr 5, 2002
Apr 5, 2002
427
428
429
430
431
432
433
434
435
PHYSFS_file *f;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openRead(args);
Oct 9, 2001
Oct 9, 2001
436
437
438
439
440
441
442
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
while (1)
{
char buffer[128];
Mar 24, 2002
Mar 24, 2002
443
444
PHYSFS_sint64 rc;
PHYSFS_sint64 i;
Oct 9, 2001
Oct 9, 2001
445
446
447
448
449
450
451
452
453
rc = PHYSFS_read(f, buffer, 1, sizeof (buffer));
for (i = 0; i < rc; i++)
fputc((int) buffer[i], stdout);
if (rc < sizeof (buffer))
{
printf("\n\n");
if (!PHYSFS_eof(f))
Mar 24, 2002
Mar 24, 2002
454
455
456
457
{
printf("\n (Error condition in reading. Reason: [%s])\n\n",
PHYSFS_getLastError());
} /* if */
Oct 9, 2001
Oct 9, 2001
458
459
460
461
462
463
464
465
466
467
PHYSFS_close(f);
return(1);
} /* if */
} /* while */
} /* else */
return(1);
} /* cmd_cat */
Apr 5, 2002
Apr 5, 2002
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
static int cmd_filelength(char *args)
{
PHYSFS_file *f;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openRead(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
PHYSFS_sint64 len = PHYSFS_fileLength(f);
if (len == -1)
printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
else
printf(" (cast to int) %d bytes.\n", (int) len);
PHYSFS_close(f);
} /* else */
return(1);
} /* cmd_filelength */
#define WRITESTR "The cat sat on the mat.\n\n"
static int cmd_append(char *args)
{
PHYSFS_file *f;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openAppend(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
size_t bw = strlen(WRITESTR);
PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
if (rc != bw)
{
Apr 5, 2002
Apr 5, 2002
517
518
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
(int) rc, (int) bw, PHYSFS_getLastError());
Apr 5, 2002
Apr 5, 2002
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
} /* if */
else
{
printf("Successful.\n");
} /* else */
PHYSFS_close(f);
} /* else */
return(1);
} /* cmd_append */
static int cmd_write(char *args)
{
PHYSFS_file *f;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openWrite(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
size_t bw = strlen(WRITESTR);
PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
if (rc != bw)
{
Apr 5, 2002
Apr 5, 2002
551
552
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
(int) rc, (int) bw, PHYSFS_getLastError());
Apr 5, 2002
Apr 5, 2002
553
554
555
556
557
558
559
560
561
562
563
564
565
} /* if */
else
{
printf("Successful.\n");
} /* else */
PHYSFS_close(f);
} /* else */
return(1);
} /* cmd_write */
Jul 16, 2001
Jul 16, 2001
566
567
568
569
/* must have spaces trimmed prior to this call. */
static int count_args(const char *str)
{
int retval = 0;
Apr 4, 2002
Apr 4, 2002
570
int in_quotes = 0;
Jul 16, 2001
Jul 16, 2001
571
572
573
574
575
if (str != NULL)
{
for (; *str != '\0'; str++)
{
Apr 4, 2002
Apr 4, 2002
576
577
578
if (*str == '\"')
in_quotes = !in_quotes;
else if ((*str == ' ') && (!in_quotes))
Jul 16, 2001
Jul 16, 2001
579
580
581
582
583
584
585
retval++;
} /* for */
retval++;
} /* if */
return(retval);
} /* count_args */
Jul 16, 2001
Jul 16, 2001
586
587
Jul 16, 2001
Jul 16, 2001
588
589
static int cmd_help(char *args);
Jul 16, 2001
Jul 16, 2001
590
591
592
typedef struct
{
const char *cmd;
Jul 16, 2001
Jul 16, 2001
593
594
595
int (*func)(char *args);
int argcount;
const char *usage;
Jul 16, 2001
Jul 16, 2001
596
597
} command_info;
Jul 16, 2001
Jul 16, 2001
598
static const command_info commands[] =
Jul 16, 2001
Jul 16, 2001
599
{
Jul 16, 2001
Jul 16, 2001
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
{ "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
618
619
620
621
622
623
{ "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>" },
Oct 9, 2001
Oct 9, 2001
624
{ "cat", cmd_cat, 1, "<fileToCat>" },
Apr 5, 2002
Apr 5, 2002
625
626
627
{ "filelength", cmd_filelength, 1, "<fileToCheck>" },
{ "append", cmd_append, 1, "<fileToAppend>" },
{ "write", cmd_write, 1, "<fileToCreateOrTrash>" },
Jul 16, 2001
Jul 16, 2001
628
{ NULL, NULL, -1, NULL }
Jul 16, 2001
Jul 16, 2001
629
630
};
Jul 16, 2001
Jul 16, 2001
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
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
689
690
691
static int process_command(char *complete_cmd)
{
Jul 16, 2001
Jul 16, 2001
692
693
694
const command_info *i;
char *cmd_copy = malloc(strlen(complete_cmd) + 1);
char *args;
Jul 16, 2001
Jul 16, 2001
695
696
int rc = 1;
Jul 16, 2001
Jul 16, 2001
697
if (cmd_copy == NULL)
Jul 16, 2001
Jul 16, 2001
698
{
Jul 16, 2001
Jul 16, 2001
699
700
printf("\n\n\nOUT OF MEMORY!\n\n\n");
return(0);
Jul 16, 2001
Jul 16, 2001
701
} /* if */
Jul 16, 2001
Jul 16, 2001
702
703
704
705
trim_command(complete_cmd, cmd_copy);
args = strchr(cmd_copy, ' ');
if (args != NULL)
Jul 16, 2001
Jul 16, 2001
706
{
Jul 16, 2001
Jul 16, 2001
707
708
*args = '\0';
args++;
Jul 16, 2001
Jul 16, 2001
709
710
} /* else */
Jul 16, 2001
Jul 16, 2001
711
if (cmd_copy[0] != '\0')
Jul 16, 2001
Jul 16, 2001
712
{
Jul 16, 2001
Jul 16, 2001
713
for (i = commands; i->cmd != NULL; i++)
Jul 16, 2001
Jul 16, 2001
714
{
Jul 16, 2001
Jul 16, 2001
715
716
717
718
719
720
721
722
723
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
724
Jul 16, 2001
Jul 16, 2001
725
726
if (i->cmd == NULL)
printf("Unknown command. Enter \"help\" for instructions.\n");
Jul 16, 2001
Jul 16, 2001
727
Aug 23, 2001
Aug 23, 2001
728
#if (defined HAVE_READLINE)
Jul 16, 2001
Jul 16, 2001
729
730
731
732
733
734
add_history(complete_cmd);
if (history_file)
{
fprintf(history_file, "%s\n", complete_cmd);
fflush(history_file);
} /* if */
Aug 23, 2001
Aug 23, 2001
735
736
#endif
Jul 16, 2001
Jul 16, 2001
737
738
739
} /* if */
free(cmd_copy);
Jul 16, 2001
Jul 16, 2001
740
741
742
743
return(rc);
} /* process_command */
Jul 16, 2001
Jul 16, 2001
744
745
static void open_history_file(void)
{
Aug 23, 2001
Aug 23, 2001
746
#if (defined HAVE_READLINE)
Jul 16, 2001
Jul 16, 2001
747
#if 0
Jul 16, 2001
Jul 16, 2001
748
749
750
const char *envr = getenv("TESTPHYSFS_HISTORY");
if (!envr)
return;
Jul 16, 2001
Jul 16, 2001
751
752
753
754
755
#else
char envr[256];
strcpy(envr, PHYSFS_getUserDir());
strcat(envr, ".testphys_history");
#endif
Jul 16, 2001
Jul 16, 2001
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
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 */
Aug 23, 2001
Aug 23, 2001
789
#endif
Jul 16, 2001
Jul 16, 2001
790
791
792
} /* open_history_file */
793
794
int main(int argc, char **argv)
{
Jul 16, 2001
Jul 16, 2001
795
796
797
char *buf = NULL;
int rc = 0;
Apr 3, 2002
Apr 3, 2002
798
799
800
801
802
803
804
805
#if (defined __MWERKS__)
extern tSIOUXSettings SIOUXSettings;
SIOUXSettings.asktosaveonclose = 0;
SIOUXSettings.autocloseonquit = 1;
SIOUXSettings.rows = 40;
SIOUXSettings.columns = 120;
#endif
Jul 16, 2001
Jul 16, 2001
806
807
printf("\n");
808
809
if (!PHYSFS_init(argv[0]))
{
Jul 16, 2001
Jul 16, 2001
810
printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
811
812
813
814
815
816
return(1);
} /* if */
output_versions();
output_archivers();
Jul 16, 2001
Jul 16, 2001
817
818
open_history_file();
Jul 16, 2001
Jul 16, 2001
819
820
821
822
printf("Enter commands. Enter \"help\" for instructions.\n");
do
{
Aug 23, 2001
Aug 23, 2001
823
#if (defined HAVE_READLINE)
Jul 16, 2001
Jul 16, 2001
824
buf = readline("> ");
Aug 23, 2001
Aug 23, 2001
825
#else
Aug 23, 2001
Aug 23, 2001
826
int i;
Aug 23, 2001
Aug 23, 2001
827
828
buf = malloc(512);
memset(buf, '\0', 512);
Aug 23, 2001
Aug 23, 2001
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
printf("> ");
for (i = 0; i < 511; i++)
{
int ch = fgetc(stdin);
if (ch == EOF)
{
strcpy(buf, "quit");
break;
} /* if */
else if ((ch == '\n') || (ch == '\r'))
{
buf[i] = '\0';
break;
} /* else if */
else if (ch == '\b')
{
if (i > 0)
i--;
} /* else if */
else
{
buf[i] = (char) ch;
} /* else */
} /* for */
Aug 23, 2001
Aug 23, 2001
853
#endif
Aug 23, 2001
Aug 23, 2001
854
Jul 16, 2001
Jul 16, 2001
855
856
857
858
rc = process_command(buf);
free(buf);
} while (rc);
Jul 16, 2001
Jul 16, 2001
859
860
861
862
863
864
865
866
867
868
869
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");
*/
870
871
872
873
return(0);
} /* main */
/* end of test_physfs.c ... */