🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1651 lines
41 KiB

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