test_physfs.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #if (defined __MWERKS__)
  13. #include <SIOUX.h>
  14. #endif
  15. #if (defined PHYSFS_HAVE_READLINE)
  16. #include <unistd.h>
  17. #include <readline/readline.h>
  18. #include <readline/history.h>
  19. #endif
  20. #include <time.h>
  21. /* Define this, so the compiler doesn't complain about using old APIs. */
  22. #define PHYSFS_DEPRECATED
  23. #include "physfs.h"
  24. #define TEST_VERSION_MAJOR 2
  25. #define TEST_VERSION_MINOR 1
  26. #define TEST_VERSION_PATCH 0
  27. static FILE *history_file = NULL;
  28. static PHYSFS_uint32 do_buffer_size = 0;
  29. static void output_versions(void)
  30. {
  31. PHYSFS_Version compiled;
  32. PHYSFS_Version linked;
  33. PHYSFS_VERSION(&compiled);
  34. PHYSFS_getLinkedVersion(&linked);
  35. printf("test_physfs version %d.%d.%d.\n"
  36. " Compiled against PhysicsFS version %d.%d.%d,\n"
  37. " and linked against %d.%d.%d.\n\n",
  38. TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
  39. (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
  40. (int) linked.major, (int) linked.minor, (int) linked.patch);
  41. } /* output_versions */
  42. static void output_archivers(void)
  43. {
  44. const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
  45. const PHYSFS_ArchiveInfo **i;
  46. printf("Supported archive types:\n");
  47. if (*rc == NULL)
  48. printf(" * Apparently, NONE!\n");
  49. else
  50. {
  51. for (i = rc; *i != NULL; i++)
  52. {
  53. printf(" * %s: %s\n Written by %s.\n %s\n",
  54. (*i)->extension, (*i)->description,
  55. (*i)->author, (*i)->url);
  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_removearchive(char *args)
  230. {
  231. if (*args == '\"')
  232. {
  233. args++;
  234. args[strlen(args) - 1] = '\0';
  235. } /* if */
  236. if (PHYSFS_unmount(args))
  237. printf("Successful.\n");
  238. else
  239. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  240. return 1;
  241. } /* cmd_removearchive */
  242. static int cmd_enumerate(char *args)
  243. {
  244. char **rc;
  245. if (*args == '\"')
  246. {
  247. args++;
  248. args[strlen(args) - 1] = '\0';
  249. } /* if */
  250. rc = PHYSFS_enumerateFiles(args);
  251. if (rc == NULL)
  252. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  253. else
  254. {
  255. int file_count;
  256. char **i;
  257. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  258. printf("%s\n", *i);
  259. printf("\n total (%d) files.\n", file_count);
  260. PHYSFS_freeList(rc);
  261. } /* else */
  262. return 1;
  263. } /* cmd_enumerate */
  264. static int cmd_getdirsep(char *args)
  265. {
  266. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  267. return 1;
  268. } /* cmd_getdirsep */
  269. static int cmd_getlasterror(char *args)
  270. {
  271. printf("last error is [%s].\n", PHYSFS_getLastError());
  272. return 1;
  273. } /* cmd_getlasterror */
  274. static int cmd_getcdromdirs(char *args)
  275. {
  276. char **rc = PHYSFS_getCdRomDirs();
  277. if (rc == NULL)
  278. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  279. else
  280. {
  281. int dir_count;
  282. char **i;
  283. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  284. printf("%s\n", *i);
  285. printf("\n total (%d) drives.\n", dir_count);
  286. PHYSFS_freeList(rc);
  287. } /* else */
  288. return 1;
  289. } /* cmd_getcdromdirs */
  290. static int cmd_getsearchpath(char *args)
  291. {
  292. char **rc = PHYSFS_getSearchPath();
  293. if (rc == NULL)
  294. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  295. else
  296. {
  297. int dir_count;
  298. char **i;
  299. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  300. printf("%s\n", *i);
  301. printf("\n total (%d) directories.\n", dir_count);
  302. PHYSFS_freeList(rc);
  303. } /* else */
  304. return 1;
  305. } /* cmd_getcdromdirs */
  306. static int cmd_getbasedir(char *args)
  307. {
  308. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  309. return 1;
  310. } /* cmd_getbasedir */
  311. static int cmd_getuserdir(char *args)
  312. {
  313. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  314. return 1;
  315. } /* cmd_getuserdir */
  316. static int cmd_getprefdir(char *args)
  317. {
  318. char *org;
  319. char *appName;
  320. char *ptr = args;
  321. org = ptr;
  322. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  323. printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
  324. return 1;
  325. } /* cmd_getprefdir */
  326. static int cmd_getwritedir(char *args)
  327. {
  328. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  329. return 1;
  330. } /* cmd_getwritedir */
  331. static int cmd_setwritedir(char *args)
  332. {
  333. if (*args == '\"')
  334. {
  335. args++;
  336. args[strlen(args) - 1] = '\0';
  337. } /* if */
  338. if (PHYSFS_setWriteDir(args))
  339. printf("Successful.\n");
  340. else
  341. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  342. return 1;
  343. } /* cmd_setwritedir */
  344. static int cmd_permitsyms(char *args)
  345. {
  346. int num;
  347. if (*args == '\"')
  348. {
  349. args++;
  350. args[strlen(args) - 1] = '\0';
  351. } /* if */
  352. num = atoi(args);
  353. PHYSFS_permitSymbolicLinks(num);
  354. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  355. return 1;
  356. } /* cmd_permitsyms */
  357. static int cmd_setbuffer(char *args)
  358. {
  359. if (*args == '\"')
  360. {
  361. args++;
  362. args[strlen(args) - 1] = '\0';
  363. } /* if */
  364. do_buffer_size = (unsigned int) atoi(args);
  365. if (do_buffer_size)
  366. {
  367. printf("Further tests will set a (%lu) size buffer.\n",
  368. (unsigned long) do_buffer_size);
  369. } /* if */
  370. else
  371. {
  372. printf("Further tests will NOT use a buffer.\n");
  373. } /* else */
  374. return 1;
  375. } /* cmd_setbuffer */
  376. static int cmd_stressbuffer(char *args)
  377. {
  378. int num;
  379. if (*args == '\"')
  380. {
  381. args++;
  382. args[strlen(args) - 1] = '\0';
  383. } /* if */
  384. num = atoi(args);
  385. if (num < 0)
  386. printf("buffer must be greater than or equal to zero.\n");
  387. else
  388. {
  389. PHYSFS_File *f;
  390. int rndnum;
  391. printf("Stress testing with (%d) byte buffer...\n", num);
  392. f = PHYSFS_openWrite("test.txt");
  393. if (f == NULL)
  394. printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
  395. else
  396. {
  397. int i, j;
  398. char buf[37];
  399. char buf2[37];
  400. if (!PHYSFS_setBuffer(f, num))
  401. {
  402. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  403. PHYSFS_close(f);
  404. PHYSFS_delete("test.txt");
  405. return 1;
  406. } /* if */
  407. strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
  408. srand((unsigned int) time(NULL));
  409. for (i = 0; i < 10; i++)
  410. {
  411. for (j = 0; j < 10000; j++)
  412. {
  413. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  414. PHYSFS_uint32 left = 36 - right;
  415. if (PHYSFS_writeBytes(f, buf, left) != left)
  416. {
  417. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  418. PHYSFS_close(f);
  419. return 1;
  420. } /* if */
  421. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  422. if (rndnum == 42)
  423. {
  424. if (!PHYSFS_flush(f))
  425. {
  426. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  427. PHYSFS_close(f);
  428. return 1;
  429. } /* if */
  430. } /* if */
  431. if (PHYSFS_writeBytes(f, buf + left, right) != right)
  432. {
  433. printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
  434. PHYSFS_close(f);
  435. return 1;
  436. } /* if */
  437. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  438. if (rndnum == 42)
  439. {
  440. if (!PHYSFS_flush(f))
  441. {
  442. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  443. PHYSFS_close(f);
  444. return 1;
  445. } /* if */
  446. } /* if */
  447. } /* for */
  448. if (!PHYSFS_flush(f))
  449. {
  450. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  451. PHYSFS_close(f);
  452. return 1;
  453. } /* if */
  454. } /* for */
  455. if (!PHYSFS_close(f))
  456. {
  457. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  458. return 1; /* oh well. */
  459. } /* if */
  460. printf(" ... test file written ...\n");
  461. f = PHYSFS_openRead("test.txt");
  462. if (f == NULL)
  463. {
  464. printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
  465. return 1;
  466. } /* if */
  467. if (!PHYSFS_setBuffer(f, num))
  468. {
  469. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  470. PHYSFS_close(f);
  471. return 1;
  472. } /* if */
  473. for (i = 0; i < 10; i++)
  474. {
  475. for (j = 0; j < 10000; j++)
  476. {
  477. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  478. PHYSFS_uint32 left = 36 - right;
  479. if (PHYSFS_readBytes(f, buf2, left) != left)
  480. {
  481. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  482. PHYSFS_close(f);
  483. return 1;
  484. } /* if */
  485. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  486. if (rndnum == 42)
  487. {
  488. if (!PHYSFS_flush(f))
  489. {
  490. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  491. PHYSFS_close(f);
  492. return 1;
  493. } /* if */
  494. } /* if */
  495. if (PHYSFS_readBytes(f, buf2 + left, right) != right)
  496. {
  497. printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
  498. PHYSFS_close(f);
  499. return 1;
  500. } /* if */
  501. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  502. if (rndnum == 42)
  503. {
  504. if (!PHYSFS_flush(f))
  505. {
  506. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  507. PHYSFS_close(f);
  508. return 1;
  509. } /* if */
  510. } /* if */
  511. if (memcmp(buf, buf2, 36) != 0)
  512. {
  513. printf("readback is mismatched on iterations (%d, %d).\n", i, j);
  514. printf("wanted: [");
  515. for (i = 0; i < 36; i++)
  516. printf("%c", buf[i]);
  517. printf("]\n");
  518. printf(" got: [");
  519. for (i = 0; i < 36; i++)
  520. printf("%c", buf2[i]);
  521. printf("]\n");
  522. PHYSFS_close(f);
  523. return 1;
  524. } /* if */
  525. } /* for */
  526. if (!PHYSFS_flush(f))
  527. {
  528. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  529. PHYSFS_close(f);
  530. return 1;
  531. } /* if */
  532. } /* for */
  533. printf(" ... test file read ...\n");
  534. if (!PHYSFS_eof(f))
  535. printf("PHYSFS_eof() returned true! That's wrong.\n");
  536. if (!PHYSFS_close(f))
  537. {
  538. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  539. return 1; /* oh well. */
  540. } /* if */
  541. PHYSFS_delete("test.txt");
  542. printf("stress test completed successfully.\n");
  543. } /* else */
  544. } /* else */
  545. return 1;
  546. } /* cmd_stressbuffer */
  547. static int cmd_setsaneconfig(char *args)
  548. {
  549. char *org;
  550. char *appName;
  551. char *arcExt;
  552. int inclCD;
  553. int arcsFirst;
  554. char *ptr = args;
  555. /* ugly. */
  556. org = ptr;
  557. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  558. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  559. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  560. arcsFirst = atoi(ptr);
  561. if (strcmp(arcExt, "!") == 0)
  562. arcExt = NULL;
  563. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  564. printf("Successful.\n");
  565. else
  566. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  567. return 1;
  568. } /* cmd_setsaneconfig */
  569. static int cmd_mkdir(char *args)
  570. {
  571. if (*args == '\"')
  572. {
  573. args++;
  574. args[strlen(args) - 1] = '\0';
  575. } /* if */
  576. if (PHYSFS_mkdir(args))
  577. printf("Successful.\n");
  578. else
  579. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  580. return 1;
  581. } /* cmd_mkdir */
  582. static int cmd_delete(char *args)
  583. {
  584. if (*args == '\"')
  585. {
  586. args++;
  587. args[strlen(args) - 1] = '\0';
  588. } /* if */
  589. if (PHYSFS_delete(args))
  590. printf("Successful.\n");
  591. else
  592. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  593. return 1;
  594. } /* cmd_delete */
  595. static int cmd_getrealdir(char *args)
  596. {
  597. const char *rc;
  598. if (*args == '\"')
  599. {
  600. args++;
  601. args[strlen(args) - 1] = '\0';
  602. } /* if */
  603. rc = PHYSFS_getRealDir(args);
  604. if (rc)
  605. printf("Found at [%s].\n", rc);
  606. else
  607. printf("Not found.\n");
  608. return 1;
  609. } /* cmd_getrealdir */
  610. static int cmd_exists(char *args)
  611. {
  612. int rc;
  613. if (*args == '\"')
  614. {
  615. args++;
  616. args[strlen(args) - 1] = '\0';
  617. } /* if */
  618. rc = PHYSFS_exists(args);
  619. printf("File %sexists.\n", rc ? "" : "does not ");
  620. return 1;
  621. } /* cmd_exists */
  622. static int cmd_isdir(char *args)
  623. {
  624. PHYSFS_Stat statbuf;
  625. int rc;
  626. if (*args == '\"')
  627. {
  628. args++;
  629. args[strlen(args) - 1] = '\0';
  630. } /* if */
  631. rc = PHYSFS_stat(args, &statbuf);
  632. if (rc)
  633. rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  634. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  635. return 1;
  636. } /* cmd_isdir */
  637. static int cmd_issymlink(char *args)
  638. {
  639. PHYSFS_Stat statbuf;
  640. int rc;
  641. if (*args == '\"')
  642. {
  643. args++;
  644. args[strlen(args) - 1] = '\0';
  645. } /* if */
  646. rc = PHYSFS_stat(args, &statbuf);
  647. if (rc)
  648. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  649. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  650. return 1;
  651. } /* cmd_issymlink */
  652. static int cmd_cat(char *args)
  653. {
  654. PHYSFS_File *f;
  655. if (*args == '\"')
  656. {
  657. args++;
  658. args[strlen(args) - 1] = '\0';
  659. } /* if */
  660. f = PHYSFS_openRead(args);
  661. if (f == NULL)
  662. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  663. else
  664. {
  665. if (do_buffer_size)
  666. {
  667. if (!PHYSFS_setBuffer(f, do_buffer_size))
  668. {
  669. printf("failed to set file buffer. Reason: [%s].\n",
  670. PHYSFS_getLastError());
  671. PHYSFS_close(f);
  672. return 1;
  673. } /* if */
  674. } /* if */
  675. while (1)
  676. {
  677. char buffer[128];
  678. PHYSFS_sint64 rc;
  679. PHYSFS_sint64 i;
  680. rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
  681. for (i = 0; i < rc; i++)
  682. fputc((int) buffer[i], stdout);
  683. if (rc < sizeof (buffer))
  684. {
  685. printf("\n\n");
  686. if (!PHYSFS_eof(f))
  687. {
  688. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  689. PHYSFS_getLastError());
  690. } /* if */
  691. PHYSFS_close(f);
  692. return 1;
  693. } /* if */
  694. } /* while */
  695. } /* else */
  696. return 1;
  697. } /* cmd_cat */
  698. #define CRC32_BUFFERSIZE 512
  699. static int cmd_crc32(char *args)
  700. {
  701. PHYSFS_File *f;
  702. if (*args == '\"')
  703. {
  704. args++;
  705. args[strlen(args) - 1] = '\0';
  706. } /* if */
  707. f = PHYSFS_openRead(args);
  708. if (f == NULL)
  709. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  710. else
  711. {
  712. PHYSFS_uint8 buffer[CRC32_BUFFERSIZE];
  713. PHYSFS_uint32 crc = -1;
  714. PHYSFS_sint64 bytesread;
  715. while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
  716. {
  717. PHYSFS_uint32 i, bit;
  718. for (i = 0; i < bytesread; i++)
  719. {
  720. for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
  721. crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
  722. } /* for */
  723. } /* while */
  724. if (bytesread < 0)
  725. {
  726. printf("error while reading. Reason: [%s].\n",
  727. PHYSFS_getLastError());
  728. return 1;
  729. } /* if */
  730. PHYSFS_close(f);
  731. crc ^= -1;
  732. printf("CRC32 for %s: 0x%08X\n", args, crc);
  733. } /* else */
  734. return 1;
  735. } /* cmd_crc32 */
  736. static int cmd_filelength(char *args)
  737. {
  738. PHYSFS_File *f;
  739. if (*args == '\"')
  740. {
  741. args++;
  742. args[strlen(args) - 1] = '\0';
  743. } /* if */
  744. f = PHYSFS_openRead(args);
  745. if (f == NULL)
  746. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  747. else
  748. {
  749. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  750. if (len == -1)
  751. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  752. else
  753. printf(" (cast to int) %d bytes.\n", (int) len);
  754. PHYSFS_close(f);
  755. } /* else */
  756. return 1;
  757. } /* cmd_filelength */
  758. #define WRITESTR "The cat sat on the mat.\n\n"
  759. static int cmd_append(char *args)
  760. {
  761. PHYSFS_File *f;
  762. if (*args == '\"')
  763. {
  764. args++;
  765. args[strlen(args) - 1] = '\0';
  766. } /* if */
  767. f = PHYSFS_openAppend(args);
  768. if (f == NULL)
  769. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  770. else
  771. {
  772. size_t bw;
  773. PHYSFS_sint64 rc;
  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. bw = strlen(WRITESTR);
  785. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  786. if (rc != bw)
  787. {
  788. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  789. (int) rc, (int) bw, PHYSFS_getLastError());
  790. } /* if */
  791. else
  792. {
  793. printf("Successful.\n");
  794. } /* else */
  795. PHYSFS_close(f);
  796. } /* else */
  797. return 1;
  798. } /* cmd_append */
  799. static int cmd_write(char *args)
  800. {
  801. PHYSFS_File *f;
  802. if (*args == '\"')
  803. {
  804. args++;
  805. args[strlen(args) - 1] = '\0';
  806. } /* if */
  807. f = PHYSFS_openWrite(args);
  808. if (f == NULL)
  809. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  810. else
  811. {
  812. size_t bw;
  813. PHYSFS_sint64 rc;
  814. if (do_buffer_size)
  815. {
  816. if (!PHYSFS_setBuffer(f, do_buffer_size))
  817. {
  818. printf("failed to set file buffer. Reason: [%s].\n",
  819. PHYSFS_getLastError());
  820. PHYSFS_close(f);
  821. return 1;
  822. } /* if */
  823. } /* if */
  824. bw = strlen(WRITESTR);
  825. rc = PHYSFS_writeBytes(f, WRITESTR, bw);
  826. if (rc != bw)
  827. {
  828. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  829. (int) rc, (int) bw, PHYSFS_getLastError());
  830. } /* if */
  831. else
  832. {
  833. printf("Successful.\n");
  834. } /* else */
  835. PHYSFS_close(f);
  836. } /* else */
  837. return 1;
  838. } /* cmd_write */
  839. static char* modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  840. {
  841. if (modtime < 0)
  842. strncpy(modstr, "Unknown\n", strsize);
  843. else
  844. {
  845. time_t t = (time_t) modtime;
  846. char *str = ctime(&t);
  847. strncpy(modstr, str, strsize);
  848. } /* else */
  849. modstr[strsize-1] = '\0';
  850. return modstr;
  851. } /* modTimeToStr */
  852. static int cmd_getlastmodtime(char *args)
  853. {
  854. PHYSFS_Stat statbuf;
  855. if (!PHYSFS_stat(args, &statbuf))
  856. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  857. else
  858. {
  859. char modstr[64];
  860. modTimeToStr(statbuf.modtime, modstr, sizeof (modstr));
  861. printf("Last modified: %s (%ld).\n", modstr, (long) statbuf.modtime);
  862. } /* else */
  863. return 1;
  864. } /* cmd_getLastModTime */
  865. static int cmd_stat(char *args)
  866. {
  867. PHYSFS_Stat stat;
  868. char timestring[65];
  869. if (*args == '\"')
  870. {
  871. args++;
  872. args[strlen(args) - 1] = '\0';
  873. } /* if */
  874. if(!PHYSFS_stat(args, &stat))
  875. {
  876. printf("failed to stat. Reason [%s].\n", PHYSFS_getLastError());
  877. return 1;
  878. } /* if */
  879. printf("Filename: %s\n", args);
  880. printf("Size %d\n",(int) stat.filesize);
  881. if(stat.filetype == PHYSFS_FILETYPE_REGULAR)
  882. printf("Type: File\n");
  883. else if(stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
  884. printf("Type: Directory\n");
  885. else if(stat.filetype == PHYSFS_FILETYPE_SYMLINK)
  886. printf("Type: Symlink\n");
  887. else
  888. printf("Type: Unknown\n");
  889. printf("Created at: %s", modTimeToStr(stat.createtime, timestring, 64));
  890. printf("Last modified at: %s", modTimeToStr(stat.modtime, timestring, 64));
  891. printf("Last accessed at: %s", modTimeToStr(stat.accesstime, timestring, 64));
  892. printf("Readonly: %s\n", stat.readonly ? "true" : "false");
  893. return 1;
  894. } /* cmd_filelength */
  895. /* must have spaces trimmed prior to this call. */
  896. static int count_args(const char *str)
  897. {
  898. int retval = 0;
  899. int in_quotes = 0;
  900. if (str != NULL)
  901. {
  902. for (; *str != '\0'; str++)
  903. {
  904. if (*str == '\"')
  905. in_quotes = !in_quotes;
  906. else if ((*str == ' ') && (!in_quotes))
  907. retval++;
  908. } /* for */
  909. retval++;
  910. } /* if */
  911. return retval;
  912. } /* count_args */
  913. static int cmd_help(char *args);
  914. typedef struct
  915. {
  916. const char *cmd;
  917. int (*func)(char *args);
  918. int argcount;
  919. const char *usage;
  920. } command_info;
  921. static const command_info commands[] =
  922. {
  923. { "quit", cmd_quit, 0, NULL },
  924. { "q", cmd_quit, 0, NULL },
  925. { "help", cmd_help, 0, NULL },
  926. { "init", cmd_init, 1, "<argv0>" },
  927. { "deinit", cmd_deinit, 0, NULL },
  928. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  929. { "mount", cmd_mount, 3, "<archiveLocation> <mntpoint> <append>" },
  930. { "mountmem", cmd_mount_mem, 3, "<archiveLocation> <mntpoint> <append>" },
  931. { "mounthandle", cmd_mount_handle, 3, "<archiveLocation> <mntpoint> <append>" },
  932. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  933. { "unmount", cmd_removearchive, 1, "<archiveLocation>" },
  934. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  935. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  936. { "getlasterror", cmd_getlasterror, 0, NULL },
  937. { "getdirsep", cmd_getdirsep, 0, NULL },
  938. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  939. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  940. { "getbasedir", cmd_getbasedir, 0, NULL },
  941. { "getuserdir", cmd_getuserdir, 0, NULL },
  942. { "getprefdir", cmd_getprefdir, 2, "<org> <app>" },
  943. { "getwritedir", cmd_getwritedir, 0, NULL },
  944. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  945. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  946. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  947. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  948. { "delete", cmd_delete, 1, "<dirToDelete>" },
  949. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  950. { "exists", cmd_exists, 1, "<fileToCheck>" },
  951. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  952. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  953. { "cat", cmd_cat, 1, "<fileToCat>" },
  954. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  955. { "stat", cmd_stat, 1, "<fileToStat>" },
  956. { "append", cmd_append, 1, "<fileToAppend>" },
  957. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  958. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  959. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  960. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  961. { "crc32", cmd_crc32, 1, "<fileToHash>" },
  962. { "getmountpoint", cmd_getmountpoint, 1, "<dir>" },
  963. { NULL, NULL, -1, NULL }
  964. };
  965. static void output_usage(const char *intro, const command_info *cmdinfo)
  966. {
  967. if (cmdinfo->argcount == 0)
  968. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  969. else
  970. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  971. } /* output_usage */
  972. static int cmd_help(char *args)
  973. {
  974. const command_info *i;
  975. printf("Commands:\n");
  976. for (i = commands; i->cmd != NULL; i++)
  977. output_usage(" -", i);
  978. return 1;
  979. } /* output_cmd_help */
  980. static void trim_command(const char *orig, char *copy)
  981. {
  982. const char *i;
  983. char *writeptr = copy;
  984. int spacecount = 0;
  985. int have_first = 0;
  986. for (i = orig; *i != '\0'; i++)
  987. {
  988. if (*i == ' ')
  989. {
  990. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  991. {
  992. if ((have_first) && (!spacecount))
  993. {
  994. spacecount++;
  995. *writeptr = ' ';
  996. writeptr++;
  997. } /* if */
  998. } /* if */
  999. } /* if */
  1000. else
  1001. {
  1002. have_first = 1;
  1003. spacecount = 0;
  1004. *writeptr = *i;
  1005. writeptr++;
  1006. } /* else */
  1007. } /* for */
  1008. *writeptr = '\0';
  1009. /*
  1010. printf("\n command is [%s].\n", copy);
  1011. */
  1012. } /* trim_command */
  1013. static int process_command(char *complete_cmd)
  1014. {
  1015. const command_info *i;
  1016. char *cmd_copy;
  1017. char *args;
  1018. int rc = 1;
  1019. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  1020. {
  1021. printf("\n");
  1022. return 0;
  1023. } /* if */
  1024. cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
  1025. if (cmd_copy == NULL)
  1026. {
  1027. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  1028. return 0;
  1029. } /* if */
  1030. trim_command(complete_cmd, cmd_copy);
  1031. args = strchr(cmd_copy, ' ');
  1032. if (args != NULL)
  1033. {
  1034. *args = '\0';
  1035. args++;
  1036. } /* else */
  1037. if (cmd_copy[0] != '\0')
  1038. {
  1039. for (i = commands; i->cmd != NULL; i++)
  1040. {
  1041. if (strcmp(i->cmd, cmd_copy) == 0)
  1042. {
  1043. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  1044. output_usage("usage:", i);
  1045. else
  1046. rc = i->func(args);
  1047. break;
  1048. } /* if */
  1049. } /* for */
  1050. if (i->cmd == NULL)
  1051. printf("Unknown command. Enter \"help\" for instructions.\n");
  1052. #if (defined PHYSFS_HAVE_READLINE)
  1053. add_history(complete_cmd);
  1054. if (history_file)
  1055. {
  1056. fprintf(history_file, "%s\n", complete_cmd);
  1057. fflush(history_file);
  1058. } /* if */
  1059. #endif
  1060. } /* if */
  1061. free(cmd_copy);
  1062. return rc;
  1063. } /* process_command */
  1064. static void open_history_file(void)
  1065. {
  1066. #if (defined PHYSFS_HAVE_READLINE)
  1067. #if 0
  1068. const char *envr = getenv("TESTPHYSFS_HISTORY");
  1069. if (!envr)
  1070. return;
  1071. #else
  1072. char envr[256];
  1073. strcpy(envr, PHYSFS_getUserDir());
  1074. strcat(envr, ".testphys_history");
  1075. #endif
  1076. if (access(envr, F_OK) == 0)
  1077. {
  1078. char buf[512];
  1079. FILE *f = fopen(envr, "r");
  1080. if (!f)
  1081. {
  1082. printf("\n\n"
  1083. "Could not open history file [%s] for reading!\n"
  1084. " Will not have past history available.\n\n",
  1085. envr);
  1086. return;
  1087. } /* if */
  1088. do
  1089. {
  1090. if (fgets(buf, sizeof (buf), f) == NULL)
  1091. break;
  1092. if (buf[strlen(buf) - 1] == '\n')
  1093. buf[strlen(buf) - 1] = '\0';
  1094. add_history(buf);
  1095. } while (!feof(f));
  1096. fclose(f);
  1097. } /* if */
  1098. history_file = fopen(envr, "ab");
  1099. if (!history_file)
  1100. {
  1101. printf("\n\n"
  1102. "Could not open history file [%s] for appending!\n"
  1103. " Will not be able to record this session's history.\n\n",
  1104. envr);
  1105. } /* if */
  1106. #endif
  1107. } /* open_history_file */
  1108. int main(int argc, char **argv)
  1109. {
  1110. char *buf = NULL;
  1111. int rc = 0;
  1112. #if (defined __MWERKS__)
  1113. extern tSIOUXSettings SIOUXSettings;
  1114. SIOUXSettings.asktosaveonclose = 0;
  1115. SIOUXSettings.autocloseonquit = 1;
  1116. SIOUXSettings.rows = 40;
  1117. SIOUXSettings.columns = 120;
  1118. #endif
  1119. printf("\n");
  1120. if (!PHYSFS_init(argv[0]))
  1121. {
  1122. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1123. return 1;
  1124. } /* if */
  1125. output_versions();
  1126. output_archivers();
  1127. open_history_file();
  1128. printf("Enter commands. Enter \"help\" for instructions.\n");
  1129. fflush(stdout);
  1130. do
  1131. {
  1132. #if (defined PHYSFS_HAVE_READLINE)
  1133. buf = readline("> ");
  1134. #else
  1135. int i;
  1136. buf = (char *) malloc(512);
  1137. memset(buf, '\0', 512);
  1138. printf("> ");
  1139. fflush(stdout);
  1140. for (i = 0; i < 511; i++)
  1141. {
  1142. int ch = fgetc(stdin);
  1143. if (ch == EOF)
  1144. {
  1145. strcpy(buf, "quit");
  1146. break;
  1147. } /* if */
  1148. else if ((ch == '\n') || (ch == '\r'))
  1149. {
  1150. buf[i] = '\0';
  1151. break;
  1152. } /* else if */
  1153. else if (ch == '\b')
  1154. {
  1155. if (i > 0)
  1156. i--;
  1157. } /* else if */
  1158. else
  1159. {
  1160. buf[i] = (char) ch;
  1161. } /* else */
  1162. } /* for */
  1163. #endif
  1164. rc = process_command(buf);
  1165. fflush(stdout);
  1166. if (buf != NULL)
  1167. free(buf);
  1168. } while (rc);
  1169. if (!PHYSFS_deinit())
  1170. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  1171. if (history_file)
  1172. fclose(history_file);
  1173. /*
  1174. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  1175. printf(" it makes you shoot teh railgun bettar.\n");
  1176. */
  1177. return 0;
  1178. } /* main */
  1179. /* end of test_physfs.c ... */