physfs.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include "physfs.h"
  17. #define __PHYSICSFS_INTERNAL__
  18. #include "physfs_internal.h"
  19. typedef struct __PHYSFS_ERRMSGTYPE__
  20. {
  21. PHYSFS_uint64 tid;
  22. int errorAvailable;
  23. char errorString[80];
  24. struct __PHYSFS_ERRMSGTYPE__ *next;
  25. } ErrMsg;
  26. typedef struct __PHYSFS_DIRINFO__
  27. {
  28. char *dirName;
  29. DirHandle *dirHandle;
  30. struct __PHYSFS_DIRINFO__ *next;
  31. } PhysDirInfo;
  32. typedef struct __PHYSFS_FILEHANDLELIST__
  33. {
  34. PHYSFS_file handle;
  35. struct __PHYSFS_FILEHANDLELIST__ *next;
  36. } FileHandleList;
  37. /* The various i/o drivers... */
  38. #if (defined PHYSFS_SUPPORTS_ZIP)
  39. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  40. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  41. #endif
  42. #if (defined PHYSFS_SUPPORTS_GRP)
  43. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  44. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  45. #endif
  46. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  47. static const PHYSFS_ArchiveInfo *supported_types[] =
  48. {
  49. #if (defined PHYSFS_SUPPORTS_ZIP)
  50. &__PHYSFS_ArchiveInfo_ZIP,
  51. #endif
  52. #if (defined PHYSFS_SUPPORTS_GRP)
  53. &__PHYSFS_ArchiveInfo_GRP,
  54. #endif
  55. NULL
  56. };
  57. static const DirFunctions *dirFunctions[] =
  58. {
  59. #if (defined PHYSFS_SUPPORTS_ZIP)
  60. &__PHYSFS_DirFunctions_ZIP,
  61. #endif
  62. #if (defined PHYSFS_SUPPORTS_GRP)
  63. &__PHYSFS_DirFunctions_GRP,
  64. #endif
  65. &__PHYSFS_DirFunctions_DIR,
  66. NULL
  67. };
  68. /* General PhysicsFS state ... */
  69. static int initialized = 0;
  70. static ErrMsg *errorMessages = NULL;
  71. static PhysDirInfo *searchPath = NULL;
  72. static PhysDirInfo *writeDir = NULL;
  73. static FileHandleList *openWriteList = NULL;
  74. static FileHandleList *openReadList = NULL;
  75. static char *baseDir = NULL;
  76. static char *userDir = NULL;
  77. static int allowSymLinks = 0;
  78. /* mutexes ... */
  79. static void *errorLock = NULL; /* protects error message list. */
  80. static void *stateLock = NULL; /* protects other PhysFS static state. */
  81. /* functions ... */
  82. static ErrMsg *findErrorForCurrentThread(void)
  83. {
  84. ErrMsg *i;
  85. PHYSFS_uint64 tid;
  86. if (initialized)
  87. __PHYSFS_platformGrabMutex(errorLock);
  88. if (errorMessages != NULL)
  89. {
  90. tid = __PHYSFS_platformGetThreadID();
  91. for (i = errorMessages; i != NULL; i = i->next)
  92. {
  93. if (i->tid == tid)
  94. {
  95. __PHYSFS_platformReleaseMutex(errorLock);
  96. return(i);
  97. } /* if */
  98. } /* for */
  99. } /* if */
  100. if (initialized)
  101. __PHYSFS_platformReleaseMutex(errorLock);
  102. return(NULL); /* no error available. */
  103. } /* findErrorForCurrentThread */
  104. void __PHYSFS_setError(const char *str)
  105. {
  106. ErrMsg *err;
  107. if (str == NULL)
  108. return;
  109. err = findErrorForCurrentThread();
  110. if (err == NULL)
  111. {
  112. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  113. if (err == NULL)
  114. return; /* uhh...? */
  115. memset((void *) err, '\0', sizeof (ErrMsg));
  116. err->tid = __PHYSFS_platformGetThreadID();
  117. __PHYSFS_platformGrabMutex(errorLock);
  118. err->next = errorMessages;
  119. errorMessages = err;
  120. __PHYSFS_platformReleaseMutex(errorLock);
  121. } /* if */
  122. err->errorAvailable = 1;
  123. strncpy(err->errorString, str, sizeof (err->errorString));
  124. err->errorString[sizeof (err->errorString) - 1] = '\0';
  125. } /* __PHYSFS_setError */
  126. const char *PHYSFS_getLastError(void)
  127. {
  128. ErrMsg *err = findErrorForCurrentThread();
  129. if ((err == NULL) || (!err->errorAvailable))
  130. return(NULL);
  131. err->errorAvailable = 0;
  132. return(err->errorString);
  133. } /* PHYSFS_getLastError */
  134. /* MAKE SURE that errorLock is held before calling this! */
  135. static void freeErrorMessages(void)
  136. {
  137. ErrMsg *i;
  138. ErrMsg *next;
  139. for (i = errorMessages; i != NULL; i = next)
  140. {
  141. next = i->next;
  142. free(i);
  143. } /* for */
  144. } /* freeErrorMessages */
  145. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  146. {
  147. if (ver != NULL)
  148. {
  149. ver->major = PHYSFS_VER_MAJOR;
  150. ver->minor = PHYSFS_VER_MINOR;
  151. ver->patch = PHYSFS_VER_PATCH;
  152. } /* if */
  153. } /* PHYSFS_getLinkedVersion */
  154. static DirHandle *openDirectory(const char *d, int forWriting)
  155. {
  156. const DirFunctions **i;
  157. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  158. for (i = dirFunctions; *i != NULL; i++)
  159. {
  160. if ((*i)->isArchive(d, forWriting))
  161. return( (*i)->openArchive(d, forWriting) );
  162. } /* for */
  163. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  164. return(NULL);
  165. } /* openDirectory */
  166. static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
  167. {
  168. DirHandle *dirHandle = NULL;
  169. PhysDirInfo *di = NULL;
  170. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  171. dirHandle = openDirectory(newDir, forWriting);
  172. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  173. di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
  174. if (di == NULL)
  175. {
  176. dirHandle->funcs->dirClose(dirHandle);
  177. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  178. } /* if */
  179. di->dirName = (char *) malloc(strlen(newDir) + 1);
  180. if (di->dirName == NULL)
  181. {
  182. free(di);
  183. dirHandle->funcs->dirClose(dirHandle);
  184. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  185. } /* if */
  186. di->next = NULL;
  187. di->dirHandle = dirHandle;
  188. strcpy(di->dirName, newDir);
  189. return(di);
  190. } /* buildDirInfo */
  191. /* MAKE SURE you've got the stateLock held before calling this! */
  192. static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
  193. {
  194. FileHandleList *i;
  195. if (di == NULL)
  196. return(1);
  197. for (i = openList; i != NULL; i = i->next)
  198. {
  199. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  200. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  201. } /* for */
  202. di->dirHandle->funcs->dirClose(di->dirHandle);
  203. free(di->dirName);
  204. free(di);
  205. return(1);
  206. } /* freeDirInfo */
  207. static char *calculateUserDir(void)
  208. {
  209. char *retval = NULL;
  210. const char *str = NULL;
  211. str = __PHYSFS_platformGetUserDir();
  212. if (str != NULL)
  213. retval = (char *) str;
  214. else
  215. {
  216. const char *dirsep = PHYSFS_getDirSeparator();
  217. const char *uname = __PHYSFS_platformGetUserName();
  218. str = (uname != NULL) ? uname : "default";
  219. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  220. strlen(dirsep) + 6);
  221. if (retval == NULL)
  222. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  223. else
  224. sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
  225. if (uname != NULL)
  226. free((void *) uname);
  227. } /* else */
  228. return(retval);
  229. } /* calculateUserDir */
  230. static int appendDirSep(char **dir)
  231. {
  232. const char *dirsep = PHYSFS_getDirSeparator();
  233. char *ptr;
  234. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  235. return(1);
  236. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  237. if (!ptr)
  238. {
  239. free(*dir);
  240. return(0);
  241. } /* if */
  242. strcat(ptr, dirsep);
  243. *dir = ptr;
  244. return(1);
  245. } /* appendDirSep */
  246. static char *calculateBaseDir(const char *argv0)
  247. {
  248. const char *dirsep = PHYSFS_getDirSeparator();
  249. char *retval;
  250. char *ptr;
  251. /*
  252. * See if the platform driver wants to handle this for us...
  253. */
  254. retval = __PHYSFS_platformCalcBaseDir(argv0);
  255. if (retval != NULL)
  256. return(retval);
  257. /*
  258. * Determine if there's a path on argv0. If there is, that's the base dir.
  259. */
  260. ptr = strstr(argv0, dirsep);
  261. if (ptr != NULL)
  262. {
  263. char *p = ptr;
  264. size_t size;
  265. while (p != NULL)
  266. {
  267. ptr = p;
  268. p = strstr(p + 1, dirsep);
  269. } /* while */
  270. size = (size_t) (ptr - argv0); /* !!! is this portable? */
  271. retval = (char *) malloc(size + 1);
  272. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  273. memcpy(retval, argv0, size);
  274. retval[size] = '\0';
  275. return(retval);
  276. } /* if */
  277. /*
  278. * Last ditch effort: it's the current working directory. (*shrug*)
  279. */
  280. retval = __PHYSFS_platformCurrentDir();
  281. if(retval != NULL) {
  282. return(retval);
  283. }
  284. /*
  285. * Ok, current directory doesn't exist, use the root directory.
  286. * Not a good alternative, but it only happens if the current
  287. * directory was deleted from under the program.
  288. */
  289. retval = (char *) malloc(strlen(dirsep) + 1);
  290. strcpy(retval, dirsep);
  291. return(retval);
  292. } /* calculateBaseDir */
  293. static int initializeMutexes(void)
  294. {
  295. errorLock = __PHYSFS_platformCreateMutex();
  296. if (errorLock == NULL)
  297. goto initializeMutexes_failed;
  298. stateLock = __PHYSFS_platformCreateMutex();
  299. if (stateLock == NULL)
  300. goto initializeMutexes_failed;
  301. return(1); /* success. */
  302. initializeMutexes_failed:
  303. if (errorLock != NULL)
  304. __PHYSFS_platformDestroyMutex(errorLock);
  305. if (stateLock != NULL)
  306. __PHYSFS_platformDestroyMutex(stateLock);
  307. errorLock = stateLock = NULL;
  308. return(0); /* failed. */
  309. } /* initializeMutexes */
  310. int PHYSFS_init(const char *argv0)
  311. {
  312. char *ptr;
  313. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  314. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  315. BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
  316. baseDir = calculateBaseDir(argv0);
  317. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  318. ptr = __PHYSFS_platformRealPath(baseDir);
  319. free(baseDir);
  320. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  321. baseDir = ptr;
  322. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  323. userDir = calculateUserDir();
  324. if (userDir != NULL)
  325. {
  326. ptr = __PHYSFS_platformRealPath(userDir);
  327. free(userDir);
  328. userDir = ptr;
  329. } /* if */
  330. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  331. {
  332. free(baseDir);
  333. baseDir = NULL;
  334. return(0);
  335. } /* if */
  336. initialized = 1;
  337. /* This makes sure that the error subsystem is initialized. */
  338. __PHYSFS_setError(PHYSFS_getLastError());
  339. return(1);
  340. } /* PHYSFS_init */
  341. /* MAKE SURE you hold stateLock before calling this! */
  342. static int closeFileHandleList(FileHandleList **list)
  343. {
  344. FileHandleList *i;
  345. FileHandleList *next = NULL;
  346. FileHandle *h;
  347. for (i = *list; i != NULL; i = next)
  348. {
  349. next = i->next;
  350. h = (FileHandle *) (i->handle.opaque);
  351. if (!h->funcs->fileClose(h))
  352. {
  353. *list = i;
  354. return(0);
  355. } /* if */
  356. free(i);
  357. } /* for */
  358. *list = NULL;
  359. return(1);
  360. } /* closeFileHandleList */
  361. /* MAKE SURE you hold the stateLock before calling this! */
  362. static void freeSearchPath(void)
  363. {
  364. PhysDirInfo *i;
  365. PhysDirInfo *next = NULL;
  366. closeFileHandleList(&openReadList);
  367. if (searchPath != NULL)
  368. {
  369. for (i = searchPath; i != NULL; i = next)
  370. {
  371. next = i->next;
  372. freeDirInfo(i, openReadList);
  373. } /* for */
  374. searchPath = NULL;
  375. } /* if */
  376. } /* freeSearchPath */
  377. int PHYSFS_deinit(void)
  378. {
  379. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  380. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  381. closeFileHandleList(&openWriteList);
  382. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  383. freeSearchPath();
  384. freeErrorMessages();
  385. if (baseDir != NULL)
  386. {
  387. free(baseDir);
  388. baseDir = NULL;
  389. } /* if */
  390. if (userDir != NULL)
  391. {
  392. free(userDir);
  393. userDir = NULL;
  394. } /* if */
  395. allowSymLinks = 0;
  396. initialized = 0;
  397. __PHYSFS_platformDestroyMutex(errorLock);
  398. __PHYSFS_platformDestroyMutex(stateLock);
  399. errorLock = stateLock = NULL;
  400. return(1);
  401. } /* PHYSFS_deinit */
  402. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  403. {
  404. return(supported_types);
  405. } /* PHYSFS_supportedArchiveTypes */
  406. void PHYSFS_freeList(void *list)
  407. {
  408. void **i;
  409. for (i = (void **) list; *i != NULL; i++)
  410. free(*i);
  411. free(list);
  412. } /* PHYSFS_freeList */
  413. const char *PHYSFS_getDirSeparator(void)
  414. {
  415. return(__PHYSFS_platformDirSeparator);
  416. } /* PHYSFS_getDirSeparator */
  417. char **PHYSFS_getCdRomDirs(void)
  418. {
  419. return(__PHYSFS_platformDetectAvailableCDs());
  420. } /* PHYSFS_getCdRomDirs */
  421. const char *PHYSFS_getBaseDir(void)
  422. {
  423. return(baseDir); /* this is calculated in PHYSFS_init()... */
  424. } /* PHYSFS_getBaseDir */
  425. const char *PHYSFS_getUserDir(void)
  426. {
  427. return(userDir); /* this is calculated in PHYSFS_init()... */
  428. } /* PHYSFS_getUserDir */
  429. const char *PHYSFS_getWriteDir(void)
  430. {
  431. const char *retval = NULL;
  432. __PHYSFS_platformGrabMutex(stateLock);
  433. if (writeDir != NULL)
  434. retval = writeDir->dirName;
  435. __PHYSFS_platformReleaseMutex(stateLock);
  436. return(retval);
  437. } /* PHYSFS_getWriteDir */
  438. int PHYSFS_setWriteDir(const char *newDir)
  439. {
  440. int retval = 1;
  441. __PHYSFS_platformGrabMutex(stateLock);
  442. if (writeDir != NULL)
  443. {
  444. BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
  445. stateLock, 0);
  446. writeDir = NULL;
  447. } /* if */
  448. if (newDir != NULL)
  449. {
  450. writeDir = buildDirInfo(newDir, 1);
  451. retval = (writeDir != NULL);
  452. } /* if */
  453. __PHYSFS_platformReleaseMutex(stateLock);
  454. return(retval);
  455. } /* PHYSFS_setWriteDir */
  456. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  457. {
  458. PhysDirInfo *di;
  459. PhysDirInfo *prev = NULL;
  460. PhysDirInfo *i;
  461. __PHYSFS_platformGrabMutex(stateLock);
  462. for (i = searchPath; i != NULL; i = i->next)
  463. {
  464. /* already in search path? */
  465. BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
  466. prev = i;
  467. } /* for */
  468. di = buildDirInfo(newDir, 0);
  469. BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
  470. if (appendToPath)
  471. {
  472. di->next = NULL;
  473. if (prev == NULL)
  474. searchPath = di;
  475. else
  476. prev->next = di;
  477. } /* if */
  478. else
  479. {
  480. di->next = searchPath;
  481. searchPath = di;
  482. } /* else */
  483. __PHYSFS_platformReleaseMutex(stateLock);
  484. return(1);
  485. } /* PHYSFS_addToSearchPath */
  486. int PHYSFS_removeFromSearchPath(const char *oldDir)
  487. {
  488. PhysDirInfo *i;
  489. PhysDirInfo *prev = NULL;
  490. PhysDirInfo *next = NULL;
  491. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  492. __PHYSFS_platformGrabMutex(stateLock);
  493. for (i = searchPath; i != NULL; i = i->next)
  494. {
  495. if (strcmp(i->dirName, oldDir) == 0)
  496. {
  497. next = i->next;
  498. BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
  499. stateLock, 0);
  500. if (prev == NULL)
  501. searchPath = next;
  502. else
  503. prev->next = next;
  504. BAIL_MACRO_MUTEX(NULL, stateLock, 1);
  505. } /* if */
  506. prev = i;
  507. } /* for */
  508. BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
  509. } /* PHYSFS_removeFromSearchPath */
  510. char **PHYSFS_getSearchPath(void)
  511. {
  512. int count = 1;
  513. int x;
  514. PhysDirInfo *i;
  515. char **retval;
  516. __PHYSFS_platformGrabMutex(stateLock);
  517. for (i = searchPath; i != NULL; i = i->next)
  518. count++;
  519. retval = (char **) malloc(sizeof (char *) * count);
  520. BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
  521. count--;
  522. retval[count] = NULL;
  523. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  524. {
  525. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  526. if (retval[x] == NULL) /* this is friggin' ugly. */
  527. {
  528. while (x > 0)
  529. {
  530. x--;
  531. free(retval[x]);
  532. } /* while */
  533. free(retval);
  534. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  535. } /* if */
  536. strcpy(retval[x], i->dirName);
  537. } /* for */
  538. __PHYSFS_platformReleaseMutex(stateLock);
  539. return(retval);
  540. } /* PHYSFS_getSearchPath */
  541. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  542. const char *archiveExt, int includeCdRoms,
  543. int archivesFirst)
  544. {
  545. const char *basedir = PHYSFS_getBaseDir();
  546. const char *userdir = PHYSFS_getUserDir();
  547. const char *dirsep = PHYSFS_getDirSeparator();
  548. char *str;
  549. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  550. /* set write dir... */
  551. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  552. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  553. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  554. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  555. if (!PHYSFS_setWriteDir(str))
  556. {
  557. int no_write = 0;
  558. sprintf(str, ".%s/%s", organization, appName);
  559. if ( (PHYSFS_setWriteDir(userdir)) &&
  560. (PHYSFS_mkdir(str)) )
  561. {
  562. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  563. if (!PHYSFS_setWriteDir(str))
  564. no_write = 1;
  565. } /* if */
  566. else
  567. {
  568. no_write = 1;
  569. } /* else */
  570. if (no_write)
  571. {
  572. PHYSFS_setWriteDir(NULL); /* just in case. */
  573. free(str);
  574. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  575. } /* if */
  576. } /* if */
  577. /* Put write dir first in search path... */
  578. PHYSFS_addToSearchPath(str, 0);
  579. free(str);
  580. /* Put base path on search path... */
  581. PHYSFS_addToSearchPath(basedir, 1);
  582. /* handle CD-ROMs... */
  583. if (includeCdRoms)
  584. {
  585. char **cds = PHYSFS_getCdRomDirs();
  586. char **i;
  587. for (i = cds; *i != NULL; i++)
  588. PHYSFS_addToSearchPath(*i, 1);
  589. PHYSFS_freeList(cds);
  590. } /* if */
  591. /* Root out archives, and add them to search path... */
  592. if (archiveExt != NULL)
  593. {
  594. char **rc = PHYSFS_enumerateFiles("");
  595. char **i;
  596. size_t extlen = strlen(archiveExt);
  597. char *ext;
  598. for (i = rc; *i != NULL; i++)
  599. {
  600. size_t l = strlen(*i);
  601. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  602. {
  603. ext = (*i) + (l - extlen);
  604. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  605. {
  606. const char *d = PHYSFS_getRealDir(*i);
  607. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  608. if (str != NULL)
  609. {
  610. sprintf(str, "%s%s%s", d, dirsep, *i);
  611. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  612. free(str);
  613. } /* if */
  614. } /* if */
  615. } /* if */
  616. } /* for */
  617. PHYSFS_freeList(rc);
  618. } /* if */
  619. return(1);
  620. } /* PHYSFS_setSaneConfig */
  621. void PHYSFS_permitSymbolicLinks(int allow)
  622. {
  623. allowSymLinks = allow;
  624. } /* PHYSFS_permitSymbolicLinks */
  625. /* string manipulation in C makes my ass itch. */
  626. char * __PHYSFS_convertToDependent(const char *prepend,
  627. const char *dirName,
  628. const char *append)
  629. {
  630. const char *dirsep = __PHYSFS_platformDirSeparator;
  631. size_t sepsize = strlen(dirsep);
  632. char *str;
  633. char *i1;
  634. char *i2;
  635. size_t allocSize;
  636. while (*dirName == '/')
  637. dirName++;
  638. allocSize = strlen(dirName) + 1;
  639. if (prepend != NULL)
  640. allocSize += strlen(prepend) + sepsize;
  641. if (append != NULL)
  642. allocSize += strlen(append) + sepsize;
  643. /* make sure there's enough space if the dir separator is bigger. */
  644. if (sepsize > 1)
  645. {
  646. str = (char *) dirName;
  647. do
  648. {
  649. str = strchr(str, '/');
  650. if (str != NULL)
  651. {
  652. allocSize += (sepsize - 1);
  653. str++;
  654. } /* if */
  655. } while (str != NULL);
  656. } /* if */
  657. str = (char *) malloc(allocSize);
  658. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  659. if (prepend == NULL)
  660. *str = '\0';
  661. else
  662. {
  663. strcpy(str, prepend);
  664. strcat(str, dirsep);
  665. } /* else */
  666. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  667. {
  668. if (*i1 == '/')
  669. {
  670. strcpy(i2, dirsep);
  671. i2 += sepsize;
  672. } /* if */
  673. else
  674. {
  675. *i2 = *i1;
  676. } /* else */
  677. } /* for */
  678. *i2 = '\0';
  679. if (append)
  680. {
  681. strcat(str, dirsep);
  682. strcpy(str, append);
  683. } /* if */
  684. return(str);
  685. } /* __PHYSFS_convertToDependent */
  686. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  687. {
  688. int retval = 1;
  689. char *start;
  690. char *end;
  691. char *str;
  692. start = str = malloc(strlen(fname) + 1);
  693. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  694. strcpy(str, fname);
  695. while (1)
  696. {
  697. end = strchr(start, '/');
  698. if (end != NULL)
  699. *end = '\0';
  700. if ( (strcmp(start, ".") == 0) ||
  701. (strcmp(start, "..") == 0) ||
  702. (strchr(start, '\\') != NULL) ||
  703. (strchr(start, ':') != NULL) )
  704. {
  705. __PHYSFS_setError(ERR_INSECURE_FNAME);
  706. retval = 0;
  707. break;
  708. } /* if */
  709. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  710. {
  711. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  712. retval = 0;
  713. break;
  714. } /* if */
  715. if (end == NULL)
  716. break;
  717. *end = '/';
  718. start = end + 1;
  719. } /* while */
  720. free(str);
  721. return(retval);
  722. } /* __PHYSFS_verifySecurity */
  723. int PHYSFS_mkdir(const char *dname)
  724. {
  725. DirHandle *h;
  726. char *str;
  727. char *start;
  728. char *end;
  729. int retval = 0;
  730. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  731. while (*dname == '/')
  732. dname++;
  733. __PHYSFS_platformGrabMutex(stateLock);
  734. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  735. h = writeDir->dirHandle;
  736. BAIL_IF_MACRO_MUTEX(!h->funcs->mkdir, ERR_NOT_SUPPORTED, stateLock, 0);
  737. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, dname), NULL, stateLock, 0);
  738. start = str = malloc(strlen(dname) + 1);
  739. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  740. strcpy(str, dname);
  741. while (1)
  742. {
  743. end = strchr(start, '/');
  744. if (end != NULL)
  745. *end = '\0';
  746. retval = h->funcs->mkdir(h, str);
  747. if (!retval)
  748. break;
  749. if (end == NULL)
  750. break;
  751. *end = '/';
  752. start = end + 1;
  753. } /* while */
  754. __PHYSFS_platformReleaseMutex(stateLock);
  755. free(str);
  756. return(retval);
  757. } /* PHYSFS_mkdir */
  758. int PHYSFS_delete(const char *fname)
  759. {
  760. int retval;
  761. DirHandle *h;
  762. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  763. while (*fname == '/')
  764. fname++;
  765. __PHYSFS_platformGrabMutex(stateLock);
  766. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  767. h = writeDir->dirHandle;
  768. BAIL_IF_MACRO_MUTEX(!h->funcs->remove, ERR_NOT_SUPPORTED, stateLock, 0);
  769. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL, stateLock, 0);
  770. retval = h->funcs->remove(h, fname);
  771. __PHYSFS_platformReleaseMutex(stateLock);
  772. return(retval);
  773. } /* PHYSFS_delete */
  774. const char *PHYSFS_getRealDir(const char *filename)
  775. {
  776. PhysDirInfo *i;
  777. while (*filename == '/')
  778. filename++;
  779. __PHYSFS_platformGrabMutex(stateLock);
  780. for (i = searchPath; i != NULL; i = i->next)
  781. {
  782. DirHandle *h = i->dirHandle;
  783. if (__PHYSFS_verifySecurity(h, filename))
  784. {
  785. if (h->funcs->exists(h, filename))
  786. {
  787. __PHYSFS_platformReleaseMutex(stateLock);
  788. return(i->dirName);
  789. } /* if */
  790. } /* if */
  791. } /* for */
  792. __PHYSFS_platformReleaseMutex(stateLock);
  793. return(NULL);
  794. } /* PHYSFS_getRealDir */
  795. static int countList(LinkedStringList *list)
  796. {
  797. int retval = 0;
  798. LinkedStringList *i;
  799. for (i = list; i != NULL; i = i->next)
  800. retval++;
  801. return(retval);
  802. } /* countList */
  803. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  804. {
  805. int i;
  806. LinkedStringList *next = NULL;
  807. int len = countList(finalList);
  808. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  809. if (retval == NULL)
  810. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  811. for (i = 0; i < len; i++)
  812. {
  813. next = finalList->next;
  814. if (retval == NULL)
  815. free(finalList->str);
  816. else
  817. retval[i] = finalList->str;
  818. free(finalList);
  819. finalList = next;
  820. } /* for */
  821. if (retval != NULL)
  822. retval[i] = NULL;
  823. return(retval);
  824. } /* convertStringListToPhysFSList */
  825. static void insertStringListItem(LinkedStringList **final,
  826. LinkedStringList *item)
  827. {
  828. LinkedStringList *i;
  829. LinkedStringList *prev = NULL;
  830. int rc;
  831. for (i = *final; i != NULL; i = i->next)
  832. {
  833. rc = strcmp(i->str, item->str);
  834. if (rc > 0) /* insertion point. */
  835. break;
  836. else if (rc == 0) /* already in list. */
  837. {
  838. free(item->str);
  839. free(item);
  840. return;
  841. } /* else if */
  842. prev = i;
  843. } /* for */
  844. /*
  845. * If we are here, we are either at the insertion point.
  846. * This may be the end of the list, or the list may be empty, too.
  847. */
  848. if (prev == NULL)
  849. *final = item;
  850. else
  851. prev->next = item;
  852. item->next = i;
  853. } /* insertStringListItem */
  854. /* if we run out of memory anywhere in here, we give back what we can. */
  855. static void interpolateStringLists(LinkedStringList **final,
  856. LinkedStringList *newList)
  857. {
  858. LinkedStringList *next = NULL;
  859. while (newList != NULL)
  860. {
  861. next = newList->next;
  862. insertStringListItem(final, newList);
  863. newList = next;
  864. } /* while */
  865. } /* interpolateStringLists */
  866. char **PHYSFS_enumerateFiles(const char *path)
  867. {
  868. PhysDirInfo *i;
  869. char **retval = NULL;
  870. LinkedStringList *rc;
  871. LinkedStringList *finalList = NULL;
  872. int omitSymLinks = !allowSymLinks;
  873. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  874. while (*path == '/')
  875. path++;
  876. __PHYSFS_platformGrabMutex(stateLock);
  877. for (i = searchPath; i != NULL; i = i->next)
  878. {
  879. DirHandle *h = i->dirHandle;
  880. if (__PHYSFS_verifySecurity(h, path))
  881. {
  882. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  883. interpolateStringLists(&finalList, rc);
  884. } /* if */
  885. } /* for */
  886. __PHYSFS_platformReleaseMutex(stateLock);
  887. retval = convertStringListToPhysFSList(finalList);
  888. return(retval);
  889. } /* PHYSFS_enumerateFiles */
  890. int PHYSFS_exists(const char *fname)
  891. {
  892. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  893. while (*fname == '/')
  894. fname++;
  895. return(PHYSFS_getRealDir(fname) != NULL);
  896. } /* PHYSFS_exists */
  897. int PHYSFS_isDirectory(const char *fname)
  898. {
  899. PhysDirInfo *i;
  900. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  901. while (*fname == '/')
  902. fname++;
  903. if (*fname == '\0')
  904. return(1);
  905. __PHYSFS_platformGrabMutex(stateLock);
  906. for (i = searchPath; i != NULL; i = i->next)
  907. {
  908. DirHandle *h = i->dirHandle;
  909. if (__PHYSFS_verifySecurity(h, fname))
  910. {
  911. if (h->funcs->exists(h, fname))
  912. {
  913. int retval = h->funcs->isDirectory(h, fname);
  914. __PHYSFS_platformReleaseMutex(stateLock);
  915. return(retval);
  916. } /* if */
  917. } /* if */
  918. } /* for */
  919. __PHYSFS_platformReleaseMutex(stateLock);
  920. return(0);
  921. } /* PHYSFS_isDirectory */
  922. int PHYSFS_isSymbolicLink(const char *fname)
  923. {
  924. PhysDirInfo *i;
  925. if (!allowSymLinks)
  926. return(0);
  927. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  928. while (*fname == '/')
  929. fname++;
  930. __PHYSFS_platformGrabMutex(stateLock);
  931. for (i = searchPath; i != NULL; i = i->next)
  932. {
  933. DirHandle *h = i->dirHandle;
  934. if (__PHYSFS_verifySecurity(h, fname))
  935. {
  936. if (h->funcs->exists(h, fname))
  937. {
  938. int retval = h->funcs->isSymLink(h, fname);
  939. __PHYSFS_platformReleaseMutex(stateLock);
  940. return(retval);
  941. } /* if */
  942. } /* if */
  943. } /* for */
  944. /* !!! FIXME: setError ERR_FILE_NOT_FOUND? */
  945. __PHYSFS_platformReleaseMutex(stateLock);
  946. return(0);
  947. } /* PHYSFS_isSymbolicLink */
  948. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  949. {
  950. PHYSFS_file *retval = NULL;
  951. FileHandle *rc = NULL;
  952. DirHandle *h;
  953. const DirFunctions *f;
  954. FileHandleList *list;
  955. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  956. while (*fname == '/')
  957. fname++;
  958. __PHYSFS_platformGrabMutex(stateLock);
  959. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  960. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  961. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
  962. stateLock, NULL);
  963. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  964. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  965. f = h->funcs;
  966. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  967. if (rc == NULL)
  968. free(list);
  969. else
  970. {
  971. list->handle.opaque = (void *) rc;
  972. list->next = openWriteList;
  973. openWriteList = list;
  974. retval = &(list->handle);
  975. } /* else */
  976. __PHYSFS_platformReleaseMutex(stateLock);
  977. return(retval);
  978. } /* doOpenWrite */
  979. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  980. {
  981. return(doOpenWrite(filename, 0));
  982. } /* PHYSFS_openWrite */
  983. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  984. {
  985. return(doOpenWrite(filename, 1));
  986. } /* PHYSFS_openAppend */
  987. PHYSFS_file *PHYSFS_openRead(const char *fname)
  988. {
  989. PHYSFS_file *retval;
  990. FileHandle *rc = NULL;
  991. FileHandleList *list;
  992. PhysDirInfo *i;
  993. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  994. while (*fname == '/')
  995. fname++;
  996. __PHYSFS_platformGrabMutex(stateLock);
  997. for (i = searchPath; i != NULL; i = i->next)
  998. {
  999. DirHandle *h = i->dirHandle;
  1000. if (__PHYSFS_verifySecurity(h, fname))
  1001. {
  1002. rc = h->funcs->openRead(h, fname);
  1003. if (rc != NULL)
  1004. break;
  1005. } /* if */
  1006. } /* for */
  1007. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1008. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1009. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  1010. list->handle.opaque = (void *) rc;
  1011. list->next = openReadList;
  1012. openReadList = list;
  1013. retval = &(list->handle);
  1014. __PHYSFS_platformReleaseMutex(stateLock);
  1015. return(retval);
  1016. } /* PHYSFS_openRead */
  1017. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1018. {
  1019. FileHandle *h = (FileHandle *) handle->opaque;
  1020. FileHandleList *prev = NULL;
  1021. FileHandleList *i;
  1022. int rc;
  1023. for (i = *list; i != NULL; i = i->next)
  1024. {
  1025. if (&i->handle == handle) /* handle is in this list? */
  1026. {
  1027. rc = h->funcs->fileClose(h);
  1028. if (!rc)
  1029. return(-1);
  1030. if (prev == NULL)
  1031. *list = i->next;
  1032. else
  1033. prev->next = i->next;
  1034. free(i);
  1035. return(1);
  1036. } /* if */
  1037. prev = i;
  1038. } /* for */
  1039. return(0);
  1040. } /* closeHandleInOpenList */
  1041. int PHYSFS_close(PHYSFS_file *handle)
  1042. {
  1043. int rc;
  1044. __PHYSFS_platformGrabMutex(stateLock);
  1045. /* -1 == close failure. 0 == not found. 1 == success. */
  1046. rc = closeHandleInOpenList(&openReadList, handle);
  1047. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1048. if (!rc)
  1049. {
  1050. rc = closeHandleInOpenList(&openWriteList, handle);
  1051. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1052. } /* if */
  1053. __PHYSFS_platformReleaseMutex(stateLock);
  1054. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1055. return(1);
  1056. } /* PHYSFS_close */
  1057. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1058. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1059. {
  1060. FileHandle *h = (FileHandle *) handle->opaque;
  1061. assert(h != NULL);
  1062. assert(h->funcs != NULL);
  1063. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  1064. return(h->funcs->read(h, buffer, objSize, objCount));
  1065. } /* PHYSFS_read */
  1066. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1067. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1068. {
  1069. FileHandle *h = (FileHandle *) handle->opaque;
  1070. assert(h != NULL);
  1071. assert(h->funcs != NULL);
  1072. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  1073. return(h->funcs->write(h, buffer, objSize, objCount));
  1074. } /* PHYSFS_write */
  1075. int PHYSFS_eof(PHYSFS_file *handle)
  1076. {
  1077. FileHandle *h = (FileHandle *) handle->opaque;
  1078. assert(h != NULL);
  1079. assert(h->funcs != NULL);
  1080. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  1081. return(h->funcs->eof(h));
  1082. } /* PHYSFS_eof */
  1083. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1084. {
  1085. FileHandle *h = (FileHandle *) handle->opaque;
  1086. assert(h != NULL);
  1087. assert(h->funcs != NULL);
  1088. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  1089. return(h->funcs->tell(h));
  1090. } /* PHYSFS_tell */
  1091. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1092. {
  1093. FileHandle *h = (FileHandle *) handle->opaque;
  1094. assert(h != NULL);
  1095. assert(h->funcs != NULL);
  1096. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1097. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1098. return(h->funcs->seek(h, pos));
  1099. } /* PHYSFS_seek */
  1100. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1101. {
  1102. FileHandle *h = (FileHandle *) handle->opaque;
  1103. assert(h != NULL);
  1104. assert(h->funcs != NULL);
  1105. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1106. return(h->funcs->fileLength(h));
  1107. } /* PHYSFS_filelength */
  1108. /* end of physfs.c ... */