Skip to content

Latest commit

 

History

History
1449 lines (1181 loc) · 36.2 KB

test_physfs.c

File metadata and controls

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