test_physfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 0
  26. #define TEST_VERSION_MINOR 1
  27. #define TEST_VERSION_PATCH 6
  28. static FILE *history_file = NULL;
  29. static void output_versions(void)
  30. {
  31. PHYSFS_Version compiled;
  32. PHYSFS_Version linked;
  33. PHYSFS_VERSION(&compiled);
  34. PHYSFS_getLinkedVersion(&linked);
  35. printf("test_physfs version %d.%d.%d.\n"
  36. " Compiled against PhysicsFS version %d.%d.%d,\n"
  37. " and linked against %d.%d.%d.\n\n",
  38. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  39. (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
  40. (int) linked.major, (int) linked.minor, (int) linked.patch);
  41. } /* output_versions */
  42. static void output_archivers(void)
  43. {
  44. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  45. const PHYSFS_ArchiveInfo **i;
  46. printf("Supported archive types:\n");
  47. if (*rc == NULL)
  48. printf(" * Apparently, NONE!\n");
  49. else
  50. {
  51. for (i = rc; *i != NULL; i++)
  52. {
  53. printf(" * %s: %s\n Written by %s.\n %s\n",
  54. (*i)->extension, (*i)->description,
  55. (*i)->author, (*i)->url);
  56. } /* for */
  57. } /* else */
  58. printf("\n");
  59. } /* output_archivers */
  60. static int cmd_quit(char *args)
  61. {
  62. return(0);
  63. } /* cmd_quit */
  64. static int cmd_init(char *args)
  65. {
  66. if (*args == '\"')
  67. {
  68. args++;
  69. args[strlen(args) - 1] = '\0';
  70. } /* if */
  71. if (PHYSFS_init(args))
  72. printf("Successful.\n");
  73. else
  74. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  75. return(1);
  76. } /* cmd_init */
  77. static int cmd_deinit(char *args)
  78. {
  79. if (PHYSFS_deinit())
  80. printf("Successful.\n");
  81. else
  82. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  83. return(1);
  84. } /* cmd_deinit */
  85. static int cmd_addarchive(char *args)
  86. {
  87. char *ptr = strrchr(args, ' ');
  88. int appending = atoi(ptr + 1);
  89. *ptr = '\0';
  90. if (*args == '\"')
  91. {
  92. args++;
  93. *(ptr - 1) = '\0';
  94. } /* if */
  95. /*printf("[%s], [%d]\n", args, appending);*/
  96. if (PHYSFS_addToSearchPath(args, appending))
  97. printf("Successful.\n");
  98. else
  99. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  100. return(1);
  101. } /* cmd_addarchive */
  102. static int cmd_removearchive(char *args)
  103. {
  104. if (*args == '\"')
  105. {
  106. args++;
  107. args[strlen(args) - 1] = '\0';
  108. } /* if */
  109. if (PHYSFS_removeFromSearchPath(args))
  110. printf("Successful.\n");
  111. else
  112. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  113. return(1);
  114. } /* cmd_removearchive */
  115. static int cmd_enumerate(char *args)
  116. {
  117. char **rc;
  118. if (*args == '\"')
  119. {
  120. args++;
  121. args[strlen(args) - 1] = '\0';
  122. } /* if */
  123. rc = PHYSFS_enumerateFiles(args);
  124. if (rc == NULL)
  125. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  126. else
  127. {
  128. int file_count;
  129. char **i;
  130. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  131. printf("%s\n", *i);
  132. printf("\n total (%d) files.\n", file_count);
  133. PHYSFS_freeList(rc);
  134. } /* else */
  135. return(1);
  136. } /* cmd_enumerate */
  137. static int cmd_getdirsep(char *args)
  138. {
  139. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  140. return(1);
  141. } /* cmd_getdirsep */
  142. static int cmd_getlasterror(char *args)
  143. {
  144. printf("last error is [%s].\n", PHYSFS_getLastError());
  145. return(1);
  146. } /* cmd_getlasterror */
  147. static int cmd_getcdromdirs(char *args)
  148. {
  149. char **rc = PHYSFS_getCdRomDirs();
  150. if (rc == NULL)
  151. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  152. else
  153. {
  154. int dir_count;
  155. char **i;
  156. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  157. printf("%s\n", *i);
  158. printf("\n total (%d) drives.\n", dir_count);
  159. PHYSFS_freeList(rc);
  160. } /* else */
  161. return(1);
  162. } /* cmd_getcdromdirs */
  163. static int cmd_getsearchpath(char *args)
  164. {
  165. char **rc = PHYSFS_getSearchPath();
  166. if (rc == NULL)
  167. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  168. else
  169. {
  170. int dir_count;
  171. char **i;
  172. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  173. printf("%s\n", *i);
  174. printf("\n total (%d) directories.\n", dir_count);
  175. PHYSFS_freeList(rc);
  176. } /* else */
  177. return(1);
  178. } /* cmd_getcdromdirs */
  179. static int cmd_getbasedir(char *args)
  180. {
  181. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  182. return(1);
  183. } /* cmd_getbasedir */
  184. static int cmd_getuserdir(char *args)
  185. {
  186. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  187. return(1);
  188. } /* cmd_getuserdir */
  189. static int cmd_getwritedir(char *args)
  190. {
  191. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  192. return(1);
  193. } /* cmd_getwritedir */
  194. static int cmd_setwritedir(char *args)
  195. {
  196. if (*args == '\"')
  197. {
  198. args++;
  199. args[strlen(args) - 1] = '\0';
  200. } /* if */
  201. if (PHYSFS_setWriteDir(args))
  202. printf("Successful.\n");
  203. else
  204. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  205. return(1);
  206. } /* cmd_setwritedir */
  207. static int cmd_permitsyms(char *args)
  208. {
  209. int num;
  210. if (*args == '\"')
  211. {
  212. args++;
  213. args[strlen(args) - 1] = '\0';
  214. } /* if */
  215. num = atoi(args);
  216. PHYSFS_permitSymbolicLinks(num);
  217. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  218. return(1);
  219. } /* cmd_permitsyms */
  220. static int cmd_setsaneconfig(char *args)
  221. {
  222. char *org;
  223. char *appName;
  224. char *arcExt;
  225. int inclCD;
  226. int arcsFirst;
  227. char *ptr = args;
  228. /* ugly. */
  229. org = ptr;
  230. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  231. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  232. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  233. arcsFirst = atoi(ptr);
  234. if (strcmp(arcExt, "!") == 0)
  235. arcExt = NULL;
  236. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  237. printf("Successful.\n");
  238. else
  239. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  240. return(1);
  241. } /* cmd_setsaneconfig */
  242. static int cmd_mkdir(char *args)
  243. {
  244. if (*args == '\"')
  245. {
  246. args++;
  247. args[strlen(args) - 1] = '\0';
  248. } /* if */
  249. if (PHYSFS_mkdir(args))
  250. printf("Successful.\n");
  251. else
  252. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  253. return(1);
  254. } /* cmd_mkdir */
  255. static int cmd_delete(char *args)
  256. {
  257. if (*args == '\"')
  258. {
  259. args++;
  260. args[strlen(args) - 1] = '\0';
  261. } /* if */
  262. if (PHYSFS_delete(args))
  263. printf("Successful.\n");
  264. else
  265. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  266. return(1);
  267. } /* cmd_delete */
  268. static int cmd_getrealdir(char *args)
  269. {
  270. const char *rc;
  271. if (*args == '\"')
  272. {
  273. args++;
  274. args[strlen(args) - 1] = '\0';
  275. } /* if */
  276. rc = PHYSFS_getRealDir(args);
  277. if (rc)
  278. printf("Found at [%s].\n", rc);
  279. else
  280. printf("Not found.\n");
  281. return(1);
  282. } /* cmd_getrealdir */
  283. static int cmd_exists(char *args)
  284. {
  285. int rc;
  286. if (*args == '\"')
  287. {
  288. args++;
  289. args[strlen(args) - 1] = '\0';
  290. } /* if */
  291. rc = PHYSFS_exists(args);
  292. printf("File %sexists.\n", rc ? "" : "does not ");
  293. return(1);
  294. } /* cmd_exists */
  295. static int cmd_isdir(char *args)
  296. {
  297. int rc;
  298. if (*args == '\"')
  299. {
  300. args++;
  301. args[strlen(args) - 1] = '\0';
  302. } /* if */
  303. rc = PHYSFS_isDirectory(args);
  304. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  305. return(1);
  306. } /* cmd_isdir */
  307. static int cmd_issymlink(char *args)
  308. {
  309. int rc;
  310. if (*args == '\"')
  311. {
  312. args++;
  313. args[strlen(args) - 1] = '\0';
  314. } /* if */
  315. rc = PHYSFS_isSymbolicLink(args);
  316. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  317. return(1);
  318. } /* cmd_issymlink */
  319. static int cmd_cat(char *args)
  320. {
  321. PHYSFS_file *f;
  322. if (*args == '\"')
  323. {
  324. args++;
  325. args[strlen(args) - 1] = '\0';
  326. } /* if */
  327. f = PHYSFS_openRead(args);
  328. if (f == NULL)
  329. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  330. else
  331. {
  332. while (1)
  333. {
  334. char buffer[128];
  335. PHYSFS_sint64 rc;
  336. PHYSFS_sint64 i;
  337. rc = PHYSFS_read(f, buffer, 1, sizeof (buffer));
  338. for (i = 0; i < rc; i++)
  339. fputc((int) buffer[i], stdout);
  340. if (rc < sizeof (buffer))
  341. {
  342. printf("\n\n");
  343. if (!PHYSFS_eof(f))
  344. {
  345. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  346. PHYSFS_getLastError());
  347. } /* if */
  348. PHYSFS_close(f);
  349. return(1);
  350. } /* if */
  351. } /* while */
  352. } /* else */
  353. return(1);
  354. } /* cmd_cat */
  355. static int cmd_filelength(char *args)
  356. {
  357. PHYSFS_file *f;
  358. if (*args == '\"')
  359. {
  360. args++;
  361. args[strlen(args) - 1] = '\0';
  362. } /* if */
  363. f = PHYSFS_openRead(args);
  364. if (f == NULL)
  365. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  366. else
  367. {
  368. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  369. if (len == -1)
  370. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  371. else
  372. printf(" (cast to int) %d bytes.\n", (int) len);
  373. PHYSFS_close(f);
  374. } /* else */
  375. return(1);
  376. } /* cmd_filelength */
  377. #define WRITESTR "The cat sat on the mat.\n\n"
  378. static int cmd_append(char *args)
  379. {
  380. PHYSFS_file *f;
  381. if (*args == '\"')
  382. {
  383. args++;
  384. args[strlen(args) - 1] = '\0';
  385. } /* if */
  386. f = PHYSFS_openAppend(args);
  387. if (f == NULL)
  388. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  389. else
  390. {
  391. size_t bw = strlen(WRITESTR);
  392. PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
  393. if (rc != bw)
  394. {
  395. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  396. (int) rc, (int) bw, PHYSFS_getLastError());
  397. } /* if */
  398. else
  399. {
  400. printf("Successful.\n");
  401. } /* else */
  402. PHYSFS_close(f);
  403. } /* else */
  404. return(1);
  405. } /* cmd_append */
  406. static int cmd_write(char *args)
  407. {
  408. PHYSFS_file *f;
  409. if (*args == '\"')
  410. {
  411. args++;
  412. args[strlen(args) - 1] = '\0';
  413. } /* if */
  414. f = PHYSFS_openWrite(args);
  415. if (f == NULL)
  416. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  417. else
  418. {
  419. size_t bw = strlen(WRITESTR);
  420. PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
  421. if (rc != bw)
  422. {
  423. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  424. (int) rc, (int) bw, PHYSFS_getLastError());
  425. } /* if */
  426. else
  427. {
  428. printf("Successful.\n");
  429. } /* else */
  430. PHYSFS_close(f);
  431. } /* else */
  432. return(1);
  433. } /* cmd_write */
  434. static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  435. {
  436. time_t t = (time_t) modtime;
  437. char *str = ctime(&t);
  438. strncpy(modstr, str, strsize);
  439. modstr[strsize-1] = '\0';
  440. } /* modTimeToStr */
  441. static int cmd_getlastmodtime(char *args)
  442. {
  443. PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);
  444. if (rc == -1)
  445. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  446. else
  447. {
  448. char modstr[64];
  449. modTimeToStr(rc, modstr, sizeof (modstr));
  450. printf("Last modified: %s (%ld).\n", modstr, (long) rc);
  451. } /* else */
  452. return(1);
  453. } /* cmd_getLastModTime */
  454. /* must have spaces trimmed prior to this call. */
  455. static int count_args(const char *str)
  456. {
  457. int retval = 0;
  458. int in_quotes = 0;
  459. if (str != NULL)
  460. {
  461. for (; *str != '\0'; str++)
  462. {
  463. if (*str == '\"')
  464. in_quotes = !in_quotes;
  465. else if ((*str == ' ') && (!in_quotes))
  466. retval++;
  467. } /* for */
  468. retval++;
  469. } /* if */
  470. return(retval);
  471. } /* count_args */
  472. static int cmd_help(char *args);
  473. typedef struct
  474. {
  475. const char *cmd;
  476. int (*func)(char *args);
  477. int argcount;
  478. const char *usage;
  479. } command_info;
  480. static const command_info commands[] =
  481. {
  482. { "quit", cmd_quit, 0, NULL },
  483. { "q", cmd_quit, 0, NULL },
  484. { "help", cmd_help, 0, NULL },
  485. { "init", cmd_init, 1, "<argv0>" },
  486. { "deinit", cmd_deinit, 0, NULL },
  487. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  488. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  489. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  490. { "getlasterror", cmd_getlasterror, 0, NULL },
  491. { "getdirsep", cmd_getdirsep, 0, NULL },
  492. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  493. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  494. { "getbasedir", cmd_getbasedir, 0, NULL },
  495. { "getuserdir", cmd_getuserdir, 0, NULL },
  496. { "getwritedir", cmd_getwritedir, 0, NULL },
  497. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  498. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  499. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  500. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  501. { "delete", cmd_delete, 1, "<dirToDelete>" },
  502. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  503. { "exists", cmd_exists, 1, "<fileToCheck>" },
  504. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  505. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  506. { "cat", cmd_cat, 1, "<fileToCat>" },
  507. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  508. { "append", cmd_append, 1, "<fileToAppend>" },
  509. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  510. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  511. { NULL, NULL, -1, NULL }
  512. };
  513. static void output_usage(const char *intro, const command_info *cmdinfo)
  514. {
  515. if (cmdinfo->argcount == 0)
  516. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  517. else
  518. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  519. } /* output_usage */
  520. static int cmd_help(char *args)
  521. {
  522. const command_info *i;
  523. printf("Commands:\n");
  524. for (i = commands; i->cmd != NULL; i++)
  525. output_usage(" -", i);
  526. return(1);
  527. } /* output_cmd_help */
  528. static void trim_command(const char *orig, char *copy)
  529. {
  530. const char *i;
  531. char *writeptr = copy;
  532. int spacecount = 0;
  533. int have_first = 0;
  534. for (i = orig; *i != '\0'; i++)
  535. {
  536. if (*i == ' ')
  537. {
  538. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  539. {
  540. if ((have_first) && (!spacecount))
  541. {
  542. spacecount++;
  543. *writeptr = ' ';
  544. writeptr++;
  545. } /* if */
  546. } /* if */
  547. } /* if */
  548. else
  549. {
  550. have_first = 1;
  551. spacecount = 0;
  552. *writeptr = *i;
  553. writeptr++;
  554. } /* else */
  555. } /* for */
  556. *writeptr = '\0';
  557. /*
  558. printf("\n command is [%s].\n", copy);
  559. */
  560. } /* trim_command */
  561. static int process_command(char *complete_cmd)
  562. {
  563. const command_info *i;
  564. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  565. char *args;
  566. int rc = 1;
  567. if (cmd_copy == NULL)
  568. {
  569. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  570. return(0);
  571. } /* if */
  572. trim_command(complete_cmd, cmd_copy);
  573. args = strchr(cmd_copy, ' ');
  574. if (args != NULL)
  575. {
  576. *args = '\0';
  577. args++;
  578. } /* else */
  579. if (cmd_copy[0] != '\0')
  580. {
  581. for (i = commands; i->cmd != NULL; i++)
  582. {
  583. if (strcmp(i->cmd, cmd_copy) == 0)
  584. {
  585. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  586. output_usage("usage:", i);
  587. else
  588. rc = i->func(args);
  589. break;
  590. } /* if */
  591. } /* for */
  592. if (i->cmd == NULL)
  593. printf("Unknown command. Enter \"help\" for instructions.\n");
  594. #if (defined PHYSFS_HAVE_READLINE)
  595. add_history(complete_cmd);
  596. if (history_file)
  597. {
  598. fprintf(history_file, "%s\n", complete_cmd);
  599. fflush(history_file);
  600. } /* if */
  601. #endif
  602. } /* if */
  603. free(cmd_copy);
  604. return(rc);
  605. } /* process_command */
  606. static void open_history_file(void)
  607. {
  608. #if (defined PHYSFS_HAVE_READLINE)
  609. #if 0
  610. const char *envr = getenv("TESTPHYSFS_HISTORY");
  611. if (!envr)
  612. return;
  613. #else
  614. char envr[256];
  615. strcpy(envr, PHYSFS_getUserDir());
  616. strcat(envr, ".testphys_history");
  617. #endif
  618. if (access(envr, F_OK) == 0)
  619. {
  620. char buf[512];
  621. FILE *f = fopen(envr, "r");
  622. if (!f)
  623. {
  624. printf("\n\n"
  625. "Could not open history file [%s] for reading!\n"
  626. " Will not have past history available.\n\n",
  627. envr);
  628. return;
  629. } /* if */
  630. do
  631. {
  632. fgets(buf, sizeof (buf), f);
  633. if (buf[strlen(buf) - 1] == '\n')
  634. buf[strlen(buf) - 1] = '\0';
  635. add_history(buf);
  636. } while (!feof(f));
  637. fclose(f);
  638. } /* if */
  639. history_file = fopen(envr, "ab");
  640. if (!history_file)
  641. {
  642. printf("\n\n"
  643. "Could not open history file [%s] for appending!\n"
  644. " Will not be able to record this session's history.\n\n",
  645. envr);
  646. } /* if */
  647. #endif
  648. } /* open_history_file */
  649. int main(int argc, char **argv)
  650. {
  651. char *buf = NULL;
  652. int rc = 0;
  653. #if (defined __MWERKS__)
  654. extern tSIOUXSettings SIOUXSettings;
  655. SIOUXSettings.asktosaveonclose = 0;
  656. SIOUXSettings.autocloseonquit = 1;
  657. SIOUXSettings.rows = 40;
  658. SIOUXSettings.columns = 120;
  659. #endif
  660. printf("\n");
  661. if (!PHYSFS_init(argv[0]))
  662. {
  663. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  664. return(1);
  665. } /* if */
  666. output_versions();
  667. output_archivers();
  668. open_history_file();
  669. printf("Enter commands. Enter \"help\" for instructions.\n");
  670. do
  671. {
  672. #if (defined PHYSFS_HAVE_READLINE)
  673. buf = readline("> ");
  674. #else
  675. int i;
  676. buf = malloc(512);
  677. memset(buf, '\0', 512);
  678. printf("> ");
  679. for (i = 0; i < 511; i++)
  680. {
  681. int ch = fgetc(stdin);
  682. if (ch == EOF)
  683. {
  684. strcpy(buf, "quit");
  685. break;
  686. } /* if */
  687. else if ((ch == '\n') || (ch == '\r'))
  688. {
  689. buf[i] = '\0';
  690. break;
  691. } /* else if */
  692. else if (ch == '\b')
  693. {
  694. if (i > 0)
  695. i--;
  696. } /* else if */
  697. else
  698. {
  699. buf[i] = (char) ch;
  700. } /* else */
  701. } /* for */
  702. #endif
  703. rc = process_command(buf);
  704. free(buf);
  705. } while (rc);
  706. if (!PHYSFS_deinit())
  707. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  708. if (history_file)
  709. fclose(history_file);
  710. /*
  711. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  712. printf(" it makes you shoot teh railgun bettar.\n");
  713. */
  714. return(0);
  715. } /* main */
  716. /* end of test_physfs.c ... */