test_physfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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)
  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. compiled.major, compiled.minor, compiled.patch,
  36. linked.major, linked.minor, 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 *appName;
  186. char *arcExt;
  187. int inclCD;
  188. int arcsFirst;
  189. char *ptr = args;
  190. /* ugly. */
  191. appName = ptr;
  192. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  193. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  194. arcsFirst = atoi(ptr);
  195. if (strcmp(appName, "!") == 0)
  196. appName = NULL;
  197. if (strcmp(arcExt, "!") == 0)
  198. arcExt = NULL;
  199. if (PHYSFS_setSaneConfig(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. /* must have spaces trimmed prior to this call. */
  249. static int count_args(const char *str)
  250. {
  251. int retval = 0;
  252. if (str != NULL)
  253. {
  254. for (; *str != '\0'; str++)
  255. {
  256. if (*str == ' ')
  257. retval++;
  258. } /* for */
  259. retval++;
  260. } /* if */
  261. return(retval);
  262. } /* count_args */
  263. static int cmd_help(char *args);
  264. typedef struct
  265. {
  266. const char *cmd;
  267. int (*func)(char *args);
  268. int argcount;
  269. const char *usage;
  270. } command_info;
  271. static const command_info commands[] =
  272. {
  273. { "quit", cmd_quit, 0, NULL },
  274. { "q", cmd_quit, 0, NULL },
  275. { "help", cmd_help, 0, NULL },
  276. { "init", cmd_init, 1, "<argv0>" },
  277. { "deinit", cmd_deinit, 0, NULL },
  278. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  279. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  280. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  281. { "getlasterror", cmd_getlasterror, 0, NULL },
  282. { "getdirsep", cmd_getdirsep, 0, NULL },
  283. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  284. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  285. { "getbasedir", cmd_getbasedir, 0, NULL },
  286. { "getuserdir", cmd_getuserdir, 0, NULL },
  287. { "getwritedir", cmd_getwritedir, 0, NULL },
  288. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  289. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  290. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  291. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  292. { "delete", cmd_delete, 1, "<dirToDelete>" },
  293. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  294. { "exists", cmd_exists, 1, "<fileToCheck>" },
  295. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  296. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  297. { NULL, NULL, -1, NULL }
  298. };
  299. static void output_usage(const char *intro, const command_info *cmdinfo)
  300. {
  301. if (cmdinfo->argcount == 0)
  302. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  303. else
  304. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  305. } /* output_usage */
  306. static int cmd_help(char *args)
  307. {
  308. const command_info *i;
  309. printf("Commands:\n");
  310. for (i = commands; i->cmd != NULL; i++)
  311. output_usage(" -", i);
  312. return(1);
  313. } /* output_cmd_help */
  314. static void trim_command(const char *orig, char *copy)
  315. {
  316. const char *i;
  317. char *writeptr = copy;
  318. int spacecount = 0;
  319. int have_first = 0;
  320. for (i = orig; *i != '\0'; i++)
  321. {
  322. if (*i == ' ')
  323. {
  324. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  325. {
  326. if ((have_first) && (!spacecount))
  327. {
  328. spacecount++;
  329. *writeptr = ' ';
  330. writeptr++;
  331. } /* if */
  332. } /* if */
  333. } /* if */
  334. else
  335. {
  336. have_first = 1;
  337. spacecount = 0;
  338. *writeptr = *i;
  339. writeptr++;
  340. } /* else */
  341. } /* for */
  342. *writeptr = '\0';
  343. /*
  344. printf("\n command is [%s].\n", copy);
  345. */
  346. } /* trim_command */
  347. static int process_command(char *complete_cmd)
  348. {
  349. const command_info *i;
  350. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  351. char *args;
  352. int rc = 1;
  353. if (cmd_copy == NULL)
  354. {
  355. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  356. return(0);
  357. } /* if */
  358. trim_command(complete_cmd, cmd_copy);
  359. args = strchr(cmd_copy, ' ');
  360. if (args != NULL)
  361. {
  362. *args = '\0';
  363. args++;
  364. } /* else */
  365. if (cmd_copy[0] != '\0')
  366. {
  367. for (i = commands; i->cmd != NULL; i++)
  368. {
  369. if (strcmp(i->cmd, cmd_copy) == 0)
  370. {
  371. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  372. output_usage("usage:", i);
  373. else
  374. rc = i->func(args);
  375. break;
  376. } /* if */
  377. } /* for */
  378. if (i->cmd == NULL)
  379. printf("Unknown command. Enter \"help\" for instructions.\n");
  380. #if (defined HAVE_READLINE)
  381. add_history(complete_cmd);
  382. if (history_file)
  383. {
  384. fprintf(history_file, "%s\n", complete_cmd);
  385. fflush(history_file);
  386. } /* if */
  387. #endif
  388. } /* if */
  389. free(cmd_copy);
  390. return(rc);
  391. } /* process_command */
  392. static void open_history_file(void)
  393. {
  394. #if (defined HAVE_READLINE)
  395. #if 0
  396. const char *envr = getenv("TESTPHYSFS_HISTORY");
  397. if (!envr)
  398. return;
  399. #else
  400. char envr[256];
  401. strcpy(envr, PHYSFS_getUserDir());
  402. strcat(envr, ".testphys_history");
  403. #endif
  404. if (access(envr, F_OK) == 0)
  405. {
  406. char buf[512];
  407. FILE *f = fopen(envr, "r");
  408. if (!f)
  409. {
  410. printf("\n\n"
  411. "Could not open history file [%s] for reading!\n"
  412. " Will not have past history available.\n\n",
  413. envr);
  414. return;
  415. } /* if */
  416. do
  417. {
  418. fgets(buf, sizeof (buf), f);
  419. if (buf[strlen(buf) - 1] == '\n')
  420. buf[strlen(buf) - 1] = '\0';
  421. add_history(buf);
  422. } while (!feof(f));
  423. fclose(f);
  424. } /* if */
  425. history_file = fopen(envr, "ab");
  426. if (!history_file)
  427. {
  428. printf("\n\n"
  429. "Could not open history file [%s] for appending!\n"
  430. " Will not be able to record this session's history.\n\n",
  431. envr);
  432. } /* if */
  433. #endif
  434. } /* open_history_file */
  435. int main(int argc, char **argv)
  436. {
  437. char *buf = NULL;
  438. int rc = 0;
  439. printf("\n");
  440. if (!PHYSFS_init(argv[0]))
  441. {
  442. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  443. return(1);
  444. } /* if */
  445. output_versions();
  446. output_archivers();
  447. open_history_file();
  448. printf("Enter commands. Enter \"help\" for instructions.\n");
  449. do
  450. {
  451. #if (defined HAVE_READLINE)
  452. buf = readline("> ");
  453. #else
  454. int i;
  455. buf = malloc(512);
  456. memset(buf, '\0', 512);
  457. printf("> ");
  458. for (i = 0; i < 511; i++)
  459. {
  460. int ch = fgetc(stdin);
  461. if (ch == EOF)
  462. {
  463. strcpy(buf, "quit");
  464. break;
  465. } /* if */
  466. else if ((ch == '\n') || (ch == '\r'))
  467. {
  468. buf[i] = '\0';
  469. break;
  470. } /* else if */
  471. else if (ch == '\b')
  472. {
  473. if (i > 0)
  474. i--;
  475. } /* else if */
  476. else
  477. {
  478. buf[i] = (char) ch;
  479. } /* else */
  480. } /* for */
  481. #endif
  482. rc = process_command(buf);
  483. free(buf);
  484. } while (rc);
  485. if (!PHYSFS_deinit())
  486. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  487. if (history_file)
  488. fclose(history_file);
  489. /*
  490. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  491. printf(" it makes you shoot teh railgun bettar.\n");
  492. */
  493. return(0);
  494. } /* main */
  495. /* end of test_physfs.c ... */