1
0

test_physfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /**
  2. * Test program for PhysicsFS. May only work on Unix.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #if HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #if (defined __MWERKS__)
  16. #include <SIOUX.h>
  17. #endif
  18. #if (defined PHYSFS_HAVE_READLINE)
  19. #include <unistd.h>
  20. #include <readline/readline.h>
  21. #include <readline/history.h>
  22. #endif
  23. #include <time.h>
  24. #include "physfs.h"
  25. #define TEST_VERSION_MAJOR 1
  26. #define TEST_VERSION_MINOR 0
  27. #define TEST_VERSION_PATCH 2
  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. } /* for */
  58. } /* else */
  59. printf("\n");
  60. } /* output_archivers */
  61. static int cmd_quit(char *args)
  62. {
  63. return(0);
  64. } /* cmd_quit */
  65. static int cmd_init(char *args)
  66. {
  67. if (*args == '\"')
  68. {
  69. args++;
  70. args[strlen(args) - 1] = '\0';
  71. } /* if */
  72. if (PHYSFS_init(args))
  73. printf("Successful.\n");
  74. else
  75. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  76. return(1);
  77. } /* cmd_init */
  78. static int cmd_deinit(char *args)
  79. {
  80. if (PHYSFS_deinit())
  81. printf("Successful.\n");
  82. else
  83. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  84. return(1);
  85. } /* cmd_deinit */
  86. static int cmd_addarchive(char *args)
  87. {
  88. char *ptr = strrchr(args, ' ');
  89. int appending = atoi(ptr + 1);
  90. *ptr = '\0';
  91. if (*args == '\"')
  92. {
  93. args++;
  94. *(ptr - 1) = '\0';
  95. } /* if */
  96. /*printf("[%s], [%d]\n", args, appending);*/
  97. if (PHYSFS_addToSearchPath(args, appending))
  98. printf("Successful.\n");
  99. else
  100. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  101. return(1);
  102. } /* cmd_addarchive */
  103. static int cmd_removearchive(char *args)
  104. {
  105. if (*args == '\"')
  106. {
  107. args++;
  108. args[strlen(args) - 1] = '\0';
  109. } /* if */
  110. if (PHYSFS_removeFromSearchPath(args))
  111. printf("Successful.\n");
  112. else
  113. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  114. return(1);
  115. } /* cmd_removearchive */
  116. static int cmd_enumerate(char *args)
  117. {
  118. char **rc;
  119. if (*args == '\"')
  120. {
  121. args++;
  122. args[strlen(args) - 1] = '\0';
  123. } /* if */
  124. rc = PHYSFS_enumerateFiles(args);
  125. if (rc == NULL)
  126. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  127. else
  128. {
  129. int file_count;
  130. char **i;
  131. for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
  132. printf("%s\n", *i);
  133. printf("\n total (%d) files.\n", file_count);
  134. PHYSFS_freeList(rc);
  135. } /* else */
  136. return(1);
  137. } /* cmd_enumerate */
  138. static int cmd_getdirsep(char *args)
  139. {
  140. printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
  141. return(1);
  142. } /* cmd_getdirsep */
  143. static int cmd_getlasterror(char *args)
  144. {
  145. printf("last error is [%s].\n", PHYSFS_getLastError());
  146. return(1);
  147. } /* cmd_getlasterror */
  148. static int cmd_getcdromdirs(char *args)
  149. {
  150. char **rc = PHYSFS_getCdRomDirs();
  151. if (rc == NULL)
  152. printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
  153. else
  154. {
  155. int dir_count;
  156. char **i;
  157. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  158. printf("%s\n", *i);
  159. printf("\n total (%d) drives.\n", dir_count);
  160. PHYSFS_freeList(rc);
  161. } /* else */
  162. return(1);
  163. } /* cmd_getcdromdirs */
  164. static int cmd_getsearchpath(char *args)
  165. {
  166. char **rc = PHYSFS_getSearchPath();
  167. if (rc == NULL)
  168. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  169. else
  170. {
  171. int dir_count;
  172. char **i;
  173. for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
  174. printf("%s\n", *i);
  175. printf("\n total (%d) directories.\n", dir_count);
  176. PHYSFS_freeList(rc);
  177. } /* else */
  178. return(1);
  179. } /* cmd_getcdromdirs */
  180. static int cmd_getbasedir(char *args)
  181. {
  182. printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
  183. return(1);
  184. } /* cmd_getbasedir */
  185. static int cmd_getuserdir(char *args)
  186. {
  187. printf("User dir is [%s].\n", PHYSFS_getUserDir());
  188. return(1);
  189. } /* cmd_getuserdir */
  190. static int cmd_getwritedir(char *args)
  191. {
  192. printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
  193. return(1);
  194. } /* cmd_getwritedir */
  195. static int cmd_setwritedir(char *args)
  196. {
  197. if (*args == '\"')
  198. {
  199. args++;
  200. args[strlen(args) - 1] = '\0';
  201. } /* if */
  202. if (PHYSFS_setWriteDir(args))
  203. printf("Successful.\n");
  204. else
  205. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  206. return(1);
  207. } /* cmd_setwritedir */
  208. static int cmd_permitsyms(char *args)
  209. {
  210. int num;
  211. if (*args == '\"')
  212. {
  213. args++;
  214. args[strlen(args) - 1] = '\0';
  215. } /* if */
  216. num = atoi(args);
  217. PHYSFS_permitSymbolicLinks(num);
  218. printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
  219. return(1);
  220. } /* cmd_permitsyms */
  221. static int cmd_setbuffer(char *args)
  222. {
  223. if (*args == '\"')
  224. {
  225. args++;
  226. args[strlen(args) - 1] = '\0';
  227. } /* if */
  228. do_buffer_size = (unsigned int) atoi(args);
  229. if (do_buffer_size)
  230. {
  231. printf("Further tests will set a (%lu) size buffer.\n",
  232. (unsigned long) do_buffer_size);
  233. } /* if */
  234. else
  235. {
  236. printf("Further tests will NOT use a buffer.\n");
  237. } /* else */
  238. return(1);
  239. } /* cmd_setbuffer */
  240. static int cmd_stressbuffer(char *args)
  241. {
  242. int num;
  243. if (*args == '\"')
  244. {
  245. args++;
  246. args[strlen(args) - 1] = '\0';
  247. } /* if */
  248. num = atoi(args);
  249. if (num < 0)
  250. printf("buffer must be greater than or equal to zero.\n");
  251. else
  252. {
  253. PHYSFS_file *f;
  254. int rndnum;
  255. printf("Stress testing with (%d) byte buffer...\n", num);
  256. f = PHYSFS_openWrite("test.txt");
  257. if (f == NULL)
  258. printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
  259. else
  260. {
  261. int i, j;
  262. char buf[37];
  263. char buf2[37];
  264. if (!PHYSFS_setBuffer(f, num))
  265. {
  266. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  267. PHYSFS_close(f);
  268. PHYSFS_delete("test.txt");
  269. return(1);
  270. } /* if */
  271. strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
  272. srand((unsigned int) time(NULL));
  273. for (i = 0; i < 10; i++)
  274. {
  275. for (j = 0; j < 10000; j++)
  276. {
  277. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  278. PHYSFS_uint32 left = 36 - right;
  279. if (PHYSFS_write(f, buf, left, 1) != 1)
  280. {
  281. printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
  282. PHYSFS_close(f);
  283. return(1);
  284. } /* if */
  285. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  286. if (rndnum == 42)
  287. {
  288. if (!PHYSFS_flush(f))
  289. {
  290. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  291. PHYSFS_close(f);
  292. return(1);
  293. } /* if */
  294. } /* if */
  295. if (PHYSFS_write(f, buf + left, 1, right) != right)
  296. {
  297. printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
  298. PHYSFS_close(f);
  299. return(1);
  300. } /* if */
  301. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  302. if (rndnum == 42)
  303. {
  304. if (!PHYSFS_flush(f))
  305. {
  306. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  307. PHYSFS_close(f);
  308. return(1);
  309. } /* if */
  310. } /* if */
  311. } /* for */
  312. if (!PHYSFS_flush(f))
  313. {
  314. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  315. PHYSFS_close(f);
  316. return(1);
  317. } /* if */
  318. } /* for */
  319. if (!PHYSFS_close(f))
  320. {
  321. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  322. return(1); /* oh well. */
  323. } /* if */
  324. printf(" ... test file written ...\n");
  325. f = PHYSFS_openRead("test.txt");
  326. if (f == NULL)
  327. {
  328. printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
  329. return(1);
  330. } /* if */
  331. if (!PHYSFS_setBuffer(f, num))
  332. {
  333. printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
  334. PHYSFS_close(f);
  335. return(1);
  336. } /* if */
  337. for (i = 0; i < 10; i++)
  338. {
  339. for (j = 0; j < 10000; j++)
  340. {
  341. PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
  342. PHYSFS_uint32 left = 36 - right;
  343. if (PHYSFS_read(f, buf2, left, 1) != 1)
  344. {
  345. printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
  346. PHYSFS_close(f);
  347. return(1);
  348. } /* if */
  349. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  350. if (rndnum == 42)
  351. {
  352. if (!PHYSFS_flush(f))
  353. {
  354. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  355. PHYSFS_close(f);
  356. return(1);
  357. } /* if */
  358. } /* if */
  359. if (PHYSFS_read(f, buf2 + left, 1, right) != right)
  360. {
  361. printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
  362. PHYSFS_close(f);
  363. return(1);
  364. } /* if */
  365. rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
  366. if (rndnum == 42)
  367. {
  368. if (!PHYSFS_flush(f))
  369. {
  370. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  371. PHYSFS_close(f);
  372. return(1);
  373. } /* if */
  374. } /* if */
  375. if (memcmp(buf, buf2, 36) != 0)
  376. {
  377. printf("readback is mismatched on iterations (%d, %d).\n", i, j);
  378. printf("wanted: [");
  379. for (i = 0; i < 36; i++)
  380. printf("%c", buf[i]);
  381. printf("]\n");
  382. printf(" got: [");
  383. for (i = 0; i < 36; i++)
  384. printf("%c", buf2[i]);
  385. printf("]\n");
  386. PHYSFS_close(f);
  387. return(1);
  388. } /* if */
  389. } /* for */
  390. if (!PHYSFS_flush(f))
  391. {
  392. printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
  393. PHYSFS_close(f);
  394. return(1);
  395. } /* if */
  396. } /* for */
  397. printf(" ... test file read ...\n");
  398. if (!PHYSFS_eof(f))
  399. printf("PHYSFS_eof() returned true! That's wrong.\n");
  400. if (!PHYSFS_close(f))
  401. {
  402. printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
  403. return(1); /* oh well. */
  404. } /* if */
  405. PHYSFS_delete("test.txt");
  406. printf("stress test completed successfully.\n");
  407. } /* else */
  408. } /* else */
  409. return(1);
  410. } /* cmd_stressbuffer */
  411. static int cmd_setsaneconfig(char *args)
  412. {
  413. char *org;
  414. char *appName;
  415. char *arcExt;
  416. int inclCD;
  417. int arcsFirst;
  418. char *ptr = args;
  419. /* ugly. */
  420. org = ptr;
  421. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
  422. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
  423. ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
  424. arcsFirst = atoi(ptr);
  425. if (strcmp(arcExt, "!") == 0)
  426. arcExt = NULL;
  427. if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
  428. printf("Successful.\n");
  429. else
  430. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  431. return(1);
  432. } /* cmd_setsaneconfig */
  433. static int cmd_mkdir(char *args)
  434. {
  435. if (*args == '\"')
  436. {
  437. args++;
  438. args[strlen(args) - 1] = '\0';
  439. } /* if */
  440. if (PHYSFS_mkdir(args))
  441. printf("Successful.\n");
  442. else
  443. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  444. return(1);
  445. } /* cmd_mkdir */
  446. static int cmd_delete(char *args)
  447. {
  448. if (*args == '\"')
  449. {
  450. args++;
  451. args[strlen(args) - 1] = '\0';
  452. } /* if */
  453. if (PHYSFS_delete(args))
  454. printf("Successful.\n");
  455. else
  456. printf("Failure. reason: %s.\n", PHYSFS_getLastError());
  457. return(1);
  458. } /* cmd_delete */
  459. static int cmd_getrealdir(char *args)
  460. {
  461. const char *rc;
  462. if (*args == '\"')
  463. {
  464. args++;
  465. args[strlen(args) - 1] = '\0';
  466. } /* if */
  467. rc = PHYSFS_getRealDir(args);
  468. if (rc)
  469. printf("Found at [%s].\n", rc);
  470. else
  471. printf("Not found.\n");
  472. return(1);
  473. } /* cmd_getrealdir */
  474. static int cmd_exists(char *args)
  475. {
  476. int rc;
  477. if (*args == '\"')
  478. {
  479. args++;
  480. args[strlen(args) - 1] = '\0';
  481. } /* if */
  482. rc = PHYSFS_exists(args);
  483. printf("File %sexists.\n", rc ? "" : "does not ");
  484. return(1);
  485. } /* cmd_exists */
  486. static int cmd_isdir(char *args)
  487. {
  488. int rc;
  489. if (*args == '\"')
  490. {
  491. args++;
  492. args[strlen(args) - 1] = '\0';
  493. } /* if */
  494. rc = PHYSFS_isDirectory(args);
  495. printf("File %s a directory.\n", rc ? "is" : "is NOT");
  496. return(1);
  497. } /* cmd_isdir */
  498. static int cmd_issymlink(char *args)
  499. {
  500. int rc;
  501. if (*args == '\"')
  502. {
  503. args++;
  504. args[strlen(args) - 1] = '\0';
  505. } /* if */
  506. rc = PHYSFS_isSymbolicLink(args);
  507. printf("File %s a symlink.\n", rc ? "is" : "is NOT");
  508. return(1);
  509. } /* cmd_issymlink */
  510. static int cmd_cat(char *args)
  511. {
  512. PHYSFS_file *f;
  513. if (*args == '\"')
  514. {
  515. args++;
  516. args[strlen(args) - 1] = '\0';
  517. } /* if */
  518. f = PHYSFS_openRead(args);
  519. if (f == NULL)
  520. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  521. else
  522. {
  523. if (do_buffer_size)
  524. {
  525. if (!PHYSFS_setBuffer(f, do_buffer_size))
  526. {
  527. printf("failed to set file buffer. Reason: [%s].\n",
  528. PHYSFS_getLastError());
  529. PHYSFS_close(f);
  530. return(1);
  531. } /* if */
  532. } /* if */
  533. while (1)
  534. {
  535. char buffer[128];
  536. PHYSFS_sint64 rc;
  537. PHYSFS_sint64 i;
  538. rc = PHYSFS_read(f, buffer, 1, sizeof (buffer));
  539. for (i = 0; i < rc; i++)
  540. fputc((int) buffer[i], stdout);
  541. if (rc < sizeof (buffer))
  542. {
  543. printf("\n\n");
  544. if (!PHYSFS_eof(f))
  545. {
  546. printf("\n (Error condition in reading. Reason: [%s])\n\n",
  547. PHYSFS_getLastError());
  548. } /* if */
  549. PHYSFS_close(f);
  550. return(1);
  551. } /* if */
  552. } /* while */
  553. } /* else */
  554. return(1);
  555. } /* cmd_cat */
  556. static int cmd_filelength(char *args)
  557. {
  558. PHYSFS_file *f;
  559. if (*args == '\"')
  560. {
  561. args++;
  562. args[strlen(args) - 1] = '\0';
  563. } /* if */
  564. f = PHYSFS_openRead(args);
  565. if (f == NULL)
  566. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  567. else
  568. {
  569. PHYSFS_sint64 len = PHYSFS_fileLength(f);
  570. if (len == -1)
  571. printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
  572. else
  573. printf(" (cast to int) %d bytes.\n", (int) len);
  574. PHYSFS_close(f);
  575. } /* else */
  576. return(1);
  577. } /* cmd_filelength */
  578. #define WRITESTR "The cat sat on the mat.\n\n"
  579. static int cmd_append(char *args)
  580. {
  581. PHYSFS_file *f;
  582. if (*args == '\"')
  583. {
  584. args++;
  585. args[strlen(args) - 1] = '\0';
  586. } /* if */
  587. f = PHYSFS_openAppend(args);
  588. if (f == NULL)
  589. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  590. else
  591. {
  592. size_t bw;
  593. PHYSFS_sint64 rc;
  594. if (do_buffer_size)
  595. {
  596. if (!PHYSFS_setBuffer(f, do_buffer_size))
  597. {
  598. printf("failed to set file buffer. Reason: [%s].\n",
  599. PHYSFS_getLastError());
  600. PHYSFS_close(f);
  601. return(1);
  602. } /* if */
  603. } /* if */
  604. bw = strlen(WRITESTR);
  605. rc = PHYSFS_write(f, WRITESTR, 1, bw);
  606. if (rc != bw)
  607. {
  608. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  609. (int) rc, (int) bw, PHYSFS_getLastError());
  610. } /* if */
  611. else
  612. {
  613. printf("Successful.\n");
  614. } /* else */
  615. PHYSFS_close(f);
  616. } /* else */
  617. return(1);
  618. } /* cmd_append */
  619. static int cmd_write(char *args)
  620. {
  621. PHYSFS_file *f;
  622. if (*args == '\"')
  623. {
  624. args++;
  625. args[strlen(args) - 1] = '\0';
  626. } /* if */
  627. f = PHYSFS_openWrite(args);
  628. if (f == NULL)
  629. printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
  630. else
  631. {
  632. size_t bw;
  633. PHYSFS_sint64 rc;
  634. if (do_buffer_size)
  635. {
  636. if (!PHYSFS_setBuffer(f, do_buffer_size))
  637. {
  638. printf("failed to set file buffer. Reason: [%s].\n",
  639. PHYSFS_getLastError());
  640. PHYSFS_close(f);
  641. return(1);
  642. } /* if */
  643. } /* if */
  644. bw = strlen(WRITESTR);
  645. rc = PHYSFS_write(f, WRITESTR, 1, bw);
  646. if (rc != bw)
  647. {
  648. printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
  649. (int) rc, (int) bw, PHYSFS_getLastError());
  650. } /* if */
  651. else
  652. {
  653. printf("Successful.\n");
  654. } /* else */
  655. PHYSFS_close(f);
  656. } /* else */
  657. return(1);
  658. } /* cmd_write */
  659. static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
  660. {
  661. time_t t = (time_t) modtime;
  662. char *str = ctime(&t);
  663. strncpy(modstr, str, strsize);
  664. modstr[strsize-1] = '\0';
  665. } /* modTimeToStr */
  666. static int cmd_getlastmodtime(char *args)
  667. {
  668. PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);
  669. if (rc == -1)
  670. printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
  671. else
  672. {
  673. char modstr[64];
  674. modTimeToStr(rc, modstr, sizeof (modstr));
  675. printf("Last modified: %s (%ld).\n", modstr, (long) rc);
  676. } /* else */
  677. return(1);
  678. } /* cmd_getLastModTime */
  679. /* must have spaces trimmed prior to this call. */
  680. static int count_args(const char *str)
  681. {
  682. int retval = 0;
  683. int in_quotes = 0;
  684. if (str != NULL)
  685. {
  686. for (; *str != '\0'; str++)
  687. {
  688. if (*str == '\"')
  689. in_quotes = !in_quotes;
  690. else if ((*str == ' ') && (!in_quotes))
  691. retval++;
  692. } /* for */
  693. retval++;
  694. } /* if */
  695. return(retval);
  696. } /* count_args */
  697. static int cmd_help(char *args);
  698. typedef struct
  699. {
  700. const char *cmd;
  701. int (*func)(char *args);
  702. int argcount;
  703. const char *usage;
  704. } command_info;
  705. static const command_info commands[] =
  706. {
  707. { "quit", cmd_quit, 0, NULL },
  708. { "q", cmd_quit, 0, NULL },
  709. { "help", cmd_help, 0, NULL },
  710. { "init", cmd_init, 1, "<argv0>" },
  711. { "deinit", cmd_deinit, 0, NULL },
  712. { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
  713. { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
  714. { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
  715. { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
  716. { "getlasterror", cmd_getlasterror, 0, NULL },
  717. { "getdirsep", cmd_getdirsep, 0, NULL },
  718. { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
  719. { "getsearchpath", cmd_getsearchpath, 0, NULL },
  720. { "getbasedir", cmd_getbasedir, 0, NULL },
  721. { "getuserdir", cmd_getuserdir, 0, NULL },
  722. { "getwritedir", cmd_getwritedir, 0, NULL },
  723. { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
  724. { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
  725. { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
  726. { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
  727. { "delete", cmd_delete, 1, "<dirToDelete>" },
  728. { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
  729. { "exists", cmd_exists, 1, "<fileToCheck>" },
  730. { "isdir", cmd_isdir, 1, "<fileToCheck>" },
  731. { "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
  732. { "cat", cmd_cat, 1, "<fileToCat>" },
  733. { "filelength", cmd_filelength, 1, "<fileToCheck>" },
  734. { "append", cmd_append, 1, "<fileToAppend>" },
  735. { "write", cmd_write, 1, "<fileToCreateOrTrash>" },
  736. { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
  737. { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
  738. { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
  739. { NULL, NULL, -1, NULL }
  740. };
  741. static void output_usage(const char *intro, const command_info *cmdinfo)
  742. {
  743. if (cmdinfo->argcount == 0)
  744. printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
  745. else
  746. printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
  747. } /* output_usage */
  748. static int cmd_help(char *args)
  749. {
  750. const command_info *i;
  751. printf("Commands:\n");
  752. for (i = commands; i->cmd != NULL; i++)
  753. output_usage(" -", i);
  754. return(1);
  755. } /* output_cmd_help */
  756. static void trim_command(const char *orig, char *copy)
  757. {
  758. const char *i;
  759. char *writeptr = copy;
  760. int spacecount = 0;
  761. int have_first = 0;
  762. for (i = orig; *i != '\0'; i++)
  763. {
  764. if (*i == ' ')
  765. {
  766. if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
  767. {
  768. if ((have_first) && (!spacecount))
  769. {
  770. spacecount++;
  771. *writeptr = ' ';
  772. writeptr++;
  773. } /* if */
  774. } /* if */
  775. } /* if */
  776. else
  777. {
  778. have_first = 1;
  779. spacecount = 0;
  780. *writeptr = *i;
  781. writeptr++;
  782. } /* else */
  783. } /* for */
  784. *writeptr = '\0';
  785. /*
  786. printf("\n command is [%s].\n", copy);
  787. */
  788. } /* trim_command */
  789. static int process_command(char *complete_cmd)
  790. {
  791. const command_info *i;
  792. char *cmd_copy;
  793. char *args;
  794. int rc = 1;
  795. if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
  796. {
  797. printf("\n");
  798. return(0);
  799. } /* if */
  800. cmd_copy = malloc(strlen(complete_cmd) + 1);
  801. if (cmd_copy == NULL)
  802. {
  803. printf("\n\n\nOUT OF MEMORY!\n\n\n");
  804. return(0);
  805. } /* if */
  806. trim_command(complete_cmd, cmd_copy);
  807. args = strchr(cmd_copy, ' ');
  808. if (args != NULL)
  809. {
  810. *args = '\0';
  811. args++;
  812. } /* else */
  813. if (cmd_copy[0] != '\0')
  814. {
  815. for (i = commands; i->cmd != NULL; i++)
  816. {
  817. if (strcmp(i->cmd, cmd_copy) == 0)
  818. {
  819. if ((i->argcount >= 0) && (count_args(args) != i->argcount))
  820. output_usage("usage:", i);
  821. else
  822. rc = i->func(args);
  823. break;
  824. } /* if */
  825. } /* for */
  826. if (i->cmd == NULL)
  827. printf("Unknown command. Enter \"help\" for instructions.\n");
  828. #if (defined PHYSFS_HAVE_READLINE)
  829. add_history(complete_cmd);
  830. if (history_file)
  831. {
  832. fprintf(history_file, "%s\n", complete_cmd);
  833. fflush(history_file);
  834. } /* if */
  835. #endif
  836. } /* if */
  837. free(cmd_copy);
  838. return(rc);
  839. } /* process_command */
  840. static void open_history_file(void)
  841. {
  842. #if (defined PHYSFS_HAVE_READLINE)
  843. #if 0
  844. const char *envr = getenv("TESTPHYSFS_HISTORY");
  845. if (!envr)
  846. return;
  847. #else
  848. char envr[256];
  849. strcpy(envr, PHYSFS_getUserDir());
  850. strcat(envr, ".testphys_history");
  851. #endif
  852. if (access(envr, F_OK) == 0)
  853. {
  854. char buf[512];
  855. FILE *f = fopen(envr, "r");
  856. if (!f)
  857. {
  858. printf("\n\n"
  859. "Could not open history file [%s] for reading!\n"
  860. " Will not have past history available.\n\n",
  861. envr);
  862. return;
  863. } /* if */
  864. do
  865. {
  866. fgets(buf, sizeof (buf), f);
  867. if (buf[strlen(buf) - 1] == '\n')
  868. buf[strlen(buf) - 1] = '\0';
  869. add_history(buf);
  870. } while (!feof(f));
  871. fclose(f);
  872. } /* if */
  873. history_file = fopen(envr, "ab");
  874. if (!history_file)
  875. {
  876. printf("\n\n"
  877. "Could not open history file [%s] for appending!\n"
  878. " Will not be able to record this session's history.\n\n",
  879. envr);
  880. } /* if */
  881. #endif
  882. } /* open_history_file */
  883. int main(int argc, char **argv)
  884. {
  885. char *buf = NULL;
  886. int rc = 0;
  887. #if (defined __MWERKS__)
  888. extern tSIOUXSettings SIOUXSettings;
  889. SIOUXSettings.asktosaveonclose = 0;
  890. SIOUXSettings.autocloseonquit = 1;
  891. SIOUXSettings.rows = 40;
  892. SIOUXSettings.columns = 120;
  893. #endif
  894. printf("\n");
  895. if (!PHYSFS_init(argv[0]))
  896. {
  897. printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
  898. return(1);
  899. } /* if */
  900. output_versions();
  901. output_archivers();
  902. open_history_file();
  903. printf("Enter commands. Enter \"help\" for instructions.\n");
  904. do
  905. {
  906. #if (defined PHYSFS_HAVE_READLINE)
  907. buf = readline("> ");
  908. #else
  909. int i;
  910. buf = malloc(512);
  911. memset(buf, '\0', 512);
  912. printf("> ");
  913. for (i = 0; i < 511; i++)
  914. {
  915. int ch = fgetc(stdin);
  916. if (ch == EOF)
  917. {
  918. strcpy(buf, "quit");
  919. break;
  920. } /* if */
  921. else if ((ch == '\n') || (ch == '\r'))
  922. {
  923. buf[i] = '\0';
  924. break;
  925. } /* else if */
  926. else if (ch == '\b')
  927. {
  928. if (i > 0)
  929. i--;
  930. } /* else if */
  931. else
  932. {
  933. buf[i] = (char) ch;
  934. } /* else */
  935. } /* for */
  936. #endif
  937. rc = process_command(buf);
  938. if (buf != NULL)
  939. free(buf);
  940. } while (rc);
  941. if (!PHYSFS_deinit())
  942. printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
  943. if (history_file)
  944. fclose(history_file);
  945. /*
  946. printf("\n\ntest_physfs written by ryan c. gordon.\n");
  947. printf(" it makes you shoot teh railgun bettar.\n");
  948. */
  949. return(0);
  950. } /* main */
  951. /* end of test_physfs.c ... */