test_physfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <unistd.h>
  12. #include <readline.h>
  13. #include <history.h>
  14. #include "physfs.h"
  15. #define TEST_VERSION_MAJOR 0
  16. #define TEST_VERSION_MINOR 1
  17. #define TEST_VERSION_PATCH 0
  18. static FILE *history_file = NULL;
  19. static void output_versions(void)
  20. {
  21. PHYSFS_Version compiled;
  22. PHYSFS_Version linked;
  23. PHYSFS_VERSION(&compiled);
  24. PHYSFS_getLinkedVersion(&linked);
  25. printf("test_physfs version %d.%d.%d.\n"
  26. " Compiled against PhysicsFS version %d.%d.%d,\n"
  27. " and linked against %d.%d.%d.\n\n",
  28. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  29. compiled.major, compiled.minor, compiled.patch,
  30. linked.major, linked.minor, linked.patch);
  31. } /* output_versions */
  32. static void output_archivers(void)
  33. {
  34. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  35. const PHYSFS_ArchiveInfo **i;
  36. printf("Supported archive types:\n");
  37. if (*rc == NULL)
  38. printf(" * Apparently, NONE!\n");
  39. else
  40. {
  41. for (i = rc; *i != NULL; i++)
  42. {
  43. printf(" * %s: %s\n Written by %s.\n %s\n",
  44. (*i)->extension, (*i)->description,
  45. (*i)->author, (*i)->url);
  46. } /* for */
  47. } /* else */
  48. printf("\n");
  49. } /* output_archivers */
  50. static int cmd_quit(char *args)
  51. {
  52. return(0);
  53. } /* cmd_quit */
  54. static int cmd_init(char *args)
  55. {
  56. if (PHYSFS_init(args))
  57. printf("Successful.\n");
  58. else
  59. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  60. return(1);
  61. } /* cmd_init */
  62. static int cmd_deinit(char *args)
  63. {
  64. if (PHYSFS_deinit())
  65. printf("Successful.\n");
  66. else
  67. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  68. return(1);
  69. } /* cmd_deinit */
  70. static int cmd_addarchive(char *args)
  71. {
  72. char *ptr = strchr(args, ' ');
  73. int appending = atoi(ptr + 1);
  74. *ptr = '\0';
  75. if (PHYSFS_addToSearchPath(args, appending))
  76. printf("Successful.\n");
  77. else
  78. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  79. return(1);
  80. } /* cmd_addarchive */
  81. static int cmd_removearchive(char *args)
  82. {
  83. if (PHYSFS_removeFromSearchPath(args))
  84. printf("Successful.\n");
  85. else
  86. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  87. return(1);
  88. } /* cmd_removearchive */
  89. static int cmd_enumerate(char *args)
  90. {
  91. char **rc = PHYSFS_enumerateFiles(args);
  92. if (rc == NULL)
  93. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  94. else
  95. {
  96. int file_count;
  97. char **i;
  98. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  99. printf("%s\n", *i);
  100. printf("\n total (%d) files.\n", file_count);
  101. PHYSFS_freeList(rc);
  102. } /* else */
  103. return(1);
  104. } /* cmd_enumerate */
  105. static int cmd_getdirsep(char *args)
  106. {
  107. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  108. return(1);
  109. } /* cmd_getdirsep */
  110. static int cmd_getlasterror(char *args)
  111. {
  112. printf("last error is [%s].\n", PHYSFS_getLastError());
  113. return(1);
  114. } /* cmd_getlasterror */
  115. static int cmd_getcdromdirs(char *args)
  116. {
  117. char **rc = PHYSFS_getCdRomDirs();
  118. if (rc == NULL)
  119. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  120. else
  121. {
  122. int dir_count;
  123. char **i;
  124. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  125. printf("%s\n", *i);
  126. printf("\n total (%d) drives.\n", dir_count);
  127. PHYSFS_freeList(rc);
  128. } /* else */
  129. return(1);
  130. } /* cmd_getcdromdirs */
  131. static int cmd_getsearchpath(char *args)
  132. {
  133. char **rc = PHYSFS_getSearchPath();
  134. if (rc == NULL)
  135. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  136. else
  137. {
  138. int dir_count;
  139. char **i;
  140. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  141. printf("%s\n", *i);
  142. printf("\n total (%d) directories.\n", dir_count);
  143. PHYSFS_freeList(rc);
  144. } /* else */
  145. return(1);
  146. } /* cmd_getcdromdirs */
  147. static int cmd_getbasedir(char *args)
  148. {
  149. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  150. return(1);
  151. } /* cmd_getbasedir */
  152. static int cmd_getuserdir(char *args)
  153. {
  154. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  155. return(1);
  156. } /* cmd_getuserdir */
  157. static int cmd_getwritedir(char *args)
  158. {
  159. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  160. return(1);
  161. } /* cmd_getwritedir */
  162. static int cmd_setwritedir(char *args)
  163. {
  164. if (PHYSFS_setWriteDir(args))
  165. printf("Successful.\n");
  166. else
  167. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  168. return(1);
  169. } /* cmd_setwritedir */
  170. static int cmd_permitsyms(char *args)
  171. {
  172. int num = atoi(args);
  173. PHYSFS_permitSymbolicLinks(num);
  174. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  175. return(1);
  176. } /* cmd_permitsyms */
  177. static int cmd_setsaneconfig(char *args)
  178. {
  179. char *appName;
  180. char *arcExt;
  181. int inclCD;
  182. int arcsFirst;
  183. char *ptr = args;
  184. /* ugly. */
  185. appName = ptr;
  186. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  187. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  188. arcsFirst = atoi(ptr);
  189. if (strcmp(appName, "!") == 0)
  190. appName = NULL;
  191. if (strcmp(arcExt, "!") == 0)
  192. arcExt = NULL;
  193. if (PHYSFS_setSaneConfig(appName, arcExt, inclCD, arcsFirst))
  194. printf("Successful.\n");
  195. else
  196. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  197. return(1);
  198. } /* cmd_setsaneconfig */
  199. /* must have spaces trimmed prior to this call. */
  200. static int count_args(const char *str)
  201. {
  202. int retval = 0;
  203. if (str != NULL)
  204. {
  205. for (; *str != '\0'; str++)
  206. {
  207. if (*str == ' ')
  208. retval++;
  209. } /* for */
  210. retval++;
  211. } /* if */
  212. return(retval);
  213. } /* count_args */
  214. static int cmd_help(char *args);
  215. typedef struct
  216. {
  217. const char *cmd;
  218. int (*func)(char *args);
  219. int argcount;
  220. const char *usage;
  221. } command_info;
  222. static const command_info commands[] =
  223. {
  224. { "quit", cmd_quit, 0, NULL },
  225. { "q", cmd_quit, 0, NULL },
  226. { "help", cmd_help, 0, NULL },
  227. { "init", cmd_init, 1, "<argv0>" },
  228. { "deinit", cmd_deinit, 0, NULL },
  229. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  230. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  231. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  232. { "getlasterror", cmd_getlasterror, 0, NULL },
  233. { "getdirsep", cmd_getdirsep, 0, NULL },
  234. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  235. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  236. { "getbasedir", cmd_getbasedir, 0, NULL },
  237. { "getuserdir", cmd_getuserdir, 0, NULL },
  238. { "getwritedir", cmd_getwritedir, 0, NULL },
  239. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  240. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  241. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  242. { NULL, NULL, -1, NULL }
  243. };
  244. static void output_usage(const char *intro, const command_info *cmdinfo)
  245. {
  246. if (cmdinfo->argcount == 0)
  247. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  248. else
  249. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  250. } /* output_usage */
  251. static int cmd_help(char *args)
  252. {
  253. const command_info *i;
  254. printf("Commands:\n");
  255. for (i = commands; i->cmd != NULL; i++)
  256. output_usage(" -", i);
  257. return(1);
  258. } /* output_cmd_help */
  259. static void trim_command(const char *orig, char *copy)
  260. {
  261. const char *i;
  262. char *writeptr = copy;
  263. int spacecount = 0;
  264. int have_first = 0;
  265. for (i = orig; *i != '\0'; i++)
  266. {
  267. if (*i == ' ')
  268. {
  269. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  270. {
  271. if ((have_first) && (!spacecount))
  272. {
  273. spacecount++;
  274. *writeptr = ' ';
  275. writeptr++;
  276. } /* if */
  277. } /* if */
  278. } /* if */
  279. else
  280. {
  281. have_first = 1;
  282. spacecount = 0;
  283. *writeptr = *i;
  284. writeptr++;
  285. } /* else */
  286. } /* for */
  287. *writeptr = '\0';
  288. /*
  289. printf("\n command is [%s].\n", copy);
  290. */
  291. } /* trim_command */
  292. static int process_command(char *complete_cmd)
  293. {
  294. const command_info *i;
  295. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  296. char *args;
  297. int rc = 1;
  298. if (cmd_copy == NULL)
  299. {
  300. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  301. return(0);
  302. } /* if */
  303. trim_command(complete_cmd, cmd_copy);
  304. args = strchr(cmd_copy, ' ');
  305. if (args != NULL)
  306. {
  307. *args = '\0';
  308. args++;
  309. } /* else */
  310. if (cmd_copy[0] != '\0')
  311. {
  312. for (i = commands; i->cmd != NULL; i++)
  313. {
  314. if (strcmp(i->cmd, cmd_copy) == 0)
  315. {
  316. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  317. output_usage("usage:", i);
  318. else
  319. rc = i->func(args);
  320. break;
  321. } /* if */
  322. } /* for */
  323. if (i->cmd == NULL)
  324. printf("Unknown command. Enter \"help\" for instructions.\n");
  325. add_history(complete_cmd);
  326. if (history_file)
  327. {
  328. fprintf(history_file, "%s\n", complete_cmd);
  329. fflush(history_file);
  330. } /* if */
  331. } /* if */
  332. free(cmd_copy);
  333. return(rc);
  334. } /* process_command */
  335. static void open_history_file(void)
  336. {
  337. const char *envr = getenv("TESTPHYSFS_HISTORY");
  338. if (!envr)
  339. return;
  340. if (access(envr, F_OK) == 0)
  341. {
  342. char buf[512];
  343. FILE *f = fopen(envr, "r");
  344. if (!f)
  345. {
  346. printf("\n\n"
  347. "Could not open history file [%s] for reading!\n"
  348. " Will not have past history available.\n\n",
  349. envr);
  350. return;
  351. } /* if */
  352. do
  353. {
  354. fgets(buf, sizeof (buf), f);
  355. if (buf[strlen(buf) - 1] == '\n')
  356. buf[strlen(buf) - 1] = '\0';
  357. add_history(buf);
  358. } while (!feof(f));
  359. fclose(f);
  360. } /* if */
  361. history_file = fopen(envr, "ab");
  362. if (!history_file)
  363. {
  364. printf("\n\n"
  365. "Could not open history file [%s] for appending!\n"
  366. " Will not be able to record this session's history.\n\n",
  367. envr);
  368. } /* if */
  369. } /* open_history_file */
  370. int main(int argc, char **argv)
  371. {
  372. char *buf = NULL;
  373. int rc = 0;
  374. printf("\n");
  375. if (!PHYSFS_init(argv[0]))
  376. {
  377. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  378. return(1);
  379. } /* if */
  380. output_versions();
  381. output_archivers();
  382. open_history_file();
  383. printf("Enter commands. Enter \"help\" for instructions.\n");
  384. do
  385. {
  386. buf = readline("> ");
  387. rc = process_command(buf);
  388. free(buf);
  389. } while (rc);
  390. if (!PHYSFS_deinit())
  391. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  392. if (history_file)
  393. fclose(history_file);
  394. /*
  395. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  396. printf(" it makes you shoot teh railgun bettar.\n");
  397. */
  398. return(0);
  399. } /* main */
  400. /* end of test_physfs.c ... */