test_physfs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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_addToSearchPath(args, 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_removeFromSearchPath(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. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  828. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  829. { "getlasterror", cmd_getlasterror, 0, NULL },
  830. { "getdirsep", cmd_getdirsep, 0, NULL },
  831. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  832. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  833. { "getbasedir", cmd_getbasedir, 0, NULL },
  834. { "getuserdir", cmd_getuserdir, 0, NULL },
  835. { "getwritedir", cmd_getwritedir, 0, NULL },
  836. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  837. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  838. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  839. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  840. { "delete", cmd_delete, 1, "<dirToDelete>" },
  841. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  842. { "exists", cmd_exists, 1, "<fileToCheck>" },
  843. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  844. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  845. { "cat", cmd_cat, 1, "<fileToCat>" },
  846. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  847. { "stat", cmd_stat, 1, "<fileToStat>" },
  848. { "append", cmd_append, 1, "<fileToAppend>" },
  849. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  850. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  851. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  852. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  853. { "crc32", cmd_crc32, 1, "<fileToHash>" },
  854. { NULL, NULL, -1, NULL }
  855. };
  856. static void output_usage(const char *intro, const command_info *cmdinfo)
  857. {
  858. if (cmdinfo->argcount == 0)
  859. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  860. else
  861. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  862. } /* output_usage */
  863. static int cmd_help(char *args)
  864. {
  865. const command_info *i;
  866. printf("Commands:\n");
  867. for (i = commands; i->cmd != NULL; i++)
  868. output_usage(" -", i);
  869. return 1;
  870. } /* output_cmd_help */
  871. static void trim_command(const char *orig, char *copy)
  872. {
  873. const char *i;
  874. char *writeptr = copy;
  875. int spacecount = 0;
  876. int have_first = 0;
  877. for (i = orig; *i != '\0'; i++)
  878. {
  879. if (*i == ' ')
  880. {
  881. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  882. {
  883. if ((have_first) && (!spacecount))
  884. {
  885. spacecount++;
  886. *writeptr = ' ';
  887. writeptr++;
  888. } /* if */
  889. } /* if */
  890. } /* if */
  891. else
  892. {
  893. have_first = 1;
  894. spacecount = 0;
  895. *writeptr = *i;
  896. writeptr++;
  897. } /* else */
  898. } /* for */
  899. *writeptr = '\0';
  900. /*
  901. printf("\n command is [%s].\n", copy);
  902. */
  903. } /* trim_command */
  904. static int process_command(char *complete_cmd)
  905. {
  906. const command_info *i;
  907. char *cmd_copy;
  908. char *args;
  909. int rc = 1;
  910. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  911. {
  912. printf("\n");
  913. return 0;
  914. } /* if */
  915. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  916. if (cmd_copy == NULL)
  917. {
  918. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  919. return 0;
  920. } /* if */
  921. trim_command(complete_cmd, cmd_copy);
  922. args = strchr(cmd_copy, ' ');
  923. if (args != NULL)
  924. {
  925. *args = '\0';
  926. args++;
  927. } /* else */
  928. if (cmd_copy[0] != '\0')
  929. {
  930. for (i = commands; i->cmd != NULL; i++)
  931. {
  932. if (strcmp(i->cmd, cmd_copy) == 0)
  933. {
  934. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  935. output_usage("usage:", i);
  936. else
  937. rc = i->func(args);
  938. break;
  939. } /* if */
  940. } /* for */
  941. if (i->cmd == NULL)
  942. printf("Unknown command. Enter \"help\" for instructions.\n");
  943. #if (defined PHYSFS_HAVE_READLINE)
  944. add_history(complete_cmd);
  945. if (history_file)
  946. {
  947. fprintf(history_file, "%s\n", complete_cmd);
  948. fflush(history_file);
  949. } /* if */
  950. #endif
  951. } /* if */
  952. free(cmd_copy);
  953. return rc;
  954. } /* process_command */
  955. static void open_history_file(void)
  956. {
  957. #if (defined PHYSFS_HAVE_READLINE)
  958. #if 0
  959. const char *envr = getenv("TESTPHYSFS_HISTORY");
  960. if (!envr)
  961. return;
  962. #else
  963. char envr[256];
  964. strcpy(envr, PHYSFS_getUserDir());
  965. strcat(envr, ".testphys_history");
  966. #endif
  967. if (access(envr, F_OK) == 0)
  968. {
  969. char buf[512];
  970. FILE *f = fopen(envr, "r");
  971. if (!f)
  972. {
  973. printf("\n\n"
  974. "Could not open history file [%s] for reading!\n"
  975. " Will not have past history available.\n\n",
  976. envr);
  977. return;
  978. } /* if */
  979. do
  980. {
  981. if (fgets(buf, sizeof (buf), f) == NULL)
  982. break;
  983. if (buf[strlen(buf) - 1] == '\n')
  984. buf[strlen(buf) - 1] = '\0';
  985. add_history(buf);
  986. } while (!feof(f));
  987. fclose(f);
  988. } /* if */
  989. history_file = fopen(envr, "ab");
  990. if (!history_file)
  991. {
  992. printf("\n\n"
  993. "Could not open history file [%s] for appending!\n"
  994. " Will not be able to record this session's history.\n\n",
  995. envr);
  996. } /* if */
  997. #endif
  998. } /* open_history_file */
  999. int main(int argc, char **argv)
  1000. {
  1001. char *buf = NULL;
  1002. int rc = 0;
  1003. #if (defined __MWERKS__)
  1004. extern tSIOUXSettings SIOUXSettings;
  1005. SIOUXSettings.asktosaveonclose = 0;
  1006. SIOUXSettings.autocloseonquit = 1;
  1007. SIOUXSettings.rows = 40;
  1008. SIOUXSettings.columns = 120;
  1009. #endif
  1010. printf("\n");
  1011. if (!PHYSFS_init(argv[0]))
  1012. {
  1013. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1014. return 1;
  1015. } /* if */
  1016. output_versions();
  1017. output_archivers();
  1018. open_history_file();
  1019. printf("Enter commands. Enter \"help\" for instructions.\n");
  1020. fflush(stdout);
  1021. do
  1022. {
  1023. #if (defined PHYSFS_HAVE_READLINE)
  1024. buf = readline("> ");
  1025. #else
  1026. int i;
  1027. buf = (char *) malloc(512);
  1028. memset(buf, '\0', 512);
  1029. printf("> ");
  1030. fflush(stdout);
  1031. for (i = 0; i < 511; i++)
  1032. {
  1033. int ch = fgetc(stdin);
  1034. if (ch == EOF)
  1035. {
  1036. strcpy(buf, "quit");
  1037. break;
  1038. } /* if */
  1039. else if ((ch == '\n') || (ch == '\r'))
  1040. {
  1041. buf[i] = '\0';
  1042. break;
  1043. } /* else if */
  1044. else if (ch == '\b')
  1045. {
  1046. if (i > 0)
  1047. i--;
  1048. } /* else if */
  1049. else
  1050. {
  1051. buf[i] = (char) ch;
  1052. } /* else */
  1053. } /* for */
  1054. #endif
  1055. rc = process_command(buf);
  1056. fflush(stdout);
  1057. if (buf != NULL)
  1058. free(buf);
  1059. } while (rc);
  1060. if (!PHYSFS_deinit())
  1061. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1062. if (history_file)
  1063. fclose(history_file);
  1064. /*
  1065. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  1066. printf(" it makes you shoot teh railgun bettar.\n");
  1067. */
  1068. return 0;
  1069. } /* main */
  1070. /* end of test_physfs.c ... */