physfs.c 36 KB

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