physfs.c 50 KB

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