test_physfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #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 WIN32) && (!defined __MACOS__))
  16. #define HAVE_READLINE
  17. #endif
  18. #if (defined HAVE_READLINE)
  19. #include <unistd.h>
  20. #include <readline.h>
  21. #include <history.h>
  22. #endif
  23. #include "physfs.h"
  24. #define TEST_VERSION_MAJOR 0
  25. #define TEST_VERSION_MINOR 1
  26. #define TEST_VERSION_PATCH 0
  27. static FILE *history_file = NULL;
  28. static void output_versions(void)
  29. {
  30. PHYSFS_Version compiled;
  31. PHYSFS_Version linked;
  32. PHYSFS_VERSION(&compiled);
  33. PHYSFS_getLinkedVersion(&linked);
  34. printf("test_physfs version %d.%d.%d.\n"
  35. " Compiled against PhysicsFS version %d.%d.%d,\n"
  36. " and linked against %d.%d.%d.\n\n",
  37. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  38. (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
  39. (int) linked.major, (int) linked.minor, (int) linked.patch);
  40. } /* output_versions */
  41. static void output_archivers(void)
  42. {
  43. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  44. const PHYSFS_ArchiveInfo **i;
  45. printf("Supported archive types:\n");
  46. if (*rc == NULL)
  47. printf(" * Apparently, NONE!\n");
  48. else
  49. {
  50. for (i = rc; *i != NULL; i++)
  51. {
  52. printf(" * %s: %s\n Written by %s.\n %s\n",
  53. (*i)->extension, (*i)->description,
  54. (*i)->author, (*i)->url);
  55. } /* for */
  56. } /* else */
  57. printf("\n");
  58. } /* output_archivers */
  59. static int cmd_quit(char *args)
  60. {
  61. return(0);
  62. } /* cmd_quit */
  63. static int cmd_init(char *args)
  64. {
  65. if (PHYSFS_init(args))
  66. printf("Successful.\n");
  67. else
  68. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  69. return(1);
  70. } /* cmd_init */
  71. static int cmd_deinit(char *args)
  72. {
  73. if (PHYSFS_deinit())
  74. printf("Successful.\n");
  75. else
  76. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  77. return(1);
  78. } /* cmd_deinit */
  79. static int cmd_addarchive(char *args)
  80. {
  81. char *ptr = strrchr(args, ' ');
  82. int appending = atoi(ptr + 1);
  83. *ptr = '\0';
  84. if (*args == '\"')
  85. {
  86. args++;
  87. *(ptr - 1) = '\0';
  88. }
  89. /*printf("[%s], [%d]\n", args, appending);*/
  90. if (PHYSFS_addToSearchPath(args, appending))
  91. printf("Successful.\n");
  92. else
  93. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  94. return(1);
  95. } /* cmd_addarchive */
  96. static int cmd_removearchive(char *args)
  97. {
  98. if (PHYSFS_removeFromSearchPath(args))
  99. printf("Successful.\n");
  100. else
  101. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  102. return(1);
  103. } /* cmd_removearchive */
  104. static int cmd_enumerate(char *args)
  105. {
  106. char **rc = PHYSFS_enumerateFiles(args);
  107. if (rc == NULL)
  108. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  109. else
  110. {
  111. int file_count;
  112. char **i;
  113. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  114. printf("%s\n", *i);
  115. printf("\n total (%d) files.\n", file_count);
  116. PHYSFS_freeList(rc);
  117. } /* else */
  118. return(1);
  119. } /* cmd_enumerate */
  120. static int cmd_getdirsep(char *args)
  121. {
  122. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  123. return(1);
  124. } /* cmd_getdirsep */
  125. static int cmd_getlasterror(char *args)
  126. {
  127. printf("last error is [%s].\n", PHYSFS_getLastError());
  128. return(1);
  129. } /* cmd_getlasterror */
  130. static int cmd_getcdromdirs(char *args)
  131. {
  132. char **rc = PHYSFS_getCdRomDirs();
  133. if (rc == NULL)
  134. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  135. else
  136. {
  137. int dir_count;
  138. char **i;
  139. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  140. printf("%s\n", *i);
  141. printf("\n total (%d) drives.\n", dir_count);
  142. PHYSFS_freeList(rc);
  143. } /* else */
  144. return(1);
  145. } /* cmd_getcdromdirs */
  146. static int cmd_getsearchpath(char *args)
  147. {
  148. char **rc = PHYSFS_getSearchPath();
  149. if (rc == NULL)
  150. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  151. else
  152. {
  153. int dir_count;
  154. char **i;
  155. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  156. printf("%s\n", *i);
  157. printf("\n total (%d) directories.\n", dir_count);
  158. PHYSFS_freeList(rc);
  159. } /* else */
  160. return(1);
  161. } /* cmd_getcdromdirs */
  162. static int cmd_getbasedir(char *args)
  163. {
  164. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  165. return(1);
  166. } /* cmd_getbasedir */
  167. static int cmd_getuserdir(char *args)
  168. {
  169. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  170. return(1);
  171. } /* cmd_getuserdir */
  172. static int cmd_getwritedir(char *args)
  173. {
  174. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  175. return(1);
  176. } /* cmd_getwritedir */
  177. static int cmd_setwritedir(char *args)
  178. {
  179. if (PHYSFS_setWriteDir(args))
  180. printf("Successful.\n");
  181. else
  182. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  183. return(1);
  184. } /* cmd_setwritedir */
  185. static int cmd_permitsyms(char *args)
  186. {
  187. int num = atoi(args);
  188. PHYSFS_permitSymbolicLinks(num);
  189. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  190. return(1);
  191. } /* cmd_permitsyms */
  192. static int cmd_setsaneconfig(char *args)
  193. {
  194. char *org;
  195. char *appName;
  196. char *arcExt;
  197. int inclCD;
  198. int arcsFirst;
  199. char *ptr = args;
  200. /* ugly. */
  201. org = ptr;
  202. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  203. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  204. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  205. arcsFirst = atoi(ptr);
  206. if (strcmp(arcExt, "!") == 0)
  207. arcExt = NULL;
  208. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  209. printf("Successful.\n");
  210. else
  211. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  212. return(1);
  213. } /* cmd_setsaneconfig */
  214. static int cmd_mkdir(char *args)
  215. {
  216. if (PHYSFS_mkdir(args))
  217. printf("Successful.\n");
  218. else
  219. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  220. return(1);
  221. } /* cmd_mkdir */
  222. static int cmd_delete(char *args)
  223. {
  224. if (PHYSFS_delete(args))
  225. printf("Successful.\n");
  226. else
  227. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  228. return(1);
  229. } /* cmd_delete */
  230. static int cmd_getrealdir(char *args)
  231. {
  232. const char *rc = PHYSFS_getRealDir(args);
  233. if (rc)
  234. printf("Found at [%s].\n", rc);
  235. else
  236. printf("Not found.\n");
  237. return(1);
  238. } /* cmd_getrealdir */
  239. static int cmd_exists(char *args)
  240. {
  241. int rc = PHYSFS_exists(args);
  242. printf("File %sexists.\n", rc ? "" : "does not ");
  243. return(1);
  244. } /* cmd_exists */
  245. static int cmd_isdir(char *args)
  246. {
  247. int rc = PHYSFS_isDirectory(args);
  248. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  249. return(1);
  250. } /* cmd_isdir */
  251. static int cmd_issymlink(char *args)
  252. {
  253. int rc = PHYSFS_isSymbolicLink(args);
  254. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  255. return(1);
  256. } /* cmd_issymlink */
  257. static int cmd_cat(char *args)
  258. {
  259. PHYSFS_file *f = PHYSFS_openRead(args);
  260. if (f == NULL)
  261. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  262. else
  263. {
  264. while (1)
  265. {
  266. char buffer[128];
  267. PHYSFS_sint64 rc;
  268. PHYSFS_sint64 i;
  269. rc = PHYSFS_read(f, buffer, 1, sizeof (buffer));
  270. for (i = 0; i < rc; i++)
  271. fputc((int) buffer[i], stdout);
  272. if (rc < sizeof (buffer))
  273. {
  274. printf("\n\n");
  275. if (!PHYSFS_eof(f))
  276. {
  277. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  278. PHYSFS_getLastError());
  279. } /* if */
  280. PHYSFS_close(f);
  281. return(1);
  282. } /* if */
  283. } /* while */
  284. } /* else */
  285. return(1);
  286. } /* cmd_cat */
  287. /* must have spaces trimmed prior to this call. */
  288. static int count_args(const char *str)
  289. {
  290. int retval = 0;
  291. int in_quotes = 0;
  292. if (str != NULL)
  293. {
  294. for (; *str != '\0'; str++)
  295. {
  296. if (*str == '\"')
  297. in_quotes = !in_quotes;
  298. else if ((*str == ' ') && (!in_quotes))
  299. retval++;
  300. } /* for */
  301. retval++;
  302. } /* if */
  303. return(retval);
  304. } /* count_args */
  305. static int cmd_help(char *args);
  306. typedef struct
  307. {
  308. const char *cmd;
  309. int (*func)(char *args);
  310. int argcount;
  311. const char *usage;
  312. } command_info;
  313. static const command_info commands[] =
  314. {
  315. { "quit", cmd_quit, 0, NULL },
  316. { "q", cmd_quit, 0, NULL },
  317. { "help", cmd_help, 0, NULL },
  318. { "init", cmd_init, 1, "<argv0>" },
  319. { "deinit", cmd_deinit, 0, NULL },
  320. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  321. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  322. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  323. { "getlasterror", cmd_getlasterror, 0, NULL },
  324. { "getdirsep", cmd_getdirsep, 0, NULL },
  325. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  326. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  327. { "getbasedir", cmd_getbasedir, 0, NULL },
  328. { "getuserdir", cmd_getuserdir, 0, NULL },
  329. { "getwritedir", cmd_getwritedir, 0, NULL },
  330. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  331. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  332. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  333. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  334. { "delete", cmd_delete, 1, "<dirToDelete>" },
  335. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  336. { "exists", cmd_exists, 1, "<fileToCheck>" },
  337. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  338. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  339. { "cat", cmd_cat, 1, "<fileToCat>" },
  340. { NULL, NULL, -1, NULL }
  341. };
  342. static void output_usage(const char *intro, const command_info *cmdinfo)
  343. {
  344. if (cmdinfo->argcount == 0)
  345. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  346. else
  347. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  348. } /* output_usage */
  349. static int cmd_help(char *args)
  350. {
  351. const command_info *i;
  352. printf("Commands:\n");
  353. for (i = commands; i->cmd != NULL; i++)
  354. output_usage(" -", i);
  355. return(1);
  356. } /* output_cmd_help */
  357. static void trim_command(const char *orig, char *copy)
  358. {
  359. const char *i;
  360. char *writeptr = copy;
  361. int spacecount = 0;
  362. int have_first = 0;
  363. for (i = orig; *i != '\0'; i++)
  364. {
  365. if (*i == ' ')
  366. {
  367. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  368. {
  369. if ((have_first) && (!spacecount))
  370. {
  371. spacecount++;
  372. *writeptr = ' ';
  373. writeptr++;
  374. } /* if */
  375. } /* if */
  376. } /* if */
  377. else
  378. {
  379. have_first = 1;
  380. spacecount = 0;
  381. *writeptr = *i;
  382. writeptr++;
  383. } /* else */
  384. } /* for */
  385. *writeptr = '\0';
  386. /*
  387. printf("\n command is [%s].\n", copy);
  388. */
  389. } /* trim_command */
  390. static int process_command(char *complete_cmd)
  391. {
  392. const command_info *i;
  393. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  394. char *args;
  395. int rc = 1;
  396. if (cmd_copy == NULL)
  397. {
  398. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  399. return(0);
  400. } /* if */
  401. trim_command(complete_cmd, cmd_copy);
  402. args = strchr(cmd_copy, ' ');
  403. if (args != NULL)
  404. {
  405. *args = '\0';
  406. args++;
  407. } /* else */
  408. if (cmd_copy[0] != '\0')
  409. {
  410. for (i = commands; i->cmd != NULL; i++)
  411. {
  412. if (strcmp(i->cmd, cmd_copy) == 0)
  413. {
  414. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  415. output_usage("usage:", i);
  416. else
  417. rc = i->func(args);
  418. break;
  419. } /* if */
  420. } /* for */
  421. if (i->cmd == NULL)
  422. printf("Unknown command. Enter \"help\" for instructions.\n");
  423. #if (defined HAVE_READLINE)
  424. add_history(complete_cmd);
  425. if (history_file)
  426. {
  427. fprintf(history_file, "%s\n", complete_cmd);
  428. fflush(history_file);
  429. } /* if */
  430. #endif
  431. } /* if */
  432. free(cmd_copy);
  433. return(rc);
  434. } /* process_command */
  435. static void open_history_file(void)
  436. {
  437. #if (defined HAVE_READLINE)
  438. #if 0
  439. const char *envr = getenv("TESTPHYSFS_HISTORY");
  440. if (!envr)
  441. return;
  442. #else
  443. char envr[256];
  444. strcpy(envr, PHYSFS_getUserDir());
  445. strcat(envr, ".testphys_history");
  446. #endif
  447. if (access(envr, F_OK) == 0)
  448. {
  449. char buf[512];
  450. FILE *f = fopen(envr, "r");
  451. if (!f)
  452. {
  453. printf("\n\n"
  454. "Could not open history file [%s] for reading!\n"
  455. " Will not have past history available.\n\n",
  456. envr);
  457. return;
  458. } /* if */
  459. do
  460. {
  461. fgets(buf, sizeof (buf), f);
  462. if (buf[strlen(buf) - 1] == '\n')
  463. buf[strlen(buf) - 1] = '\0';
  464. add_history(buf);
  465. } while (!feof(f));
  466. fclose(f);
  467. } /* if */
  468. history_file = fopen(envr, "ab");
  469. if (!history_file)
  470. {
  471. printf("\n\n"
  472. "Could not open history file [%s] for appending!\n"
  473. " Will not be able to record this session's history.\n\n",
  474. envr);
  475. } /* if */
  476. #endif
  477. } /* open_history_file */
  478. int main(int argc, char **argv)
  479. {
  480. char *buf = NULL;
  481. int rc = 0;
  482. #if (defined __MWERKS__)
  483. extern tSIOUXSettings SIOUXSettings;
  484. SIOUXSettings.asktosaveonclose = 0;
  485. SIOUXSettings.autocloseonquit = 1;
  486. SIOUXSettings.rows = 40;
  487. SIOUXSettings.columns = 120;
  488. #endif
  489. printf("\n");
  490. if (!PHYSFS_init(argv[0]))
  491. {
  492. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  493. return(1);
  494. } /* if */
  495. output_versions();
  496. output_archivers();
  497. open_history_file();
  498. printf("Enter commands. Enter \"help\" for instructions.\n");
  499. do
  500. {
  501. #if (defined HAVE_READLINE)
  502. buf = readline("> ");
  503. #else
  504. int i;
  505. buf = malloc(512);
  506. memset(buf, '\0', 512);
  507. printf("> ");
  508. for (i = 0; i < 511; i++)
  509. {
  510. int ch = fgetc(stdin);
  511. if (ch == EOF)
  512. {
  513. strcpy(buf, "quit");
  514. break;
  515. } /* if */
  516. else if ((ch == '\n') || (ch == '\r'))
  517. {
  518. buf[i] = '\0';
  519. break;
  520. } /* else if */
  521. else if (ch == '\b')
  522. {
  523. if (i > 0)
  524. i--;
  525. } /* else if */
  526. else
  527. {
  528. buf[i] = (char) ch;
  529. } /* else */
  530. } /* for */
  531. #endif
  532. rc = process_command(buf);
  533. free(buf);
  534. } while (rc);
  535. if (!PHYSFS_deinit())
  536. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  537. if (history_file)
  538. fclose(history_file);
  539. /*
  540. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  541. printf(" it makes you shoot teh railgun bettar.\n");
  542. */
  543. return(0);
  544. } /* main */
  545. /* end of test_physfs.c ... */