physfs.c 37 KB

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