physfs.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /**
  2. * PhysicsFS; a portable, flexible file i/o abstraction.
  3. *
  4. * Documentation is in physfs.h. It's verbose, honest. :)
  5. *
  6. * Please see the file LICENSE in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. #if HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #if (defined PHYSFS_PROFILING)
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "physfs.h"
  21. #define __PHYSICSFS_INTERNAL__
  22. #include "physfs_internal.h"
  23. typedef struct __PHYSFS_ERRMSGTYPE__
  24. {
  25. PHYSFS_uint64 tid;
  26. int errorAvailable;
  27. char errorString[80];
  28. struct __PHYSFS_ERRMSGTYPE__ *next;
  29. } ErrMsg;
  30. typedef struct __PHYSFS_DIRINFO__
  31. {
  32. char *dirName;
  33. DirHandle *dirHandle;
  34. struct __PHYSFS_DIRINFO__ *next;
  35. } PhysDirInfo;
  36. typedef struct __PHYSFS_FILEHANDLELIST__
  37. {
  38. PHYSFS_file handle;
  39. struct __PHYSFS_FILEHANDLELIST__ *next;
  40. } FileHandleList;
  41. /* The various i/o drivers... */
  42. #if (defined PHYSFS_SUPPORTS_ZIP)
  43. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  44. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  45. #endif
  46. #if (defined PHYSFS_SUPPORTS_GRP)
  47. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  48. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  49. #endif
  50. #if (defined PHYSFS_SUPPORTS_QPAK)
  51. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
  52. extern const DirFunctions __PHYSFS_DirFunctions_QPAK;
  53. #endif
  54. #if (defined PHYSFS_SUPPORTS_HOG)
  55. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
  56. extern const DirFunctions __PHYSFS_DirFunctions_HOG;
  57. #endif
  58. #if (defined PHYSFS_SUPPORTS_MVL)
  59. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
  60. extern const DirFunctions __PHYSFS_DirFunctions_MVL;
  61. #endif
  62. #if (defined PHYSFS_SUPPORTS_WAD)
  63. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
  64. extern const DirFunctions __PHYSFS_DirFunctions_WAD;
  65. #endif
  66. #if (defined PHYSFS_SUPPORTS_MIX)
  67. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX;
  68. extern const DirFunctions __PHYSFS_DirFunctions_MIX;
  69. #endif
  70. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  71. static const PHYSFS_ArchiveInfo *supported_types[] =
  72. {
  73. #if (defined PHYSFS_SUPPORTS_ZIP)
  74. &__PHYSFS_ArchiveInfo_ZIP,
  75. #endif
  76. #if (defined PHYSFS_SUPPORTS_GRP)
  77. &__PHYSFS_ArchiveInfo_GRP,
  78. #endif
  79. #if (defined PHYSFS_SUPPORTS_QPAK)
  80. &__PHYSFS_ArchiveInfo_QPAK,
  81. #endif
  82. #if (defined PHYSFS_SUPPORTS_HOG)
  83. &__PHYSFS_ArchiveInfo_HOG,
  84. #endif
  85. #if (defined PHYSFS_SUPPORTS_MVL)
  86. &__PHYSFS_ArchiveInfo_MVL,
  87. #endif
  88. #if (defined PHYSFS_SUPPORTS_WAD)
  89. &__PHYSFS_ArchiveInfo_WAD,
  90. #endif
  91. #if (defined PHYSFS_SUPPORTS_MIX)
  92. &__PHYSFS_ArchiveInfo_MIX,
  93. #endif
  94. NULL
  95. };
  96. static const DirFunctions *dirFunctions[] =
  97. {
  98. #if (defined PHYSFS_SUPPORTS_ZIP)
  99. &__PHYSFS_DirFunctions_ZIP,
  100. #endif
  101. #if (defined PHYSFS_SUPPORTS_GRP)
  102. &__PHYSFS_DirFunctions_GRP,
  103. #endif
  104. #if (defined PHYSFS_SUPPORTS_QPAK)
  105. &__PHYSFS_DirFunctions_QPAK,
  106. #endif
  107. #if (defined PHYSFS_SUPPORTS_HOG)
  108. &__PHYSFS_DirFunctions_HOG,
  109. #endif
  110. #if (defined PHYSFS_SUPPORTS_MVL)
  111. &__PHYSFS_DirFunctions_MVL,
  112. #endif
  113. #if (defined PHYSFS_SUPPORTS_WAD)
  114. &__PHYSFS_DirFunctions_WAD,
  115. #endif
  116. #if (defined PHYSFS_SUPPORTS_MIX)
  117. &__PHYSFS_DirFunctions_MIX,
  118. #endif
  119. &__PHYSFS_DirFunctions_DIR,
  120. NULL
  121. };
  122. /* General PhysicsFS state ... */
  123. static int initialized = 0;
  124. static ErrMsg *errorMessages = NULL;
  125. static PhysDirInfo *searchPath = NULL;
  126. static PhysDirInfo *writeDir = NULL;
  127. static FileHandleList *openWriteList = NULL;
  128. static FileHandleList *openReadList = NULL;
  129. static char *baseDir = NULL;
  130. static char *userDir = NULL;
  131. static int allowSymLinks = 0;
  132. /* mutexes ... */
  133. static void *errorLock = NULL; /* protects error message list. */
  134. static void *stateLock = NULL; /* protects other PhysFS static state. */
  135. /* functions ... */
  136. static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  137. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  138. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  139. {
  140. PHYSFS_uint32 i;
  141. int sorted;
  142. do
  143. {
  144. sorted = 1;
  145. for (i = lo; i < hi; i++)
  146. {
  147. if (cmpfn(a, i, i + 1) > 0)
  148. {
  149. swapfn(a, i, i + 1);
  150. sorted = 0;
  151. } /* if */
  152. } /* for */
  153. } while (!sorted);
  154. } /* __PHYSFS_bubble_sort */
  155. static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  156. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  157. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  158. {
  159. PHYSFS_uint32 i;
  160. PHYSFS_uint32 j;
  161. PHYSFS_uint32 v;
  162. if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
  163. __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
  164. else
  165. {
  166. i = (hi + lo) / 2;
  167. if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
  168. if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
  169. if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
  170. j = hi - 1;
  171. swapfn(a, i, j);
  172. i = lo;
  173. v = j;
  174. while (1)
  175. {
  176. while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
  177. while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
  178. if (j < i)
  179. break;
  180. swapfn(a, i, j);
  181. } /* while */
  182. swapfn(a, i, hi-1);
  183. __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
  184. __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
  185. } /* else */
  186. } /* __PHYSFS_quick_sort */
  187. void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
  188. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  189. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  190. {
  191. /*
  192. * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
  193. * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  194. */
  195. __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
  196. } /* __PHYSFS_sort */
  197. #if (defined PHYSFS_PROFILING)
  198. #define PHYSFS_TEST_SORT_ITERATIONS 150
  199. #define PHYSFS_TEST_SORT_ELEMENTS (64 * 1024)
  200. static int __PHYSFS_test_sort_cmp(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  201. {
  202. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  203. PHYSFS_sint32 one = a[x];
  204. PHYSFS_sint32 two = a[y];
  205. if (one < two)
  206. return(-1);
  207. else if (one > two)
  208. return(1);
  209. return(0);
  210. } /* __PHYSFS_test_sort_cmp */
  211. static void __PHYSFS_test_sort_swap(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  212. {
  213. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  214. PHYSFS_sint32 tmp;
  215. tmp = a[x];
  216. a[x] = a[y];
  217. a[y] = tmp;
  218. } /* __PHYSFS_test_sort_swap */
  219. static int __PHYSFS_test_sort_do(PHYSFS_uint32 *timer,
  220. PHYSFS_sint32 *a, PHYSFS_uint32 max,
  221. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  222. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  223. {
  224. PHYSFS_uint32 i;
  225. struct timeval starttime, endtime;
  226. gettimeofday(&starttime, NULL);
  227. __PHYSFS_sort(a, max, cmpfn, swapfn);
  228. gettimeofday(&endtime, NULL);
  229. for (i = 1; i < max; i++)
  230. {
  231. if (a[i] < a[i - 1])
  232. return(0);
  233. } /* for */
  234. if (timer != NULL)
  235. {
  236. *timer = ( ((endtime.tv_sec - starttime.tv_sec) * 1000) +
  237. ((endtime.tv_usec - starttime.tv_usec) / 1000) );
  238. } /* if */
  239. return(1);
  240. } /* __PHYSFS_test_sort_time */
  241. static void __PHYSFS_test_sort(void)
  242. {
  243. PHYSFS_uint32 elasped[PHYSFS_TEST_SORT_ITERATIONS];
  244. PHYSFS_sint32 iter;
  245. PHYSFS_sint32 a[PHYSFS_TEST_SORT_ELEMENTS];
  246. PHYSFS_sint32 i, x;
  247. int success;
  248. printf("Testing __PHYSFS_sort (linear presorted) ... ");
  249. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  250. {
  251. /* set up array to sort. */
  252. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  253. a[i] = i;
  254. /* sort it. */
  255. success = __PHYSFS_test_sort_do(&elasped[iter],
  256. a, PHYSFS_TEST_SORT_ELEMENTS,
  257. __PHYSFS_test_sort_cmp,
  258. __PHYSFS_test_sort_swap);
  259. if (!success)
  260. break;
  261. } /* for */
  262. if (!success)
  263. printf("Failed!\n");
  264. else
  265. {
  266. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  267. x += elasped[iter];
  268. x /= PHYSFS_TEST_SORT_ITERATIONS;
  269. printf("Average run (%lu) ms.\n", (unsigned long) x);
  270. } /* else */
  271. printf("Testing __PHYSFS_sort (linear presorted reverse) ... ");
  272. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  273. {
  274. /* set up array to sort. */
  275. for (i = 0, x = PHYSFS_TEST_SORT_ELEMENTS;
  276. i < PHYSFS_TEST_SORT_ELEMENTS;
  277. i++, x--)
  278. {
  279. a[i] = x;
  280. } /* for */
  281. /* sort it. */
  282. success = __PHYSFS_test_sort_do(&elasped[iter],
  283. a, PHYSFS_TEST_SORT_ELEMENTS,
  284. __PHYSFS_test_sort_cmp,
  285. __PHYSFS_test_sort_swap);
  286. if (!success)
  287. break;
  288. } /* for */
  289. if (!success)
  290. printf("Failed!\n");
  291. else
  292. {
  293. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  294. x += elasped[iter];
  295. x /= PHYSFS_TEST_SORT_ITERATIONS;
  296. printf("Average run (%lu) ms.\n", (unsigned long) x);
  297. } /* else */
  298. printf("Testing __PHYSFS_sort (randomized) ... ");
  299. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  300. {
  301. /* set up array to sort. */
  302. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  303. a[i] = (PHYSFS_uint32) rand();
  304. /* sort it. */
  305. success = __PHYSFS_test_sort_do(&elasped[iter],
  306. a, PHYSFS_TEST_SORT_ELEMENTS,
  307. __PHYSFS_test_sort_cmp,
  308. __PHYSFS_test_sort_swap);
  309. if (!success)
  310. break;
  311. } /* for */
  312. if (!success)
  313. printf("Failed!\n");
  314. else
  315. {
  316. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  317. x += elasped[iter];
  318. x /= PHYSFS_TEST_SORT_ITERATIONS;
  319. printf("Average run (%lu) ms.\n", (unsigned long) x);
  320. } /* else */
  321. printf("__PHYSFS_test_sort() complete.\n\n");
  322. } /* __PHYSFS_test_sort */
  323. #endif
  324. static ErrMsg *findErrorForCurrentThread(void)
  325. {
  326. ErrMsg *i;
  327. PHYSFS_uint64 tid;
  328. if (errorLock != NULL)
  329. __PHYSFS_platformGrabMutex(errorLock);
  330. if (errorMessages != NULL)
  331. {
  332. tid = __PHYSFS_platformGetThreadID();
  333. for (i = errorMessages; i != NULL; i = i->next)
  334. {
  335. if (i->tid == tid)
  336. {
  337. if (errorLock != NULL)
  338. __PHYSFS_platformReleaseMutex(errorLock);
  339. return(i);
  340. } /* if */
  341. } /* for */
  342. } /* if */
  343. if (errorLock != NULL)
  344. __PHYSFS_platformReleaseMutex(errorLock);
  345. return(NULL); /* no error available. */
  346. } /* findErrorForCurrentThread */
  347. void __PHYSFS_setError(const char *str)
  348. {
  349. ErrMsg *err;
  350. if (str == NULL)
  351. return;
  352. err = findErrorForCurrentThread();
  353. if (err == NULL)
  354. {
  355. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  356. if (err == NULL)
  357. return; /* uhh...? */
  358. memset((void *) err, '\0', sizeof (ErrMsg));
  359. err->tid = __PHYSFS_platformGetThreadID();
  360. if (errorLock != NULL)
  361. __PHYSFS_platformGrabMutex(errorLock);
  362. err->next = errorMessages;
  363. errorMessages = err;
  364. if (errorLock != NULL)
  365. __PHYSFS_platformReleaseMutex(errorLock);
  366. } /* if */
  367. err->errorAvailable = 1;
  368. strncpy(err->errorString, str, sizeof (err->errorString));
  369. err->errorString[sizeof (err->errorString) - 1] = '\0';
  370. } /* __PHYSFS_setError */
  371. const char *PHYSFS_getLastError(void)
  372. {
  373. ErrMsg *err = findErrorForCurrentThread();
  374. if ((err == NULL) || (!err->errorAvailable))
  375. return(NULL);
  376. err->errorAvailable = 0;
  377. return(err->errorString);
  378. } /* PHYSFS_getLastError */
  379. /* MAKE SURE that errorLock is held before calling this! */
  380. static void freeErrorMessages(void)
  381. {
  382. ErrMsg *i;
  383. ErrMsg *next;
  384. for (i = errorMessages; i != NULL; i = next)
  385. {
  386. next = i->next;
  387. free(i);
  388. } /* for */
  389. errorMessages = NULL;
  390. } /* freeErrorMessages */
  391. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  392. {
  393. if (ver != NULL)
  394. {
  395. ver->major = PHYSFS_VER_MAJOR;
  396. ver->minor = PHYSFS_VER_MINOR;
  397. ver->patch = PHYSFS_VER_PATCH;
  398. } /* if */
  399. } /* PHYSFS_getLinkedVersion */
  400. static const char *find_filename_extension(const char *fname)
  401. {
  402. const char *retval = strchr(fname, '.');
  403. const char *p = retval;
  404. while (p != NULL)
  405. {
  406. p = strchr(p + 1, '.');
  407. if (p != NULL)
  408. retval = p;
  409. } /* while */
  410. if (retval != NULL)
  411. retval++; /* skip '.' */
  412. return(retval);
  413. } /* find_filename_extension */
  414. static DirHandle *openDirectory(const char *d, int forWriting)
  415. {
  416. const DirFunctions **i;
  417. const char *ext;
  418. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  419. ext = find_filename_extension(d);
  420. if (ext != NULL)
  421. {
  422. /* Look for archivers with matching file extensions first... */
  423. for (i = dirFunctions; *i != NULL; i++)
  424. {
  425. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
  426. {
  427. if ((*i)->isArchive(d, forWriting))
  428. return( (*i)->openArchive(d, forWriting) );
  429. } /* if */
  430. } /* for */
  431. /* failing an exact file extension match, try all the others... */
  432. for (i = dirFunctions; *i != NULL; i++)
  433. {
  434. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
  435. {
  436. if ((*i)->isArchive(d, forWriting))
  437. return( (*i)->openArchive(d, forWriting) );
  438. } /* if */
  439. } /* for */
  440. } /* if */
  441. else /* no extension? Try them all. */
  442. {
  443. for (i = dirFunctions; *i != NULL; i++)
  444. {
  445. if ((*i)->isArchive(d, forWriting))
  446. return( (*i)->openArchive(d, forWriting) );
  447. } /* for */
  448. } /* else */
  449. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  450. return(NULL);
  451. } /* openDirectory */
  452. static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
  453. {
  454. DirHandle *dirHandle = NULL;
  455. PhysDirInfo *di = NULL;
  456. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  457. dirHandle = openDirectory(newDir, forWriting);
  458. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  459. di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
  460. if (di == NULL)
  461. {
  462. dirHandle->funcs->dirClose(dirHandle);
  463. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  464. } /* if */
  465. di->dirName = (char *) malloc(strlen(newDir) + 1);
  466. if (di->dirName == NULL)
  467. {
  468. free(di);
  469. dirHandle->funcs->dirClose(dirHandle);
  470. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  471. } /* if */
  472. di->next = NULL;
  473. di->dirHandle = dirHandle;
  474. strcpy(di->dirName, newDir);
  475. return(di);
  476. } /* buildDirInfo */
  477. /* MAKE SURE you've got the stateLock held before calling this! */
  478. static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
  479. {
  480. FileHandleList *i;
  481. if (di == NULL)
  482. return(1);
  483. for (i = openList; i != NULL; i = i->next)
  484. {
  485. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  486. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  487. } /* for */
  488. di->dirHandle->funcs->dirClose(di->dirHandle);
  489. free(di->dirName);
  490. free(di);
  491. return(1);
  492. } /* freeDirInfo */
  493. static char *calculateUserDir(void)
  494. {
  495. char *retval = NULL;
  496. const char *str = NULL;
  497. str = __PHYSFS_platformGetUserDir();
  498. if (str != NULL)
  499. retval = (char *) str;
  500. else
  501. {
  502. const char *dirsep = PHYSFS_getDirSeparator();
  503. const char *uname = __PHYSFS_platformGetUserName();
  504. str = (uname != NULL) ? uname : "default";
  505. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  506. strlen(dirsep) + 6);
  507. if (retval == NULL)
  508. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  509. else
  510. sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
  511. if (uname != NULL)
  512. free((void *) uname);
  513. } /* else */
  514. return(retval);
  515. } /* calculateUserDir */
  516. static int appendDirSep(char **dir)
  517. {
  518. const char *dirsep = PHYSFS_getDirSeparator();
  519. char *ptr;
  520. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  521. return(1);
  522. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  523. if (!ptr)
  524. {
  525. free(*dir);
  526. return(0);
  527. } /* if */
  528. strcat(ptr, dirsep);
  529. *dir = ptr;
  530. return(1);
  531. } /* appendDirSep */
  532. static char *calculateBaseDir(const char *argv0)
  533. {
  534. const char *dirsep = PHYSFS_getDirSeparator();
  535. char *retval;
  536. char *ptr;
  537. /*
  538. * See if the platform driver wants to handle this for us...
  539. */
  540. retval = __PHYSFS_platformCalcBaseDir(argv0);
  541. if (retval != NULL)
  542. return(retval);
  543. /*
  544. * Determine if there's a path on argv0. If there is, that's the base dir.
  545. */
  546. ptr = strstr(argv0, dirsep);
  547. if (ptr != NULL)
  548. {
  549. char *p = ptr;
  550. size_t size;
  551. while (p != NULL)
  552. {
  553. ptr = p;
  554. p = strstr(p + 1, dirsep);
  555. } /* while */
  556. size = (size_t) (ptr - argv0);
  557. retval = (char *) malloc(size + 1);
  558. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  559. memcpy(retval, argv0, size);
  560. retval[size] = '\0';
  561. return(retval);
  562. } /* if */
  563. /*
  564. * Last ditch effort: it's the current working directory. (*shrug*)
  565. */
  566. retval = __PHYSFS_platformCurrentDir();
  567. if(retval != NULL) {
  568. return(retval);
  569. }
  570. /*
  571. * Ok, current directory doesn't exist, use the root directory.
  572. * Not a good alternative, but it only happens if the current
  573. * directory was deleted from under the program.
  574. */
  575. retval = (char *) malloc(strlen(dirsep) + 1);
  576. strcpy(retval, dirsep);
  577. return(retval);
  578. } /* calculateBaseDir */
  579. static int initializeMutexes(void)
  580. {
  581. errorLock = __PHYSFS_platformCreateMutex();
  582. if (errorLock == NULL)
  583. goto initializeMutexes_failed;
  584. stateLock = __PHYSFS_platformCreateMutex();
  585. if (stateLock == NULL)
  586. goto initializeMutexes_failed;
  587. return(1); /* success. */
  588. initializeMutexes_failed:
  589. if (errorLock != NULL)
  590. __PHYSFS_platformDestroyMutex(errorLock);
  591. if (stateLock != NULL)
  592. __PHYSFS_platformDestroyMutex(stateLock);
  593. errorLock = stateLock = NULL;
  594. return(0); /* failed. */
  595. } /* initializeMutexes */
  596. int PHYSFS_init(const char *argv0)
  597. {
  598. char *ptr;
  599. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  600. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  601. BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
  602. baseDir = calculateBaseDir(argv0);
  603. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  604. ptr = __PHYSFS_platformRealPath(baseDir);
  605. free(baseDir);
  606. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  607. baseDir = ptr;
  608. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  609. userDir = calculateUserDir();
  610. if (userDir != NULL)
  611. {
  612. ptr = __PHYSFS_platformRealPath(userDir);
  613. free(userDir);
  614. userDir = ptr;
  615. } /* if */
  616. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  617. {
  618. free(baseDir);
  619. baseDir = NULL;
  620. return(0);
  621. } /* if */
  622. initialized = 1;
  623. /* This makes sure that the error subsystem is initialized. */
  624. __PHYSFS_setError(PHYSFS_getLastError());
  625. #if (defined PHYSFS_PROFILING)
  626. srand(time(NULL));
  627. setbuf(stdout, NULL);
  628. printf("\n");
  629. printf("********************************************************\n");
  630. printf("Warning! Profiling is built into this copy of PhysicsFS!\n");
  631. printf("********************************************************\n");
  632. printf("\n");
  633. printf("\n");
  634. __PHYSFS_test_sort();
  635. #endif
  636. return(1);
  637. } /* PHYSFS_init */
  638. /* MAKE SURE you hold stateLock before calling this! */
  639. static int closeFileHandleList(FileHandleList **list)
  640. {
  641. FileHandleList *i;
  642. FileHandleList *next = NULL;
  643. FileHandle *h;
  644. for (i = *list; i != NULL; i = next)
  645. {
  646. next = i->next;
  647. h = (FileHandle *) (i->handle.opaque);
  648. if (!h->funcs->fileClose(h))
  649. {
  650. *list = i;
  651. return(0);
  652. } /* if */
  653. free(i);
  654. } /* for */
  655. *list = NULL;
  656. return(1);
  657. } /* closeFileHandleList */
  658. /* MAKE SURE you hold the stateLock before calling this! */
  659. static void freeSearchPath(void)
  660. {
  661. PhysDirInfo *i;
  662. PhysDirInfo *next = NULL;
  663. closeFileHandleList(&openReadList);
  664. if (searchPath != NULL)
  665. {
  666. for (i = searchPath; i != NULL; i = next)
  667. {
  668. next = i->next;
  669. freeDirInfo(i, openReadList);
  670. } /* for */
  671. searchPath = NULL;
  672. } /* if */
  673. } /* freeSearchPath */
  674. int PHYSFS_deinit(void)
  675. {
  676. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  677. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  678. closeFileHandleList(&openWriteList);
  679. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  680. freeSearchPath();
  681. freeErrorMessages();
  682. if (baseDir != NULL)
  683. {
  684. free(baseDir);
  685. baseDir = NULL;
  686. } /* if */
  687. if (userDir != NULL)
  688. {
  689. free(userDir);
  690. userDir = NULL;
  691. } /* if */
  692. allowSymLinks = 0;
  693. initialized = 0;
  694. __PHYSFS_platformDestroyMutex(errorLock);
  695. __PHYSFS_platformDestroyMutex(stateLock);
  696. errorLock = stateLock = NULL;
  697. return(1);
  698. } /* PHYSFS_deinit */
  699. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  700. {
  701. return(supported_types);
  702. } /* PHYSFS_supportedArchiveTypes */
  703. void PHYSFS_freeList(void *list)
  704. {
  705. void **i;
  706. for (i = (void **) list; *i != NULL; i++)
  707. free(*i);
  708. free(list);
  709. } /* PHYSFS_freeList */
  710. const char *PHYSFS_getDirSeparator(void)
  711. {
  712. return(__PHYSFS_platformDirSeparator);
  713. } /* PHYSFS_getDirSeparator */
  714. char **PHYSFS_getCdRomDirs(void)
  715. {
  716. return(__PHYSFS_platformDetectAvailableCDs());
  717. } /* PHYSFS_getCdRomDirs */
  718. const char *PHYSFS_getBaseDir(void)
  719. {
  720. return(baseDir); /* this is calculated in PHYSFS_init()... */
  721. } /* PHYSFS_getBaseDir */
  722. const char *PHYSFS_getUserDir(void)
  723. {
  724. return(userDir); /* this is calculated in PHYSFS_init()... */
  725. } /* PHYSFS_getUserDir */
  726. const char *PHYSFS_getWriteDir(void)
  727. {
  728. const char *retval = NULL;
  729. __PHYSFS_platformGrabMutex(stateLock);
  730. if (writeDir != NULL)
  731. retval = writeDir->dirName;
  732. __PHYSFS_platformReleaseMutex(stateLock);
  733. return(retval);
  734. } /* PHYSFS_getWriteDir */
  735. int PHYSFS_setWriteDir(const char *newDir)
  736. {
  737. int retval = 1;
  738. __PHYSFS_platformGrabMutex(stateLock);
  739. if (writeDir != NULL)
  740. {
  741. BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
  742. stateLock, 0);
  743. writeDir = NULL;
  744. } /* if */
  745. if (newDir != NULL)
  746. {
  747. writeDir = buildDirInfo(newDir, 1);
  748. retval = (writeDir != NULL);
  749. } /* if */
  750. __PHYSFS_platformReleaseMutex(stateLock);
  751. return(retval);
  752. } /* PHYSFS_setWriteDir */
  753. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  754. {
  755. PhysDirInfo *di;
  756. PhysDirInfo *prev = NULL;
  757. PhysDirInfo *i;
  758. __PHYSFS_platformGrabMutex(stateLock);
  759. for (i = searchPath; i != NULL; i = i->next)
  760. {
  761. /* already in search path? */
  762. BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
  763. prev = i;
  764. } /* for */
  765. di = buildDirInfo(newDir, 0);
  766. BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
  767. if (appendToPath)
  768. {
  769. di->next = NULL;
  770. if (prev == NULL)
  771. searchPath = di;
  772. else
  773. prev->next = di;
  774. } /* if */
  775. else
  776. {
  777. di->next = searchPath;
  778. searchPath = di;
  779. } /* else */
  780. __PHYSFS_platformReleaseMutex(stateLock);
  781. return(1);
  782. } /* PHYSFS_addToSearchPath */
  783. int PHYSFS_removeFromSearchPath(const char *oldDir)
  784. {
  785. PhysDirInfo *i;
  786. PhysDirInfo *prev = NULL;
  787. PhysDirInfo *next = NULL;
  788. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  789. __PHYSFS_platformGrabMutex(stateLock);
  790. for (i = searchPath; i != NULL; i = i->next)
  791. {
  792. if (strcmp(i->dirName, oldDir) == 0)
  793. {
  794. next = i->next;
  795. BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
  796. stateLock, 0);
  797. if (prev == NULL)
  798. searchPath = next;
  799. else
  800. prev->next = next;
  801. BAIL_MACRO_MUTEX(NULL, stateLock, 1);
  802. } /* if */
  803. prev = i;
  804. } /* for */
  805. BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
  806. } /* PHYSFS_removeFromSearchPath */
  807. char **PHYSFS_getSearchPath(void)
  808. {
  809. int count = 1;
  810. int x;
  811. PhysDirInfo *i;
  812. char **retval;
  813. __PHYSFS_platformGrabMutex(stateLock);
  814. for (i = searchPath; i != NULL; i = i->next)
  815. count++;
  816. retval = (char **) malloc(sizeof (char *) * count);
  817. BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
  818. count--;
  819. retval[count] = NULL;
  820. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  821. {
  822. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  823. if (retval[x] == NULL) /* this is friggin' ugly. */
  824. {
  825. while (x > 0)
  826. {
  827. x--;
  828. free(retval[x]);
  829. } /* while */
  830. free(retval);
  831. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  832. } /* if */
  833. strcpy(retval[x], i->dirName);
  834. } /* for */
  835. __PHYSFS_platformReleaseMutex(stateLock);
  836. return(retval);
  837. } /* PHYSFS_getSearchPath */
  838. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  839. const char *archiveExt, int includeCdRoms,
  840. int archivesFirst)
  841. {
  842. const char *basedir = PHYSFS_getBaseDir();
  843. const char *userdir = PHYSFS_getUserDir();
  844. const char *dirsep = PHYSFS_getDirSeparator();
  845. char *str;
  846. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  847. /* set write dir... */
  848. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  849. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  850. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  851. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  852. if (!PHYSFS_setWriteDir(str))
  853. {
  854. int no_write = 0;
  855. sprintf(str, ".%s/%s", organization, appName);
  856. if ( (PHYSFS_setWriteDir(userdir)) &&
  857. (PHYSFS_mkdir(str)) )
  858. {
  859. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  860. if (!PHYSFS_setWriteDir(str))
  861. no_write = 1;
  862. } /* if */
  863. else
  864. {
  865. no_write = 1;
  866. } /* else */
  867. if (no_write)
  868. {
  869. PHYSFS_setWriteDir(NULL); /* just in case. */
  870. free(str);
  871. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  872. } /* if */
  873. } /* if */
  874. /* Put write dir first in search path... */
  875. PHYSFS_addToSearchPath(str, 0);
  876. free(str);
  877. /* Put base path on search path... */
  878. PHYSFS_addToSearchPath(basedir, 1);
  879. /* handle CD-ROMs... */
  880. if (includeCdRoms)
  881. {
  882. char **cds = PHYSFS_getCdRomDirs();
  883. char **i;
  884. for (i = cds; *i != NULL; i++)
  885. PHYSFS_addToSearchPath(*i, 1);
  886. PHYSFS_freeList(cds);
  887. } /* if */
  888. /* Root out archives, and add them to search path... */
  889. if (archiveExt != NULL)
  890. {
  891. char **rc = PHYSFS_enumerateFiles("/");
  892. char **i;
  893. size_t extlen = strlen(archiveExt);
  894. char *ext;
  895. for (i = rc; *i != NULL; i++)
  896. {
  897. size_t l = strlen(*i);
  898. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  899. {
  900. ext = (*i) + (l - extlen);
  901. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  902. {
  903. const char *d = PHYSFS_getRealDir(*i);
  904. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  905. if (str != NULL)
  906. {
  907. sprintf(str, "%s%s%s", d, dirsep, *i);
  908. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  909. free(str);
  910. } /* if */
  911. } /* if */
  912. } /* if */
  913. } /* for */
  914. PHYSFS_freeList(rc);
  915. } /* if */
  916. return(1);
  917. } /* PHYSFS_setSaneConfig */
  918. void PHYSFS_permitSymbolicLinks(int allow)
  919. {
  920. allowSymLinks = allow;
  921. } /* PHYSFS_permitSymbolicLinks */
  922. /* string manipulation in C makes my ass itch. */
  923. char * __PHYSFS_convertToDependent(const char *prepend,
  924. const char *dirName,
  925. const char *append)
  926. {
  927. const char *dirsep = __PHYSFS_platformDirSeparator;
  928. size_t sepsize = strlen(dirsep);
  929. char *str;
  930. char *i1;
  931. char *i2;
  932. size_t allocSize;
  933. while (*dirName == '/')
  934. dirName++;
  935. allocSize = strlen(dirName) + 1;
  936. if (prepend != NULL)
  937. allocSize += strlen(prepend) + sepsize;
  938. if (append != NULL)
  939. allocSize += strlen(append) + sepsize;
  940. /* make sure there's enough space if the dir separator is bigger. */
  941. if (sepsize > 1)
  942. {
  943. str = (char *) dirName;
  944. do
  945. {
  946. str = strchr(str, '/');
  947. if (str != NULL)
  948. {
  949. allocSize += (sepsize - 1);
  950. str++;
  951. } /* if */
  952. } while (str != NULL);
  953. } /* if */
  954. str = (char *) malloc(allocSize);
  955. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  956. if (prepend == NULL)
  957. *str = '\0';
  958. else
  959. {
  960. strcpy(str, prepend);
  961. strcat(str, dirsep);
  962. } /* else */
  963. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  964. {
  965. if (*i1 == '/')
  966. {
  967. strcpy(i2, dirsep);
  968. i2 += sepsize;
  969. } /* if */
  970. else
  971. {
  972. *i2 = *i1;
  973. } /* else */
  974. } /* for */
  975. *i2 = '\0';
  976. if (append)
  977. {
  978. strcat(str, dirsep);
  979. strcpy(str, append);
  980. } /* if */
  981. return(str);
  982. } /* __PHYSFS_convertToDependent */
  983. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname, int allowMissing)
  984. {
  985. int retval = 1;
  986. char *start;
  987. char *end;
  988. char *str;
  989. if (*fname == '\0') /* quick rejection. */
  990. return(1);
  991. /* !!! FIXME: Can we ditch this malloc()? */
  992. start = str = malloc(strlen(fname) + 1);
  993. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  994. strcpy(str, fname);
  995. while (1)
  996. {
  997. end = strchr(start, '/');
  998. if (end != NULL)
  999. *end = '\0';
  1000. if ( (strcmp(start, ".") == 0) ||
  1001. (strcmp(start, "..") == 0) ||
  1002. (strchr(start, '\\') != NULL) ||
  1003. (strchr(start, ':') != NULL) )
  1004. {
  1005. __PHYSFS_setError(ERR_INSECURE_FNAME);
  1006. retval = 0;
  1007. break;
  1008. } /* if */
  1009. if (!allowSymLinks)
  1010. {
  1011. if (h->funcs->isSymLink(h, str, &retval))
  1012. {
  1013. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  1014. free(str);
  1015. return(0); /* insecure. */
  1016. } /* if */
  1017. /* break out early if path element is missing. */
  1018. if (!retval)
  1019. {
  1020. /*
  1021. * We need to clear it if it's the last element of the path,
  1022. * since this might be a non-existant file we're opening
  1023. * for writing...
  1024. */
  1025. if ((end == NULL) || (allowMissing))
  1026. retval = 1;
  1027. break;
  1028. } /* if */
  1029. } /* if */
  1030. if (end == NULL)
  1031. break;
  1032. *end = '/';
  1033. start = end + 1;
  1034. } /* while */
  1035. free(str);
  1036. return(retval);
  1037. } /* __PHYSFS_verifySecurity */
  1038. int PHYSFS_mkdir(const char *dname)
  1039. {
  1040. DirHandle *h;
  1041. char *str;
  1042. char *start;
  1043. char *end;
  1044. int retval = 0;
  1045. int exists = 1; /* force existance check on first path element. */
  1046. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  1047. while (*dname == '/')
  1048. dname++;
  1049. __PHYSFS_platformGrabMutex(stateLock);
  1050. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1051. h = writeDir->dirHandle;
  1052. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,dname,1),NULL,stateLock,0);
  1053. start = str = malloc(strlen(dname) + 1);
  1054. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  1055. strcpy(str, dname);
  1056. while (1)
  1057. {
  1058. end = strchr(start, '/');
  1059. if (end != NULL)
  1060. *end = '\0';
  1061. /* only check for existance if all parent dirs existed, too... */
  1062. if (exists)
  1063. retval = h->funcs->isDirectory(h, str, &exists);
  1064. if (!exists)
  1065. retval = h->funcs->mkdir(h, str);
  1066. if (!retval)
  1067. break;
  1068. if (end == NULL)
  1069. break;
  1070. *end = '/';
  1071. start = end + 1;
  1072. } /* while */
  1073. __PHYSFS_platformReleaseMutex(stateLock);
  1074. free(str);
  1075. return(retval);
  1076. } /* PHYSFS_mkdir */
  1077. int PHYSFS_delete(const char *fname)
  1078. {
  1079. int retval;
  1080. DirHandle *h;
  1081. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1082. while (*fname == '/')
  1083. fname++;
  1084. __PHYSFS_platformGrabMutex(stateLock);
  1085. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1086. h = writeDir->dirHandle;
  1087. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,fname,0),NULL,stateLock,0);
  1088. retval = h->funcs->remove(h, fname);
  1089. __PHYSFS_platformReleaseMutex(stateLock);
  1090. return(retval);
  1091. } /* PHYSFS_delete */
  1092. const char *PHYSFS_getRealDir(const char *filename)
  1093. {
  1094. PhysDirInfo *i;
  1095. const char *retval = NULL;
  1096. while (*filename == '/')
  1097. filename++;
  1098. __PHYSFS_platformGrabMutex(stateLock);
  1099. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1100. {
  1101. DirHandle *h = i->dirHandle;
  1102. if (__PHYSFS_verifySecurity(h, filename, 0))
  1103. {
  1104. if (h->funcs->exists(h, filename))
  1105. retval = i->dirName;
  1106. } /* if */
  1107. } /* for */
  1108. __PHYSFS_platformReleaseMutex(stateLock);
  1109. return(retval);
  1110. } /* PHYSFS_getRealDir */
  1111. static int countList(LinkedStringList *list)
  1112. {
  1113. int retval = 0;
  1114. LinkedStringList *i;
  1115. for (i = list; i != NULL; i = i->next)
  1116. retval++;
  1117. return(retval);
  1118. } /* countList */
  1119. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  1120. {
  1121. int i;
  1122. LinkedStringList *next = NULL;
  1123. int len = countList(finalList);
  1124. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  1125. if (retval == NULL)
  1126. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  1127. for (i = 0; i < len; i++)
  1128. {
  1129. next = finalList->next;
  1130. if (retval == NULL)
  1131. free(finalList->str);
  1132. else
  1133. retval[i] = finalList->str;
  1134. free(finalList);
  1135. finalList = next;
  1136. } /* for */
  1137. if (retval != NULL)
  1138. retval[i] = NULL;
  1139. return(retval);
  1140. } /* convertStringListToPhysFSList */
  1141. static void insertStringListItem(LinkedStringList **final,
  1142. LinkedStringList *item)
  1143. {
  1144. LinkedStringList *i;
  1145. LinkedStringList *prev = NULL;
  1146. int rc;
  1147. for (i = *final; i != NULL; i = i->next)
  1148. {
  1149. rc = strcmp(i->str, item->str);
  1150. if (rc > 0) /* insertion point. */
  1151. break;
  1152. else if (rc == 0) /* already in list. */
  1153. {
  1154. free(item->str);
  1155. free(item);
  1156. return;
  1157. } /* else if */
  1158. prev = i;
  1159. } /* for */
  1160. /*
  1161. * If we are here, we are either at the insertion point.
  1162. * This may be the end of the list, or the list may be empty, too.
  1163. */
  1164. if (prev == NULL)
  1165. *final = item;
  1166. else
  1167. prev->next = item;
  1168. item->next = i;
  1169. } /* insertStringListItem */
  1170. /* if we run out of memory anywhere in here, we give back what we can. */
  1171. static void interpolateStringLists(LinkedStringList **final,
  1172. LinkedStringList *newList)
  1173. {
  1174. LinkedStringList *next = NULL;
  1175. while (newList != NULL)
  1176. {
  1177. next = newList->next;
  1178. insertStringListItem(final, newList);
  1179. newList = next;
  1180. } /* while */
  1181. } /* interpolateStringLists */
  1182. char **PHYSFS_enumerateFiles(const char *path)
  1183. {
  1184. PhysDirInfo *i;
  1185. char **retval = NULL;
  1186. LinkedStringList *rc;
  1187. LinkedStringList *finalList = NULL;
  1188. int omitSymLinks = !allowSymLinks;
  1189. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  1190. while (*path == '/')
  1191. path++;
  1192. __PHYSFS_platformGrabMutex(stateLock);
  1193. for (i = searchPath; i != NULL; i = i->next)
  1194. {
  1195. DirHandle *h = i->dirHandle;
  1196. if (__PHYSFS_verifySecurity(h, path, 0))
  1197. {
  1198. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  1199. interpolateStringLists(&finalList, rc);
  1200. } /* if */
  1201. } /* for */
  1202. __PHYSFS_platformReleaseMutex(stateLock);
  1203. retval = convertStringListToPhysFSList(finalList);
  1204. return(retval);
  1205. } /* PHYSFS_enumerateFiles */
  1206. int PHYSFS_exists(const char *fname)
  1207. {
  1208. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1209. while (*fname == '/')
  1210. fname++;
  1211. return(PHYSFS_getRealDir(fname) != NULL);
  1212. } /* PHYSFS_exists */
  1213. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1214. {
  1215. PhysDirInfo *i;
  1216. PHYSFS_sint64 retval = -1;
  1217. int fileExists = 0;
  1218. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1219. while (*fname == '/')
  1220. fname++;
  1221. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1222. return(1);
  1223. __PHYSFS_platformGrabMutex(stateLock);
  1224. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1225. {
  1226. DirHandle *h = i->dirHandle;
  1227. if (__PHYSFS_verifySecurity(h, fname, 0))
  1228. retval = h->funcs->getLastModTime(h, fname, &fileExists);
  1229. } /* for */
  1230. __PHYSFS_platformReleaseMutex(stateLock);
  1231. return(retval);
  1232. } /* PHYSFS_getLastModTime */
  1233. int PHYSFS_isDirectory(const char *fname)
  1234. {
  1235. PhysDirInfo *i;
  1236. int retval = 0;
  1237. int fileExists = 0;
  1238. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1239. while (*fname == '/')
  1240. fname++;
  1241. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1242. __PHYSFS_platformGrabMutex(stateLock);
  1243. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1244. {
  1245. DirHandle *h = i->dirHandle;
  1246. if (__PHYSFS_verifySecurity(h, fname, 0))
  1247. retval = h->funcs->isDirectory(h, fname, &fileExists);
  1248. } /* for */
  1249. __PHYSFS_platformReleaseMutex(stateLock);
  1250. return(retval);
  1251. } /* PHYSFS_isDirectory */
  1252. int PHYSFS_isSymbolicLink(const char *fname)
  1253. {
  1254. PhysDirInfo *i;
  1255. int retval = 0;
  1256. int fileExists = 0;
  1257. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1258. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1259. while (*fname == '/')
  1260. fname++;
  1261. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1262. __PHYSFS_platformGrabMutex(stateLock);
  1263. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1264. {
  1265. DirHandle *h = i->dirHandle;
  1266. if (__PHYSFS_verifySecurity(h, fname, 0))
  1267. retval = h->funcs->isSymLink(h, fname, &fileExists);
  1268. } /* for */
  1269. __PHYSFS_platformReleaseMutex(stateLock);
  1270. return(retval);
  1271. } /* PHYSFS_isSymbolicLink */
  1272. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1273. {
  1274. PHYSFS_file *retval = NULL;
  1275. FileHandle *rc = NULL;
  1276. DirHandle *h;
  1277. const DirFunctions *f;
  1278. FileHandleList *list;
  1279. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1280. while (*fname == '/')
  1281. fname++;
  1282. __PHYSFS_platformGrabMutex(stateLock);
  1283. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  1284. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1285. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname, 0), NULL,
  1286. stateLock, NULL);
  1287. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1288. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1289. f = h->funcs;
  1290. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  1291. if (rc == NULL)
  1292. free(list);
  1293. else
  1294. {
  1295. rc->buffer = NULL; /* just in case. */
  1296. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1297. rc->forReading = 0;
  1298. list->handle.opaque = (void *) rc;
  1299. list->next = openWriteList;
  1300. openWriteList = list;
  1301. retval = &(list->handle);
  1302. } /* else */
  1303. __PHYSFS_platformReleaseMutex(stateLock);
  1304. return(retval);
  1305. } /* doOpenWrite */
  1306. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1307. {
  1308. return(doOpenWrite(filename, 0));
  1309. } /* PHYSFS_openWrite */
  1310. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1311. {
  1312. return(doOpenWrite(filename, 1));
  1313. } /* PHYSFS_openAppend */
  1314. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1315. {
  1316. PHYSFS_file *retval = NULL;
  1317. FileHandle *rc = NULL;
  1318. FileHandleList *list;
  1319. int fileExists = 0;
  1320. PhysDirInfo *i;
  1321. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1322. while (*fname == '/')
  1323. fname++;
  1324. __PHYSFS_platformGrabMutex(stateLock);
  1325. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1326. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1327. {
  1328. DirHandle *h = i->dirHandle;
  1329. if (__PHYSFS_verifySecurity(h, fname, 0))
  1330. rc = h->funcs->openRead(h, fname, &fileExists);
  1331. } /* for */
  1332. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1333. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1334. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1335. list->handle.opaque = (void *) rc;
  1336. list->next = openReadList;
  1337. openReadList = list;
  1338. retval = &(list->handle);
  1339. __PHYSFS_platformReleaseMutex(stateLock);
  1340. rc->buffer = NULL; /* just in case. */
  1341. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1342. rc->forReading = 1;
  1343. return(retval);
  1344. } /* PHYSFS_openRead */
  1345. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1346. {
  1347. FileHandle *h = (FileHandle *) handle->opaque;
  1348. FileHandleList *prev = NULL;
  1349. FileHandleList *i;
  1350. int rc = 1;
  1351. for (i = *list; i != NULL; i = i->next)
  1352. {
  1353. if (&i->handle == handle) /* handle is in this list? */
  1354. {
  1355. PHYSFS_uint8 *tmp = h->buffer;
  1356. rc = PHYSFS_flush(handle);
  1357. if (rc)
  1358. rc = h->funcs->fileClose(h);
  1359. if (!rc)
  1360. return(-1);
  1361. if (tmp != NULL) /* free any associated buffer. */
  1362. free(tmp);
  1363. if (prev == NULL)
  1364. *list = i->next;
  1365. else
  1366. prev->next = i->next;
  1367. free(i);
  1368. return(1);
  1369. } /* if */
  1370. prev = i;
  1371. } /* for */
  1372. return(0);
  1373. } /* closeHandleInOpenList */
  1374. int PHYSFS_close(PHYSFS_file *handle)
  1375. {
  1376. int rc;
  1377. __PHYSFS_platformGrabMutex(stateLock);
  1378. /* -1 == close failure. 0 == not found. 1 == success. */
  1379. rc = closeHandleInOpenList(&openReadList, handle);
  1380. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1381. if (!rc)
  1382. {
  1383. rc = closeHandleInOpenList(&openWriteList, handle);
  1384. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1385. } /* if */
  1386. __PHYSFS_platformReleaseMutex(stateLock);
  1387. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1388. return(1);
  1389. } /* PHYSFS_close */
  1390. static PHYSFS_sint64 doBufferedRead(PHYSFS_file *handle, void *buffer,
  1391. PHYSFS_uint32 objSize,
  1392. PHYSFS_uint32 objCount)
  1393. {
  1394. FileHandle *h = (FileHandle *) handle->opaque;
  1395. PHYSFS_sint64 retval = 0;
  1396. PHYSFS_uint32 remainder = 0;
  1397. while (objCount > 0)
  1398. {
  1399. PHYSFS_uint32 buffered = h->buffill - h->bufpos;
  1400. PHYSFS_uint64 mustread = (objSize * objCount) - remainder;
  1401. PHYSFS_uint32 copied;
  1402. if (buffered == 0) /* need to refill buffer? */
  1403. {
  1404. PHYSFS_sint64 rc = h->funcs->read(h, h->buffer, 1, h->bufsize);
  1405. if (rc <= 0)
  1406. {
  1407. h->bufpos -= remainder;
  1408. return(((rc == -1) && (retval == 0)) ? -1 : retval);
  1409. } /* if */
  1410. buffered = h->buffill = (PHYSFS_uint32) rc;
  1411. h->bufpos = 0;
  1412. } /* if */
  1413. if (buffered > mustread)
  1414. buffered = (PHYSFS_uint32) mustread;
  1415. memcpy(buffer, h->buffer + h->bufpos, (size_t) buffered);
  1416. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  1417. h->bufpos += buffered;
  1418. buffered += remainder; /* take remainder into account. */
  1419. copied = (buffered / objSize);
  1420. remainder = (buffered % objSize);
  1421. retval += copied;
  1422. objCount -= copied;
  1423. } /* while */
  1424. return(retval);
  1425. } /* doBufferedRead */
  1426. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1427. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1428. {
  1429. FileHandle *h = (FileHandle *) handle->opaque;
  1430. BAIL_IF_MACRO(!h->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1431. if (h->buffer != NULL)
  1432. return(doBufferedRead(handle, buffer, objSize, objCount));
  1433. return(h->funcs->read(h, buffer, objSize, objCount));
  1434. } /* PHYSFS_read */
  1435. static PHYSFS_sint64 doBufferedWrite(PHYSFS_file *handle, const void *buffer,
  1436. PHYSFS_uint32 objSize,
  1437. PHYSFS_uint32 objCount)
  1438. {
  1439. FileHandle *h = (FileHandle *) handle->opaque;
  1440. /* whole thing fits in the buffer? */
  1441. if (h->buffill + (objSize * objCount) < h->bufsize)
  1442. {
  1443. memcpy(h->buffer + h->buffill, buffer, objSize * objCount);
  1444. h->buffill += (objSize * objCount);
  1445. return(objCount);
  1446. } /* if */
  1447. /* would overflow buffer. Flush and then write the new objects, too. */
  1448. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1449. return(h->funcs->write(h, buffer, objSize, objCount));
  1450. } /* doBufferedWrite */
  1451. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1452. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1453. {
  1454. FileHandle *h = (FileHandle *) handle->opaque;
  1455. BAIL_IF_MACRO(h->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1456. if (h->buffer != NULL)
  1457. return(doBufferedWrite(handle, buffer, objSize, objCount));
  1458. return(h->funcs->write(h, buffer, objSize, objCount));
  1459. } /* PHYSFS_write */
  1460. int PHYSFS_eof(PHYSFS_file *handle)
  1461. {
  1462. FileHandle *h = (FileHandle *) handle->opaque;
  1463. if (!h->forReading) /* never EOF on files opened for write/append. */
  1464. return(0);
  1465. /* eof if buffer is empty and archiver says so. */
  1466. return((h->bufpos == h->buffill) && (h->funcs->eof(h)));
  1467. } /* PHYSFS_eof */
  1468. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1469. {
  1470. FileHandle *h = (FileHandle *) handle->opaque;
  1471. PHYSFS_sint64 retval = h->forReading ?
  1472. (h->funcs->tell(h) - h->buffill) + h->bufpos :
  1473. (h->funcs->tell(h) + h->buffill);
  1474. return(retval);
  1475. } /* PHYSFS_tell */
  1476. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1477. {
  1478. FileHandle *h = (FileHandle *) handle->opaque;
  1479. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1480. h->buffill = h->bufpos = 0; /* just in case. */
  1481. return(h->funcs->seek(h, pos));
  1482. } /* PHYSFS_seek */
  1483. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1484. {
  1485. FileHandle *h = (FileHandle *) handle->opaque;
  1486. return(h->funcs->fileLength(h));
  1487. } /* PHYSFS_filelength */
  1488. int PHYSFS_setBuffer(PHYSFS_file *handle, PHYSFS_uint64 _bufsize)
  1489. {
  1490. FileHandle *h = (FileHandle *) handle->opaque;
  1491. PHYSFS_uint32 bufsize = (PHYSFS_uint32) _bufsize;
  1492. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1493. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1494. /*
  1495. * For reads, we need to move the file pointer to where it would be
  1496. * if we weren't buffering, so that the next read will get the
  1497. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  1498. */
  1499. if ((h->forReading) && (h->buffill != h->bufpos))
  1500. {
  1501. PHYSFS_uint64 pos;
  1502. PHYSFS_sint64 curpos = h->funcs->tell(h);
  1503. BAIL_IF_MACRO(curpos == -1, NULL, 0);
  1504. pos = ((curpos - h->buffill) + h->bufpos);
  1505. BAIL_IF_MACRO(!h->funcs->seek(h, pos), NULL, 0);
  1506. } /* if */
  1507. if (bufsize == 0) /* delete existing buffer. */
  1508. {
  1509. if (h->buffer != NULL)
  1510. {
  1511. free(h->buffer);
  1512. h->buffer = NULL;
  1513. } /* if */
  1514. } /* if */
  1515. else
  1516. {
  1517. PHYSFS_uint8 *newbuf = realloc(h->buffer, bufsize);
  1518. BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
  1519. h->buffer = newbuf;
  1520. } /* else */
  1521. h->bufsize = bufsize;
  1522. h->buffill = h->bufpos = 0;
  1523. return(1);
  1524. } /* PHYSFS_setBuffer */
  1525. int PHYSFS_flush(PHYSFS_file *handle)
  1526. {
  1527. FileHandle *h = (FileHandle *) handle->opaque;
  1528. PHYSFS_sint64 rc;
  1529. if ((h->forReading) || (h->bufpos == h->buffill))
  1530. return(1); /* open for read or buffer empty are successful no-ops. */
  1531. /* dump buffer to disk. */
  1532. rc = h->funcs->write(h, h->buffer + h->bufpos, h->buffill - h->bufpos, 1);
  1533. BAIL_IF_MACRO(rc <= 0, NULL, 0);
  1534. h->bufpos = h->buffill = 0;
  1535. return(1);
  1536. } /* PHYSFS_flush */
  1537. LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
  1538. LinkedStringList **prev,
  1539. const char *str,
  1540. PHYSFS_sint32 len)
  1541. {
  1542. LinkedStringList *l;
  1543. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  1544. BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
  1545. if (len < 0)
  1546. len = strlen(str);
  1547. l->str = (char *) malloc(len + 1);
  1548. if (l->str == NULL)
  1549. {
  1550. free(l);
  1551. BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
  1552. } /* if */
  1553. strncpy(l->str, str, len);
  1554. l->str[len] = '\0';
  1555. if (retval == NULL)
  1556. retval = l;
  1557. else
  1558. (*prev)->next = l;
  1559. *prev = l;
  1560. l->next = NULL;
  1561. return(retval);
  1562. } /* __PHYSFS_addToLinkedStringList */
  1563. /* end of physfs.c ... */