test_physfs.c 36 KB

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