test_physfs.c 29 KB

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