/
test_physfs.c
1737 lines (1433 loc) · 44.6 KB
1
2
3
/**
* Test program for PhysicsFS. May only work on Unix.
*
4
* Please see the file LICENSE.txt in the source's root directory.
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
9
10
#define _CRT_SECURE_NO_WARNINGS 1
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <errno.h>
14
15
#include <string.h>
16
17
18
19
#if (defined __MWERKS__)
#include <SIOUX.h>
#endif
20
#if (defined PHYSFS_HAVE_READLINE)
21
#include <unistd.h>
22
23
#include <readline/readline.h>
#include <readline/history.h>
24
25
#endif
26
27
#include <time.h>
28
29
30
/* Define this, so the compiler doesn't complain about using old APIs. */
#define PHYSFS_DEPRECATED
31
32
#include "physfs.h"
33
#define TEST_VERSION_MAJOR 3
34
#define TEST_VERSION_MINOR 3
35
#define TEST_VERSION_PATCH 0
36
37
static FILE *history_file = NULL;
38
static PHYSFS_uint32 do_buffer_size = 0;
39
40
static void output_versions(void)
41
42
43
44
45
46
47
48
49
50
51
{
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,
52
53
(int) compiled.major, (int) compiled.minor, (int) compiled.patch,
(int) linked.major, (int) linked.minor, (int) linked.patch);
54
55
56
} /* output_versions */
57
static void output_archivers(void)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
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);
72
73
printf(" %s symbolic links.\n",
(*i)->supportsSymlinks ? "Supports" : "Does not support");
74
75
} /* for */
} /* else */
76
77
printf("\n");
78
79
80
} /* output_archivers */
81
82
static int cmd_quit(char *args)
{
83
return 0;
84
85
86
87
} /* cmd_quit */
static int cmd_init(char *args)
88
{
89
90
91
92
93
94
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
95
96
97
98
99
if (PHYSFS_init(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
100
return 1;
101
} /* cmd_init */
102
103
104
static int cmd_deinit(char *args)
105
{
106
107
108
109
110
if (PHYSFS_deinit())
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
111
return 1;
112
113
114
115
116
} /* cmd_deinit */
static int cmd_addarchive(char *args)
{
117
char *ptr = strrchr(args, ' ');
118
119
120
int appending = atoi(ptr + 1);
*ptr = '\0';
121
122
123
124
if (*args == '\"')
{
args++;
*(ptr - 1) = '\0';
125
} /* if */
126
127
128
/*printf("[%s], [%d]\n", args, appending);*/
129
if (PHYSFS_mount(args, NULL, appending))
130
131
132
133
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
134
return 1;
135
136
137
} /* cmd_addarchive */
138
139
140
141
142
143
/* wrap free() to avoid calling convention wankery. */
static void freeBuf(void *buf)
{
free(buf);
} /* freeBuf */
144
145
146
147
148
149
typedef enum
{
MNTTYPE_PATH,
MNTTYPE_MEMORY,
MNTTYPE_HANDLE
} MountType;
150
151
static int cmd_mount_internal(char *args, const MountType mnttype)
152
153
154
155
{
char *ptr;
char *mntpoint = NULL;
int appending = 0;
156
int rc = 0;
157
158
159
160
161
162
163
164
if (*args == '\"')
{
args++;
ptr = strchr(args, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
165
return 1;
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
} /* 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");
183
return 1;
184
185
186
187
188
189
190
191
192
193
} /* if */
*(ptr) = '\0';
} /* if */
else
{
ptr = strchr(mntpoint, ' ');
*(ptr) = '\0';
} /* else */
appending = atoi(ptr + 1);
194
/*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
195
196
if (mnttype == MNTTYPE_PATH)
197
rc = PHYSFS_mount(args, mntpoint, appending);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
else if (mnttype == MNTTYPE_HANDLE)
{
PHYSFS_File *f = PHYSFS_openRead(args);
if (f == NULL)
{
printf("PHYSFS_openRead('%s') failed. reason: %s.\n", args, PHYSFS_getLastError());
return 1;
} /* if */
rc = PHYSFS_mountHandle(f, args, mntpoint, appending);
if (!rc)
PHYSFS_close(f);
} /* else if */
else if (mnttype == MNTTYPE_MEMORY)
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
{
FILE *in = fopen(args, "rb");
void *buf = NULL;
long len = 0;
if (in == NULL)
{
printf("Failed to open %s to read into memory: %s.\n", args, strerror(errno));
return 1;
} /* if */
if ( (fseek(in, 0, SEEK_END) != 0) || ((len = ftell(in)) < 0) )
{
printf("Failed to find size of %s to read into memory: %s.\n", args, strerror(errno));
fclose(in);
return 1;
} /* if */
buf = malloc(len);
if (buf == NULL)
{
printf("Failed to allocate space to read %s into memory: %s.\n", args, strerror(errno));
fclose(in);
return 1;
} /* if */
if ((fseek(in, 0, SEEK_SET) != 0) || (fread(buf, len, 1, in) != 1))
{
printf("Failed to read %s into memory: %s.\n", args, strerror(errno));
fclose(in);
free(buf);
return 1;
} /* if */
fclose(in);
rc = PHYSFS_mountMemory(buf, len, freeBuf, args, mntpoint, appending);
} /* else */
if (rc)
254
255
256
257
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
258
return 1;
259
260
261
262
263
} /* cmd_mount_internal */
static int cmd_mount(char *args)
{
264
return cmd_mount_internal(args, MNTTYPE_PATH);
265
266
267
} /* cmd_mount */
268
269
static int cmd_mount_mem(char *args)
{
270
return cmd_mount_internal(args, MNTTYPE_MEMORY);
271
272
273
} /* cmd_mount_mem */
274
275
276
277
278
static int cmd_mount_handle(char *args)
{
return cmd_mount_internal(args, MNTTYPE_HANDLE);
} /* cmd_mount_handle */
279
280
281
282
283
284
285
286
287
288
289
static int cmd_getmountpoint(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
printf("Dir [%s] is mounted at [%s].\n", args, PHYSFS_getMountPoint(args));
return 1;
} /* cmd_getmountpoint */
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
static int cmd_setroot(char *args)
{
char *archive;
char *subdir;
char *ptr;
archive = args;
if (*archive == '\"')
{
archive++;
ptr = strchr(archive, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
return 1;
} /* if */
*(ptr) = '\0';
} /* if */
else
{
ptr = strchr(archive, ' ');
*ptr = '\0';
} /* else */
subdir = ptr + 1;
if (*subdir == '\"')
{
subdir++;
ptr = strchr(subdir, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
return 1;
} /* if */
*(ptr) = '\0';
} /* if */
if (PHYSFS_setRoot(archive, subdir))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
return 1;
} /* cmd_setroot */
338
339
static int cmd_removearchive(char *args)
{
340
341
342
343
344
345
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
346
if (PHYSFS_unmount(args))
347
348
349
350
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
351
return 1;
352
353
354
355
356
} /* cmd_removearchive */
static int cmd_enumerate(char *args)
{
357
358
359
360
361
362
363
364
365
char **rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_enumerateFiles(args);
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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 */
380
return 1;
381
382
} /* cmd_enumerate */
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
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
#define STR_BOX_VERTICAL_RIGHT "\xe2\x94\x9c"
#define STR_BOX_VERTICAL "\xe2\x94\x82"
#define STR_BOX_HORIZONTAL "\xe2\x94\x80"
#define STR_BOX_UP_RIGHT "\xe2\x94\x94"
#define STR_NBSP "\xc2\xa0"
#define PREFIX_DIRENTRY STR_BOX_VERTICAL_RIGHT STR_BOX_HORIZONTAL STR_BOX_HORIZONTAL STR_NBSP
#define PREFIX_DIRENTRY_LAST STR_BOX_UP_RIGHT STR_BOX_HORIZONTAL STR_BOX_HORIZONTAL STR_NBSP
#define PREFIX_RECURSIVE STR_BOX_VERTICAL STR_NBSP STR_NBSP STR_NBSP
#define PREFIX_RECURSIVE_LAST STR_NBSP STR_NBSP STR_NBSP STR_NBSP
static void cmd_tree_recursive(const char *prefix, const char *fullPath, const char *name, unsigned depth, int *total_dir_count, int *total_file_count) {
char **rc;
printf("%s", name);
rc = PHYSFS_enumerateFiles(fullPath);
if (rc == NULL) {
printf(" [Failure. reason: %s]\n", PHYSFS_getLastError());
}
else
{
int file_count;
char **i;
printf("\n");
for (i = rc, file_count = 0; *i != NULL; i++, file_count++) {
char *newFullPath = malloc(strlen(fullPath) + strlen(*i) + 2);
char *thisPrefix;
strcpy(newFullPath, fullPath);
strcat(newFullPath, "/");
strcat(newFullPath, *i);
if (i[1]) {
thisPrefix = PREFIX_DIRENTRY;
} else {
thisPrefix = PREFIX_DIRENTRY_LAST;
}
if (PHYSFS_isSymbolicLink(newFullPath)) {
printf("%s%s%s [symbolic link]\n", prefix, thisPrefix, *i);
} else if (PHYSFS_isDirectory(newFullPath)) {
char *newPrefix;
*total_dir_count += 1;
if (i[1]) {
newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE) + 1);
strcpy(newPrefix, prefix);
strcat(newPrefix, PREFIX_RECURSIVE);
} else {
newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE_LAST) + 1);
strcpy(newPrefix, prefix);
strcat(newPrefix, PREFIX_RECURSIVE_LAST);
}
printf("%s%s", prefix, thisPrefix);
cmd_tree_recursive(newPrefix, newFullPath, *i, depth + 1, total_dir_count, total_file_count);
free(newPrefix);
} else {
*total_file_count += 1;
printf("%s%s%s\n", prefix, thisPrefix, *i);
}
free(newFullPath);
}
PHYSFS_freeList(rc);
} /* else */
} /* cmd_tree_recursive */
static int cmd_tree(char *args)
{
int total_dir_count = 0, total_file_count = 0; /* FIXME: should be PHYSFS_uint64 */
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
cmd_tree_recursive("", args, args, 0, &total_dir_count, &total_file_count);
printf("\n%d directories, %d files\n", total_dir_count, total_file_count);
return 1;
} /* cmd_enumerate */
463
464
465
466
static int cmd_getdirsep(char *args)
{
printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
467
return 1;
468
469
470
471
472
473
} /* cmd_getdirsep */
static int cmd_getlasterror(char *args)
{
printf("last error is [%s].\n", PHYSFS_getLastError());
474
return 1;
475
476
477
478
479
480
481
482
} /* cmd_getlasterror */
static int cmd_getcdromdirs(char *args)
{
char **rc = PHYSFS_getCdRomDirs();
if (rc == NULL)
483
printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
484
485
486
487
488
489
490
491
492
493
494
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 */
495
return 1;
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
} /* 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 */
516
return 1;
517
518
519
520
521
522
} /* cmd_getcdromdirs */
static int cmd_getbasedir(char *args)
{
printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
523
return 1;
524
525
526
527
528
529
} /* cmd_getbasedir */
static int cmd_getuserdir(char *args)
{
printf("User dir is [%s].\n", PHYSFS_getUserDir());
530
return 1;
531
532
533
} /* cmd_getuserdir */
534
535
536
537
538
539
540
541
542
543
544
545
546
static int cmd_getprefdir(char *args)
{
char *org;
char *appName;
char *ptr = args;
org = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
return 1;
} /* cmd_getprefdir */
547
548
549
static int cmd_getwritedir(char *args)
{
printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
550
return 1;
551
552
553
554
555
} /* cmd_getwritedir */
static int cmd_setwritedir(char *args)
{
556
557
558
559
560
561
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
562
563
564
565
566
if (PHYSFS_setWriteDir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
567
return 1;
568
569
570
571
572
} /* cmd_setwritedir */
static int cmd_permitsyms(char *args)
{
573
574
575
576
577
578
579
580
581
int num;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
num = atoi(args);
582
583
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
584
return 1;
585
586
587
} /* cmd_permitsyms */
588
589
590
591
592
593
594
595
static int cmd_setbuffer(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
596
do_buffer_size = (unsigned int) atoi(args);
597
598
599
600
601
602
603
604
605
606
607
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 */
608
return 1;
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
} /* 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
{
627
PHYSFS_File *f;
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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");
645
return 1;
646
647
648
} /* if */
strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
649
srand((unsigned int) time(NULL));
650
651
652
653
654
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
655
656
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
657
if (PHYSFS_writeBytes(f, buf, left) != left)
658
{
659
printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
660
PHYSFS_close(f);
661
return 1;
662
663
664
665
666
667
668
669
670
} /* 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);
671
return 1;
672
673
674
} /* if */
} /* if */
675
if (PHYSFS_writeBytes(f, buf + left, right) != right)
676
{
677
printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
678
PHYSFS_close(f);
679
return 1;
680
681
682
683
684
685
686
687
688
} /* 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);
689
return 1;
690
691
692
693
694
695
696
697
} /* if */
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
698
return 1;
699
700
701
702
703
704
705
} /* if */
} /* for */
if (!PHYSFS_close(f))
{
printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
706
return 1; /* oh well. */
707
708
709
710
711
712
713
} /* 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());
714
return 1;
715
716
717
718
719
720
} /* if */
if (!PHYSFS_setBuffer(f, num))
{
printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
721
return 1;
722
723
724
725
726
727
} /* if */
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
728
729
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
730
if (PHYSFS_readBytes(f, buf2, left) != left)
731
{
732
printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
733
PHYSFS_close(f);
734
return 1;
735
736
737
738
739
740
741
742
743
} /* 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);
744
return 1;
745
746
747
} /* if */
} /* if */
748
if (PHYSFS_readBytes(f, buf2 + left, right) != right)
749
{
750
printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
751
PHYSFS_close(f);
752
return 1;
753
754
755
756
757
758
759
760
761
} /* 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);
762
return 1;
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
} /* 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);
779
return 1;
780
781
782
783
784
785
786
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
787
return 1;
788
789
790
791
792
793
794
795
796
797
798
799
} /* 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());
800
return 1; /* oh well. */
801
802
803
804
805
806
807
} /* if */
PHYSFS_delete("test.txt");
printf("stress test completed successfully.\n");
} /* else */
} /* else */
808
return 1;
809
810
811
} /* cmd_stressbuffer */
812
813
static int cmd_setsaneconfig(char *args)
{
814
char *org;
815
816
817
818
819
820
821
char *appName;
char *arcExt;
int inclCD;
int arcsFirst;
char *ptr = args;
/* ugly. */
822
823
org = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
824
825
826
827
828
829
830
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;
831
if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
832
833
834
835
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
836
return 1;
837
838
839
} /* cmd_setsaneconfig */
840
841
static int cmd_mkdir(char *args)
{
842
843
844
845
846
847
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
848
849
850
851
852
if (PHYSFS_mkdir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
853
return 1;
854
855
856
857
858
} /* cmd_mkdir */
static int cmd_delete(char *args)
{
859
860
861
862
863
864
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
865
866
867
868
869
if (PHYSFS_delete(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
870
return 1;
871
872
873
874
875
} /* cmd_delete */
static int cmd_getrealdir(char *args)
{
876
877
878
879
880
881
882
883
884
const char *rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_getRealDir(args);
885
886
887
888
889
if (rc)
printf("Found at [%s].\n", rc);
else
printf("Not found.\n");
890
return 1;
891
892
893
894
895
} /* cmd_getrealdir */
static int cmd_exists(char *args)
{
896
897
898
899
900
901
902
903
904
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_exists(args);
905
printf("File %sexists.\n", rc ? "" : "does not ");
906
return 1;
907
908
909
910
911
} /* cmd_exists */
static int cmd_isdir(char *args)
{
912
PHYSFS_Stat statbuf;
913
914
915
916
917
918
919
920
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
921
922
923
rc = PHYSFS_stat(args, &statbuf);
if (rc)
rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
924
printf("File %s a directory.\n", rc ? "is" : "is NOT");
925
return 1;
926
927
928
929
930
} /* cmd_isdir */
static int cmd_issymlink(char *args)
{
931
PHYSFS_Stat statbuf;
932
933
934
935
936
937
938
939
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
940
941
942
rc = PHYSFS_stat(args, &statbuf);
if (rc)
rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
943
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
944
return 1;
945
946
947
} /* cmd_issymlink */
948
949
static int cmd_cat(char *args)
{
950
PHYSFS_File *f;
951
952
953
954
955
956
957
958
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openRead(args);
959
960
961
962
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
963
964
965
966
967
968
969
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);
970
return 1;
971
972
973
} /* if */
} /* if */
974
975
976
while (1)
{
char buffer[128];
977
978
PHYSFS_sint64 rc;
PHYSFS_sint64 i;
979
rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
980
981
982
983
984
985
986
987
for (i = 0; i < rc; i++)
fputc((int) buffer[i], stdout);
if (rc < sizeof (buffer))
{
printf("\n\n");
if (!PHYSFS_eof(f))
988
989
990
991
{
printf("\n (Error condition in reading. Reason: [%s])\n\n",
PHYSFS_getLastError());
} /* if */
992
PHYSFS_close(f);
993
return 1;
994
995
996
997
} /* if */
} /* while */
} /* else */
998
return 1;
999
1000
} /* cmd_cat */