test_physfs.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /**
  2. * Test program for PhysicsFS. May only work on Unix.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #if (defined __MWERKS__)
  13. #include <SIOUX.h>
  14. #endif
  15. #if (defined PHYSFS_HAVE_READLINE)
  16. #include <unistd.h>
  17. #include <readline/readline.h>
  18. #include <readline/history.h>
  19. #endif
  20. #include <time.h>
  21. /* Define this, so the compiler doesn't complain about using old APIs. */
  22. #define PHYSFS_DEPRECATED
  23. #include "physfs.h"
  24. #define TEST_VERSION_MAJOR 2
  25. #define TEST_VERSION_MINOR 1
  26. #define TEST_VERSION_PATCH 0
  27. static FILE *history_file = NULL;
  28. static PHYSFS_uint32 do_buffer_size = 0;
  29. static void output_versions(void)
  30. {
  31. PHYSFS_Version compiled;
  32. PHYSFS_Version linked;
  33. PHYSFS_VERSION(&compiled);
  34. PHYSFS_getLinkedVersion(&linked);
  35. printf("test_physfs version %d.%d.%d.\n"
  36. " Compiled against PhysicsFS version %d.%d.%d,\n"
  37. " and linked against %d.%d.%d.\n\n",
  38. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  39. (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
  40. (int) linked.major, (int) linked.minor, (int) linked.patch);
  41. } /* output_versions */
  42. static void output_archivers(void)
  43. {
  44. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  45. const PHYSFS_ArchiveInfo **i;
  46. printf("Supported archive types:\n");
  47. if (*rc == NULL)
  48. printf(" * Apparently, NONE!\n");
  49. else
  50. {
  51. for (i = rc; *i != NULL; i++)
  52. {
  53. printf(" * %s: %s\n Written by %s.\n %s\n",
  54. (*i)->extension, (*i)->description,
  55. (*i)->author, (*i)->url);
  56. printf(" %s symbolic links.\n",
  57. (*i)->supportsSymlinks ? "Supports" : "Does not support");
  58. } /* for */
  59. } /* else */
  60. printf("\n");
  61. } /* output_archivers */
  62. static int cmd_quit(char *args)
  63. {
  64. return 0;
  65. } /* cmd_quit */
  66. static int cmd_init(char *args)
  67. {
  68. if (*args == '\"')
  69. {
  70. args++;
  71. args[strlen(args) - 1] = '\0';
  72. } /* if */
  73. if (PHYSFS_init(args))
  74. printf("Successful.\n");
  75. else
  76. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  77. return 1;
  78. } /* cmd_init */
  79. static int cmd_deinit(char *args)
  80. {
  81. if (PHYSFS_deinit())
  82. printf("Successful.\n");
  83. else
  84. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  85. return 1;
  86. } /* cmd_deinit */
  87. static int cmd_addarchive(char *args)
  88. {
  89. char *ptr = strrchr(args, ' ');
  90. int appending = atoi(ptr + 1);
  91. *ptr = '\0';
  92. if (*args == '\"')
  93. {
  94. args++;
  95. *(ptr - 1) = '\0';
  96. } /* if */
  97. /*printf("[%s], [%d]\n", args, appending);*/
  98. if (PHYSFS_mount(args, NULL, appending))
  99. printf("Successful.\n");
  100. else
  101. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  102. return 1;
  103. } /* cmd_addarchive */
  104. /* wrap free() to avoid calling convention wankery. */
  105. static void freeBuf(void *buf)
  106. {
  107. free(buf);
  108. } /* freeBuf */
  109. typedef enum
  110. {
  111. MNTTYPE_PATH,
  112. MNTTYPE_MEMORY,
  113. MNTTYPE_HANDLE
  114. } MountType;
  115. static int cmd_mount_internal(char *args, const MountType mnttype)
  116. {
  117. char *ptr;
  118. char *mntpoint = NULL;
  119. int appending = 0;
  120. int rc = 0;
  121. if (*args == '\"')
  122. {
  123. args++;
  124. ptr = strchr(args, '\"');
  125. if (ptr == NULL)
  126. {
  127. printf("missing string terminator in argument.\n");
  128. return 1;
  129. } /* if */
  130. *(ptr) = '\0';
  131. } /* if */
  132. else
  133. {
  134. ptr = strchr(args, ' ');
  135. *ptr = '\0';
  136. } /* else */
  137. mntpoint = ptr + 1;
  138. if (*mntpoint == '\"')
  139. {
  140. mntpoint++;
  141. ptr = strchr(mntpoint, '\"');
  142. if (ptr == NULL)
  143. {
  144. printf("missing string terminator in argument.\n");
  145. return 1;
  146. } /* if */
  147. *(ptr) = '\0';
  148. } /* if */
  149. else
  150. {
  151. ptr = strchr(mntpoint, ' ');
  152. *(ptr) = '\0';
  153. } /* else */
  154. appending = atoi(ptr + 1);
  155. /*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
  156. if (mnttype == MNTTYPE_PATH)
  157. rc = PHYSFS_mount(args, mntpoint, appending);
  158. else if (mnttype == MNTTYPE_HANDLE)
  159. {
  160. PHYSFS_File *f = PHYSFS_openRead(args);
  161. if (f == NULL)
  162. {
  163. printf("PHYSFS_openRead('%s') failed. reason: %s.\n", args, PHYSFS_getLastError());
  164. return 1;
  165. } /* if */
  166. rc = PHYSFS_mountHandle(f, args, mntpoint, appending);
  167. if (!rc)
  168. PHYSFS_close(f);
  169. } /* else if */
  170. else if (mnttype == MNTTYPE_MEMORY)
  171. {
  172. FILE *in = fopen(args, "rb");
  173. void *buf = NULL;
  174. long len = 0;
  175. if (in == NULL)
  176. {
  177. printf("Failed to open %s to read into memory: %s.\n", args, strerror(errno));
  178. return 1;
  179. } /* if */
  180. if ( (fseek(in, 0, SEEK_END) != 0) || ((len = ftell(in)) < 0) )
  181. {
  182. printf("Failed to find size of %s to read into memory: %s.\n", args, strerror(errno));
  183. fclose(in);
  184. return 1;
  185. } /* if */
  186. buf = malloc(len);
  187. if (buf == NULL)
  188. {
  189. printf("Failed to allocate space to read %s into memory: %s.\n", args, strerror(errno));
  190. fclose(in);
  191. return 1;
  192. } /* if */
  193. if ((fseek(in, 0, SEEK_SET) != 0) || (fread(buf, len, 1, in) != 1))
  194. {
  195. printf("Failed to read %s into memory: %s.\n", args, strerror(errno));
  196. fclose(in);
  197. free(buf);
  198. return 1;
  199. } /* if */
  200. fclose(in);
  201. rc = PHYSFS_mountMemory(buf, len, freeBuf, args, mntpoint, appending);
  202. } /* else */
  203. if (rc)
  204. printf("Successful.\n");
  205. else
  206. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  207. return 1;
  208. } /* cmd_mount_internal */
  209. static int cmd_mount(char *args)
  210. {
  211. return cmd_mount_internal(args, MNTTYPE_PATH);
  212. } /* cmd_mount */
  213. static int cmd_mount_mem(char *args)
  214. {
  215. return cmd_mount_internal(args, MNTTYPE_MEMORY);
  216. } /* cmd_mount_mem */
  217. static int cmd_mount_handle(char *args)
  218. {
  219. return cmd_mount_internal(args, MNTTYPE_HANDLE);
  220. } /* cmd_mount_handle */
  221. static int cmd_getmountpoint(char *args)
  222. {
  223. if (*args == '\"')
  224. {
  225. args++;
  226. args[strlen(args) - 1] = '\0';
  227. } /* if */
  228. printf("Dir [%s] is mounted at [%s].\n", args, PHYSFS_getMountPoint(args));
  229. return 1;
  230. } /* cmd_getmountpoint */
  231. static int cmd_removearchive(char *args)
  232. {
  233. if (*args == '\"')
  234. {
  235. args++;
  236. args[strlen(args) - 1] = '\0';
  237. } /* if */
  238. if (PHYSFS_unmount(args))
  239. printf("Successful.\n");
  240. else
  241. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  242. return 1;
  243. } /* cmd_removearchive */
  244. static int cmd_enumerate(char *args)
  245. {
  246. char **rc;
  247. if (*args == '\"')
  248. {
  249. args++;
  250. args[strlen(args) - 1] = '\0';
  251. } /* if */
  252. rc = PHYSFS_enumerateFiles(args);
  253. if (rc == NULL)
  254. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  255. else
  256. {
  257. int file_count;
  258. char **i;
  259. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  260. printf("%s\n", *i);
  261. printf("\n total (%d) files.\n", file_count);
  262. PHYSFS_freeList(rc);
  263. } /* else */
  264. return 1;
  265. } /* cmd_enumerate */
  266. static int cmd_getdirsep(char *args)
  267. {
  268. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  269. return 1;
  270. } /* cmd_getdirsep */
  271. static int cmd_getlasterror(char *args)
  272. {
  273. printf("last error is [%s].\n", PHYSFS_getLastError());
  274. return 1;
  275. } /* cmd_getlasterror */
  276. static int cmd_getcdromdirs(char *args)
  277. {
  278. char **rc = PHYSFS_getCdRomDirs();
  279. if (rc == NULL)
  280. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  281. else
  282. {
  283. int dir_count;
  284. char **i;
  285. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  286. printf("%s\n", *i);
  287. printf("\n total (%d) drives.\n", dir_count);
  288. PHYSFS_freeList(rc);
  289. } /* else */
  290. return 1;
  291. } /* cmd_getcdromdirs */
  292. static int cmd_getsearchpath(char *args)
  293. {
  294. char **rc = PHYSFS_getSearchPath();
  295. if (rc == NULL)
  296. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  297. else
  298. {
  299. int dir_count;
  300. char **i;
  301. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  302. printf("%s\n", *i);
  303. printf("\n total (%d) directories.\n", dir_count);
  304. PHYSFS_freeList(rc);
  305. } /* else */
  306. return 1;
  307. } /* cmd_getcdromdirs */
  308. static int cmd_getbasedir(char *args)
  309. {
  310. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  311. return 1;
  312. } /* cmd_getbasedir */
  313. static int cmd_getuserdir(char *args)
  314. {
  315. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  316. return 1;
  317. } /* cmd_getuserdir */
  318. static int cmd_getprefdir(char *args)
  319. {
  320. char *org;
  321. char *appName;
  322. char *ptr = args;
  323. org = ptr;
  324. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  325. printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
  326. return 1;
  327. } /* cmd_getprefdir */
  328. static int cmd_getwritedir(char *args)
  329. {
  330. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  331. return 1;
  332. } /* cmd_getwritedir */
  333. static int cmd_setwritedir(char *args)
  334. {
  335. if (*args == '\"')
  336. {
  337. args++;
  338. args[strlen(args) - 1] = '\0';
  339. } /* if */
  340. if (PHYSFS_setWriteDir(args))
  341. printf("Successful.\n");
  342. else
  343. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  344. return 1;
  345. } /* cmd_setwritedir */
  346. static int cmd_permitsyms(char *args)
  347. {
  348. int num;
  349. if (*args == '\"')
  350. {
  351. args++;
  352. args[strlen(args) - 1] = '\0';
  353. } /* if */
  354. num = atoi(args);
  355. PHYSFS_permitSymbolicLinks(num);
  356. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  357. return 1;
  358. } /* cmd_permitsyms */
  359. static int cmd_setbuffer(char *args)
  360. {
  361. if (*args == '\"')
  362. {
  363. args++;
  364. args[strlen(args) - 1] = '\0';
  365. } /* if */
  366. do_buffer_size = (unsigned int) atoi(args);
  367. if (do_buffer_size)
  368. {
  369. printf("Further tests will set a (%lu) size buffer.\n",
  370. (unsigned long) do_buffer_size);
  371. } /* if */
  372. else
  373. {
  374. printf("Further tests will NOT use a buffer.\n");
  375. } /* else */
  376. return 1;
  377. } /* cmd_setbuffer */
  378. static int cmd_stressbuffer(char *args)
  379. {
  380. int num;
  381. if (*args == '\"')
  382. {
  383. args++;
  384. args[strlen(args) - 1] = '\0';
  385. } /* if */
  386. num = atoi(args);
  387. if (num < 0)
  388. printf("buffer must be greater than or equal to zero.\n");
  389. else
  390. {
  391. PHYSFS_File *f;
  392. int rndnum;
  393. printf("Stress testing with (%d) byte buffer...\n", num);
  394. f = PHYSFS_openWrite("test.txt");
  395. if (f == NULL)
  396. printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
  397. else
  398. {
  399. int i, j;
  400. char buf[37];
  401. char buf2[37];
  402. if (!PHYSFS_setBuffer(f, num))
  403. {
  404. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  405. PHYSFS_close(f);
  406. PHYSFS_delete("test.txt");
  407. return 1;
  408. } /* if */
  409. strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
  410. srand((unsigned int) time(NULL));
  411. for (i = 0; i < 10; i++)
  412. {
  413. for (j = 0; j < 10000; j++)
  414. {
  415. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  416. PHYSFS_uint32 left = 36 - right;
  417. if (PHYSFS_writeBytes(f, buf, left) != left)
  418. {
  419. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  420. PHYSFS_close(f);
  421. return 1;
  422. } /* if */
  423. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  424. if (rndnum == 42)
  425. {
  426. if (!PHYSFS_flush(f))
  427. {
  428. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  429. PHYSFS_close(f);
  430. return 1;
  431. } /* if */
  432. } /* if */
  433. if (PHYSFS_writeBytes(f, buf + left, right) != right)
  434. {
  435. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  436. PHYSFS_close(f);
  437. return 1;
  438. } /* if */
  439. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  440. if (rndnum == 42)
  441. {
  442. if (!PHYSFS_flush(f))
  443. {
  444. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  445. PHYSFS_close(f);
  446. return 1;
  447. } /* if */
  448. } /* if */
  449. } /* for */
  450. if (!PHYSFS_flush(f))
  451. {
  452. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  453. PHYSFS_close(f);
  454. return 1;
  455. } /* if */
  456. } /* for */
  457. if (!PHYSFS_close(f))
  458. {
  459. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  460. return 1; /* oh well. */
  461. } /* if */
  462. printf(" ... test file written ...\n");
  463. f = PHYSFS_openRead("test.txt");
  464. if (f == NULL)
  465. {
  466. printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
  467. return 1;
  468. } /* if */
  469. if (!PHYSFS_setBuffer(f, num))
  470. {
  471. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  472. PHYSFS_close(f);
  473. return 1;
  474. } /* if */
  475. for (i = 0; i < 10; i++)
  476. {
  477. for (j = 0; j < 10000; j++)
  478. {
  479. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  480. PHYSFS_uint32 left = 36 - right;
  481. if (PHYSFS_readBytes(f, buf2, left) != left)
  482. {
  483. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  484. PHYSFS_close(f);
  485. return 1;
  486. } /* if */
  487. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  488. if (rndnum == 42)
  489. {
  490. if (!PHYSFS_flush(f))
  491. {
  492. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  493. PHYSFS_close(f);
  494. return 1;
  495. } /* if */
  496. } /* if */
  497. if (PHYSFS_readBytes(f, buf2 + left, right) != right)
  498. {
  499. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  500. PHYSFS_close(f);
  501. return 1;
  502. } /* if */
  503. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  504. if (rndnum == 42)
  505. {
  506. if (!PHYSFS_flush(f))
  507. {
  508. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  509. PHYSFS_close(f);
  510. return 1;
  511. } /* if */
  512. } /* if */
  513. if (memcmp(buf, buf2, 36) != 0)
  514. {
  515. printf("readback is mismatched on iterations (%d, %d).\n", i, j);
  516. printf("wanted: [");
  517. for (i = 0; i < 36; i++)
  518. printf("%c", buf[i]);
  519. printf("]\n");
  520. printf(" got: [");
  521. for (i = 0; i < 36; i++)
  522. printf("%c", buf2[i]);
  523. printf("]\n");
  524. PHYSFS_close(f);
  525. return 1;
  526. } /* if */
  527. } /* for */
  528. if (!PHYSFS_flush(f))
  529. {
  530. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  531. PHYSFS_close(f);
  532. return 1;
  533. } /* if */
  534. } /* for */
  535. printf(" ... test file read ...\n");
  536. if (!PHYSFS_eof(f))
  537. printf("PHYSFS_eof() returned true! That's wrong.\n");
  538. if (!PHYSFS_close(f))
  539. {
  540. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  541. return 1; /* oh well. */
  542. } /* if */
  543. PHYSFS_delete("test.txt");
  544. printf("stress test completed successfully.\n");
  545. } /* else */
  546. } /* else */
  547. return 1;
  548. } /* cmd_stressbuffer */
  549. static int cmd_setsaneconfig(char *args)
  550. {
  551. char *org;
  552. char *appName;
  553. char *arcExt;
  554. int inclCD;
  555. int arcsFirst;
  556. char *ptr = args;
  557. /* ugly. */
  558. org = ptr;
  559. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  560. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  561. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  562. arcsFirst = atoi(ptr);
  563. if (strcmp(arcExt, "!") == 0)
  564. arcExt = NULL;
  565. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  566. printf("Successful.\n");
  567. else
  568. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  569. return 1;
  570. } /* cmd_setsaneconfig */
  571. static int cmd_mkdir(char *args)
  572. {
  573. if (*args == '\"')
  574. {
  575. args++;
  576. args[strlen(args) - 1] = '\0';
  577. } /* if */
  578. if (PHYSFS_mkdir(args))
  579. printf("Successful.\n");
  580. else
  581. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  582. return 1;
  583. } /* cmd_mkdir */
  584. static int cmd_delete(char *args)
  585. {
  586. if (*args == '\"')
  587. {
  588. args++;
  589. args[strlen(args) - 1] = '\0';
  590. } /* if */
  591. if (PHYSFS_delete(args))
  592. printf("Successful.\n");
  593. else
  594. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  595. return 1;
  596. } /* cmd_delete */
  597. static int cmd_getrealdir(char *args)
  598. {
  599. const char *rc;
  600. if (*args == '\"')
  601. {
  602. args++;
  603. args[strlen(args) - 1] = '\0';
  604. } /* if */
  605. rc = PHYSFS_getRealDir(args);
  606. if (rc)
  607. printf("Found at [%s].\n", rc);
  608. else
  609. printf("Not found.\n");
  610. return 1;
  611. } /* cmd_getrealdir */
  612. static int cmd_exists(char *args)
  613. {
  614. int rc;
  615. if (*args == '\"')
  616. {
  617. args++;
  618. args[strlen(args) - 1] = '\0';
  619. } /* if */
  620. rc = PHYSFS_exists(args);
  621. printf("File %sexists.\n", rc ? "" : "does not ");
  622. return 1;
  623. } /* cmd_exists */
  624. static int cmd_isdir(char *args)
  625. {
  626. PHYSFS_Stat statbuf;
  627. int rc;
  628. if (*args == '\"')
  629. {
  630. args++;
  631. args[strlen(args) - 1] = '\0';
  632. } /* if */
  633. rc = PHYSFS_stat(args, &statbuf);
  634. if (rc)
  635. rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  636. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  637. return 1;
  638. } /* cmd_isdir */
  639. static int cmd_issymlink(char *args)
  640. {
  641. PHYSFS_Stat statbuf;
  642. int rc;
  643. if (*args == '\"')
  644. {
  645. args++;
  646. args[strlen(args) - 1] = '\0';
  647. } /* if */
  648. rc = PHYSFS_stat(args, &statbuf);
  649. if (rc)
  650. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  651. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  652. return 1;
  653. } /* cmd_issymlink */
  654. static int cmd_cat(char *args)
  655. {
  656. PHYSFS_File *f;
  657. if (*args == '\"')
  658. {
  659. args++;
  660. args[strlen(args) - 1] = '\0';
  661. } /* if */
  662. f = PHYSFS_openRead(args);
  663. if (f == NULL)
  664. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  665. else
  666. {
  667. if (do_buffer_size)
  668. {
  669. if (!PHYSFS_setBuffer(f, do_buffer_size))
  670. {
  671. printf("failed to set file buffer. Reason: [%s].\n",
  672. PHYSFS_getLastError());
  673. PHYSFS_close(f);
  674. return 1;
  675. } /* if */
  676. } /* if */
  677. while (1)
  678. {
  679. char buffer[128];
  680. PHYSFS_sint64 rc;
  681. PHYSFS_sint64 i;
  682. rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
  683. for (i = 0; i < rc; i++)
  684. fputc((int) buffer[i], stdout);
  685. if (rc < sizeof (buffer))
  686. {
  687. printf("\n\n");
  688. if (!PHYSFS_eof(f))
  689. {
  690. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  691. PHYSFS_getLastError());
  692. } /* if */
  693. PHYSFS_close(f);
  694. return 1;
  695. } /* if */
  696. } /* while */
  697. } /* else */
  698. return 1;
  699. } /* cmd_cat */
  700. #define CRC32_BUFFERSIZE 512
  701. static int cmd_crc32(char *args)
  702. {
  703. PHYSFS_File *f;
  704. if (*args == '\"')
  705. {
  706. args++;
  707. args[strlen(args) - 1] = '\0';
  708. } /* if */
  709. f = PHYSFS_openRead(args);
  710. if (f == NULL)
  711. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  712. else
  713. {
  714. PHYSFS_uint8 buffer[CRC32_BUFFERSIZE];
  715. PHYSFS_uint32 crc = -1;
  716. PHYSFS_sint64 bytesread;
  717. while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
  718. {
  719. PHYSFS_uint32 i, bit;
  720. for (i = 0; i < bytesread; i++)
  721. {
  722. for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
  723. crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
  724. } /* for */
  725. } /* while */
  726. if (bytesread < 0)
  727. {
  728. printf("error while reading. Reason: [%s].\n",
  729. PHYSFS_getLastError());
  730. return 1;
  731. } /* if */
  732. PHYSFS_close(f);
  733. crc ^= -1;
  734. printf("CRC32 for %s: 0x%08X\n", args, crc);
  735. } /* else */
  736. return 1;
  737. } /* cmd_crc32 */
  738. static int cmd_filelength(char *args)
  739. {
  740. PHYSFS_File *f;
  741. if (*args == '\"')
  742. {
  743. args++;
  744. args[strlen(args) - 1] = '\0';
  745. } /* if */
  746. f = PHYSFS_openRead(args);
  747. if (f == NULL)
  748. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  749. else
  750. {
  751. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  752. if (len == -1)
  753. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  754. else
  755. printf(" (cast to int) %d bytes.\n", (int) len);
  756. PHYSFS_close(f);
  757. } /* else */
  758. return 1;
  759. } /* cmd_filelength */
  760. #define WRITESTR "The cat sat on the mat.\n\n"
  761. static int cmd_append(char *args)
  762. {
  763. PHYSFS_File *f;
  764. if (*args == '\"')
  765. {
  766. args++;
  767. args[strlen(args) - 1] = '\0';
  768. } /* if */
  769. f = PHYSFS_openAppend(args);
  770. if (f == NULL)
  771. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  772. else
  773. {
  774. size_t bw;
  775. PHYSFS_sint64 rc;
  776. if (do_buffer_size)
  777. {
  778. if (!PHYSFS_setBuffer(f, do_buffer_size))
  779. {
  780. printf("failed to set file buffer. Reason: [%s].\n",
  781. PHYSFS_getLastError());
  782. PHYSFS_close(f);
  783. return 1;
  784. } /* if */
  785. } /* if */
  786. bw = strlen(WRITESTR);
  787. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  788. if (rc != bw)
  789. {
  790. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  791. (int) rc, (int) bw, PHYSFS_getLastError());
  792. } /* if */
  793. else
  794. {
  795. printf("Successful.\n");
  796. } /* else */
  797. PHYSFS_close(f);
  798. } /* else */
  799. return 1;
  800. } /* cmd_append */
  801. static int cmd_write(char *args)
  802. {
  803. PHYSFS_File *f;
  804. if (*args == '\"')
  805. {
  806. args++;
  807. args[strlen(args) - 1] = '\0';
  808. } /* if */
  809. f = PHYSFS_openWrite(args);
  810. if (f == NULL)
  811. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  812. else
  813. {
  814. size_t bw;
  815. PHYSFS_sint64 rc;
  816. if (do_buffer_size)
  817. {
  818. if (!PHYSFS_setBuffer(f, do_buffer_size))
  819. {
  820. printf("failed to set file buffer. Reason: [%s].\n",
  821. PHYSFS_getLastError());
  822. PHYSFS_close(f);
  823. return 1;
  824. } /* if */
  825. } /* if */
  826. bw = strlen(WRITESTR);
  827. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  828. if (rc != bw)
  829. {
  830. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  831. (int) rc, (int) bw, PHYSFS_getLastError());
  832. } /* if */
  833. else
  834. {
  835. printf("Successful.\n");
  836. } /* else */
  837. PHYSFS_close(f);
  838. } /* else */
  839. return 1;
  840. } /* cmd_write */
  841. static char* modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  842. {
  843. if (modtime < 0)
  844. strncpy(modstr, "Unknown\n", strsize);
  845. else
  846. {
  847. time_t t = (time_t) modtime;
  848. char *str = ctime(&t);
  849. strncpy(modstr, str, strsize);
  850. } /* else */
  851. modstr[strsize-1] = '\0';
  852. return modstr;
  853. } /* modTimeToStr */
  854. static int cmd_getlastmodtime(char *args)
  855. {
  856. PHYSFS_Stat statbuf;
  857. if (!PHYSFS_stat(args, &statbuf))
  858. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  859. else
  860. {
  861. char modstr[64];
  862. modTimeToStr(statbuf.modtime, modstr, sizeof (modstr));
  863. printf("Last modified: %s (%ld).\n", modstr, (long) statbuf.modtime);
  864. } /* else */
  865. return 1;
  866. } /* cmd_getLastModTime */
  867. static int cmd_stat(char *args)
  868. {
  869. PHYSFS_Stat stat;
  870. char timestring[65];
  871. if (*args == '\"')
  872. {
  873. args++;
  874. args[strlen(args) - 1] = '\0';
  875. } /* if */
  876. if(!PHYSFS_stat(args, &stat))
  877. {
  878. printf("failed to stat. Reason [%s].\n", PHYSFS_getLastError());
  879. return 1;
  880. } /* if */
  881. printf("Filename: %s\n", args);
  882. printf("Size %d\n",(int) stat.filesize);
  883. if(stat.filetype == PHYSFS_FILETYPE_REGULAR)
  884. printf("Type: File\n");
  885. else if(stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
  886. printf("Type: Directory\n");
  887. else if(stat.filetype == PHYSFS_FILETYPE_SYMLINK)
  888. printf("Type: Symlink\n");
  889. else
  890. printf("Type: Unknown\n");
  891. printf("Created at: %s", modTimeToStr(stat.createtime, timestring, 64));
  892. printf("Last modified at: %s", modTimeToStr(stat.modtime, timestring, 64));
  893. printf("Last accessed at: %s", modTimeToStr(stat.accesstime, timestring, 64));
  894. printf("Readonly: %s\n", stat.readonly ? "true" : "false");
  895. return 1;
  896. } /* cmd_filelength */
  897. /* must have spaces trimmed prior to this call. */
  898. static int count_args(const char *str)
  899. {
  900. int retval = 0;
  901. int in_quotes = 0;
  902. if (str != NULL)
  903. {
  904. for (; *str != '\0'; str++)
  905. {
  906. if (*str == '\"')
  907. in_quotes = !in_quotes;
  908. else if ((*str == ' ') && (!in_quotes))
  909. retval++;
  910. } /* for */
  911. retval++;
  912. } /* if */
  913. return retval;
  914. } /* count_args */
  915. static int cmd_help(char *args);
  916. typedef struct
  917. {
  918. const char *cmd;
  919. int (*func)(char *args);
  920. int argcount;
  921. const char *usage;
  922. } command_info;
  923. static const command_info commands[] =
  924. {
  925. { "quit", cmd_quit, 0, NULL },
  926. { "q", cmd_quit, 0, NULL },
  927. { "help", cmd_help, 0, NULL },
  928. { "init", cmd_init, 1, "<argv0>" },
  929. { "deinit", cmd_deinit, 0, NULL },
  930. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  931. { "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
  932. { "mountmem", cmd_mount_mem, 3, "<archiveLocation> <mntpoint> <append>" },
  933. { "mounthandle", cmd_mount_handle, 3, "<archiveLocation> <mntpoint> <append>" },
  934. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  935. { "unmount", cmd_removearchive, 1, "<archiveLocation>" },
  936. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  937. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  938. { "getlasterror", cmd_getlasterror, 0, NULL },
  939. { "getdirsep", cmd_getdirsep, 0, NULL },
  940. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  941. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  942. { "getbasedir", cmd_getbasedir, 0, NULL },
  943. { "getuserdir", cmd_getuserdir, 0, NULL },
  944. { "getprefdir", cmd_getprefdir, 2, "<org> <app>" },
  945. { "getwritedir", cmd_getwritedir, 0, NULL },
  946. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  947. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  948. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  949. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  950. { "delete", cmd_delete, 1, "<dirToDelete>" },
  951. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  952. { "exists", cmd_exists, 1, "<fileToCheck>" },
  953. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  954. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  955. { "cat", cmd_cat, 1, "<fileToCat>" },
  956. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  957. { "stat", cmd_stat, 1, "<fileToStat>" },
  958. { "append", cmd_append, 1, "<fileToAppend>" },
  959. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  960. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  961. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  962. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  963. { "crc32", cmd_crc32, 1, "<fileToHash>" },
  964. { "getmountpoint", cmd_getmountpoint, 1, "<dir>" },
  965. { NULL, NULL, -1, NULL }
  966. };
  967. static void output_usage(const char *intro, const command_info *cmdinfo)
  968. {
  969. if (cmdinfo->argcount == 0)
  970. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  971. else
  972. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  973. } /* output_usage */
  974. static int cmd_help(char *args)
  975. {
  976. const command_info *i;
  977. printf("Commands:\n");
  978. for (i = commands; i->cmd != NULL; i++)
  979. output_usage(" -", i);
  980. return 1;
  981. } /* output_cmd_help */
  982. static void trim_command(const char *orig, char *copy)
  983. {
  984. const char *i;
  985. char *writeptr = copy;
  986. int spacecount = 0;
  987. int have_first = 0;
  988. for (i = orig; *i != '\0'; i++)
  989. {
  990. if (*i == ' ')
  991. {
  992. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  993. {
  994. if ((have_first) && (!spacecount))
  995. {
  996. spacecount++;
  997. *writeptr = ' ';
  998. writeptr++;
  999. } /* if */
  1000. } /* if */
  1001. } /* if */
  1002. else
  1003. {
  1004. have_first = 1;
  1005. spacecount = 0;
  1006. *writeptr = *i;
  1007. writeptr++;
  1008. } /* else */
  1009. } /* for */
  1010. *writeptr = '\0';
  1011. /*
  1012. printf("\n command is [%s].\n", copy);
  1013. */
  1014. } /* trim_command */
  1015. static int process_command(char *complete_cmd)
  1016. {
  1017. const command_info *i;
  1018. char *cmd_copy;
  1019. char *args;
  1020. int rc = 1;
  1021. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  1022. {
  1023. printf("\n");
  1024. return 0;
  1025. } /* if */
  1026. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  1027. if (cmd_copy == NULL)
  1028. {
  1029. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  1030. return 0;
  1031. } /* if */
  1032. trim_command(complete_cmd, cmd_copy);
  1033. args = strchr(cmd_copy, ' ');
  1034. if (args != NULL)
  1035. {
  1036. *args = '\0';
  1037. args++;
  1038. } /* else */
  1039. if (cmd_copy[0] != '\0')
  1040. {
  1041. for (i = commands; i->cmd != NULL; i++)
  1042. {
  1043. if (strcmp(i->cmd, cmd_copy) == 0)
  1044. {
  1045. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  1046. output_usage("usage:", i);
  1047. else
  1048. rc = i->func(args);
  1049. break;
  1050. } /* if */
  1051. } /* for */
  1052. if (i->cmd == NULL)
  1053. printf("Unknown command. Enter \"help\" for instructions.\n");
  1054. #if (defined PHYSFS_HAVE_READLINE)
  1055. add_history(complete_cmd);
  1056. if (history_file)
  1057. {
  1058. fprintf(history_file, "%s\n", complete_cmd);
  1059. fflush(history_file);
  1060. } /* if */
  1061. #endif
  1062. } /* if */
  1063. free(cmd_copy);
  1064. return rc;
  1065. } /* process_command */
  1066. static void open_history_file(void)
  1067. {
  1068. #if (defined PHYSFS_HAVE_READLINE)
  1069. #if 0
  1070. const char *envr = getenv("TESTPHYSFS_HISTORY");
  1071. if (!envr)
  1072. return;
  1073. #else
  1074. char envr[256];
  1075. strcpy(envr, PHYSFS_getUserDir());
  1076. strcat(envr, ".testphys_history");
  1077. #endif
  1078. if (access(envr, F_OK) == 0)
  1079. {
  1080. char buf[512];
  1081. FILE *f = fopen(envr, "r");
  1082. if (!f)
  1083. {
  1084. printf("\n\n"
  1085. "Could not open history file [%s] for reading!\n"
  1086. " Will not have past history available.\n\n",
  1087. envr);
  1088. return;
  1089. } /* if */
  1090. do
  1091. {
  1092. if (fgets(buf, sizeof (buf), f) == NULL)
  1093. break;
  1094. if (buf[strlen(buf) - 1] == '\n')
  1095. buf[strlen(buf) - 1] = '\0';
  1096. add_history(buf);
  1097. } while (!feof(f));
  1098. fclose(f);
  1099. } /* if */
  1100. history_file = fopen(envr, "ab");
  1101. if (!history_file)
  1102. {
  1103. printf("\n\n"
  1104. "Could not open history file [%s] for appending!\n"
  1105. " Will not be able to record this session's history.\n\n",
  1106. envr);
  1107. } /* if */
  1108. #endif
  1109. } /* open_history_file */
  1110. int main(int argc, char **argv)
  1111. {
  1112. char *buf = NULL;
  1113. int rc = 0;
  1114. #if (defined __MWERKS__)
  1115. extern tSIOUXSettings SIOUXSettings;
  1116. SIOUXSettings.asktosaveonclose = 0;
  1117. SIOUXSettings.autocloseonquit = 1;
  1118. SIOUXSettings.rows = 40;
  1119. SIOUXSettings.columns = 120;
  1120. #endif
  1121. printf("\n");
  1122. if (!PHYSFS_init(argv[0]))
  1123. {
  1124. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1125. return 1;
  1126. } /* if */
  1127. output_versions();
  1128. output_archivers();
  1129. open_history_file();
  1130. printf("Enter commands. Enter \"help\" for instructions.\n");
  1131. fflush(stdout);
  1132. do
  1133. {
  1134. #if (defined PHYSFS_HAVE_READLINE)
  1135. buf = readline("> ");
  1136. #else
  1137. int i;
  1138. buf = (char *) malloc(512);
  1139. memset(buf, '\0', 512);
  1140. printf("> ");
  1141. fflush(stdout);
  1142. for (i = 0; i < 511; i++)
  1143. {
  1144. int ch = fgetc(stdin);
  1145. if (ch == EOF)
  1146. {
  1147. strcpy(buf, "quit");
  1148. break;
  1149. } /* if */
  1150. else if ((ch == '\n') || (ch == '\r'))
  1151. {
  1152. buf[i] = '\0';
  1153. break;
  1154. } /* else if */
  1155. else if (ch == '\b')
  1156. {
  1157. if (i > 0)
  1158. i--;
  1159. } /* else if */
  1160. else
  1161. {
  1162. buf[i] = (char) ch;
  1163. } /* else */
  1164. } /* for */
  1165. #endif
  1166. rc = process_command(buf);
  1167. fflush(stdout);
  1168. if (buf != NULL)
  1169. free(buf);
  1170. } while (rc);
  1171. if (!PHYSFS_deinit())
  1172. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1173. if (history_file)
  1174. fclose(history_file);
  1175. /*
  1176. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  1177. printf(" it makes you shoot teh railgun bettar.\n");
  1178. */
  1179. return 0;
  1180. } /* main */
  1181. /* end of test_physfs.c ... */