test_physfs.c 45 KB

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