physfs.c 50 KB

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