/
test_physfs.c
1226 lines (998 loc) · 30.2 KB
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
10
11
12
#if HAVE_CONFIG_H
# include <config.h>
#endif
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <errno.h>
16
17
#include <string.h>
18
19
20
21
#if (defined __MWERKS__)
#include <SIOUX.h>
#endif
22
#if (defined PHYSFS_HAVE_READLINE)
23
#include <unistd.h>
24
25
#include <readline/readline.h>
#include <readline/history.h>
26
27
#endif
28
29
#include <time.h>
30
31
#include "physfs.h"
32
#define TEST_VERSION_MAJOR 1
33
#define TEST_VERSION_MINOR 1
34
#define TEST_VERSION_PATCH 0
35
36
static FILE *history_file = NULL;
37
static PHYSFS_uint32 do_buffer_size = 0;
38
39
static void output_versions(void)
40
41
42
43
44
45
46
47
48
49
50
{
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,
51
52
(int) compiled.major, (int) compiled.minor, (int) compiled.patch,
(int) linked.major, (int) linked.minor, (int) linked.patch);
53
54
55
} /* output_versions */
56
static void output_archivers(void)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{
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 */
73
74
printf("\n");
75
76
77
} /* output_archivers */
78
79
80
81
82
83
84
static int cmd_quit(char *args)
{
return(0);
} /* cmd_quit */
static int cmd_init(char *args)
85
{
86
87
88
89
90
91
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
92
93
94
95
96
if (PHYSFS_init(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
97
return(1);
98
} /* cmd_init */
99
100
101
static int cmd_deinit(char *args)
102
{
103
104
105
106
107
108
109
110
111
112
113
if (PHYSFS_deinit())
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_deinit */
static int cmd_addarchive(char *args)
{
114
char *ptr = strrchr(args, ' ');
115
116
117
int appending = atoi(ptr + 1);
*ptr = '\0';
118
119
120
121
if (*args == '\"')
{
args++;
*(ptr - 1) = '\0';
122
} /* if */
123
124
125
/*printf("[%s], [%d]\n", args, appending);*/
126
127
128
129
130
131
132
133
134
if (PHYSFS_addToSearchPath(args, appending))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_addarchive */
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
static int cmd_mount(char *args)
{
char *ptr;
char *mntpoint = NULL;
int appending = 0;
if (*args == '\"')
{
args++;
ptr = strchr(args, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
return(1);
} /* if */
*(ptr) = '\0';
} /* if */
else
{
ptr = strchr(args, ' ');
*ptr = '\0';
} /* else */
mntpoint = ptr + 1;
if (*mntpoint == '\"')
{
mntpoint++;
ptr = strchr(mntpoint, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
return(1);
} /* if */
*(ptr) = '\0';
} /* if */
else
{
ptr = strchr(mntpoint, ' ');
*(ptr) = '\0';
} /* else */
appending = atoi(ptr + 1);
177
/*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
178
179
180
181
182
183
184
185
186
187
if (PHYSFS_mount(args, mntpoint, appending))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_mount */
188
189
static int cmd_removearchive(char *args)
{
190
191
192
193
194
195
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
196
197
198
199
200
201
202
203
204
205
206
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)
{
207
208
209
210
211
212
213
214
215
char **rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_enumerateFiles(args);
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
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)
253
printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
254
255
256
257
258
259
260
261
262
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
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)
{
313
314
315
316
317
318
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
319
320
321
322
323
324
325
326
327
328
329
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)
{
330
331
332
333
334
335
336
337
338
int num;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
num = atoi(args);
339
340
341
342
343
344
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
return(1);
} /* cmd_permitsyms */
345
346
347
348
349
350
351
352
static int cmd_setbuffer(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
353
do_buffer_size = (unsigned int) atoi(args);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
if (do_buffer_size)
{
printf("Further tests will set a (%lu) size buffer.\n",
(unsigned long) do_buffer_size);
} /* if */
else
{
printf("Further tests will NOT use a buffer.\n");
} /* else */
return(1);
} /* cmd_setbuffer */
static int cmd_stressbuffer(char *args)
{
int num;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
num = atoi(args);
if (num < 0)
printf("buffer must be greater than or equal to zero.\n");
else
{
384
PHYSFS_File *f;
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
int rndnum;
printf("Stress testing with (%d) byte buffer...\n", num);
f = PHYSFS_openWrite("test.txt");
if (f == NULL)
printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
else
{
int i, j;
char buf[37];
char buf2[37];
if (!PHYSFS_setBuffer(f, num))
{
printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
PHYSFS_delete("test.txt");
return(1);
} /* if */
strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
406
srand((unsigned int) time(NULL));
407
408
409
410
411
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
412
413
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
if (PHYSFS_write(f, buf, left, 1) != 1)
{
printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
if (rndnum == 42)
{
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
if (PHYSFS_write(f, buf + left, 1, right) != right)
{
printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
if (rndnum == 42)
{
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* for */
if (!PHYSFS_close(f))
{
printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
return(1); /* oh well. */
} /* if */
printf(" ... test file written ...\n");
f = PHYSFS_openRead("test.txt");
if (f == NULL)
{
printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
return(1);
} /* if */
if (!PHYSFS_setBuffer(f, num))
{
printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
485
486
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
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
517
518
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
if (PHYSFS_read(f, buf2, left, 1) != 1)
{
printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
if (rndnum == 42)
{
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
if (PHYSFS_read(f, buf2 + left, 1, right) != right)
{
printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
if (rndnum == 42)
{
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
if (memcmp(buf, buf2, 36) != 0)
{
printf("readback is mismatched on iterations (%d, %d).\n", i, j);
printf("wanted: [");
for (i = 0; i < 36; i++)
printf("%c", buf[i]);
printf("]\n");
printf(" got: [");
for (i = 0; i < 36; i++)
printf("%c", buf2[i]);
printf("]\n");
PHYSFS_close(f);
return(1);
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* for */
printf(" ... test file read ...\n");
if (!PHYSFS_eof(f))
printf("PHYSFS_eof() returned true! That's wrong.\n");
if (!PHYSFS_close(f))
{
printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
return(1); /* oh well. */
} /* if */
PHYSFS_delete("test.txt");
printf("stress test completed successfully.\n");
} /* else */
} /* else */
return(1);
} /* cmd_stressbuffer */
569
570
static int cmd_setsaneconfig(char *args)
{
571
char *org;
572
573
574
575
576
577
578
char *appName;
char *arcExt;
int inclCD;
int arcsFirst;
char *ptr = args;
/* ugly. */
579
580
org = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
581
582
583
584
585
586
587
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;
588
if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
589
590
591
592
593
594
595
596
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return(1);
} /* cmd_setsaneconfig */
597
598
static int cmd_mkdir(char *args)
{
599
600
601
602
603
604
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
605
606
607
608
609
610
611
612
613
614
615
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)
{
616
617
618
619
620
621
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
622
623
624
625
626
627
628
629
630
631
632
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)
{
633
634
635
636
637
638
639
640
641
const char *rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_getRealDir(args);
642
643
644
645
646
647
648
649
650
651
652
if (rc)
printf("Found at [%s].\n", rc);
else
printf("Not found.\n");
return(1);
} /* cmd_getrealdir */
static int cmd_exists(char *args)
{
653
654
655
656
657
658
659
660
661
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_exists(args);
662
663
664
665
666
667
668
printf("File %sexists.\n", rc ? "" : "does not ");
return(1);
} /* cmd_exists */
static int cmd_isdir(char *args)
{
669
670
671
672
673
674
675
676
677
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_isDirectory(args);
678
679
680
681
682
683
684
printf("File %s a directory.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_isdir */
static int cmd_issymlink(char *args)
{
685
686
687
688
689
690
691
692
693
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_isSymbolicLink(args);
694
695
696
697
698
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_issymlink */
699
700
static int cmd_cat(char *args)
{
701
PHYSFS_File *f;
702
703
704
705
706
707
708
709
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openRead(args);
710
711
712
713
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
714
715
716
717
718
719
720
721
722
723
724
if (do_buffer_size)
{
if (!PHYSFS_setBuffer(f, do_buffer_size))
{
printf("failed to set file buffer. Reason: [%s].\n",
PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
725
726
727
while (1)
{
char buffer[128];
728
729
PHYSFS_sint64 rc;
PHYSFS_sint64 i;
730
731
732
733
734
735
736
737
738
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))
739
740
741
742
{
printf("\n (Error condition in reading. Reason: [%s])\n\n",
PHYSFS_getLastError());
} /* if */
743
744
745
746
747
748
749
750
751
752
PHYSFS_close(f);
return(1);
} /* if */
} /* while */
} /* else */
return(1);
} /* cmd_cat */
753
754
static int cmd_filelength(char *args)
{
755
PHYSFS_File *f;
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
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)
{
785
PHYSFS_File *f;
786
787
788
789
790
791
792
793
794
795
796
797
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
{
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
size_t bw;
PHYSFS_sint64 rc;
if (do_buffer_size)
{
if (!PHYSFS_setBuffer(f, do_buffer_size))
{
printf("failed to set file buffer. Reason: [%s].\n",
PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
bw = strlen(WRITESTR);
rc = PHYSFS_write(f, WRITESTR, 1, bw);
814
815
if (rc != bw)
{
816
817
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
(int) rc, (int) bw, PHYSFS_getLastError());
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
} /* if */
else
{
printf("Successful.\n");
} /* else */
PHYSFS_close(f);
} /* else */
return(1);
} /* cmd_append */
static int cmd_write(char *args)
{
833
PHYSFS_File *f;
834
835
836
837
838
839
840
841
842
843
844
845
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
{
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
size_t bw;
PHYSFS_sint64 rc;
if (do_buffer_size)
{
if (!PHYSFS_setBuffer(f, do_buffer_size))
{
printf("failed to set file buffer. Reason: [%s].\n",
PHYSFS_getLastError());
PHYSFS_close(f);
return(1);
} /* if */
} /* if */
bw = strlen(WRITESTR);
rc = PHYSFS_write(f, WRITESTR, 1, bw);
862
863
if (rc != bw)
{
864
865
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
(int) rc, (int) bw, PHYSFS_getLastError());
866
867
868
869
870
871
872
873
874
875
876
877
878
} /* if */
else
{
printf("Successful.\n");
} /* else */
PHYSFS_close(f);
} /* else */
return(1);
} /* cmd_write */
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
{
time_t t = (time_t) modtime;
char *str = ctime(&t);
strncpy(modstr, str, strsize);
modstr[strsize-1] = '\0';
} /* modTimeToStr */
static int cmd_getlastmodtime(char *args)
{
PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);
if (rc == -1)
printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
else
{
char modstr[64];
modTimeToStr(rc, modstr, sizeof (modstr));
printf("Last modified: %s (%ld).\n", modstr, (long) rc);
} /* else */
return(1);
} /* cmd_getLastModTime */
904
905
906
907
/* must have spaces trimmed prior to this call. */
static int count_args(const char *str)
{
int retval = 0;
908
int in_quotes = 0;
909
910
911
912
913
if (str != NULL)
{
for (; *str != '\0'; str++)
{
914
915
916
if (*str == '\"')
in_quotes = !in_quotes;
else if ((*str == ' ') && (!in_quotes))
917
918
919
920
921
922
923
retval++;
} /* for */
retval++;
} /* if */
return(retval);
} /* count_args */
924
925
926
927
static int cmd_help(char *args);
928
929
930
typedef struct
{
const char *cmd;
931
932
933
int (*func)(char *args);
int argcount;
const char *usage;
934
935
} command_info;
936
static const command_info commands[] =
937
{
938
939
940
941
942
943
{ "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>" },
944
{ "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
945
946
{ "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
{ "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
947
{ "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
948
949
950
951
952
953
954
955
956
{ "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>" },
957
{ "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
958
959
960
961
962
963
{ "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>" },
964
{ "cat", cmd_cat, 1, "<fileToCat>" },
965
966
967
{ "filelength", cmd_filelength, 1, "<fileToCheck>" },
{ "append", cmd_append, 1, "<fileToAppend>" },
{ "write", cmd_write, 1, "<fileToCreateOrTrash>" },
968
{ "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
969
970
{ "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
{ "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
971
{ NULL, NULL, -1, NULL }
972
973
};
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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;