physfs.c 34 KB

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