Skip to content

Latest commit

 

History

History
1266 lines (1031 loc) · 31.4 KB

test_physfs.c

File metadata and controls

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