test_physfs.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /**
  2. * Test program for PhysicsFS. May only work on Unix.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define _CRT_SECURE_NO_WARNINGS 1
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #if (defined __MWERKS__)
  14. #include <SIOUX.h>
  15. #endif
  16. #if (defined PHYSFS_HAVE_READLINE)
  17. #include <unistd.h>
  18. #include <readline/readline.h>
  19. #include <readline/history.h>
  20. #endif
  21. #include <time.h>
  22. /* Define this, so the compiler doesn't complain about using old APIs. */
  23. #define PHYSFS_DEPRECATED
  24. #include "physfs.h"
  25. #define TEST_VERSION_MAJOR 3
  26. #define TEST_VERSION_MINOR 3
  27. #define TEST_VERSION_PATCH 0
  28. static FILE *history_file = NULL;
  29. static PHYSFS_uint32 do_buffer_size = 0;
  30. static void output_versions(void)
  31. {
  32. PHYSFS_Version compiled;
  33. PHYSFS_Version linked;
  34. PHYSFS_VERSION(&compiled);
  35. PHYSFS_getLinkedVersion(&linked);
  36. printf("test_physfs version %d.%d.%d.\n"
  37. " Compiled against PhysicsFS version %d.%d.%d,\n"
  38. " and linked against %d.%d.%d.\n\n",
  39. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  40. (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
  41. (int) linked.major, (int) linked.minor, (int) linked.patch);
  42. } /* output_versions */
  43. static void output_archivers(void)
  44. {
  45. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  46. const PHYSFS_ArchiveInfo **i;
  47. printf("Supported archive types:\n");
  48. if (*rc == NULL)
  49. printf(" * Apparently, NONE!\n");
  50. else
  51. {
  52. for (i = rc; *i != NULL; i++)
  53. {
  54. printf(" * %s: %s\n Written by %s.\n %s\n",
  55. (*i)->extension, (*i)->description,
  56. (*i)->author, (*i)->url);
  57. printf(" %s symbolic links.\n",
  58. (*i)->supportsSymlinks ? "Supports" : "Does not support");
  59. } /* for */
  60. } /* else */
  61. printf("\n");
  62. } /* output_archivers */
  63. static int cmd_quit(char *args)
  64. {
  65. return 0;
  66. } /* cmd_quit */
  67. static int cmd_init(char *args)
  68. {
  69. if (*args == '\"')
  70. {
  71. args++;
  72. args[strlen(args) - 1] = '\0';
  73. } /* if */
  74. if (PHYSFS_init(args))
  75. printf("Successful.\n");
  76. else
  77. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  78. return 1;
  79. } /* cmd_init */
  80. static int cmd_deinit(char *args)
  81. {
  82. if (PHYSFS_deinit())
  83. printf("Successful.\n");
  84. else
  85. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  86. return 1;
  87. } /* cmd_deinit */
  88. static int cmd_addarchive(char *args)
  89. {
  90. char *ptr = strrchr(args, ' ');
  91. int appending = atoi(ptr + 1);
  92. *ptr = '\0';
  93. if (*args == '\"')
  94. {
  95. args++;
  96. *(ptr - 1) = '\0';
  97. } /* if */
  98. /*printf("[%s], [%d]\n", args, appending);*/
  99. if (PHYSFS_mount(args, NULL, appending))
  100. printf("Successful.\n");
  101. else
  102. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  103. return 1;
  104. } /* cmd_addarchive */
  105. /* wrap free() to avoid calling convention wankery. */
  106. static void freeBuf(void *buf)
  107. {
  108. free(buf);
  109. } /* freeBuf */
  110. typedef enum
  111. {
  112. MNTTYPE_PATH,
  113. MNTTYPE_MEMORY,
  114. MNTTYPE_HANDLE
  115. } MountType;
  116. static int cmd_mount_internal(char *args, const MountType mnttype)
  117. {
  118. char *ptr;
  119. char *mntpoint = NULL;
  120. int appending = 0;
  121. int rc = 0;
  122. if (*args == '\"')
  123. {
  124. args++;
  125. ptr = strchr(args, '\"');
  126. if (ptr == NULL)
  127. {
  128. printf("missing string terminator in argument.\n");
  129. return 1;
  130. } /* if */
  131. *(ptr) = '\0';
  132. } /* if */
  133. else
  134. {
  135. ptr = strchr(args, ' ');
  136. *ptr = '\0';
  137. } /* else */
  138. mntpoint = ptr + 1;
  139. if (*mntpoint == '\"')
  140. {
  141. mntpoint++;
  142. ptr = strchr(mntpoint, '\"');
  143. if (ptr == NULL)
  144. {
  145. printf("missing string terminator in argument.\n");
  146. return 1;
  147. } /* if */
  148. *(ptr) = '\0';
  149. } /* if */
  150. else
  151. {
  152. ptr = strchr(mntpoint, ' ');
  153. *(ptr) = '\0';
  154. } /* else */
  155. appending = atoi(ptr + 1);
  156. /*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
  157. if (mnttype == MNTTYPE_PATH)
  158. rc = PHYSFS_mount(args, mntpoint, appending);
  159. else if (mnttype == MNTTYPE_HANDLE)
  160. {
  161. PHYSFS_File *f = PHYSFS_openRead(args);
  162. if (f == NULL)
  163. {
  164. printf("PHYSFS_openRead('%s') failed. reason: %s.\n", args, PHYSFS_getLastError());
  165. return 1;
  166. } /* if */
  167. rc = PHYSFS_mountHandle(f, args, mntpoint, appending);
  168. if (!rc)
  169. PHYSFS_close(f);
  170. } /* else if */
  171. else if (mnttype == MNTTYPE_MEMORY)
  172. {
  173. FILE *in = fopen(args, "rb");
  174. void *buf = NULL;
  175. long len = 0;
  176. if (in == NULL)
  177. {
  178. printf("Failed to open %s to read into memory: %s.\n", args, strerror(errno));
  179. return 1;
  180. } /* if */
  181. if ( (fseek(in, 0, SEEK_END) != 0) || ((len = ftell(in)) < 0) )
  182. {
  183. printf("Failed to find size of %s to read into memory: %s.\n", args, strerror(errno));
  184. fclose(in);
  185. return 1;
  186. } /* if */
  187. buf = malloc(len);
  188. if (buf == NULL)
  189. {
  190. printf("Failed to allocate space to read %s into memory: %s.\n", args, strerror(errno));
  191. fclose(in);
  192. return 1;
  193. } /* if */
  194. if ((fseek(in, 0, SEEK_SET) != 0) || (fread(buf, len, 1, in) != 1))
  195. {
  196. printf("Failed to read %s into memory: %s.\n", args, strerror(errno));
  197. fclose(in);
  198. free(buf);
  199. return 1;
  200. } /* if */
  201. fclose(in);
  202. rc = PHYSFS_mountMemory(buf, len, freeBuf, args, mntpoint, appending);
  203. } /* else */
  204. if (rc)
  205. printf("Successful.\n");
  206. else
  207. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  208. return 1;
  209. } /* cmd_mount_internal */
  210. static int cmd_mount(char *args)
  211. {
  212. return cmd_mount_internal(args, MNTTYPE_PATH);
  213. } /* cmd_mount */
  214. static int cmd_mount_mem(char *args)
  215. {
  216. return cmd_mount_internal(args, MNTTYPE_MEMORY);
  217. } /* cmd_mount_mem */
  218. static int cmd_mount_handle(char *args)
  219. {
  220. return cmd_mount_internal(args, MNTTYPE_HANDLE);
  221. } /* cmd_mount_handle */
  222. static int cmd_getmountpoint(char *args)
  223. {
  224. if (*args == '\"')
  225. {
  226. args++;
  227. args[strlen(args) - 1] = '\0';
  228. } /* if */
  229. printf("Dir [%s] is mounted at [%s].\n", args, PHYSFS_getMountPoint(args));
  230. return 1;
  231. } /* cmd_getmountpoint */
  232. static int cmd_setroot(char *args)
  233. {
  234. char *archive;
  235. char *subdir;
  236. char *ptr;
  237. archive = args;
  238. if (*archive == '\"')
  239. {
  240. archive++;
  241. ptr = strchr(archive, '\"');
  242. if (ptr == NULL)
  243. {
  244. printf("missing string terminator in argument.\n");
  245. return 1;
  246. } /* if */
  247. *(ptr) = '\0';
  248. } /* if */
  249. else
  250. {
  251. ptr = strchr(archive, ' ');
  252. *ptr = '\0';
  253. } /* else */
  254. subdir = ptr + 1;
  255. if (*subdir == '\"')
  256. {
  257. subdir++;
  258. ptr = strchr(subdir, '\"');
  259. if (ptr == NULL)
  260. {
  261. printf("missing string terminator in argument.\n");
  262. return 1;
  263. } /* if */
  264. *(ptr) = '\0';
  265. } /* if */
  266. if (PHYSFS_setRoot(archive, subdir))
  267. printf("Successful.\n");
  268. else
  269. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  270. return 1;
  271. } /* cmd_setroot */
  272. static int cmd_removearchive(char *args)
  273. {
  274. if (*args == '\"')
  275. {
  276. args++;
  277. args[strlen(args) - 1] = '\0';
  278. } /* if */
  279. if (PHYSFS_unmount(args))
  280. printf("Successful.\n");
  281. else
  282. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  283. return 1;
  284. } /* cmd_removearchive */
  285. static int cmd_enumerate(char *args)
  286. {
  287. char **rc;
  288. if (*args == '\"')
  289. {
  290. args++;
  291. args[strlen(args) - 1] = '\0';
  292. } /* if */
  293. rc = PHYSFS_enumerateFiles(args);
  294. if (rc == NULL)
  295. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  296. else
  297. {
  298. int file_count;
  299. char **i;
  300. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  301. printf("%s\n", *i);
  302. printf("\n total (%d) files.\n", file_count);
  303. PHYSFS_freeList(rc);
  304. } /* else */
  305. return 1;
  306. } /* cmd_enumerate */
  307. #define STR_BOX_VERTICAL_RIGHT "\xe2\x94\x9c"
  308. #define STR_BOX_VERTICAL "\xe2\x94\x82"
  309. #define STR_BOX_HORIZONTAL "\xe2\x94\x80"
  310. #define STR_BOX_UP_RIGHT "\xe2\x94\x94"
  311. #define STR_NBSP "\xc2\xa0"
  312. #define PREFIX_DIRENTRY STR_BOX_VERTICAL_RIGHT STR_BOX_HORIZONTAL STR_BOX_HORIZONTAL STR_NBSP
  313. #define PREFIX_DIRENTRY_LAST STR_BOX_UP_RIGHT STR_BOX_HORIZONTAL STR_BOX_HORIZONTAL STR_NBSP
  314. #define PREFIX_RECURSIVE STR_BOX_VERTICAL STR_NBSP STR_NBSP STR_NBSP
  315. #define PREFIX_RECURSIVE_LAST STR_NBSP STR_NBSP STR_NBSP STR_NBSP
  316. static void cmd_tree_recursive(const char *prefix, const char *fullPath, const char *name, unsigned depth, int *total_dir_count, int *total_file_count) {
  317. char **rc;
  318. printf("%s", name);
  319. rc = PHYSFS_enumerateFiles(fullPath);
  320. if (rc == NULL) {
  321. printf(" [Failure. reason: %s]\n", PHYSFS_getLastError());
  322. }
  323. else
  324. {
  325. int file_count;
  326. char **i;
  327. printf("\n");
  328. for (i = rc, file_count = 0; *i != NULL; i++, file_count++) {
  329. char *newFullPath = malloc(strlen(fullPath) + strlen(*i) + 2);
  330. char *thisPrefix;
  331. strcpy(newFullPath, fullPath);
  332. strcat(newFullPath, "/");
  333. strcat(newFullPath, *i);
  334. if (i[1]) {
  335. thisPrefix = PREFIX_DIRENTRY;
  336. } else {
  337. thisPrefix = PREFIX_DIRENTRY_LAST;
  338. }
  339. if (PHYSFS_isSymbolicLink(newFullPath)) {
  340. printf("%s%s%s [symbolic link]\n", prefix, thisPrefix, *i);
  341. } else if (PHYSFS_isDirectory(newFullPath)) {
  342. char *newPrefix;
  343. *total_dir_count += 1;
  344. if (i[1]) {
  345. newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE) + 1);
  346. strcpy(newPrefix, prefix);
  347. strcat(newPrefix, PREFIX_RECURSIVE);
  348. } else {
  349. newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE_LAST) + 1);
  350. strcpy(newPrefix, prefix);
  351. strcat(newPrefix, PREFIX_RECURSIVE_LAST);
  352. }
  353. printf("%s%s", prefix, thisPrefix);
  354. cmd_tree_recursive(newPrefix, newFullPath, *i, depth + 1, total_dir_count, total_file_count);
  355. free(newPrefix);
  356. } else {
  357. *total_file_count += 1;
  358. printf("%s%s%s\n", prefix, thisPrefix, *i);
  359. }
  360. free(newFullPath);
  361. }
  362. PHYSFS_freeList(rc);
  363. } /* else */
  364. } /* cmd_tree_recursive */
  365. static int cmd_tree(char *args)
  366. {
  367. int total_dir_count = 0, total_file_count = 0; /* FIXME: should be PHYSFS_uint64 */
  368. if (*args == '\"')
  369. {
  370. args++;
  371. args[strlen(args) - 1] = '\0';
  372. } /* if */
  373. cmd_tree_recursive("", args, args, 0, &total_dir_count, &total_file_count);
  374. printf("\n%d directories, %d files\n", total_dir_count, total_file_count);
  375. return 1;
  376. } /* cmd_enumerate */
  377. static int cmd_getdirsep(char *args)
  378. {
  379. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  380. return 1;
  381. } /* cmd_getdirsep */
  382. static int cmd_getlasterror(char *args)
  383. {
  384. printf("last error is [%s].\n", PHYSFS_getLastError());
  385. return 1;
  386. } /* cmd_getlasterror */
  387. static int cmd_getcdromdirs(char *args)
  388. {
  389. char **rc = PHYSFS_getCdRomDirs();
  390. if (rc == NULL)
  391. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  392. else
  393. {
  394. int dir_count;
  395. char **i;
  396. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  397. printf("%s\n", *i);
  398. printf("\n total (%d) drives.\n", dir_count);
  399. PHYSFS_freeList(rc);
  400. } /* else */
  401. return 1;
  402. } /* cmd_getcdromdirs */
  403. static int cmd_getsearchpath(char *args)
  404. {
  405. char **rc = PHYSFS_getSearchPath();
  406. if (rc == NULL)
  407. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  408. else
  409. {
  410. int dir_count;
  411. char **i;
  412. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  413. printf("%s\n", *i);
  414. printf("\n total (%d) directories.\n", dir_count);
  415. PHYSFS_freeList(rc);
  416. } /* else */
  417. return 1;
  418. } /* cmd_getcdromdirs */
  419. static int cmd_getbasedir(char *args)
  420. {
  421. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  422. return 1;
  423. } /* cmd_getbasedir */
  424. static int cmd_getuserdir(char *args)
  425. {
  426. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  427. return 1;
  428. } /* cmd_getuserdir */
  429. static int cmd_getprefdir(char *args)
  430. {
  431. char *org;
  432. char *appName;
  433. char *ptr = args;
  434. org = ptr;
  435. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  436. printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
  437. return 1;
  438. } /* cmd_getprefdir */
  439. static int cmd_getwritedir(char *args)
  440. {
  441. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  442. return 1;
  443. } /* cmd_getwritedir */
  444. static int cmd_setwritedir(char *args)
  445. {
  446. if (*args == '\"')
  447. {
  448. args++;
  449. args[strlen(args) - 1] = '\0';
  450. } /* if */
  451. if (PHYSFS_setWriteDir(args))
  452. printf("Successful.\n");
  453. else
  454. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  455. return 1;
  456. } /* cmd_setwritedir */
  457. static int cmd_permitsyms(char *args)
  458. {
  459. int num;
  460. if (*args == '\"')
  461. {
  462. args++;
  463. args[strlen(args) - 1] = '\0';
  464. } /* if */
  465. num = atoi(args);
  466. PHYSFS_permitSymbolicLinks(num);
  467. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  468. return 1;
  469. } /* cmd_permitsyms */
  470. static int cmd_setbuffer(char *args)
  471. {
  472. if (*args == '\"')
  473. {
  474. args++;
  475. args[strlen(args) - 1] = '\0';
  476. } /* if */
  477. do_buffer_size = (unsigned int) atoi(args);
  478. if (do_buffer_size)
  479. {
  480. printf("Further tests will set a (%lu) size buffer.\n",
  481. (unsigned long) do_buffer_size);
  482. } /* if */
  483. else
  484. {
  485. printf("Further tests will NOT use a buffer.\n");
  486. } /* else */
  487. return 1;
  488. } /* cmd_setbuffer */
  489. static int cmd_stressbuffer(char *args)
  490. {
  491. int num;
  492. if (*args == '\"')
  493. {
  494. args++;
  495. args[strlen(args) - 1] = '\0';
  496. } /* if */
  497. num = atoi(args);
  498. if (num < 0)
  499. printf("buffer must be greater than or equal to zero.\n");
  500. else
  501. {
  502. PHYSFS_File *f;
  503. int rndnum;
  504. printf("Stress testing with (%d) byte buffer...\n", num);
  505. f = PHYSFS_openWrite("test.txt");
  506. if (f == NULL)
  507. printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
  508. else
  509. {
  510. int i, j;
  511. char buf[37];
  512. char buf2[37];
  513. if (!PHYSFS_setBuffer(f, num))
  514. {
  515. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  516. PHYSFS_close(f);
  517. PHYSFS_delete("test.txt");
  518. return 1;
  519. } /* if */
  520. strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
  521. srand((unsigned int) time(NULL));
  522. for (i = 0; i < 10; i++)
  523. {
  524. for (j = 0; j < 10000; j++)
  525. {
  526. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  527. PHYSFS_uint32 left = 36 - right;
  528. if (PHYSFS_writeBytes(f, buf, left) != left)
  529. {
  530. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  531. PHYSFS_close(f);
  532. return 1;
  533. } /* if */
  534. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  535. if (rndnum == 42)
  536. {
  537. if (!PHYSFS_flush(f))
  538. {
  539. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  540. PHYSFS_close(f);
  541. return 1;
  542. } /* if */
  543. } /* if */
  544. if (PHYSFS_writeBytes(f, buf + left, right) != right)
  545. {
  546. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  547. PHYSFS_close(f);
  548. return 1;
  549. } /* if */
  550. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  551. if (rndnum == 42)
  552. {
  553. if (!PHYSFS_flush(f))
  554. {
  555. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  556. PHYSFS_close(f);
  557. return 1;
  558. } /* if */
  559. } /* if */
  560. } /* for */
  561. if (!PHYSFS_flush(f))
  562. {
  563. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  564. PHYSFS_close(f);
  565. return 1;
  566. } /* if */
  567. } /* for */
  568. if (!PHYSFS_close(f))
  569. {
  570. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  571. return 1; /* oh well. */
  572. } /* if */
  573. printf(" ... test file written ...\n");
  574. f = PHYSFS_openRead("test.txt");
  575. if (f == NULL)
  576. {
  577. printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
  578. return 1;
  579. } /* if */
  580. if (!PHYSFS_setBuffer(f, num))
  581. {
  582. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  583. PHYSFS_close(f);
  584. return 1;
  585. } /* if */
  586. for (i = 0; i < 10; i++)
  587. {
  588. for (j = 0; j < 10000; j++)
  589. {
  590. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  591. PHYSFS_uint32 left = 36 - right;
  592. if (PHYSFS_readBytes(f, buf2, left) != left)
  593. {
  594. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  595. PHYSFS_close(f);
  596. return 1;
  597. } /* if */
  598. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  599. if (rndnum == 42)
  600. {
  601. if (!PHYSFS_flush(f))
  602. {
  603. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  604. PHYSFS_close(f);
  605. return 1;
  606. } /* if */
  607. } /* if */
  608. if (PHYSFS_readBytes(f, buf2 + left, right) != right)
  609. {
  610. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  611. PHYSFS_close(f);
  612. return 1;
  613. } /* if */
  614. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  615. if (rndnum == 42)
  616. {
  617. if (!PHYSFS_flush(f))
  618. {
  619. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  620. PHYSFS_close(f);
  621. return 1;
  622. } /* if */
  623. } /* if */
  624. if (memcmp(buf, buf2, 36) != 0)
  625. {
  626. printf("readback is mismatched on iterations (%d, %d).\n", i, j);
  627. printf("wanted: [");
  628. for (i = 0; i < 36; i++)
  629. printf("%c", buf[i]);
  630. printf("]\n");
  631. printf(" got: [");
  632. for (i = 0; i < 36; i++)
  633. printf("%c", buf2[i]);
  634. printf("]\n");
  635. PHYSFS_close(f);
  636. return 1;
  637. } /* if */
  638. } /* for */
  639. if (!PHYSFS_flush(f))
  640. {
  641. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  642. PHYSFS_close(f);
  643. return 1;
  644. } /* if */
  645. } /* for */
  646. printf(" ... test file read ...\n");
  647. if (!PHYSFS_eof(f))
  648. printf("PHYSFS_eof() returned true! That's wrong.\n");
  649. if (!PHYSFS_close(f))
  650. {
  651. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  652. return 1; /* oh well. */
  653. } /* if */
  654. PHYSFS_delete("test.txt");
  655. printf("stress test completed successfully.\n");
  656. } /* else */
  657. } /* else */
  658. return 1;
  659. } /* cmd_stressbuffer */
  660. static int cmd_setsaneconfig(char *args)
  661. {
  662. char *org;
  663. char *appName;
  664. char *arcExt;
  665. int inclCD;
  666. int arcsFirst;
  667. char *ptr = args;
  668. /* ugly. */
  669. org = ptr;
  670. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  671. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  672. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  673. arcsFirst = atoi(ptr);
  674. if (strcmp(arcExt, "!") == 0)
  675. arcExt = NULL;
  676. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  677. printf("Successful.\n");
  678. else
  679. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  680. return 1;
  681. } /* cmd_setsaneconfig */
  682. static int cmd_mkdir(char *args)
  683. {
  684. if (*args == '\"')
  685. {
  686. args++;
  687. args[strlen(args) - 1] = '\0';
  688. } /* if */
  689. if (PHYSFS_mkdir(args))
  690. printf("Successful.\n");
  691. else
  692. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  693. return 1;
  694. } /* cmd_mkdir */
  695. static int cmd_delete(char *args)
  696. {
  697. if (*args == '\"')
  698. {
  699. args++;
  700. args[strlen(args) - 1] = '\0';
  701. } /* if */
  702. if (PHYSFS_delete(args))
  703. printf("Successful.\n");
  704. else
  705. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  706. return 1;
  707. } /* cmd_delete */
  708. static int cmd_getrealdir(char *args)
  709. {
  710. const char *rc;
  711. if (*args == '\"')
  712. {
  713. args++;
  714. args[strlen(args) - 1] = '\0';
  715. } /* if */
  716. rc = PHYSFS_getRealDir(args);
  717. if (rc)
  718. printf("Found at [%s].\n", rc);
  719. else
  720. printf("Not found.\n");
  721. return 1;
  722. } /* cmd_getrealdir */
  723. static int cmd_exists(char *args)
  724. {
  725. int rc;
  726. if (*args == '\"')
  727. {
  728. args++;
  729. args[strlen(args) - 1] = '\0';
  730. } /* if */
  731. rc = PHYSFS_exists(args);
  732. printf("File %sexists.\n", rc ? "" : "does not ");
  733. return 1;
  734. } /* cmd_exists */
  735. static int cmd_isdir(char *args)
  736. {
  737. PHYSFS_Stat statbuf;
  738. int rc;
  739. if (*args == '\"')
  740. {
  741. args++;
  742. args[strlen(args) - 1] = '\0';
  743. } /* if */
  744. rc = PHYSFS_stat(args, &statbuf);
  745. if (rc)
  746. rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  747. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  748. return 1;
  749. } /* cmd_isdir */
  750. static int cmd_issymlink(char *args)
  751. {
  752. PHYSFS_Stat statbuf;
  753. int rc;
  754. if (*args == '\"')
  755. {
  756. args++;
  757. args[strlen(args) - 1] = '\0';
  758. } /* if */
  759. rc = PHYSFS_stat(args, &statbuf);
  760. if (rc)
  761. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  762. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  763. return 1;
  764. } /* cmd_issymlink */
  765. static int cmd_cat(char *args)
  766. {
  767. PHYSFS_File *f;
  768. if (*args == '\"')
  769. {
  770. args++;
  771. args[strlen(args) - 1] = '\0';
  772. } /* if */
  773. f = PHYSFS_openRead(args);
  774. if (f == NULL)
  775. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  776. else
  777. {
  778. if (do_buffer_size)
  779. {
  780. if (!PHYSFS_setBuffer(f, do_buffer_size))
  781. {
  782. printf("failed to set file buffer. Reason: [%s].\n",
  783. PHYSFS_getLastError());
  784. PHYSFS_close(f);
  785. return 1;
  786. } /* if */
  787. } /* if */
  788. while (1)
  789. {
  790. char buffer[128];
  791. PHYSFS_sint64 rc;
  792. PHYSFS_sint64 i;
  793. rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
  794. for (i = 0; i < rc; i++)
  795. fputc((int) buffer[i], stdout);
  796. if (rc < sizeof (buffer))
  797. {
  798. printf("\n\n");
  799. if (!PHYSFS_eof(f))
  800. {
  801. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  802. PHYSFS_getLastError());
  803. } /* if */
  804. PHYSFS_close(f);
  805. return 1;
  806. } /* if */
  807. } /* while */
  808. } /* else */
  809. return 1;
  810. } /* cmd_cat */
  811. static int cmd_cat2(char *args)
  812. {
  813. PHYSFS_File *f1 = NULL;
  814. PHYSFS_File *f2 = NULL;
  815. char *fname1;
  816. char *fname2;
  817. char *ptr;
  818. fname1 = args;
  819. if (*fname1 == '\"')
  820. {
  821. fname1++;
  822. ptr = strchr(fname1, '\"');
  823. if (ptr == NULL)
  824. {
  825. printf("missing string terminator in argument.\n");
  826. return 1;
  827. } /* if */
  828. *(ptr) = '\0';
  829. } /* if */
  830. else
  831. {
  832. ptr = strchr(fname1, ' ');
  833. *ptr = '\0';
  834. } /* else */
  835. fname2 = ptr + 1;
  836. if (*fname2 == '\"')
  837. {
  838. fname2++;
  839. ptr = strchr(fname2, '\"');
  840. if (ptr == NULL)
  841. {
  842. printf("missing string terminator in argument.\n");
  843. return 1;
  844. } /* if */
  845. *(ptr) = '\0';
  846. } /* if */
  847. if ((f1 = PHYSFS_openRead(fname1)) == NULL)
  848. printf("failed to open '%s'. Reason: [%s].\n", fname1, PHYSFS_getLastError());
  849. else if ((f2 = PHYSFS_openRead(fname2)) == NULL)
  850. printf("failed to open '%s'. Reason: [%s].\n", fname2, PHYSFS_getLastError());
  851. else
  852. {
  853. char *buffer1 = NULL;
  854. size_t buffer1len = 0;
  855. char *buffer2 = NULL;
  856. size_t buffer2len = 0;
  857. char *ptr = NULL;
  858. size_t i;
  859. if (do_buffer_size)
  860. {
  861. if (!PHYSFS_setBuffer(f1, do_buffer_size))
  862. {
  863. printf("failed to set file buffer for '%s'. Reason: [%s].\n",
  864. fname1, PHYSFS_getLastError());
  865. PHYSFS_close(f1);
  866. PHYSFS_close(f2);
  867. return 1;
  868. } /* if */
  869. else if (!PHYSFS_setBuffer(f2, do_buffer_size))
  870. {
  871. printf("failed to set file buffer for '%s'. Reason: [%s].\n",
  872. fname2, PHYSFS_getLastError());
  873. PHYSFS_close(f1);
  874. PHYSFS_close(f2);
  875. return 1;
  876. } /* if */
  877. } /* if */
  878. do
  879. {
  880. int readlen = 128;
  881. PHYSFS_sint64 rc;
  882. ptr = realloc(buffer1, buffer1len + readlen);
  883. if (!ptr)
  884. {
  885. printf("(Out of memory.)\n\n");
  886. free(buffer1);
  887. free(buffer2);
  888. PHYSFS_close(f1);
  889. PHYSFS_close(f2);
  890. return 1;
  891. } /* if */
  892. buffer1 = ptr;
  893. rc = PHYSFS_readBytes(f1, buffer1 + buffer1len, readlen);
  894. if (rc < 0)
  895. {
  896. printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
  897. fname1, PHYSFS_getLastError());
  898. free(buffer1);
  899. free(buffer2);
  900. PHYSFS_close(f1);
  901. PHYSFS_close(f2);
  902. return 1;
  903. } /* if */
  904. buffer1len += (size_t) rc;
  905. ptr = realloc(buffer2, buffer2len + readlen);
  906. if (!ptr)
  907. {
  908. printf("(Out of memory.)\n\n");
  909. free(buffer1);
  910. free(buffer2);
  911. PHYSFS_close(f1);
  912. PHYSFS_close(f2);
  913. return 1;
  914. } /* if */
  915. buffer2 = ptr;
  916. rc = PHYSFS_readBytes(f2, buffer2 + buffer2len, readlen);
  917. if (rc < 0)
  918. {
  919. printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
  920. fname2, PHYSFS_getLastError());
  921. free(buffer1);
  922. free(buffer2);
  923. PHYSFS_close(f1);
  924. PHYSFS_close(f2);
  925. return 1;
  926. } /* if */
  927. buffer2len += (size_t) rc;
  928. } while (!PHYSFS_eof(f1) || !PHYSFS_eof(f2));
  929. printf("file '%s' ...\n\n", fname1);
  930. for (i = 0; i < buffer1len; i++)
  931. fputc((int) buffer1[i], stdout);
  932. free(buffer1);
  933. printf("\n\nfile '%s' ...\n\n", fname2);
  934. for (i = 0; i < buffer2len; i++)
  935. fputc((int) buffer2[i], stdout);
  936. free(buffer2);
  937. printf("\n\n");
  938. } /* else */
  939. if (f1)
  940. PHYSFS_close(f1);
  941. if (f2)
  942. PHYSFS_close(f2);
  943. return 1;
  944. } /* cmd_cat2 */
  945. #define CRC32_BUFFERSIZE 512
  946. static int cmd_crc32(char *args)
  947. {
  948. PHYSFS_File *f;
  949. if (*args == '\"')
  950. {
  951. args++;
  952. args[strlen(args) - 1] = '\0';
  953. } /* if */
  954. f = PHYSFS_openRead(args);
  955. if (f == NULL)
  956. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  957. else
  958. {
  959. PHYSFS_uint8 buffer[CRC32_BUFFERSIZE];
  960. PHYSFS_uint32 crc = -1;
  961. PHYSFS_sint64 bytesread;
  962. while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
  963. {
  964. PHYSFS_uint32 i, bit;
  965. for (i = 0; i < bytesread; i++)
  966. {
  967. for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
  968. crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
  969. } /* for */
  970. } /* while */
  971. if (bytesread < 0)
  972. {
  973. printf("error while reading. Reason: [%s].\n",
  974. PHYSFS_getLastError());
  975. return 1;
  976. } /* if */
  977. PHYSFS_close(f);
  978. crc ^= -1;
  979. printf("CRC32 for %s: 0x%08X\n", args, crc);
  980. } /* else */
  981. return 1;
  982. } /* cmd_crc32 */
  983. static int cmd_filelength(char *args)
  984. {
  985. PHYSFS_File *f;
  986. if (*args == '\"')
  987. {
  988. args++;
  989. args[strlen(args) - 1] = '\0';
  990. } /* if */
  991. f = PHYSFS_openRead(args);
  992. if (f == NULL)
  993. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  994. else
  995. {
  996. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  997. if (len == -1)
  998. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  999. else
  1000. printf(" (cast to int) %d bytes.\n", (int) len);
  1001. PHYSFS_close(f);
  1002. } /* else */
  1003. return 1;
  1004. } /* cmd_filelength */
  1005. #define WRITESTR "The cat sat on the mat.\n\n"
  1006. static int cmd_append(char *args)
  1007. {
  1008. PHYSFS_File *f;
  1009. if (*args == '\"')
  1010. {
  1011. args++;
  1012. args[strlen(args) - 1] = '\0';
  1013. } /* if */
  1014. f = PHYSFS_openAppend(args);
  1015. if (f == NULL)
  1016. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  1017. else
  1018. {
  1019. size_t bw;
  1020. PHYSFS_sint64 rc;
  1021. if (do_buffer_size)
  1022. {
  1023. if (!PHYSFS_setBuffer(f, do_buffer_size))
  1024. {
  1025. printf("failed to set file buffer. Reason: [%s].\n",
  1026. PHYSFS_getLastError());
  1027. PHYSFS_close(f);
  1028. return 1;
  1029. } /* if */
  1030. } /* if */
  1031. bw = strlen(WRITESTR);
  1032. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  1033. if (rc != bw)
  1034. {
  1035. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  1036. (int) rc, (int) bw, PHYSFS_getLastError());
  1037. } /* if */
  1038. else
  1039. {
  1040. printf("Successful.\n");
  1041. } /* else */
  1042. PHYSFS_close(f);
  1043. } /* else */
  1044. return 1;
  1045. } /* cmd_append */
  1046. static int cmd_write(char *args)
  1047. {
  1048. PHYSFS_File *f;
  1049. if (*args == '\"')
  1050. {
  1051. args++;
  1052. args[strlen(args) - 1] = '\0';
  1053. } /* if */
  1054. f = PHYSFS_openWrite(args);
  1055. if (f == NULL)
  1056. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  1057. else
  1058. {
  1059. size_t bw;
  1060. PHYSFS_sint64 rc;
  1061. if (do_buffer_size)
  1062. {
  1063. if (!PHYSFS_setBuffer(f, do_buffer_size))
  1064. {
  1065. printf("failed to set file buffer. Reason: [%s].\n",
  1066. PHYSFS_getLastError());
  1067. PHYSFS_close(f);
  1068. return 1;
  1069. } /* if */
  1070. } /* if */
  1071. bw = strlen(WRITESTR);
  1072. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  1073. if (rc != bw)
  1074. {
  1075. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  1076. (int) rc, (int) bw, PHYSFS_getLastError());
  1077. } /* if */
  1078. else
  1079. {
  1080. printf("Successful.\n");
  1081. } /* else */
  1082. PHYSFS_close(f);
  1083. } /* else */
  1084. return 1;
  1085. } /* cmd_write */
  1086. static char* modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  1087. {
  1088. if (modtime < 0)
  1089. strncpy(modstr, "Unknown\n", strsize);
  1090. else
  1091. {
  1092. time_t t = (time_t) modtime;
  1093. char *str = ctime(&t);
  1094. strncpy(modstr, str, strsize);
  1095. } /* else */
  1096. modstr[strsize-1] = '\0';
  1097. return modstr;
  1098. } /* modTimeToStr */
  1099. static int cmd_getlastmodtime(char *args)
  1100. {
  1101. PHYSFS_Stat statbuf;
  1102. if (!PHYSFS_stat(args, &statbuf))
  1103. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  1104. else
  1105. {
  1106. char modstr[64];
  1107. modTimeToStr(statbuf.modtime, modstr, sizeof (modstr));
  1108. printf("Last modified: %s (%ld).\n", modstr, (long) statbuf.modtime);
  1109. } /* else */
  1110. return 1;
  1111. } /* cmd_getLastModTime */
  1112. static int cmd_stat(char *args)
  1113. {
  1114. PHYSFS_Stat stat;
  1115. char timestring[65];
  1116. if (*args == '\"')
  1117. {
  1118. args++;
  1119. args[strlen(args) - 1] = '\0';
  1120. } /* if */
  1121. if(!PHYSFS_stat(args, &stat))
  1122. {
  1123. printf("failed to stat. Reason [%s].\n", PHYSFS_getLastError());
  1124. return 1;
  1125. } /* if */
  1126. printf("Filename: %s\n", args);
  1127. printf("Size %d\n",(int) stat.filesize);
  1128. if(stat.filetype == PHYSFS_FILETYPE_REGULAR)
  1129. printf("Type: File\n");
  1130. else if(stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
  1131. printf("Type: Directory\n");
  1132. else if(stat.filetype == PHYSFS_FILETYPE_SYMLINK)
  1133. printf("Type: Symlink\n");
  1134. else
  1135. printf("Type: Unknown\n");
  1136. printf("Created at: %s", modTimeToStr(stat.createtime, timestring, 64));
  1137. printf("Last modified at: %s", modTimeToStr(stat.modtime, timestring, 64));
  1138. printf("Last accessed at: %s", modTimeToStr(stat.accesstime, timestring, 64));
  1139. printf("Readonly: %s\n", stat.readonly ? "true" : "false");
  1140. return 1;
  1141. } /* cmd_filelength */
  1142. /* must have spaces trimmed prior to this call. */
  1143. static int count_args(const char *str)
  1144. {
  1145. int retval = 0;
  1146. int in_quotes = 0;
  1147. if (str != NULL)
  1148. {
  1149. for (; *str != '\0'; str++)
  1150. {
  1151. if (*str == '\"')
  1152. in_quotes = !in_quotes;
  1153. else if ((*str == ' ') && (!in_quotes))
  1154. retval++;
  1155. } /* for */
  1156. retval++;
  1157. } /* if */
  1158. return retval;
  1159. } /* count_args */
  1160. static int cmd_help(char *args);
  1161. typedef struct
  1162. {
  1163. const char *cmd;
  1164. int (*func)(char *args);
  1165. int argcount;
  1166. const char *usage;
  1167. } command_info;
  1168. static const command_info commands[] =
  1169. {
  1170. { "quit", cmd_quit, 0, NULL },
  1171. { "q", cmd_quit, 0, NULL },
  1172. { "help", cmd_help, 0, NULL },
  1173. { "init", cmd_init, 1, "<argv0>" },
  1174. { "deinit", cmd_deinit, 0, NULL },
  1175. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  1176. { "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
  1177. { "mountmem", cmd_mount_mem, 3, "<archiveLocation> <mntpoint> <append>" },
  1178. { "mounthandle", cmd_mount_handle, 3, "<archiveLocation> <mntpoint> <append>" },
  1179. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  1180. { "unmount", cmd_removearchive, 1, "<archiveLocation>" },
  1181. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  1182. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  1183. { "tree", cmd_tree, 1, "<dirToEnumerate>" },
  1184. { "getlasterror", cmd_getlasterror, 0, NULL },
  1185. { "getdirsep", cmd_getdirsep, 0, NULL },
  1186. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  1187. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  1188. { "getbasedir", cmd_getbasedir, 0, NULL },
  1189. { "getuserdir", cmd_getuserdir, 0, NULL },
  1190. { "getprefdir", cmd_getprefdir, 2, "<org> <app>" },
  1191. { "getwritedir", cmd_getwritedir, 0, NULL },
  1192. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  1193. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  1194. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  1195. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  1196. { "delete", cmd_delete, 1, "<dirToDelete>" },
  1197. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  1198. { "exists", cmd_exists, 1, "<fileToCheck>" },
  1199. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  1200. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  1201. { "cat", cmd_cat, 1, "<fileToCat>" },
  1202. { "cat2", cmd_cat2, 2, "<fileToCat1> <fileToCat2>" },
  1203. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  1204. { "stat", cmd_stat, 1, "<fileToStat>" },
  1205. { "append", cmd_append, 1, "<fileToAppend>" },
  1206. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  1207. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  1208. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  1209. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  1210. { "crc32", cmd_crc32, 1, "<fileToHash>" },
  1211. { "getmountpoint", cmd_getmountpoint, 1, "<dir>" },
  1212. { "setroot", cmd_setroot, 2, "<archiveLocation> <root>" },
  1213. { NULL, NULL, -1, NULL }
  1214. };
  1215. static void output_usage(const char *intro, const command_info *cmdinfo)
  1216. {
  1217. if (cmdinfo->argcount == 0)
  1218. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  1219. else
  1220. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  1221. } /* output_usage */
  1222. static int cmd_help(char *args)
  1223. {
  1224. const command_info *i;
  1225. printf("Commands:\n");
  1226. for (i = commands; i->cmd != NULL; i++)
  1227. output_usage(" -", i);
  1228. return 1;
  1229. } /* output_cmd_help */
  1230. static void trim_command(const char *orig, char *copy)
  1231. {
  1232. const char *i;
  1233. char *writeptr = copy;
  1234. int spacecount = 0;
  1235. int have_first = 0;
  1236. for (i = orig; *i != '\0'; i++)
  1237. {
  1238. if (*i == ' ')
  1239. {
  1240. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  1241. {
  1242. if ((have_first) && (!spacecount))
  1243. {
  1244. spacecount++;
  1245. *writeptr = ' ';
  1246. writeptr++;
  1247. } /* if */
  1248. } /* if */
  1249. } /* if */
  1250. else
  1251. {
  1252. have_first = 1;
  1253. spacecount = 0;
  1254. *writeptr = *i;
  1255. writeptr++;
  1256. } /* else */
  1257. } /* for */
  1258. *writeptr = '\0';
  1259. /*
  1260. printf("\n command is [%s].\n", copy);
  1261. */
  1262. } /* trim_command */
  1263. static int process_command(char *complete_cmd)
  1264. {
  1265. const command_info *i;
  1266. char *cmd_copy;
  1267. char *args;
  1268. int rc = 1;
  1269. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  1270. {
  1271. printf("\n");
  1272. return 0;
  1273. } /* if */
  1274. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  1275. if (cmd_copy == NULL)
  1276. {
  1277. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  1278. return 0;
  1279. } /* if */
  1280. trim_command(complete_cmd, cmd_copy);
  1281. args = strchr(cmd_copy, ' ');
  1282. if (args != NULL)
  1283. {
  1284. *args = '\0';
  1285. args++;
  1286. } /* else */
  1287. if (cmd_copy[0] != '\0')
  1288. {
  1289. for (i = commands; i->cmd != NULL; i++)
  1290. {
  1291. if (strcmp(i->cmd, cmd_copy) == 0)
  1292. {
  1293. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  1294. output_usage("usage:", i);
  1295. else
  1296. rc = i->func(args);
  1297. break;
  1298. } /* if */
  1299. } /* for */
  1300. if (i->cmd == NULL)
  1301. printf("Unknown command. Enter \"help\" for instructions.\n");
  1302. #if (defined PHYSFS_HAVE_READLINE)
  1303. add_history(complete_cmd);
  1304. if (history_file)
  1305. {
  1306. fprintf(history_file, "%s\n", complete_cmd);
  1307. fflush(history_file);
  1308. } /* if */
  1309. #endif
  1310. } /* if */
  1311. free(cmd_copy);
  1312. return rc;
  1313. } /* process_command */
  1314. static void open_history_file(void)
  1315. {
  1316. #if (defined PHYSFS_HAVE_READLINE)
  1317. #if 0
  1318. const char *envr = getenv("TESTPHYSFS_HISTORY");
  1319. if (!envr)
  1320. return;
  1321. #else
  1322. char envr[256];
  1323. strcpy(envr, PHYSFS_getUserDir());
  1324. strcat(envr, ".testphys_history");
  1325. #endif
  1326. if (access(envr, F_OK) == 0)
  1327. {
  1328. char buf[512];
  1329. FILE *f = fopen(envr, "r");
  1330. if (!f)
  1331. {
  1332. printf("\n\n"
  1333. "Could not open history file [%s] for reading!\n"
  1334. " Will not have past history available.\n\n",
  1335. envr);
  1336. return;
  1337. } /* if */
  1338. do
  1339. {
  1340. if (fgets(buf, sizeof (buf), f) == NULL)
  1341. break;
  1342. if (buf[strlen(buf) - 1] == '\n')
  1343. buf[strlen(buf) - 1] = '\0';
  1344. add_history(buf);
  1345. } while (!feof(f));
  1346. fclose(f);
  1347. } /* if */
  1348. history_file = fopen(envr, "ab");
  1349. if (!history_file)
  1350. {
  1351. printf("\n\n"
  1352. "Could not open history file [%s] for appending!\n"
  1353. " Will not be able to record this session's history.\n\n",
  1354. envr);
  1355. } /* if */
  1356. #endif
  1357. } /* open_history_file */
  1358. int main(int argc, char **argv)
  1359. {
  1360. char *buf = NULL;
  1361. int rc = 1;
  1362. int i;
  1363. #if (defined __MWERKS__)
  1364. extern tSIOUXSettings SIOUXSettings;
  1365. SIOUXSettings.asktosaveonclose = 0;
  1366. SIOUXSettings.autocloseonquit = 1;
  1367. SIOUXSettings.rows = 40;
  1368. SIOUXSettings.columns = 120;
  1369. #endif
  1370. printf("\n");
  1371. if (!PHYSFS_init(argv[0]))
  1372. {
  1373. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1374. return 1;
  1375. } /* if */
  1376. output_versions();
  1377. output_archivers();
  1378. open_history_file();
  1379. printf("Enter commands. Enter \"help\" for instructions.\n");
  1380. fflush(stdout);
  1381. for (i = 1; i < argc && rc; i++) {
  1382. rc = process_command(argv[i]);
  1383. }
  1384. while (rc)
  1385. {
  1386. #if (defined PHYSFS_HAVE_READLINE)
  1387. buf = readline("> ");
  1388. #else
  1389. int i;
  1390. buf = (char *) malloc(512);
  1391. memset(buf, '\0', 512);
  1392. printf("> ");
  1393. fflush(stdout);
  1394. for (i = 0; i < 511; i++)
  1395. {
  1396. int ch = fgetc(stdin);
  1397. if (ch == EOF)
  1398. {
  1399. strcpy(buf, "quit");
  1400. break;
  1401. } /* if */
  1402. else if ((ch == '\n') || (ch == '\r'))
  1403. {
  1404. buf[i] = '\0';
  1405. break;
  1406. } /* else if */
  1407. else if (ch == '\b')
  1408. {
  1409. if (i > 0)
  1410. i--;
  1411. } /* else if */
  1412. else
  1413. {
  1414. buf[i] = (char) ch;
  1415. } /* else */
  1416. } /* for */
  1417. #endif
  1418. rc = process_command(buf);
  1419. fflush(stdout);
  1420. if (buf != NULL)
  1421. free(buf);
  1422. }
  1423. if (!PHYSFS_deinit())
  1424. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1425. if (history_file)
  1426. fclose(history_file);
  1427. /*
  1428. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  1429. printf(" it makes you shoot teh railgun bettar.\n");
  1430. */
  1431. return 0;
  1432. } /* main */
  1433. /* end of test_physfs.c ... */