macclassic.c 24 KB

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