physfs.c 51 KB

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