test_physfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. static int cmd_mkdir(char *args)
  200. {
  201. if (PHYSFS_mkdir(args))
  202. printf("Successful.\n");
  203. else
  204. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  205. return(1);
  206. } /* cmd_mkdir */
  207. static int cmd_delete(char *args)
  208. {
  209. if (PHYSFS_delete(args))
  210. printf("Successful.\n");
  211. else
  212. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  213. return(1);
  214. } /* cmd_delete */
  215. static int cmd_getrealdir(char *args)
  216. {
  217. const char *rc = PHYSFS_getRealDir(args);
  218. if (rc)
  219. printf("Found at [%s].\n", rc);
  220. else
  221. printf("Not found.\n");
  222. return(1);
  223. } /* cmd_getrealdir */
  224. static int cmd_exists(char *args)
  225. {
  226. int rc = PHYSFS_exists(args);
  227. printf("File %sexists.\n", rc ? "" : "does not ");
  228. return(1);
  229. } /* cmd_exists */
  230. static int cmd_isdir(char *args)
  231. {
  232. int rc = PHYSFS_isDirectory(args);
  233. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  234. return(1);
  235. } /* cmd_isdir */
  236. static int cmd_issymlink(char *args)
  237. {
  238. int rc = PHYSFS_isSymbolicLink(args);
  239. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  240. return(1);
  241. } /* cmd_issymlink */
  242. /* must have spaces trimmed prior to this call. */
  243. static int count_args(const char *str)
  244. {
  245. int retval = 0;
  246. if (str != NULL)
  247. {
  248. for (; *str != '\0'; str++)
  249. {
  250. if (*str == ' ')
  251. retval++;
  252. } /* for */
  253. retval++;
  254. } /* if */
  255. return(retval);
  256. } /* count_args */
  257. static int cmd_help(char *args);
  258. typedef struct
  259. {
  260. const char *cmd;
  261. int (*func)(char *args);
  262. int argcount;
  263. const char *usage;
  264. } command_info;
  265. static const command_info commands[] =
  266. {
  267. { "quit", cmd_quit, 0, NULL },
  268. { "q", cmd_quit, 0, NULL },
  269. { "help", cmd_help, 0, NULL },
  270. { "init", cmd_init, 1, "<argv0>" },
  271. { "deinit", cmd_deinit, 0, NULL },
  272. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  273. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  274. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  275. { "getlasterror", cmd_getlasterror, 0, NULL },
  276. { "getdirsep", cmd_getdirsep, 0, NULL },
  277. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  278. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  279. { "getbasedir", cmd_getbasedir, 0, NULL },
  280. { "getuserdir", cmd_getuserdir, 0, NULL },
  281. { "getwritedir", cmd_getwritedir, 0, NULL },
  282. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  283. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  284. { "setsaneconfig", cmd_setsaneconfig, 4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  285. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  286. { "delete", cmd_delete, 1, "<dirToDelete>" },
  287. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  288. { "exists", cmd_exists, 1, "<fileToCheck>" },
  289. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  290. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  291. { NULL, NULL, -1, NULL }
  292. };
  293. static void output_usage(const char *intro, const command_info *cmdinfo)
  294. {
  295. if (cmdinfo->argcount == 0)
  296. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  297. else
  298. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  299. } /* output_usage */
  300. static int cmd_help(char *args)
  301. {
  302. const command_info *i;
  303. printf("Commands:\n");
  304. for (i = commands; i->cmd != NULL; i++)
  305. output_usage(" -", i);
  306. return(1);
  307. } /* output_cmd_help */
  308. static void trim_command(const char *orig, char *copy)
  309. {
  310. const char *i;
  311. char *writeptr = copy;
  312. int spacecount = 0;
  313. int have_first = 0;
  314. for (i = orig; *i != '\0'; i++)
  315. {
  316. if (*i == ' ')
  317. {
  318. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  319. {
  320. if ((have_first) && (!spacecount))
  321. {
  322. spacecount++;
  323. *writeptr = ' ';
  324. writeptr++;
  325. } /* if */
  326. } /* if */
  327. } /* if */
  328. else
  329. {
  330. have_first = 1;
  331. spacecount = 0;
  332. *writeptr = *i;
  333. writeptr++;
  334. } /* else */
  335. } /* for */
  336. *writeptr = '\0';
  337. /*
  338. printf("\n command is [%s].\n", copy);
  339. */
  340. } /* trim_command */
  341. static int process_command(char *complete_cmd)
  342. {
  343. const command_info *i;
  344. char *cmd_copy = malloc(strlen(complete_cmd) + 1);
  345. char *args;
  346. int rc = 1;
  347. if (cmd_copy == NULL)
  348. {
  349. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  350. return(0);
  351. } /* if */
  352. trim_command(complete_cmd, cmd_copy);
  353. args = strchr(cmd_copy, ' ');
  354. if (args != NULL)
  355. {
  356. *args = '\0';
  357. args++;
  358. } /* else */
  359. if (cmd_copy[0] != '\0')
  360. {
  361. for (i = commands; i->cmd != NULL; i++)
  362. {
  363. if (strcmp(i->cmd, cmd_copy) == 0)
  364. {
  365. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  366. output_usage("usage:", i);
  367. else
  368. rc = i->func(args);
  369. break;
  370. } /* if */
  371. } /* for */
  372. if (i->cmd == NULL)
  373. printf("Unknown command. Enter \"help\" for instructions.\n");
  374. add_history(complete_cmd);
  375. if (history_file)
  376. {
  377. fprintf(history_file, "%s\n", complete_cmd);
  378. fflush(history_file);
  379. } /* if */
  380. } /* if */
  381. free(cmd_copy);
  382. return(rc);
  383. } /* process_command */
  384. static void open_history_file(void)
  385. {
  386. #if 0
  387. const char *envr = getenv("TESTPHYSFS_HISTORY");
  388. if (!envr)
  389. return;
  390. #else
  391. char envr[256];
  392. strcpy(envr, PHYSFS_getUserDir());
  393. strcat(envr, ".testphys_history");
  394. #endif
  395. if (access(envr, F_OK) == 0)
  396. {
  397. char buf[512];
  398. FILE *f = fopen(envr, "r");
  399. if (!f)
  400. {
  401. printf("\n\n"
  402. "Could not open history file [%s] for reading!\n"
  403. " Will not have past history available.\n\n",
  404. envr);
  405. return;
  406. } /* if */
  407. do
  408. {
  409. fgets(buf, sizeof (buf), f);
  410. if (buf[strlen(buf) - 1] == '\n')
  411. buf[strlen(buf) - 1] = '\0';
  412. add_history(buf);
  413. } while (!feof(f));
  414. fclose(f);
  415. } /* if */
  416. history_file = fopen(envr, "ab");
  417. if (!history_file)
  418. {
  419. printf("\n\n"
  420. "Could not open history file [%s] for appending!\n"
  421. " Will not be able to record this session's history.\n\n",
  422. envr);
  423. } /* if */
  424. } /* open_history_file */
  425. int main(int argc, char **argv)
  426. {
  427. char *buf = NULL;
  428. int rc = 0;
  429. printf("\n");
  430. if (!PHYSFS_init(argv[0]))
  431. {
  432. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  433. return(1);
  434. } /* if */
  435. output_versions();
  436. output_archivers();
  437. open_history_file();
  438. printf("Enter commands. Enter \"help\" for instructions.\n");
  439. do
  440. {
  441. buf = readline("> ");
  442. rc = process_command(buf);
  443. free(buf);
  444. } while (rc);
  445. if (!PHYSFS_deinit())
  446. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  447. if (history_file)
  448. fclose(history_file);
  449. /*
  450. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  451. printf(" it makes you shoot teh railgun bettar.\n");
  452. */
  453. return(0);
  454. } /* main */
  455. /* end of test_physfs.c ... */