1
0

test_physfs.c 45 KB

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