1
0

physfs.c 50 KB

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