test_physfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 7
  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. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  491. { "getlasterror", cmd_getlasterror, 0, NULL },
  492. { "getdirsep", cmd_getdirsep, 0, NULL },
  493. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  494. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  495. { "getbasedir", cmd_getbasedir, 0, NULL },
  496. { "getuserdir", cmd_getuserdir, 0, NULL },
  497. { "getwritedir", cmd_getwritedir, 0, NULL },
  498. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  499. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  500. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  501. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  502. { "delete", cmd_delete, 1, "<dirToDelete>" },
  503. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  504. { "exists", cmd_exists, 1, "<fileToCheck>" },
  505. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  506. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  507. { "cat", cmd_cat, 1, "<fileToCat>" },
  508. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  509. { "append", cmd_append, 1, "<fileToAppend>" },
  510. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  511. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  512. { NULL, NULL, -1, NULL }
  513. };
  514. static void output_usage(const char *intro, const command_info *cmdinfo)
  515. {
  516. if (cmdinfo->argcount == 0)
  517. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  518. else
  519. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  520. } /* output_usage */
  521. static int cmd_help(char *args)
  522. {
  523. const command_info *i;
  524. printf("Commands:\n");
  525. for (i = commands; i->cmd != NULL; i++)
  526. output_usage(" -", i);
  527. return(1);
  528. } /* output_cmd_help */
  529. static void trim_command(const char *orig, char *copy)
  530. {
  531. const char *i;
  532. char *writeptr = copy;
  533. int spacecount = 0;
  534. int have_first = 0;
  535. for (i = orig; *i != '\0'; i++)
  536. {
  537. if (*i == ' ')
  538. {
  539. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  540. {
  541. if ((have_first) && (!spacecount))
  542. {
  543. spacecount++;
  544. *writeptr = ' ';
  545. writeptr++;
  546. } /* if */
  547. } /* if */
  548. } /* if */
  549. else
  550. {
  551. have_first = 1;
  552. spacecount = 0;
  553. *writeptr = *i;
  554. writeptr++;
  555. } /* else */
  556. } /* for */
  557. *writeptr = '\0';
  558. /*
  559. printf("\n command is [%s].\n", copy);
  560. */
  561. } /* trim_command */
  562. static int process_command(char *complete_cmd)
  563. {
  564. const command_info *i;
  565. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  566. char *args;
  567. int rc = 1;
  568. if (cmd_copy == NULL)
  569. {
  570. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  571. return(0);
  572. } /* if */
  573. trim_command(complete_cmd, cmd_copy);
  574. args = strchr(cmd_copy, ' ');
  575. if (args != NULL)
  576. {
  577. *args = '\0';
  578. args++;
  579. } /* else */
  580. if (cmd_copy[0] != '\0')
  581. {
  582. for (i = commands; i->cmd != NULL; i++)
  583. {
  584. if (strcmp(i->cmd, cmd_copy) == 0)
  585. {
  586. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  587. output_usage("usage:", i);
  588. else
  589. rc = i->func(args);
  590. break;
  591. } /* if */
  592. } /* for */
  593. if (i->cmd == NULL)
  594. printf("Unknown command. Enter \"help\" for instructions.\n");
  595. #if (defined PHYSFS_HAVE_READLINE)
  596. add_history(complete_cmd);
  597. if (history_file)
  598. {
  599. fprintf(history_file, "%s\n", complete_cmd);
  600. fflush(history_file);
  601. } /* if */
  602. #endif
  603. } /* if */
  604. free(cmd_copy);
  605. return(rc);
  606. } /* process_command */
  607. static void open_history_file(void)
  608. {
  609. #if (defined PHYSFS_HAVE_READLINE)
  610. #if 0
  611. const char *envr = getenv("TESTPHYSFS_HISTORY");
  612. if (!envr)
  613. return;
  614. #else
  615. char envr[256];
  616. strcpy(envr, PHYSFS_getUserDir());
  617. strcat(envr, ".testphys_history");
  618. #endif
  619. if (access(envr, F_OK) == 0)
  620. {
  621. char buf[512];
  622. FILE *f = fopen(envr, "r");
  623. if (!f)
  624. {
  625. printf("\n\n"
  626. "Could not open history file [%s] for reading!\n"
  627. " Will not have past history available.\n\n",
  628. envr);
  629. return;
  630. } /* if */
  631. do
  632. {
  633. fgets(buf, sizeof (buf), f);
  634. if (buf[strlen(buf) - 1] == '\n')
  635. buf[strlen(buf) - 1] = '\0';
  636. add_history(buf);
  637. } while (!feof(f));
  638. fclose(f);
  639. } /* if */
  640. history_file = fopen(envr, "ab");
  641. if (!history_file)
  642. {
  643. printf("\n\n"
  644. "Could not open history file [%s] for appending!\n"
  645. " Will not be able to record this session's history.\n\n",
  646. envr);
  647. } /* if */
  648. #endif
  649. } /* open_history_file */
  650. int main(int argc, char **argv)
  651. {
  652. char *buf = NULL;
  653. int rc = 0;
  654. #if (defined __MWERKS__)
  655. extern tSIOUXSettings SIOUXSettings;
  656. SIOUXSettings.asktosaveonclose = 0;
  657. SIOUXSettings.autocloseonquit = 1;
  658. SIOUXSettings.rows = 40;
  659. SIOUXSettings.columns = 120;
  660. #endif
  661. printf("\n");
  662. if (!PHYSFS_init(argv[0]))
  663. {
  664. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  665. return(1);
  666. } /* if */
  667. output_versions();
  668. output_archivers();
  669. open_history_file();
  670. printf("Enter commands. Enter \"help\" for instructions.\n");
  671. do
  672. {
  673. #if (defined PHYSFS_HAVE_READLINE)
  674. buf = readline("> ");
  675. #else
  676. int i;
  677. buf = malloc(512);
  678. memset(buf, '\0', 512);
  679. printf("> ");
  680. for (i = 0; i < 511; i++)
  681. {
  682. int ch = fgetc(stdin);
  683. if (ch == EOF)
  684. {
  685. strcpy(buf, "quit");
  686. break;
  687. } /* if */
  688. else if ((ch == '\n') || (ch == '\r'))
  689. {
  690. buf[i] = '\0';
  691. break;
  692. } /* else if */
  693. else if (ch == '\b')
  694. {
  695. if (i > 0)
  696. i--;
  697. } /* else if */
  698. else
  699. {
  700. buf[i] = (char) ch;
  701. } /* else */
  702. } /* for */
  703. #endif
  704. rc = process_command(buf);
  705. free(buf);
  706. } while (rc);
  707. if (!PHYSFS_deinit())
  708. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  709. if (history_file)
  710. fclose(history_file);
  711. /*
  712. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  713. printf(" it makes you shoot teh railgun bettar.\n");
  714. */
  715. return(0);
  716. } /* main */
  717. /* end of test_physfs.c ... */