test_physfs.c 30 KB

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