test_physfs.c 15 KB

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