macclassic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * MacOS Classic support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #if HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <alloca.h>
  14. /*
  15. * Most of the API calls in here are, according to ADC, available since
  16. * MacOS 8.1. I don't think I used any MacOS 9 or CarbonLib-specific
  17. * functions. There might be one or two 8.5 calls, and perhaps when the
  18. * ADC docs say "Available in MacOS 8.1" they really mean "this works
  19. * with System 6, but we don't want to hear about it at this point."
  20. *
  21. * IsAliasFile() showed up in MacOS 8.5. You can duplicate this code with
  22. * PBGetCatInfoSync(), which is an older API, if you hope the bits in the
  23. * catalog info never change (which they won't for, say, MacOS 8.1).
  24. * See Apple Technote FL-30:
  25. * http://developer.apple.com/technotes/fl/fl_30.html
  26. *
  27. * If you want to use weak pointers and Gestalt, and choose the correct
  28. * code to use during __PHYSFS_platformInit(), I'll accept a patch, but
  29. * chances are, it wasn't worth the time it took to write this, let alone
  30. * implement that.
  31. */
  32. /*
  33. * Please note that I haven't tried this code with CarbonLib or under
  34. * MacOS X at all. The code in unix.c is known to work with Darwin,
  35. * and you may or may not be better off using that, especially since
  36. * mutexes are no-ops in this file. Patches welcome.
  37. */
  38. #ifdef __PHYSFS_CARBONIZED__ /* this is currently not defined anywhere. */
  39. #include <Carbon.h>
  40. #else
  41. #include <OSUtils.h>
  42. #include <Processes.h>
  43. #include <Files.h>
  44. #include <TextUtils.h>
  45. #include <Resources.h>
  46. #include <MacMemory.h>
  47. #include <Events.h>
  48. #include <DriverGestalt.h>
  49. #include <Aliases.h>
  50. #endif
  51. #define __PHYSICSFS_INTERNAL__
  52. #include "physfs_internal.h"
  53. const char *__PHYSFS_platformDirSeparator = ":";
  54. static struct ProcessInfoRec procInfo;
  55. static FSSpec procfsspec;
  56. int __PHYSFS_platformInit(void)
  57. {
  58. OSErr err;
  59. ProcessSerialNumber psn;
  60. BAIL_IF_MACRO(GetCurrentProcess(&psn) != noErr, ERR_OS_ERROR, 0);
  61. memset(&procInfo, '\0', sizeof (ProcessInfoRec));
  62. memset(&procfsspec, '\0', sizeof (FSSpec));
  63. procInfo.processInfoLength = sizeof (ProcessInfoRec);
  64. procInfo.processAppSpec = &procfsspec;
  65. err = GetProcessInformation(&psn, &procInfo);
  66. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  67. return(1); /* we're golden. */
  68. } /* __PHYSFS_platformInit */
  69. int __PHYSFS_platformDeinit(void)
  70. {
  71. return(1); /* always succeed. */
  72. } /* __PHYSFS_platformDeinit */
  73. /*
  74. * CD detection code is borrowed from Apple Technical Q&A DV18.
  75. * http://developer.apple.com/qa/dv/dv18.html
  76. */
  77. char **__PHYSFS_platformDetectAvailableCDs(void)
  78. {
  79. DriverGestaltParam pb;
  80. DrvQEl *dqp;
  81. OSErr status;
  82. char **retval = (char **) malloc(sizeof (char *));
  83. int cd_count = 1;
  84. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  85. *retval = NULL;
  86. pb.csCode = kDriverGestaltCode;
  87. pb.driverGestaltSelector = kdgDeviceType;
  88. dqp = (DrvQEl *) GetDrvQHdr()->qHead;
  89. while (dqp != NULL)
  90. {
  91. pb.ioCRefNum = dqp->dQRefNum;
  92. pb.ioVRefNum = dqp->dQDrive;
  93. status = PBStatusSync((ParmBlkPtr) &pb);
  94. if ((status == noErr) && (pb.driverGestaltResponse == kdgCDType))
  95. {
  96. Str63 volName;
  97. HParamBlockRec hpbr;
  98. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  99. hpbr.volumeParam.ioNamePtr = volName;
  100. hpbr.volumeParam.ioVRefNum = dqp->dQDrive;
  101. hpbr.volumeParam.ioVolIndex = 0;
  102. if (PBHGetVInfoSync(&hpbr) == noErr)
  103. {
  104. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  105. if (tmp)
  106. {
  107. char *str = (char *) malloc(volName[0] + 1);
  108. retval = tmp;
  109. if (str != NULL)
  110. {
  111. memcpy(str, &volName[1], volName[0]);
  112. str[volName[0]] = '\0';
  113. retval[cd_count-1] = str;
  114. cd_count++;
  115. } /* if */
  116. } /* if */
  117. } /* if */
  118. } /* if */
  119. dqp = (DrvQEl *) dqp->qLink;
  120. } /* while */
  121. retval[cd_count - 1] = NULL;
  122. return(retval);
  123. } /* __PHYSFS_platformDetectAvailableCDs */
  124. static char *convFSSpecToPath(FSSpec *spec, int includeFile)
  125. {
  126. char *ptr;
  127. char *retval = NULL;
  128. UInt32 retLength = 0;
  129. CInfoPBRec infoPB;
  130. Str255 str255;
  131. str255[0] = spec->name[0];
  132. memcpy(&str255[1], &spec->name[1], str255[0]);
  133. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  134. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  135. infoPB.dirInfo.ioVRefNum = spec->vRefNum; /* ID of bin's volume. */
  136. infoPB.dirInfo.ioDrParID = spec->parID; /* ID of bin's dir. */
  137. infoPB.dirInfo.ioFDirIndex = (includeFile) ? 0 : -1;
  138. /* walk the tree back to the root dir (volume), building path string... */
  139. do
  140. {
  141. /* check parent dir of what we last looked at... */
  142. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  143. if (PBGetCatInfoSync(&infoPB) != noErr)
  144. {
  145. if (retval != NULL)
  146. free(retval);
  147. BAIL_MACRO(ERR_OS_ERROR, NULL);
  148. } /* if */
  149. infoPB.dirInfo.ioFDirIndex = -1; /* look at parent dir next time. */
  150. /* allocate more space for the retval... */
  151. retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
  152. ptr = (char *) malloc(retLength);
  153. if (ptr == NULL)
  154. {
  155. if (retval != NULL)
  156. free(retval);
  157. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  158. } /* if */
  159. /* prepend new dir to retval and cleanup... */
  160. memcpy(ptr, &str255[1], str255[0]);
  161. ptr[str255[0]] = '\0'; /* null terminate it. */
  162. if (retval != NULL)
  163. {
  164. strcat(ptr, ":");
  165. strcat(ptr, retval);
  166. free(retval);
  167. } /* if */
  168. retval = ptr;
  169. } while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
  170. return(retval);
  171. } /* convFSSpecToPath */
  172. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  173. {
  174. FSSpec spec;
  175. /* Get the name of the binary's parent directory. */
  176. FSMakeFSSpec(procfsspec.vRefNum, procfsspec.parID, procfsspec.name, &spec);
  177. return(convFSSpecToPath(&spec, 0));
  178. } /* __PHYSFS_platformCalcBaseDir */
  179. char *__PHYSFS_platformGetUserName(void)
  180. {
  181. char *retval = NULL;
  182. StringHandle strHandle;
  183. short origResourceFile = CurResFile();
  184. /* use the System resource file. */
  185. UseResFile(0);
  186. /* apparently, -16096 specifies the username. */
  187. strHandle = GetString(-16096);
  188. UseResFile(origResourceFile);
  189. BAIL_IF_MACRO(strHandle == NULL, ERR_OS_ERROR, NULL);
  190. HLock((Handle) strHandle);
  191. retval = (char *) malloc((*strHandle)[0] + 1);
  192. if (retval == NULL)
  193. {
  194. HUnlock((Handle) strHandle);
  195. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  196. } /* if */
  197. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  198. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  199. HUnlock((Handle) strHandle);
  200. return(retval);
  201. } /* __PHYSFS_platformGetUserName */
  202. char *__PHYSFS_platformGetUserDir(void)
  203. {
  204. #if 0
  205. return(NULL); /* bah...use default behaviour, I guess. */
  206. #else
  207. /* (Hmm. Default behaviour is broken in the base library. :) ) */
  208. return(__PHYSFS_platformCalcBaseDir(NULL));
  209. #endif
  210. } /* __PHYSFS_platformGetUserDir */
  211. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  212. {
  213. return(1); /* single threaded. */
  214. } /* __PHYSFS_platformGetThreadID */
  215. int __PHYSFS_platformStricmp(const char *x, const char *y)
  216. {
  217. extern int _stricmp(const char *, const char *);
  218. return(_stricmp(x, y)); /* (*shrug*) */
  219. } /* __PHYSFS_platformStricmp */
  220. static OSErr fnameToFSSpecNoAlias(const char *fname, FSSpec *spec)
  221. {
  222. OSErr err;
  223. Str255 str255;
  224. int needColon = (strchr(fname, ':') == NULL);
  225. int len = strlen(fname) + ((needColon) ? 1 : 0);
  226. if (len > 255)
  227. return(bdNamErr);
  228. /* !!! FIXME: What happens with relative pathnames? */
  229. str255[0] = len;
  230. memcpy(&str255[1], fname, len);
  231. /* probably just a volume name, which seems to need a ':' at the end. */
  232. if (needColon)
  233. str255[len] = ':';
  234. err = FSMakeFSSpec(0, 0, str255, spec);
  235. return(err);
  236. } /* fnameToFSSpecNoAlias */
  237. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  238. {
  239. Boolean alias = 0;
  240. Boolean folder = 0;
  241. OSErr err = fnameToFSSpecNoAlias(fname, spec);
  242. if (err == dirNFErr) /* might be an alias in the middle of the path. */
  243. {
  244. /*
  245. * Has to be at least two ':' chars, or we wouldn't get a
  246. * dir-not-found condition. (no ':' means it was just a volume,
  247. * just one ':' means we would have gotten a fnfErr, if anything.
  248. */
  249. char *ptr;
  250. char *start;
  251. char *path = alloca(strlen(fname) + 1);
  252. strcpy(path, fname);
  253. ptr = strchr(path, ':');
  254. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  255. ptr = strchr(ptr + 1, ':');
  256. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  257. *ptr = '\0';
  258. err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
  259. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  260. start = ptr;
  261. ptr = strchr(start + 1, ':');
  262. /* Now check each element of the path for aliases... */
  263. do
  264. {
  265. CInfoPBRec infoPB;
  266. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  267. infoPB.dirInfo.ioNamePtr = spec->name;
  268. infoPB.dirInfo.ioVRefNum = spec->vRefNum;
  269. infoPB.dirInfo.ioDrDirID = spec->parID;
  270. infoPB.dirInfo.ioFDirIndex = 0;
  271. err = PBGetCatInfoSync(&infoPB);
  272. if (err != noErr) /* not an alias, really just a bogus path. */
  273. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  274. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0)
  275. spec->parID = infoPB.dirInfo.ioDrDirID;
  276. if (ptr != NULL) /* terminate string after next element. */
  277. *ptr = '\0';
  278. *start = strlen(start + 1); /* make it a pstring. */
  279. err = FSMakeFSSpec(spec->vRefNum, spec->parID,
  280. (const unsigned char *) start, spec);
  281. if (err != noErr) /* not an alias, really a bogus path. */
  282. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  283. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  284. if (err != noErr) /* not an alias, really a bogus path. */
  285. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  286. start = ptr; /* move to the next element. */
  287. if (ptr != NULL)
  288. ptr = strchr(start + 1, ':');
  289. } while (start != NULL);
  290. } /* if */
  291. else /* there's something there; make sure final file is not an alias. */
  292. {
  293. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  294. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  295. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  296. } /* else */
  297. return(noErr); /* w00t. */
  298. } /* fnameToFSSpec */
  299. int __PHYSFS_platformExists(const char *fname)
  300. {
  301. FSSpec spec;
  302. return(fnameToFSSpec(fname, &spec) == noErr);
  303. } /* __PHYSFS_platformExists */
  304. int __PHYSFS_platformIsSymLink(const char *fname)
  305. {
  306. OSErr err;
  307. FSSpec spec;
  308. Boolean a = 0;
  309. Boolean f = 0;
  310. CInfoPBRec infoPB;
  311. char *ptr;
  312. char *dir = alloca(strlen(fname) + 1);
  313. BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
  314. strcpy(dir, fname);
  315. ptr = strrchr(dir, ':');
  316. if (ptr == NULL) /* just a volume name? Can't be a symlink. */
  317. return(0);
  318. /* resolve aliases up to the actual file... */
  319. *ptr = '\0';
  320. BAIL_IF_MACRO(fnameToFSSpec(dir, &spec) != noErr, ERR_OS_ERROR, 0);
  321. *ptr = strlen(ptr + 1); /* ptr is now a pascal string. Yikes! */
  322. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  323. infoPB.dirInfo.ioNamePtr = spec.name;
  324. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  325. infoPB.dirInfo.ioDrDirID = spec.parID;
  326. infoPB.dirInfo.ioFDirIndex = 0;
  327. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, 0);
  328. err = FSMakeFSSpec(spec.vRefNum, infoPB.dirInfo.ioDrDirID,
  329. (const unsigned char *) ptr, &spec);
  330. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  331. BAIL_IF_MACRO(IsAliasFile(&spec, &a, &f) != noErr, ERR_OS_ERROR, 0);
  332. return(a);
  333. } /* __PHYSFS_platformIsSymlink */
  334. int __PHYSFS_platformIsDirectory(const char *fname)
  335. {
  336. FSSpec spec;
  337. CInfoPBRec infoPB;
  338. OSErr err;
  339. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, 0);
  340. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  341. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  342. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  343. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  344. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  345. err = PBGetCatInfoSync(&infoPB);
  346. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  347. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  348. } /* __PHYSFS_platformIsDirectory */
  349. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  350. const char *dirName,
  351. const char *append)
  352. {
  353. int len = ((prepend) ? strlen(prepend) : 0) +
  354. ((append) ? strlen(append) : 0) +
  355. strlen(dirName) + 1;
  356. const char *src;
  357. char *dst;
  358. char *retval = malloc(len);
  359. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  360. if (prepend != NULL)
  361. {
  362. strcpy(retval, prepend);
  363. dst = retval + strlen(retval);
  364. } /* if */
  365. else
  366. {
  367. *retval = '\0';
  368. dst = retval;
  369. } /* else */
  370. for (src = dirName; *src; src++, dst++)
  371. *dst = ((*src == '/') ? ':' : *src);
  372. *dst = '\0';
  373. return(retval);
  374. } /* __PHYSFS_platformCvtToDependent */
  375. void __PHYSFS_platformTimeslice(void)
  376. {
  377. SystemTask();
  378. } /* __PHYSFS_platformTimeslice */
  379. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  380. int omitSymLinks)
  381. {
  382. LinkedStringList *ret = NULL, *p = NULL;
  383. UInt16 i;
  384. UInt16 max;
  385. FSSpec spec;
  386. CInfoPBRec infoPB;
  387. Str255 str255;
  388. long dirID;
  389. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
  390. /* get the dir ID of what we want to enumerate... */
  391. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  392. infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
  393. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  394. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
  395. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  396. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, NULL);
  397. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
  398. BAIL_MACRO(ERR_NOT_A_DIR, NULL);
  399. dirID = infoPB.dirInfo.ioDrDirID;
  400. max = infoPB.dirInfo.ioDrNmFls;
  401. for (i = 1; i <= max; i++)
  402. {
  403. FSSpec aliasspec;
  404. Boolean alias = 0;
  405. Boolean folder = 0;
  406. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  407. str255[0] = 0;
  408. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  409. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
  410. infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
  411. infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
  412. if (PBGetCatInfoSync(&infoPB) != noErr)
  413. continue; /* skip this file. Oh well. */
  414. if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
  415. continue; /* skip it. */
  416. if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
  417. continue; /* skip it. */
  418. if ((alias) && (omitSymLinks))
  419. continue;
  420. /* still here? Add it to the list. */
  421. ret = __PHYSFS_addToLinkedStringList(ret, &p, &str255[1], str255[0]);
  422. } /* for */
  423. return(ret);
  424. } /* __PHYSFS_platformEnumerateFiles */
  425. char *__PHYSFS_platformCurrentDir(void)
  426. {
  427. /*
  428. * I don't think MacOS has a concept of "current directory", beyond
  429. * what is grafted on by a given standard C library implementation,
  430. * so just return the base dir.
  431. * We don't use this for anything crucial at the moment anyhow.
  432. */
  433. return(__PHYSFS_platformCalcBaseDir(NULL));
  434. } /* __PHYSFS_platformCurrentDir */
  435. char *__PHYSFS_platformRealPath(const char *path)
  436. {
  437. /*
  438. * fnameToFSSpec() will resolve any symlinks to get to the real
  439. * file's FSSpec, which, when converted, will contain the real
  440. * direct path to a given file. convFSSpecToPath() mallocs a
  441. * return value buffer.
  442. */
  443. FSSpec spec;
  444. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, NULL);
  445. return(convFSSpecToPath(&spec, 1));
  446. } /* __PHYSFS_platformRealPath */
  447. int __PHYSFS_platformMkDir(const char *path)
  448. {
  449. SInt32 val = 0;
  450. FSSpec spec;
  451. OSErr err = fnameToFSSpec(path, &spec);
  452. BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
  453. BAIL_IF_MACRO(err != fnfErr, ERR_OS_ERROR, 0);
  454. err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
  455. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  456. return(1);
  457. } /* __PHYSFS_platformMkDir */
  458. static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
  459. {
  460. int created = 0;
  461. SInt16 *retval = NULL;
  462. FSSpec spec;
  463. OSErr err = fnameToFSSpec(fname, &spec);
  464. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), ERR_OS_ERROR, NULL);
  465. if (err == fnfErr)
  466. {
  467. BAIL_IF_MACRO(!createIfMissing, ERR_NO_SUCH_FILE, NULL);
  468. err = HCreate(spec.vRefNum, spec.parID, spec.name,
  469. procInfo.processSignature, 'BINA');
  470. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
  471. created = 1;
  472. } /* if */
  473. retval = (SInt16 *) malloc(sizeof (SInt16));
  474. if (retval == NULL)
  475. {
  476. if (created)
  477. HDelete(spec.vRefNum, spec.parID, spec.name);
  478. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  479. } /* if */
  480. if (HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval) != noErr)
  481. {
  482. free(retval);
  483. if (created)
  484. HDelete(spec.vRefNum, spec.parID, spec.name);
  485. BAIL_MACRO(ERR_OS_ERROR, NULL);
  486. } /* if */
  487. return(retval);
  488. } /* macDoOpen */
  489. void *__PHYSFS_platformOpenRead(const char *filename)
  490. {
  491. SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
  492. if (retval != NULL) /* got a file; seek to start. */
  493. {
  494. if (SetFPos(*retval, fsFromStart, 0) != noErr)
  495. {
  496. FSClose(*retval);
  497. BAIL_MACRO(ERR_OS_ERROR, NULL);
  498. } /* if */
  499. } /* if */
  500. return((void *) retval);
  501. } /* __PHYSFS_platformOpenRead */
  502. void *__PHYSFS_platformOpenWrite(const char *filename)
  503. {
  504. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  505. if (retval != NULL) /* got a file; truncate it. */
  506. {
  507. if ((SetEOF(*retval, 0) != noErr) ||
  508. (SetFPos(*retval, fsFromStart, 0) != noErr))
  509. {
  510. FSClose(*retval);
  511. BAIL_MACRO(ERR_OS_ERROR, NULL);
  512. } /* if */
  513. } /* if */
  514. return((void *) retval);
  515. } /* __PHYSFS_platformOpenWrite */
  516. void *__PHYSFS_platformOpenAppend(const char *filename)
  517. {
  518. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  519. if (retval != NULL) /* got a file; seek to end. */
  520. {
  521. if (SetFPos(*retval, fsFromLEOF, 0) != noErr)
  522. {
  523. FSClose(*retval);
  524. BAIL_MACRO(ERR_OS_ERROR, NULL);
  525. } /* if */
  526. } /* if */
  527. return(retval);
  528. } /* __PHYSFS_platformOpenAppend */
  529. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  530. PHYSFS_uint32 size, PHYSFS_uint32 count)
  531. {
  532. SInt16 ref = *((SInt16 *) opaque);
  533. SInt32 br;
  534. PHYSFS_uint32 i;
  535. for (i = 0; i < count; i++)
  536. {
  537. br = size;
  538. BAIL_IF_MACRO(FSRead(ref, &br, buffer) != noErr, ERR_OS_ERROR, i);
  539. BAIL_IF_MACRO(br != size, ERR_OS_ERROR, i);
  540. buffer = ((PHYSFS_uint8 *) buffer) + size;
  541. } /* for */
  542. return(count);
  543. } /* __PHYSFS_platformRead */
  544. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  545. PHYSFS_uint32 size, PHYSFS_uint32 count)
  546. {
  547. SInt16 ref = *((SInt16 *) opaque);
  548. SInt32 bw;
  549. PHYSFS_uint32 i;
  550. for (i = 0; i < count; i++)
  551. {
  552. bw = size;
  553. BAIL_IF_MACRO(FSWrite(ref, &bw, buffer) != noErr, ERR_OS_ERROR, i);
  554. BAIL_IF_MACRO(bw != size, ERR_OS_ERROR, i);
  555. buffer = ((PHYSFS_uint8 *) buffer) + size;
  556. } /* for */
  557. return(count);
  558. } /* __PHYSFS_platformWrite */
  559. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  560. {
  561. SInt16 ref = *((SInt16 *) opaque);
  562. OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
  563. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  564. return(1);
  565. } /* __PHYSFS_platformSeek */
  566. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  567. {
  568. SInt16 ref = *((SInt16 *) opaque);
  569. SInt32 curPos;
  570. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, -1);
  571. return((PHYSFS_sint64) curPos);
  572. } /* __PHYSFS_platformTell */
  573. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  574. {
  575. SInt16 ref = *((SInt16 *) opaque);
  576. SInt32 eofPos;
  577. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, -1);
  578. return((PHYSFS_sint64) eofPos);
  579. } /* __PHYSFS_platformFileLength */
  580. int __PHYSFS_platformEOF(void *opaque)
  581. {
  582. SInt16 ref = *((SInt16 *) opaque);
  583. SInt32 eofPos, curPos;
  584. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, 1);
  585. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, 1);
  586. return(curPos >= eofPos);
  587. } /* __PHYSFS_platformEOF */
  588. int __PHYSFS_platformFlush(void *opaque)
  589. {
  590. SInt16 ref = *((SInt16 *) opaque);
  591. ParamBlockRec pb;
  592. memset(&pb, '\0', sizeof (ParamBlockRec));
  593. pb.ioParam.ioRefNum = ref;
  594. BAIL_IF_MACRO(PBFlushFileSync(&pb) != noErr, ERR_OS_ERROR, 0);
  595. return(1);
  596. } /* __PHYSFS_platformFlush */
  597. int __PHYSFS_platformClose(void *opaque)
  598. {
  599. SInt16 ref = *((SInt16 *) opaque);
  600. SInt16 vRefNum;
  601. HParamBlockRec hpbr;
  602. Str63 volName;
  603. BAIL_IF_MACRO(GetVRefNum (ref, &vRefNum) != noErr, ERR_OS_ERROR, 0);
  604. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  605. hpbr.volumeParam.ioNamePtr = volName;
  606. hpbr.volumeParam.ioVRefNum = vRefNum;
  607. hpbr.volumeParam.ioVolIndex = 0;
  608. BAIL_IF_MACRO(PBHGetVInfoSync(&hpbr) != noErr, ERR_OS_ERROR, 0);
  609. BAIL_IF_MACRO(FSClose(ref) != noErr, ERR_OS_ERROR, 0);
  610. free(opaque);
  611. FlushVol(volName, vRefNum);
  612. return(1);
  613. } /* __PHYSFS_platformClose */
  614. int __PHYSFS_platformDelete(const char *path)
  615. {
  616. FSSpec spec;
  617. OSErr err;
  618. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, 0);
  619. err = HDelete(spec.vRefNum, spec.parID, spec.name);
  620. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  621. return(1);
  622. } /* __PHYSFS_platformDelete */
  623. void *__PHYSFS_platformCreateMutex(void)
  624. {
  625. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  626. } /* __PHYSFS_platformCreateMutex */
  627. void __PHYSFS_platformDestroyMutex(void *mutex)
  628. {
  629. /* no mutexes on MacOS Classic. */
  630. } /* __PHYSFS_platformDestroyMutex */
  631. int __PHYSFS_platformGrabMutex(void *mutex)
  632. {
  633. return(1); /* no mutexes on MacOS Classic. */
  634. } /* __PHYSFS_platformGrabMutex */
  635. void __PHYSFS_platformReleaseMutex(void *mutex)
  636. {
  637. /* no mutexes on MacOS Classic. */
  638. } /* __PHYSFS_platformReleaseMutex */
  639. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  640. {
  641. FSSpec spec;
  642. CInfoPBRec infoPB;
  643. UInt32 modDate;
  644. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, -1);
  645. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  646. infoPB.dirInfo.ioNamePtr = spec.name;
  647. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  648. infoPB.dirInfo.ioDrDirID = spec.parID;
  649. infoPB.dirInfo.ioFDirIndex = 0;
  650. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, -1);
  651. modDate = ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0) ?
  652. infoPB.dirInfo.ioDrMdDat : infoPB.hFileInfo.ioFlMdDat;
  653. /* epoch is different on MacOS. They use Jan 1, 1904, apparently. */
  654. /* subtract seconds between those epochs, counting leap years. */
  655. modDate -= 2082844800;
  656. return((PHYSFS_sint64) modDate);
  657. } /* __PHYSFS_platformGetLastModTime */
  658. /* end of macclassic.c ... */