2
0

test_physfs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 0
  24. #define TEST_VERSION_PATCH 3
  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_write(f, buf, left, 1) != 1)
  323. {
  324. printf("PHYSFS_write() 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_write(f, buf + left, 1, right) != right)
  339. {
  340. printf("PHYSFS_write() 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_read(f, buf2, left, 1) != 1)
  387. {
  388. printf("PHYSFS_read() 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_read(f, buf2 + left, 1, right) != right)
  403. {
  404. printf("PHYSFS_read() 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_read(f, buffer, 1, 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. static int cmd_filelength(char *args)
  600. {
  601. PHYSFS_File *f;
  602. if (*args == '\"')
  603. {
  604. args++;
  605. args[strlen(args) - 1] = '\0';
  606. } /* if */
  607. f = PHYSFS_openRead(args);
  608. if (f == NULL)
  609. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  610. else
  611. {
  612. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  613. if (len == -1)
  614. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  615. else
  616. printf(" (cast to int) %d bytes.\n", (int) len);
  617. PHYSFS_close(f);
  618. } /* else */
  619. return(1);
  620. } /* cmd_filelength */
  621. #define WRITESTR "The cat sat on the mat.\n\n"
  622. static int cmd_append(char *args)
  623. {
  624. PHYSFS_File *f;
  625. if (*args == '\"')
  626. {
  627. args++;
  628. args[strlen(args) - 1] = '\0';
  629. } /* if */
  630. f = PHYSFS_openAppend(args);
  631. if (f == NULL)
  632. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  633. else
  634. {
  635. size_t bw;
  636. PHYSFS_sint64 rc;
  637. if (do_buffer_size)
  638. {
  639. if (!PHYSFS_setBuffer(f, do_buffer_size))
  640. {
  641. printf("failed to set file buffer. Reason: [%s].\n",
  642. PHYSFS_getLastError());
  643. PHYSFS_close(f);
  644. return(1);
  645. } /* if */
  646. } /* if */
  647. bw = strlen(WRITESTR);
  648. rc = PHYSFS_write(f, WRITESTR, 1, bw);
  649. if (rc != bw)
  650. {
  651. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  652. (int) rc, (int) bw, PHYSFS_getLastError());
  653. } /* if */
  654. else
  655. {
  656. printf("Successful.\n");
  657. } /* else */
  658. PHYSFS_close(f);
  659. } /* else */
  660. return(1);
  661. } /* cmd_append */
  662. static int cmd_write(char *args)
  663. {
  664. PHYSFS_File *f;
  665. if (*args == '\"')
  666. {
  667. args++;
  668. args[strlen(args) - 1] = '\0';
  669. } /* if */
  670. f = PHYSFS_openWrite(args);
  671. if (f == NULL)
  672. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  673. else
  674. {
  675. size_t bw;
  676. PHYSFS_sint64 rc;
  677. if (do_buffer_size)
  678. {
  679. if (!PHYSFS_setBuffer(f, do_buffer_size))
  680. {
  681. printf("failed to set file buffer. Reason: [%s].\n",
  682. PHYSFS_getLastError());
  683. PHYSFS_close(f);
  684. return(1);
  685. } /* if */
  686. } /* if */
  687. bw = strlen(WRITESTR);
  688. rc = PHYSFS_write(f, WRITESTR, 1, bw);
  689. if (rc != bw)
  690. {
  691. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  692. (int) rc, (int) bw, PHYSFS_getLastError());
  693. } /* if */
  694. else
  695. {
  696. printf("Successful.\n");
  697. } /* else */
  698. PHYSFS_close(f);
  699. } /* else */
  700. return(1);
  701. } /* cmd_write */
  702. static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  703. {
  704. time_t t = (time_t) modtime;
  705. char *str = ctime(&t);
  706. strncpy(modstr, str, strsize);
  707. modstr[strsize-1] = '\0';
  708. } /* modTimeToStr */
  709. static int cmd_getlastmodtime(char *args)
  710. {
  711. PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);
  712. if (rc == -1)
  713. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  714. else
  715. {
  716. char modstr[64];
  717. modTimeToStr(rc, modstr, sizeof (modstr));
  718. printf("Last modified: %s (%ld).\n", modstr, (long) rc);
  719. } /* else */
  720. return(1);
  721. } /* cmd_getLastModTime */
  722. /* must have spaces trimmed prior to this call. */
  723. static int count_args(const char *str)
  724. {
  725. int retval = 0;
  726. int in_quotes = 0;
  727. if (str != NULL)
  728. {
  729. for (; *str != '\0'; str++)
  730. {
  731. if (*str == '\"')
  732. in_quotes = !in_quotes;
  733. else if ((*str == ' ') && (!in_quotes))
  734. retval++;
  735. } /* for */
  736. retval++;
  737. } /* if */
  738. return(retval);
  739. } /* count_args */
  740. static int cmd_help(char *args);
  741. typedef struct
  742. {
  743. const char *cmd;
  744. int (*func)(char *args);
  745. int argcount;
  746. const char *usage;
  747. } command_info;
  748. static const command_info commands[] =
  749. {
  750. { "quit", cmd_quit, 0, NULL },
  751. { "q", cmd_quit, 0, NULL },
  752. { "help", cmd_help, 0, NULL },
  753. { "init", cmd_init, 1, "<argv0>" },
  754. { "deinit", cmd_deinit, 0, NULL },
  755. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  756. { "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
  757. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  758. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  759. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  760. { "getlasterror", cmd_getlasterror, 0, NULL },
  761. { "getdirsep", cmd_getdirsep, 0, NULL },
  762. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  763. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  764. { "getbasedir", cmd_getbasedir, 0, NULL },
  765. { "getuserdir", cmd_getuserdir, 0, NULL },
  766. { "getwritedir", cmd_getwritedir, 0, NULL },
  767. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  768. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  769. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  770. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  771. { "delete", cmd_delete, 1, "<dirToDelete>" },
  772. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  773. { "exists", cmd_exists, 1, "<fileToCheck>" },
  774. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  775. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  776. { "cat", cmd_cat, 1, "<fileToCat>" },
  777. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  778. { "append", cmd_append, 1, "<fileToAppend>" },
  779. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  780. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  781. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  782. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  783. { NULL, NULL, -1, NULL }
  784. };
  785. static void output_usage(const char *intro, const command_info *cmdinfo)
  786. {
  787. if (cmdinfo->argcount == 0)
  788. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  789. else
  790. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  791. } /* output_usage */
  792. static int cmd_help(char *args)
  793. {
  794. const command_info *i;
  795. printf("Commands:\n");
  796. for (i = commands; i->cmd != NULL; i++)
  797. output_usage(" -", i);
  798. return(1);
  799. } /* output_cmd_help */
  800. static void trim_command(const char *orig, char *copy)
  801. {
  802. const char *i;
  803. char *writeptr = copy;
  804. int spacecount = 0;
  805. int have_first = 0;
  806. for (i = orig; *i != '\0'; i++)
  807. {
  808. if (*i == ' ')
  809. {
  810. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  811. {
  812. if ((have_first) && (!spacecount))
  813. {
  814. spacecount++;
  815. *writeptr = ' ';
  816. writeptr++;
  817. } /* if */
  818. } /* if */
  819. } /* if */
  820. else
  821. {
  822. have_first = 1;
  823. spacecount = 0;
  824. *writeptr = *i;
  825. writeptr++;
  826. } /* else */
  827. } /* for */
  828. *writeptr = '\0';
  829. /*
  830. printf("\n command is [%s].\n", copy);
  831. */
  832. } /* trim_command */
  833. static int process_command(char *complete_cmd)
  834. {
  835. const command_info *i;
  836. char *cmd_copy;
  837. char *args;
  838. int rc = 1;
  839. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  840. {
  841. printf("\n");
  842. return(0);
  843. } /* if */
  844. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  845. if (cmd_copy == NULL)
  846. {
  847. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  848. return(0);
  849. } /* if */
  850. trim_command(complete_cmd, cmd_copy);
  851. args = strchr(cmd_copy, ' ');
  852. if (args != NULL)
  853. {
  854. *args = '\0';
  855. args++;
  856. } /* else */
  857. if (cmd_copy[0] != '\0')
  858. {
  859. for (i = commands; i->cmd != NULL; i++)
  860. {
  861. if (strcmp(i->cmd, cmd_copy) == 0)
  862. {
  863. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  864. output_usage("usage:", i);
  865. else
  866. rc = i->func(args);
  867. break;
  868. } /* if */
  869. } /* for */
  870. if (i->cmd == NULL)
  871. printf("Unknown command. Enter \"help\" for instructions.\n");
  872. #if (defined PHYSFS_HAVE_READLINE)
  873. add_history(complete_cmd);
  874. if (history_file)
  875. {
  876. fprintf(history_file, "%s\n", complete_cmd);
  877. fflush(history_file);
  878. } /* if */
  879. #endif
  880. } /* if */
  881. free(cmd_copy);
  882. return(rc);
  883. } /* process_command */
  884. static void open_history_file(void)
  885. {
  886. #if (defined PHYSFS_HAVE_READLINE)
  887. #if 0
  888. const char *envr = getenv("TESTPHYSFS_HISTORY");
  889. if (!envr)
  890. return;
  891. #else
  892. char envr[256];
  893. strcpy(envr, PHYSFS_getUserDir());
  894. strcat(envr, ".testphys_history");
  895. #endif
  896. if (access(envr, F_OK) == 0)
  897. {
  898. char buf[512];
  899. FILE *f = fopen(envr, "r");
  900. if (!f)
  901. {
  902. printf("\n\n"
  903. "Could not open history file [%s] for reading!\n"
  904. " Will not have past history available.\n\n",
  905. envr);
  906. return;
  907. } /* if */
  908. do
  909. {
  910. if (fgets(buf, sizeof (buf), f) == NULL)
  911. break;
  912. if (buf[strlen(buf) - 1] == '\n')
  913. buf[strlen(buf) - 1] = '\0';
  914. add_history(buf);
  915. } while (!feof(f));
  916. fclose(f);
  917. } /* if */
  918. history_file = fopen(envr, "ab");
  919. if (!history_file)
  920. {
  921. printf("\n\n"
  922. "Could not open history file [%s] for appending!\n"
  923. " Will not be able to record this session's history.\n\n",
  924. envr);
  925. } /* if */
  926. #endif
  927. } /* open_history_file */
  928. int main(int argc, char **argv)
  929. {
  930. char *buf = NULL;
  931. int rc = 0;
  932. #if (defined __MWERKS__)
  933. extern tSIOUXSettings SIOUXSettings;
  934. SIOUXSettings.asktosaveonclose = 0;
  935. SIOUXSettings.autocloseonquit = 1;
  936. SIOUXSettings.rows = 40;
  937. SIOUXSettings.columns = 120;
  938. #endif
  939. printf("\n");
  940. if (!PHYSFS_init(argv[0]))
  941. {
  942. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  943. return(1);
  944. } /* if */
  945. output_versions();
  946. output_archivers();
  947. open_history_file();
  948. printf("Enter commands. Enter \"help\" for instructions.\n");
  949. do
  950. {
  951. #if (defined PHYSFS_HAVE_READLINE)
  952. buf = readline("> ");
  953. #else
  954. int i;
  955. buf = (char *) malloc(512);
  956. memset(buf, '\0', 512);
  957. printf("> ");
  958. for (i = 0; i < 511; i++)
  959. {
  960. int ch = fgetc(stdin);
  961. if (ch == EOF)
  962. {
  963. strcpy(buf, "quit");
  964. break;
  965. } /* if */
  966. else if ((ch == '\n') || (ch == '\r'))
  967. {
  968. buf[i] = '\0';
  969. break;
  970. } /* else if */
  971. else if (ch == '\b')
  972. {
  973. if (i > 0)
  974. i--;
  975. } /* else if */
  976. else
  977. {
  978. buf[i] = (char) ch;
  979. } /* else */
  980. } /* for */
  981. #endif
  982. rc = process_command(buf);
  983. if (buf != NULL)
  984. free(buf);
  985. } while (rc);
  986. if (!PHYSFS_deinit())
  987. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  988. if (history_file)
  989. fclose(history_file);
  990. /*
  991. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  992. printf(" it makes you shoot teh railgun bettar.\n");
  993. */
  994. return(0);
  995. } /* main */
  996. /* end of test_physfs.c ... */