test_physfs.c 34 KB

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