test_physfs.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. char **i;
  326. printf("\n");
  327. for (i = rc; *i != NULL; i++) {
  328. char *newFullPath = malloc(strlen(fullPath) + strlen(*i) + 2);
  329. char *thisPrefix;
  330. strcpy(newFullPath, fullPath);
  331. strcat(newFullPath, "/");
  332. strcat(newFullPath, *i);
  333. if (i[1]) {
  334. thisPrefix = PREFIX_DIRENTRY;
  335. } else {
  336. thisPrefix = PREFIX_DIRENTRY_LAST;
  337. }
  338. if (PHYSFS_isSymbolicLink(newFullPath)) {
  339. printf("%s%s%s [symbolic link]\n", prefix, thisPrefix, *i);
  340. } else if (PHYSFS_isDirectory(newFullPath)) {
  341. char *newPrefix;
  342. *total_dir_count += 1;
  343. if (i[1]) {
  344. newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE) + 1);
  345. strcpy(newPrefix, prefix);
  346. strcat(newPrefix, PREFIX_RECURSIVE);
  347. } else {
  348. newPrefix = malloc(strlen(prefix) + strlen(PREFIX_RECURSIVE_LAST) + 1);
  349. strcpy(newPrefix, prefix);
  350. strcat(newPrefix, PREFIX_RECURSIVE_LAST);
  351. }
  352. printf("%s%s", prefix, thisPrefix);
  353. cmd_tree_recursive(newPrefix, newFullPath, *i, depth + 1, total_dir_count, total_file_count);
  354. free(newPrefix);
  355. } else {
  356. *total_file_count += 1;
  357. printf("%s%s%s\n", prefix, thisPrefix, *i);
  358. }
  359. free(newFullPath);
  360. }
  361. PHYSFS_freeList(rc);
  362. } /* else */
  363. } /* cmd_tree_recursive */
  364. static int cmd_tree(char *args)
  365. {
  366. int total_dir_count = 0, total_file_count = 0; /* FIXME: should be PHYSFS_uint64 */
  367. if (*args == '\"')
  368. {
  369. args++;
  370. args[strlen(args) - 1] = '\0';
  371. } /* if */
  372. cmd_tree_recursive("", args, args, 0, &total_dir_count, &total_file_count);
  373. printf("\n%d directories, %d files\n", total_dir_count, total_file_count);
  374. return 1;
  375. } /* cmd_enumerate */
  376. static int cmd_getdirsep(char *args)
  377. {
  378. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  379. return 1;
  380. } /* cmd_getdirsep */
  381. static int cmd_getlasterror(char *args)
  382. {
  383. printf("last error is [%s].\n", PHYSFS_getLastError());
  384. return 1;
  385. } /* cmd_getlasterror */
  386. static int cmd_getcdromdirs(char *args)
  387. {
  388. char **rc = PHYSFS_getCdRomDirs();
  389. if (rc == NULL)
  390. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  391. else
  392. {
  393. int dir_count;
  394. char **i;
  395. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  396. printf("%s\n", *i);
  397. printf("\n total (%d) drives.\n", dir_count);
  398. PHYSFS_freeList(rc);
  399. } /* else */
  400. return 1;
  401. } /* cmd_getcdromdirs */
  402. static int cmd_getsearchpath(char *args)
  403. {
  404. char **rc = PHYSFS_getSearchPath();
  405. if (rc == NULL)
  406. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  407. else
  408. {
  409. int dir_count;
  410. char **i;
  411. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  412. printf("%s\n", *i);
  413. printf("\n total (%d) directories.\n", dir_count);
  414. PHYSFS_freeList(rc);
  415. } /* else */
  416. return 1;
  417. } /* cmd_getcdromdirs */
  418. static int cmd_getbasedir(char *args)
  419. {
  420. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  421. return 1;
  422. } /* cmd_getbasedir */
  423. static int cmd_getuserdir(char *args)
  424. {
  425. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  426. return 1;
  427. } /* cmd_getuserdir */
  428. static int cmd_getprefdir(char *args)
  429. {
  430. char *org;
  431. char *appName;
  432. char *ptr = args;
  433. org = ptr;
  434. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  435. printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
  436. return 1;
  437. } /* cmd_getprefdir */
  438. static int cmd_getwritedir(char *args)
  439. {
  440. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  441. return 1;
  442. } /* cmd_getwritedir */
  443. static int cmd_setwritedir(char *args)
  444. {
  445. if (*args == '\"')
  446. {
  447. args++;
  448. args[strlen(args) - 1] = '\0';
  449. } /* if */
  450. if (PHYSFS_setWriteDir(args))
  451. printf("Successful.\n");
  452. else
  453. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  454. return 1;
  455. } /* cmd_setwritedir */
  456. static int cmd_permitsyms(char *args)
  457. {
  458. int num;
  459. if (*args == '\"')
  460. {
  461. args++;
  462. args[strlen(args) - 1] = '\0';
  463. } /* if */
  464. num = atoi(args);
  465. PHYSFS_permitSymbolicLinks(num);
  466. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  467. return 1;
  468. } /* cmd_permitsyms */
  469. static int cmd_setbuffer(char *args)
  470. {
  471. if (*args == '\"')
  472. {
  473. args++;
  474. args[strlen(args) - 1] = '\0';
  475. } /* if */
  476. do_buffer_size = (unsigned int) atoi(args);
  477. if (do_buffer_size)
  478. {
  479. printf("Further tests will set a (%lu) size buffer.\n",
  480. (unsigned long) do_buffer_size);
  481. } /* if */
  482. else
  483. {
  484. printf("Further tests will NOT use a buffer.\n");
  485. } /* else */
  486. return 1;
  487. } /* cmd_setbuffer */
  488. static int cmd_stressbuffer(char *args)
  489. {
  490. int num;
  491. if (*args == '\"')
  492. {
  493. args++;
  494. args[strlen(args) - 1] = '\0';
  495. } /* if */
  496. num = atoi(args);
  497. if (num < 0)
  498. printf("buffer must be greater than or equal to zero.\n");
  499. else
  500. {
  501. PHYSFS_File *f;
  502. int rndnum;
  503. printf("Stress testing with (%d) byte buffer...\n", num);
  504. f = PHYSFS_openWrite("test.txt");
  505. if (f == NULL)
  506. printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
  507. else
  508. {
  509. int i, j;
  510. char buf[37];
  511. char buf2[37];
  512. if (!PHYSFS_setBuffer(f, num))
  513. {
  514. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  515. PHYSFS_close(f);
  516. PHYSFS_delete("test.txt");
  517. return 1;
  518. } /* if */
  519. strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
  520. srand((unsigned int) time(NULL));
  521. for (i = 0; i < 10; i++)
  522. {
  523. for (j = 0; j < 10000; j++)
  524. {
  525. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  526. PHYSFS_uint32 left = 36 - right;
  527. if (PHYSFS_writeBytes(f, buf, left) != left)
  528. {
  529. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  530. PHYSFS_close(f);
  531. return 1;
  532. } /* if */
  533. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  534. if (rndnum == 42)
  535. {
  536. if (!PHYSFS_flush(f))
  537. {
  538. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  539. PHYSFS_close(f);
  540. return 1;
  541. } /* if */
  542. } /* if */
  543. if (PHYSFS_writeBytes(f, buf + left, right) != right)
  544. {
  545. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  546. PHYSFS_close(f);
  547. return 1;
  548. } /* if */
  549. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  550. if (rndnum == 42)
  551. {
  552. if (!PHYSFS_flush(f))
  553. {
  554. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  555. PHYSFS_close(f);
  556. return 1;
  557. } /* if */
  558. } /* if */
  559. } /* for */
  560. if (!PHYSFS_flush(f))
  561. {
  562. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  563. PHYSFS_close(f);
  564. return 1;
  565. } /* if */
  566. } /* for */
  567. if (!PHYSFS_close(f))
  568. {
  569. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  570. return 1; /* oh well. */
  571. } /* if */
  572. printf(" ... test file written ...\n");
  573. f = PHYSFS_openRead("test.txt");
  574. if (f == NULL)
  575. {
  576. printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
  577. return 1;
  578. } /* if */
  579. if (!PHYSFS_setBuffer(f, num))
  580. {
  581. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  582. PHYSFS_close(f);
  583. return 1;
  584. } /* if */
  585. for (i = 0; i < 10; i++)
  586. {
  587. for (j = 0; j < 10000; j++)
  588. {
  589. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  590. PHYSFS_uint32 left = 36 - right;
  591. if (PHYSFS_readBytes(f, buf2, left) != left)
  592. {
  593. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  594. PHYSFS_close(f);
  595. return 1;
  596. } /* if */
  597. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  598. if (rndnum == 42)
  599. {
  600. if (!PHYSFS_flush(f))
  601. {
  602. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  603. PHYSFS_close(f);
  604. return 1;
  605. } /* if */
  606. } /* if */
  607. if (PHYSFS_readBytes(f, buf2 + left, right) != right)
  608. {
  609. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  610. PHYSFS_close(f);
  611. return 1;
  612. } /* if */
  613. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  614. if (rndnum == 42)
  615. {
  616. if (!PHYSFS_flush(f))
  617. {
  618. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  619. PHYSFS_close(f);
  620. return 1;
  621. } /* if */
  622. } /* if */
  623. if (memcmp(buf, buf2, 36) != 0)
  624. {
  625. printf("readback is mismatched on iterations (%d, %d).\n", i, j);
  626. printf("wanted: [");
  627. for (i = 0; i < 36; i++)
  628. printf("%c", buf[i]);
  629. printf("]\n");
  630. printf(" got: [");
  631. for (i = 0; i < 36; i++)
  632. printf("%c", buf2[i]);
  633. printf("]\n");
  634. PHYSFS_close(f);
  635. return 1;
  636. } /* if */
  637. } /* for */
  638. if (!PHYSFS_flush(f))
  639. {
  640. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  641. PHYSFS_close(f);
  642. return 1;
  643. } /* if */
  644. } /* for */
  645. printf(" ... test file read ...\n");
  646. if (!PHYSFS_eof(f))
  647. printf("PHYSFS_eof() returned true! That's wrong.\n");
  648. if (!PHYSFS_close(f))
  649. {
  650. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  651. return 1; /* oh well. */
  652. } /* if */
  653. PHYSFS_delete("test.txt");
  654. printf("stress test completed successfully.\n");
  655. } /* else */
  656. } /* else */
  657. return 1;
  658. } /* cmd_stressbuffer */
  659. static int cmd_setsaneconfig(char *args)
  660. {
  661. char *org;
  662. char *appName;
  663. char *arcExt;
  664. int inclCD;
  665. int arcsFirst;
  666. char *ptr = args;
  667. /* ugly. */
  668. org = ptr;
  669. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  670. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  671. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  672. arcsFirst = atoi(ptr);
  673. if (strcmp(arcExt, "!") == 0)
  674. arcExt = NULL;
  675. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  676. printf("Successful.\n");
  677. else
  678. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  679. return 1;
  680. } /* cmd_setsaneconfig */
  681. static int cmd_mkdir(char *args)
  682. {
  683. if (*args == '\"')
  684. {
  685. args++;
  686. args[strlen(args) - 1] = '\0';
  687. } /* if */
  688. if (PHYSFS_mkdir(args))
  689. printf("Successful.\n");
  690. else
  691. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  692. return 1;
  693. } /* cmd_mkdir */
  694. static int cmd_delete(char *args)
  695. {
  696. if (*args == '\"')
  697. {
  698. args++;
  699. args[strlen(args) - 1] = '\0';
  700. } /* if */
  701. if (PHYSFS_delete(args))
  702. printf("Successful.\n");
  703. else
  704. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  705. return 1;
  706. } /* cmd_delete */
  707. static int cmd_getrealdir(char *args)
  708. {
  709. const char *rc;
  710. if (*args == '\"')
  711. {
  712. args++;
  713. args[strlen(args) - 1] = '\0';
  714. } /* if */
  715. rc = PHYSFS_getRealDir(args);
  716. if (rc)
  717. printf("Found at [%s].\n", rc);
  718. else
  719. printf("Not found.\n");
  720. return 1;
  721. } /* cmd_getrealdir */
  722. static int cmd_exists(char *args)
  723. {
  724. int rc;
  725. if (*args == '\"')
  726. {
  727. args++;
  728. args[strlen(args) - 1] = '\0';
  729. } /* if */
  730. rc = PHYSFS_exists(args);
  731. printf("File %sexists.\n", rc ? "" : "does not ");
  732. return 1;
  733. } /* cmd_exists */
  734. static int cmd_isdir(char *args)
  735. {
  736. PHYSFS_Stat statbuf;
  737. int rc;
  738. if (*args == '\"')
  739. {
  740. args++;
  741. args[strlen(args) - 1] = '\0';
  742. } /* if */
  743. rc = PHYSFS_stat(args, &statbuf);
  744. if (rc)
  745. rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  746. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  747. return 1;
  748. } /* cmd_isdir */
  749. static int cmd_issymlink(char *args)
  750. {
  751. PHYSFS_Stat statbuf;
  752. int rc;
  753. if (*args == '\"')
  754. {
  755. args++;
  756. args[strlen(args) - 1] = '\0';
  757. } /* if */
  758. rc = PHYSFS_stat(args, &statbuf);
  759. if (rc)
  760. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  761. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  762. return 1;
  763. } /* cmd_issymlink */
  764. static int cmd_cat(char *args)
  765. {
  766. PHYSFS_File *f;
  767. if (*args == '\"')
  768. {
  769. args++;
  770. args[strlen(args) - 1] = '\0';
  771. } /* if */
  772. f = PHYSFS_openRead(args);
  773. if (f == NULL)
  774. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  775. else
  776. {
  777. if (do_buffer_size)
  778. {
  779. if (!PHYSFS_setBuffer(f, do_buffer_size))
  780. {
  781. printf("failed to set file buffer. Reason: [%s].\n",
  782. PHYSFS_getLastError());
  783. PHYSFS_close(f);
  784. return 1;
  785. } /* if */
  786. } /* if */
  787. while (1)
  788. {
  789. char buffer[128];
  790. PHYSFS_sint64 rc;
  791. PHYSFS_sint64 i;
  792. rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
  793. for (i = 0; i < rc; i++)
  794. fputc((int) buffer[i], stdout);
  795. if (rc < (PHYSFS_sint64)sizeof (buffer))
  796. {
  797. printf("\n\n");
  798. if (!PHYSFS_eof(f))
  799. {
  800. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  801. PHYSFS_getLastError());
  802. } /* if */
  803. PHYSFS_close(f);
  804. return 1;
  805. } /* if */
  806. } /* while */
  807. } /* else */
  808. return 1;
  809. } /* cmd_cat */
  810. static int cmd_cat2(char *args)
  811. {
  812. PHYSFS_File *f1 = NULL;
  813. PHYSFS_File *f2 = NULL;
  814. char *fname1;
  815. char *fname2;
  816. char *ptr;
  817. fname1 = args;
  818. if (*fname1 == '\"')
  819. {
  820. fname1++;
  821. ptr = strchr(fname1, '\"');
  822. if (ptr == NULL)
  823. {
  824. printf("missing string terminator in argument.\n");
  825. return 1;
  826. } /* if */
  827. *(ptr) = '\0';
  828. } /* if */
  829. else
  830. {
  831. ptr = strchr(fname1, ' ');
  832. *ptr = '\0';
  833. } /* else */
  834. fname2 = ptr + 1;
  835. if (*fname2 == '\"')
  836. {
  837. fname2++;
  838. ptr = strchr(fname2, '\"');
  839. if (ptr == NULL)
  840. {
  841. printf("missing string terminator in argument.\n");
  842. return 1;
  843. } /* if */
  844. *(ptr) = '\0';
  845. } /* if */
  846. if ((f1 = PHYSFS_openRead(fname1)) == NULL)
  847. printf("failed to open '%s'. Reason: [%s].\n", fname1, PHYSFS_getLastError());
  848. else if ((f2 = PHYSFS_openRead(fname2)) == NULL)
  849. printf("failed to open '%s'. Reason: [%s].\n", fname2, PHYSFS_getLastError());
  850. else
  851. {
  852. char *buffer1 = NULL;
  853. size_t buffer1len = 0;
  854. char *buffer2 = NULL;
  855. size_t buffer2len = 0;
  856. char *ptr = NULL;
  857. size_t i;
  858. if (do_buffer_size)
  859. {
  860. if (!PHYSFS_setBuffer(f1, do_buffer_size))
  861. {
  862. printf("failed to set file buffer for '%s'. Reason: [%s].\n",
  863. fname1, PHYSFS_getLastError());
  864. PHYSFS_close(f1);
  865. PHYSFS_close(f2);
  866. return 1;
  867. } /* if */
  868. else if (!PHYSFS_setBuffer(f2, do_buffer_size))
  869. {
  870. printf("failed to set file buffer for '%s'. Reason: [%s].\n",
  871. fname2, PHYSFS_getLastError());
  872. PHYSFS_close(f1);
  873. PHYSFS_close(f2);
  874. return 1;
  875. } /* if */
  876. } /* if */
  877. do
  878. {
  879. int readlen = 128;
  880. PHYSFS_sint64 rc;
  881. ptr = realloc(buffer1, buffer1len + readlen);
  882. if (!ptr)
  883. {
  884. printf("(Out of memory.)\n\n");
  885. free(buffer1);
  886. free(buffer2);
  887. PHYSFS_close(f1);
  888. PHYSFS_close(f2);
  889. return 1;
  890. } /* if */
  891. buffer1 = ptr;
  892. rc = PHYSFS_readBytes(f1, buffer1 + buffer1len, readlen);
  893. if (rc < 0)
  894. {
  895. printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
  896. fname1, PHYSFS_getLastError());
  897. free(buffer1);
  898. free(buffer2);
  899. PHYSFS_close(f1);
  900. PHYSFS_close(f2);
  901. return 1;
  902. } /* if */
  903. buffer1len += (size_t) rc;
  904. ptr = realloc(buffer2, buffer2len + readlen);
  905. if (!ptr)
  906. {
  907. printf("(Out of memory.)\n\n");
  908. free(buffer1);
  909. free(buffer2);
  910. PHYSFS_close(f1);
  911. PHYSFS_close(f2);
  912. return 1;
  913. } /* if */
  914. buffer2 = ptr;
  915. rc = PHYSFS_readBytes(f2, buffer2 + buffer2len, readlen);
  916. if (rc < 0)
  917. {
  918. printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
  919. fname2, PHYSFS_getLastError());
  920. free(buffer1);
  921. free(buffer2);
  922. PHYSFS_close(f1);
  923. PHYSFS_close(f2);
  924. return 1;
  925. } /* if */
  926. buffer2len += (size_t) rc;
  927. } while (!PHYSFS_eof(f1) || !PHYSFS_eof(f2));
  928. printf("file '%s' ...\n\n", fname1);
  929. for (i = 0; i < buffer1len; i++)
  930. fputc((int) buffer1[i], stdout);
  931. free(buffer1);
  932. printf("\n\nfile '%s' ...\n\n", fname2);
  933. for (i = 0; i < buffer2len; i++)
  934. fputc((int) buffer2[i], stdout);
  935. free(buffer2);
  936. printf("\n\n");
  937. } /* else */
  938. if (f1)
  939. PHYSFS_close(f1);
  940. if (f2)
  941. PHYSFS_close(f2);
  942. return 1;
  943. } /* cmd_cat2 */
  944. #define CRC32_BUFFERSIZE 512
  945. static int cmd_crc32(char *args)
  946. {
  947. PHYSFS_File *f;
  948. if (*args == '\"')
  949. {
  950. args++;
  951. args[strlen(args) - 1] = '\0';
  952. } /* if */
  953. f = PHYSFS_openRead(args);
  954. if (f == NULL)
  955. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  956. else
  957. {
  958. PHYSFS_uint8 buffer[CRC32_BUFFERSIZE];
  959. PHYSFS_uint32 crc = -1;
  960. PHYSFS_sint64 bytesread;
  961. while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
  962. {
  963. PHYSFS_uint32 i, bit;
  964. for (i = 0; i < bytesread; i++)
  965. {
  966. for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
  967. crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
  968. } /* for */
  969. } /* while */
  970. if (bytesread < 0)
  971. {
  972. printf("error while reading. Reason: [%s].\n",
  973. PHYSFS_getLastError());
  974. return 1;
  975. } /* if */
  976. PHYSFS_close(f);
  977. crc ^= -1;
  978. printf("CRC32 for %s: 0x%08X\n", args, crc);
  979. } /* else */
  980. return 1;
  981. } /* cmd_crc32 */
  982. static int cmd_filelength(char *args)
  983. {
  984. PHYSFS_File *f;
  985. if (*args == '\"')
  986. {
  987. args++;
  988. args[strlen(args) - 1] = '\0';
  989. } /* if */
  990. f = PHYSFS_openRead(args);
  991. if (f == NULL)
  992. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  993. else
  994. {
  995. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  996. if (len == -1)
  997. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  998. else
  999. printf(" (cast to int) %d bytes.\n", (int) len);
  1000. PHYSFS_close(f);
  1001. } /* else */
  1002. return 1;
  1003. } /* cmd_filelength */
  1004. #define WRITESTR "The cat sat on the mat.\n\n"
  1005. static int cmd_append(char *args)
  1006. {
  1007. PHYSFS_File *f;
  1008. if (*args == '\"')
  1009. {
  1010. args++;
  1011. args[strlen(args) - 1] = '\0';
  1012. } /* if */
  1013. f = PHYSFS_openAppend(args);
  1014. if (f == NULL)
  1015. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  1016. else
  1017. {
  1018. size_t bw;
  1019. PHYSFS_sint64 rc;
  1020. if (do_buffer_size)
  1021. {
  1022. if (!PHYSFS_setBuffer(f, do_buffer_size))
  1023. {
  1024. printf("failed to set file buffer. Reason: [%s].\n",
  1025. PHYSFS_getLastError());
  1026. PHYSFS_close(f);
  1027. return 1;
  1028. } /* if */
  1029. } /* if */
  1030. bw = strlen(WRITESTR);
  1031. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  1032. if (rc < 0)
  1033. {
  1034. printf("Writing failed. Reason: [%s].\n",
  1035. PHYSFS_getLastError());
  1036. }
  1037. else if ((PHYSFS_uint64)rc != bw)
  1038. {
  1039. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  1040. (int) rc, (int) bw, PHYSFS_getLastError());
  1041. } /* if */
  1042. else
  1043. {
  1044. printf("Successful.\n");
  1045. } /* else */
  1046. PHYSFS_close(f);
  1047. } /* else */
  1048. return 1;
  1049. } /* cmd_append */
  1050. static int cmd_write(char *args)
  1051. {
  1052. PHYSFS_File *f;
  1053. if (*args == '\"')
  1054. {
  1055. args++;
  1056. args[strlen(args) - 1] = '\0';
  1057. } /* if */
  1058. f = PHYSFS_openWrite(args);
  1059. if (f == NULL)
  1060. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  1061. else
  1062. {
  1063. size_t bw;
  1064. PHYSFS_sint64 rc;
  1065. if (do_buffer_size)
  1066. {
  1067. if (!PHYSFS_setBuffer(f, do_buffer_size))
  1068. {
  1069. printf("failed to set file buffer. Reason: [%s].\n",
  1070. PHYSFS_getLastError());
  1071. PHYSFS_close(f);
  1072. return 1;
  1073. } /* if */
  1074. } /* if */
  1075. bw = strlen(WRITESTR);
  1076. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  1077. if (rc < 0)
  1078. {
  1079. printf("Writing failed. Reason: [%s].\n",
  1080. PHYSFS_getLastError());
  1081. }
  1082. else if ((size_t)rc != bw)
  1083. {
  1084. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  1085. (int) rc, (int) bw, PHYSFS_getLastError());
  1086. } /* if */
  1087. else
  1088. {
  1089. printf("Successful.\n");
  1090. } /* else */
  1091. PHYSFS_close(f);
  1092. } /* else */
  1093. return 1;
  1094. } /* cmd_write */
  1095. static char* modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  1096. {
  1097. if (modtime < 0)
  1098. strncpy(modstr, "Unknown\n", strsize - 1);
  1099. else
  1100. {
  1101. time_t t = (time_t) modtime;
  1102. char *str = ctime(&t);
  1103. strncpy(modstr, str, strsize - 1);
  1104. } /* else */
  1105. modstr[strsize-1] = '\0';
  1106. return modstr;
  1107. } /* modTimeToStr */
  1108. static int cmd_getlastmodtime(char *args)
  1109. {
  1110. PHYSFS_Stat statbuf;
  1111. if (!PHYSFS_stat(args, &statbuf))
  1112. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  1113. else
  1114. {
  1115. char modstr[64];
  1116. modTimeToStr(statbuf.modtime, modstr, sizeof (modstr));
  1117. printf("Last modified: %s (%ld).\n", modstr, (long) statbuf.modtime);
  1118. } /* else */
  1119. return 1;
  1120. } /* cmd_getLastModTime */
  1121. static int cmd_stat(char *args)
  1122. {
  1123. PHYSFS_Stat stat;
  1124. char timestring[65];
  1125. if (*args == '\"')
  1126. {
  1127. args++;
  1128. args[strlen(args) - 1] = '\0';
  1129. } /* if */
  1130. if(!PHYSFS_stat(args, &stat))
  1131. {
  1132. printf("failed to stat. Reason [%s].\n", PHYSFS_getLastError());
  1133. return 1;
  1134. } /* if */
  1135. printf("Filename: %s\n", args);
  1136. printf("Size %d\n",(int) stat.filesize);
  1137. if(stat.filetype == PHYSFS_FILETYPE_REGULAR)
  1138. printf("Type: File\n");
  1139. else if(stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
  1140. printf("Type: Directory\n");
  1141. else if(stat.filetype == PHYSFS_FILETYPE_SYMLINK)
  1142. printf("Type: Symlink\n");
  1143. else
  1144. printf("Type: Unknown\n");
  1145. printf("Created at: %s", modTimeToStr(stat.createtime, timestring, 64));
  1146. printf("Last modified at: %s", modTimeToStr(stat.modtime, timestring, 64));
  1147. printf("Last accessed at: %s", modTimeToStr(stat.accesstime, timestring, 64));
  1148. printf("Readonly: %s\n", stat.readonly ? "true" : "false");
  1149. return 1;
  1150. } /* cmd_filelength */
  1151. /* must have spaces trimmed prior to this call. */
  1152. static int count_args(const char *str)
  1153. {
  1154. int retval = 0;
  1155. int in_quotes = 0;
  1156. if (str != NULL)
  1157. {
  1158. for (; *str != '\0'; str++)
  1159. {
  1160. if (*str == '\"')
  1161. in_quotes = !in_quotes;
  1162. else if ((*str == ' ') && (!in_quotes))
  1163. retval++;
  1164. } /* for */
  1165. retval++;
  1166. } /* if */
  1167. return retval;
  1168. } /* count_args */
  1169. static int cmd_help(char *args);
  1170. typedef struct
  1171. {
  1172. const char *cmd;
  1173. int (*func)(char *args);
  1174. int argcount;
  1175. const char *usage;
  1176. } command_info;
  1177. static const command_info commands[] =
  1178. {
  1179. { "quit", cmd_quit, 0, NULL },
  1180. { "q", cmd_quit, 0, NULL },
  1181. { "help", cmd_help, 0, NULL },
  1182. { "init", cmd_init, 1, "<argv0>" },
  1183. { "deinit", cmd_deinit, 0, NULL },
  1184. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  1185. { "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
  1186. { "mountmem", cmd_mount_mem, 3, "<archiveLocation> <mntpoint> <append>" },
  1187. { "mounthandle", cmd_mount_handle, 3, "<archiveLocation> <mntpoint> <append>" },
  1188. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  1189. { "unmount", cmd_removearchive, 1, "<archiveLocation>" },
  1190. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  1191. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  1192. { "tree", cmd_tree, 1, "<dirToEnumerate>" },
  1193. { "getlasterror", cmd_getlasterror, 0, NULL },
  1194. { "getdirsep", cmd_getdirsep, 0, NULL },
  1195. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  1196. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  1197. { "getbasedir", cmd_getbasedir, 0, NULL },
  1198. { "getuserdir", cmd_getuserdir, 0, NULL },
  1199. { "getprefdir", cmd_getprefdir, 2, "<org> <app>" },
  1200. { "getwritedir", cmd_getwritedir, 0, NULL },
  1201. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  1202. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  1203. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  1204. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  1205. { "delete", cmd_delete, 1, "<dirToDelete>" },
  1206. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  1207. { "exists", cmd_exists, 1, "<fileToCheck>" },
  1208. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  1209. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  1210. { "cat", cmd_cat, 1, "<fileToCat>" },
  1211. { "cat2", cmd_cat2, 2, "<fileToCat1> <fileToCat2>" },
  1212. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  1213. { "stat", cmd_stat, 1, "<fileToStat>" },
  1214. { "append", cmd_append, 1, "<fileToAppend>" },
  1215. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  1216. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  1217. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  1218. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  1219. { "crc32", cmd_crc32, 1, "<fileToHash>" },
  1220. { "getmountpoint", cmd_getmountpoint, 1, "<dir>" },
  1221. { "setroot", cmd_setroot, 2, "<archiveLocation> <root>" },
  1222. { NULL, NULL, -1, NULL }
  1223. };
  1224. static void output_usage(const char *intro, const command_info *cmdinfo)
  1225. {
  1226. if (cmdinfo->argcount == 0)
  1227. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  1228. else
  1229. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  1230. } /* output_usage */
  1231. static int cmd_help(char *args)
  1232. {
  1233. const command_info *i;
  1234. printf("Commands:\n");
  1235. for (i = commands; i->cmd != NULL; i++)
  1236. output_usage(" -", i);
  1237. return 1;
  1238. } /* output_cmd_help */
  1239. static void trim_command(const char *orig, char *copy)
  1240. {
  1241. const char *i;
  1242. char *writeptr = copy;
  1243. int spacecount = 0;
  1244. int have_first = 0;
  1245. for (i = orig; *i != '\0'; i++)
  1246. {
  1247. if (*i == ' ')
  1248. {
  1249. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  1250. {
  1251. if ((have_first) && (!spacecount))
  1252. {
  1253. spacecount++;
  1254. *writeptr = ' ';
  1255. writeptr++;
  1256. } /* if */
  1257. } /* if */
  1258. } /* if */
  1259. else
  1260. {
  1261. have_first = 1;
  1262. spacecount = 0;
  1263. *writeptr = *i;
  1264. writeptr++;
  1265. } /* else */
  1266. } /* for */
  1267. *writeptr = '\0';
  1268. /*
  1269. printf("\n command is [%s].\n", copy);
  1270. */
  1271. } /* trim_command */
  1272. static int process_command(char *complete_cmd)
  1273. {
  1274. const command_info *i;
  1275. char *cmd_copy;
  1276. char *args;
  1277. int rc = 1;
  1278. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  1279. {
  1280. printf("\n");
  1281. return 0;
  1282. } /* if */
  1283. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  1284. if (cmd_copy == NULL)
  1285. {
  1286. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  1287. return 0;
  1288. } /* if */
  1289. trim_command(complete_cmd, cmd_copy);
  1290. args = strchr(cmd_copy, ' ');
  1291. if (args != NULL)
  1292. {
  1293. *args = '\0';
  1294. args++;
  1295. } /* else */
  1296. if (cmd_copy[0] != '\0')
  1297. {
  1298. for (i = commands; i->cmd != NULL; i++)
  1299. {
  1300. if (strcmp(i->cmd, cmd_copy) == 0)
  1301. {
  1302. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  1303. output_usage("usage:", i);
  1304. else
  1305. rc = i->func(args);
  1306. break;
  1307. } /* if */
  1308. } /* for */
  1309. if (i->cmd == NULL)
  1310. printf("Unknown command. Enter \"help\" for instructions.\n");
  1311. #if (defined PHYSFS_HAVE_READLINE)
  1312. add_history(complete_cmd);
  1313. if (history_file)
  1314. {
  1315. fprintf(history_file, "%s\n", complete_cmd);
  1316. fflush(history_file);
  1317. } /* if */
  1318. #endif
  1319. } /* if */
  1320. free(cmd_copy);
  1321. return rc;
  1322. } /* process_command */
  1323. static void open_history_file(void)
  1324. {
  1325. #if (defined PHYSFS_HAVE_READLINE)
  1326. #if 0
  1327. const char *envr = getenv("TESTPHYSFS_HISTORY");
  1328. if (!envr)
  1329. return;
  1330. #else
  1331. char envr[256];
  1332. strcpy(envr, PHYSFS_getUserDir());
  1333. strcat(envr, ".testphys_history");
  1334. #endif
  1335. if (access(envr, F_OK) == 0)
  1336. {
  1337. char buf[512];
  1338. FILE *f = fopen(envr, "r");
  1339. if (!f)
  1340. {
  1341. printf("\n\n"
  1342. "Could not open history file [%s] for reading!\n"
  1343. " Will not have past history available.\n\n",
  1344. envr);
  1345. return;
  1346. } /* if */
  1347. do
  1348. {
  1349. if (fgets(buf, sizeof (buf), f) == NULL)
  1350. break;
  1351. if (buf[strlen(buf) - 1] == '\n')
  1352. buf[strlen(buf) - 1] = '\0';
  1353. add_history(buf);
  1354. } while (!feof(f));
  1355. fclose(f);
  1356. } /* if */
  1357. history_file = fopen(envr, "ab");
  1358. if (!history_file)
  1359. {
  1360. printf("\n\n"
  1361. "Could not open history file [%s] for appending!\n"
  1362. " Will not be able to record this session's history.\n\n",
  1363. envr);
  1364. } /* if */
  1365. #endif
  1366. } /* open_history_file */
  1367. int main(int argc, char **argv)
  1368. {
  1369. char *buf = NULL;
  1370. int rc = 1;
  1371. int i;
  1372. #if (defined __MWERKS__)
  1373. extern tSIOUXSettings SIOUXSettings;
  1374. SIOUXSettings.asktosaveonclose = 0;
  1375. SIOUXSettings.autocloseonquit = 1;
  1376. SIOUXSettings.rows = 40;
  1377. SIOUXSettings.columns = 120;
  1378. #endif
  1379. printf("\n");
  1380. if (!PHYSFS_init(argv[0]))
  1381. {
  1382. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1383. return 1;
  1384. } /* if */
  1385. output_versions();
  1386. output_archivers();
  1387. open_history_file();
  1388. printf("Enter commands. Enter \"help\" for instructions.\n");
  1389. fflush(stdout);
  1390. for (i = 1; i < argc && rc; i++) {
  1391. rc = process_command(argv[i]);
  1392. }
  1393. while (rc)
  1394. {
  1395. #if (defined PHYSFS_HAVE_READLINE)
  1396. buf = readline("> ");
  1397. #else
  1398. int i;
  1399. buf = (char *) malloc(512);
  1400. memset(buf, '\0', 512);
  1401. printf("> ");
  1402. fflush(stdout);
  1403. for (i = 0; i < 511; i++)
  1404. {
  1405. int ch = fgetc(stdin);
  1406. if (ch == EOF)
  1407. {
  1408. strcpy(buf, "quit");
  1409. break;
  1410. } /* if */
  1411. else if ((ch == '\n') || (ch == '\r'))
  1412. {
  1413. buf[i] = '\0';
  1414. break;
  1415. } /* else if */
  1416. else if (ch == '\b')
  1417. {
  1418. if (i > 0)
  1419. i--;
  1420. } /* else if */
  1421. else
  1422. {
  1423. buf[i] = (char) ch;
  1424. } /* else */
  1425. } /* for */
  1426. #endif
  1427. rc = process_command(buf);
  1428. fflush(stdout);
  1429. if (buf != NULL)
  1430. free(buf);
  1431. }
  1432. if (!PHYSFS_deinit())
  1433. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1434. if (history_file)
  1435. fclose(history_file);
  1436. /*
  1437. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  1438. printf(" it makes you shoot teh railgun bettar.\n");
  1439. */
  1440. return 0;
  1441. } /* main */
  1442. /* end of test_physfs.c ... */