test_physfs.c 16 KB

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