/
test_physfs.c
1412 lines (1150 loc) · 35.2 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
#include <stdio.h>
10
#include <stdlib.h>
11
#include <errno.h>
12
13
#include <string.h>
14
15
16
17
#if (defined __MWERKS__)
#include <SIOUX.h>
#endif
18
#if (defined PHYSFS_HAVE_READLINE)
19
#include <unistd.h>
20
21
#include <readline/readline.h>
#include <readline/history.h>
22
23
#endif
24
25
#include <time.h>
26
27
#include "physfs.h"
28
#define TEST_VERSION_MAJOR 2
29
#define TEST_VERSION_MINOR 1
30
#define TEST_VERSION_PATCH 0
31
32
static FILE *history_file = NULL;
33
static PHYSFS_uint32 do_buffer_size = 0;
34
35
static void output_versions(void)
36
37
38
39
40
41
42
43
44
45
46
{
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,
47
48
(int) compiled.major, (int) compiled.minor, (int) compiled.patch,
(int) linked.major, (int) linked.minor, (int) linked.patch);
49
50
51
} /* output_versions */
52
static void output_archivers(void)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
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 */
69
70
printf("\n");
71
72
73
} /* output_archivers */
74
75
static int cmd_quit(char *args)
{
76
return 0;
77
78
79
80
} /* cmd_quit */
static int cmd_init(char *args)
81
{
82
83
84
85
86
87
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
88
89
90
91
92
if (PHYSFS_init(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
93
return 1;
94
} /* cmd_init */
95
96
97
static int cmd_deinit(char *args)
98
{
99
100
101
102
103
if (PHYSFS_deinit())
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
104
return 1;
105
106
107
108
109
} /* cmd_deinit */
static int cmd_addarchive(char *args)
{
110
char *ptr = strrchr(args, ' ');
111
112
113
int appending = atoi(ptr + 1);
*ptr = '\0';
114
115
116
117
if (*args == '\"')
{
args++;
*(ptr - 1) = '\0';
118
} /* if */
119
120
121
/*printf("[%s], [%d]\n", args, appending);*/
122
if (PHYSFS_mount(args, NULL, appending))
123
124
125
126
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
127
return 1;
128
129
130
} /* cmd_addarchive */
131
132
133
134
135
136
/* wrap free() to avoid calling convention wankery. */
static void freeBuf(void *buf)
{
free(buf);
} /* freeBuf */
137
138
139
140
141
142
typedef enum
{
MNTTYPE_PATH,
MNTTYPE_MEMORY,
MNTTYPE_HANDLE
} MountType;
143
144
static int cmd_mount_internal(char *args, const MountType mnttype)
145
146
147
148
{
char *ptr;
char *mntpoint = NULL;
int appending = 0;
149
int rc = 0;
150
151
152
153
154
155
156
157
if (*args == '\"')
{
args++;
ptr = strchr(args, '\"');
if (ptr == NULL)
{
printf("missing string terminator in argument.\n");
158
return 1;
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
} /* 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");
176
return 1;
177
178
179
180
181
182
183
184
185
186
} /* if */
*(ptr) = '\0';
} /* if */
else
{
ptr = strchr(mntpoint, ' ');
*(ptr) = '\0';
} /* else */
appending = atoi(ptr + 1);
187
/*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
188
189
if (mnttype == MNTTYPE_PATH)
190
rc = PHYSFS_mount(args, mntpoint, appending);
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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)
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
{
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)
247
248
249
250
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
251
return 1;
252
253
254
255
256
} /* cmd_mount_internal */
static int cmd_mount(char *args)
{
257
return cmd_mount_internal(args, MNTTYPE_PATH);
258
259
260
} /* cmd_mount */
261
262
static int cmd_mount_mem(char *args)
{
263
return cmd_mount_internal(args, MNTTYPE_MEMORY);
264
265
266
} /* cmd_mount_mem */
267
268
269
270
271
272
static int cmd_mount_handle(char *args)
{
return cmd_mount_internal(args, MNTTYPE_HANDLE);
} /* cmd_mount_handle */
273
274
static int cmd_removearchive(char *args)
{
275
276
277
278
279
280
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
281
if (PHYSFS_unmount(args))
282
283
284
285
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
286
return 1;
287
288
289
290
291
} /* cmd_removearchive */
static int cmd_enumerate(char *args)
{
292
293
294
295
296
297
298
299
300
char **rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_enumerateFiles(args);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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 */
315
return 1;
316
317
318
319
320
321
} /* cmd_enumerate */
static int cmd_getdirsep(char *args)
{
printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
322
return 1;
323
324
325
326
327
328
} /* cmd_getdirsep */
static int cmd_getlasterror(char *args)
{
printf("last error is [%s].\n", PHYSFS_getLastError());
329
return 1;
330
331
332
333
334
335
336
337
} /* cmd_getlasterror */
static int cmd_getcdromdirs(char *args)
{
char **rc = PHYSFS_getCdRomDirs();
if (rc == NULL)
338
printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
339
340
341
342
343
344
345
346
347
348
349
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 */
350
return 1;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
} /* 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 */
371
return 1;
372
373
374
375
376
377
} /* cmd_getcdromdirs */
static int cmd_getbasedir(char *args)
{
printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
378
return 1;
379
380
381
382
383
384
} /* cmd_getbasedir */
static int cmd_getuserdir(char *args)
{
printf("User dir is [%s].\n", PHYSFS_getUserDir());
385
return 1;
386
387
388
389
390
391
} /* cmd_getuserdir */
static int cmd_getwritedir(char *args)
{
printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
392
return 1;
393
394
395
396
397
} /* cmd_getwritedir */
static int cmd_setwritedir(char *args)
{
398
399
400
401
402
403
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
404
405
406
407
408
if (PHYSFS_setWriteDir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
409
return 1;
410
411
412
413
414
} /* cmd_setwritedir */
static int cmd_permitsyms(char *args)
{
415
416
417
418
419
420
421
422
423
int num;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
num = atoi(args);
424
425
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
426
return 1;
427
428
429
} /* cmd_permitsyms */
430
431
432
433
434
435
436
437
static int cmd_setbuffer(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
438
do_buffer_size = (unsigned int) atoi(args);
439
440
441
442
443
444
445
446
447
448
449
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 */
450
return 1;
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
} /* 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
{
469
PHYSFS_File *f;
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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");
487
return 1;
488
489
490
} /* if */
strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
491
srand((unsigned int) time(NULL));
492
493
494
495
496
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
497
498
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
499
if (PHYSFS_writeBytes(f, buf, left) != left)
500
{
501
printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
502
PHYSFS_close(f);
503
return 1;
504
505
506
507
508
509
510
511
512
} /* 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);
513
return 1;
514
515
516
} /* if */
} /* if */
517
if (PHYSFS_writeBytes(f, buf + left, right) != right)
518
{
519
printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
520
PHYSFS_close(f);
521
return 1;
522
523
524
525
526
527
528
529
530
} /* 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);
531
return 1;
532
533
534
535
536
537
538
539
} /* if */
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
540
return 1;
541
542
543
544
545
546
547
} /* if */
} /* for */
if (!PHYSFS_close(f))
{
printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
548
return 1; /* oh well. */
549
550
551
552
553
554
555
} /* 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());
556
return 1;
557
558
559
560
561
562
} /* if */
if (!PHYSFS_setBuffer(f, num))
{
printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
563
return 1;
564
565
566
567
568
569
} /* if */
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10000; j++)
{
570
571
PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
PHYSFS_uint32 left = 36 - right;
572
if (PHYSFS_readBytes(f, buf2, left) != left)
573
{
574
printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
575
PHYSFS_close(f);
576
return 1;
577
578
579
580
581
582
583
584
585
} /* 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);
586
return 1;
587
588
589
} /* if */
} /* if */
590
if (PHYSFS_readBytes(f, buf2 + left, right) != right)
591
{
592
printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
593
PHYSFS_close(f);
594
return 1;
595
596
597
598
599
600
601
602
603
} /* 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);
604
return 1;
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
} /* 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);
621
return 1;
622
623
624
625
626
627
628
} /* if */
} /* for */
if (!PHYSFS_flush(f))
{
printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
PHYSFS_close(f);
629
return 1;
630
631
632
633
634
635
636
637
638
639
640
641
} /* 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());
642
return 1; /* oh well. */
643
644
645
646
647
648
649
} /* if */
PHYSFS_delete("test.txt");
printf("stress test completed successfully.\n");
} /* else */
} /* else */
650
return 1;
651
652
653
} /* cmd_stressbuffer */
654
655
static int cmd_setsaneconfig(char *args)
{
656
char *org;
657
658
659
660
661
662
663
char *appName;
char *arcExt;
int inclCD;
int arcsFirst;
char *ptr = args;
/* ugly. */
664
665
org = ptr;
ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
666
667
668
669
670
671
672
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;
673
if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
674
675
676
677
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
678
return 1;
679
680
681
} /* cmd_setsaneconfig */
682
683
static int cmd_mkdir(char *args)
{
684
685
686
687
688
689
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
690
691
692
693
694
if (PHYSFS_mkdir(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
695
return 1;
696
697
698
699
700
} /* cmd_mkdir */
static int cmd_delete(char *args)
{
701
702
703
704
705
706
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
707
708
709
710
711
if (PHYSFS_delete(args))
printf("Successful.\n");
else
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
712
return 1;
713
714
715
716
717
} /* cmd_delete */
static int cmd_getrealdir(char *args)
{
718
719
720
721
722
723
724
725
726
const char *rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_getRealDir(args);
727
728
729
730
731
if (rc)
printf("Found at [%s].\n", rc);
else
printf("Not found.\n");
732
return 1;
733
734
735
736
737
} /* cmd_getrealdir */
static int cmd_exists(char *args)
{
738
739
740
741
742
743
744
745
746
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
rc = PHYSFS_exists(args);
747
printf("File %sexists.\n", rc ? "" : "does not ");
748
return 1;
749
750
751
752
753
} /* cmd_exists */
static int cmd_isdir(char *args)
{
754
PHYSFS_Stat statbuf;
755
756
757
758
759
760
761
762
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
763
764
765
rc = PHYSFS_stat(args, &statbuf);
if (rc)
rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
766
printf("File %s a directory.\n", rc ? "is" : "is NOT");
767
return 1;
768
769
770
771
772
} /* cmd_isdir */
static int cmd_issymlink(char *args)
{
773
PHYSFS_Stat statbuf;
774
775
776
777
778
779
780
781
int rc;
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
782
783
784
rc = PHYSFS_stat(args, &statbuf);
if (rc)
rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
785
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
786
return 1;
787
788
789
} /* cmd_issymlink */
790
791
static int cmd_cat(char *args)
{
792
PHYSFS_File *f;
793
794
795
796
797
798
799
800
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */
f = PHYSFS_openRead(args);
801
802
803
804
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
805
806
807
808
809
810
811
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);
812
return 1;
813
814
815
} /* if */
} /* if */
816
817
818
while (1)
{
char buffer[128];
819
820
PHYSFS_sint64 rc;
PHYSFS_sint64 i;
821
rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
822
823
824
825
826
827
828
829
for (i = 0; i < rc; i++)
fputc((int) buffer[i], stdout);
if (rc < sizeof (buffer))
{
printf("\n\n");
if (!PHYSFS_eof(f))
830
831
832
833
{
printf("\n (Error condition in reading. Reason: [%s])\n\n",
PHYSFS_getLastError());
} /* if */
834
PHYSFS_close(f);
835
return 1;
836
837
838
839
} /* if */
} /* while */
} /* else */
840
return 1;
841
842
843
} /* cmd_cat */
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#define CRC32_BUFFERSIZE 512
static int cmd_crc32(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_uint8 buffer[CRC32_BUFFERSIZE];
PHYSFS_uint32 crc = -1;
PHYSFS_sint64 bytesread;
864
while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
{
PHYSFS_uint32 i, bit;
for (i = 0; i < bytesread; i++)
{
for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
} /* for */
} /* while */
if (bytesread < 0)
{
printf("error while reading. Reason: [%s].\n",
PHYSFS_getLastError());
return 1;
} /* if */
PHYSFS_close(f);
crc ^= -1;
printf("CRC32 for %s: 0x%08X\n", args, crc);
} /* else */
return 1;
} /* cmd_crc32 */
891
892
static int cmd_filelength(char *args)
{
893
PHYSFS_File *f;
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
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 */
915
return 1;
916
917
918
919
920
921
} /* cmd_filelength */
#define WRITESTR "The cat sat on the mat.\n\n"
static int cmd_append(char *args)
{
922
PHYSFS_File *f;
923
924
925
926
927
928
929
930
931
932
933
934
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
{
935
936
937
938
939
940
941
942
943
944
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);
945
return 1;
946
947
948
949
} /* if */
} /* if */
bw = strlen(WRITESTR);
950
rc = PHYSFS_writeBytes(f, WRITESTR, bw);
951
952
if (rc != bw)
{
953
954
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
(int) rc, (int) bw, PHYSFS_getLastError());
955
956
957
958
959
960
961
962
963
} /* if */
else
{
printf("Successful.\n");
} /* else */
PHYSFS_close(f);
} /* else */
964
return 1;
965
966
967
968
969
} /* cmd_append */
static int cmd_write(char *args)
{
970
PHYSFS_File *f;
971
972
973
974
975
976
977
978
979
980
981
982
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
{
983
984
985
986
987
988
989
990
991
992
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);
993
return 1;
994
995
996
997
} /* if */
} /* if */
bw = strlen(WRITESTR);
998
rc = PHYSFS_writeBytes(f, WRITESTR, bw);
999
1000
if (rc != bw)
{