test_physfs.c 33 KB

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