macclassic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  122. {
  123. char *ptr;
  124. char *retval = NULL;
  125. UInt32 retLength = 0;
  126. CInfoPBRec infoPB;
  127. Str255 str255;
  128. FSSpec spec;
  129. /* Get the name of the binary's parent directory. */
  130. memcpy(&spec, &procfsspec, sizeof (FSSpec));
  131. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  132. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  133. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of bin's volume. */
  134. infoPB.dirInfo.ioDrParID = spec.parID; /* ID of bin's dir. */
  135. infoPB.dirInfo.ioFDirIndex = -1; /* get dir (not file) info. */
  136. /* walk the tree back to the root dir (volume), building path string... */
  137. do
  138. {
  139. /* check parent dir of what we last looked at... */
  140. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  141. if (PBGetCatInfoSync(&infoPB) != noErr)
  142. {
  143. if (retval != NULL)
  144. free(retval);
  145. BAIL_MACRO(ERR_OS_ERROR, NULL);
  146. } /* if */
  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. } /* __PHYSFS_platformCalcBaseDir */
  169. char *__PHYSFS_platformGetUserName(void)
  170. {
  171. char *retval = NULL;
  172. StringHandle strHandle;
  173. short origResourceFile = CurResFile();
  174. /* use the System resource file. */
  175. UseResFile(0);
  176. /* apparently, -16096 specifies the username. */
  177. strHandle = GetString(-16096);
  178. UseResFile(origResourceFile);
  179. BAIL_IF_MACRO(strHandle == NULL, ERR_OS_ERROR, NULL);
  180. HLock((Handle) strHandle);
  181. retval = (char *) malloc((*strHandle)[0] + 1);
  182. if (retval == NULL)
  183. {
  184. HUnlock((Handle) strHandle);
  185. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  186. } /* if */
  187. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  188. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  189. HUnlock((Handle) strHandle);
  190. return(retval);
  191. } /* __PHYSFS_platformGetUserName */
  192. char *__PHYSFS_platformGetUserDir(void)
  193. {
  194. return(NULL); /* bah...use default behaviour, I guess. */
  195. } /* __PHYSFS_platformGetUserDir */
  196. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  197. {
  198. return(1); /* single threaded. */
  199. } /* __PHYSFS_platformGetThreadID */
  200. int __PHYSFS_platformStricmp(const char *x, const char *y)
  201. {
  202. extern int _stricmp(const char *, const char *);
  203. return(_stricmp(x, y)); /* (*shrug*) */
  204. } /* __PHYSFS_platformStricmp */
  205. static OSErr fnameToFSSpecNoAlias(const char *fname, FSSpec *spec)
  206. {
  207. OSErr err;
  208. Str255 str255;
  209. int needColon = (strchr(fname, ':') == NULL);
  210. int len = strlen(fname) + ((needColon) ? 1 : 0);
  211. if (len > 255)
  212. return(bdNamErr);
  213. /* !!! FIXME: What happens with relative pathnames? */
  214. str255[0] = len;
  215. memcpy(&str255[1], fname, len);
  216. /* probably just a volume name, which seems to need a ':' at the end. */
  217. if (needColon)
  218. str255[len] = ':';
  219. err = FSMakeFSSpec(0, 0, str255, spec);
  220. return(err);
  221. } /* fnameToFSSpecNoAlias */
  222. /* !!! FIXME: This code is pretty heinous. */
  223. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  224. {
  225. Boolean alias = 0;
  226. Boolean folder = 0;
  227. OSErr err = fnameToFSSpecNoAlias(fname, spec);
  228. if (err == dirNFErr) /* might be an alias in the middle of the path. */
  229. {
  230. /*
  231. * Has to be at least two ':' chars, or we wouldn't get a
  232. * dir-not-found condition. (no ':' means it was just a volume,
  233. * just one ':' means we would have gotten a fnfErr, if anything.
  234. */
  235. char *ptr;
  236. char *start;
  237. char *path = alloca(strlen(fname) + 1);
  238. strcpy(path, fname);
  239. ptr = strchr(path, ':');
  240. BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
  241. ptr = strchr(ptr + 1, ':');
  242. BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
  243. *ptr = '\0';
  244. err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
  245. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  246. start = ptr;
  247. ptr = strchr(start + 1, ':');
  248. do
  249. {
  250. CInfoPBRec infoPB;
  251. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  252. infoPB.dirInfo.ioNamePtr = spec->name;
  253. infoPB.dirInfo.ioVRefNum = spec->vRefNum;
  254. infoPB.dirInfo.ioDrDirID = spec->parID;
  255. infoPB.dirInfo.ioFDirIndex = 0;
  256. err = PBGetCatInfoSync(&infoPB);
  257. if (err != noErr) /* not an alias, really a bogus path. */
  258. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  259. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0)
  260. spec->parID = infoPB.dirInfo.ioDrDirID;
  261. if (ptr != NULL)
  262. *ptr = '\0';
  263. *start = strlen(start + 1); /* make it a pstring. */
  264. err = FSMakeFSSpec(spec->vRefNum, spec->parID,
  265. (const unsigned char *) start, spec);
  266. if (err != noErr) /* not an alias, really a bogus path. */
  267. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  268. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  269. if (err != noErr) /* not an alias, really a bogus path. */
  270. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  271. start = ptr;
  272. if (ptr != NULL)
  273. ptr = strchr(start + 1, ':');
  274. } while (start != NULL);
  275. } /* if */
  276. else /* there's something there; make sure final file is not an alias. */
  277. {
  278. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  279. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  280. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  281. } /* else */
  282. return(noErr); /* w00t. */
  283. } /* fnameToFSSpec */
  284. int __PHYSFS_platformExists(const char *fname)
  285. {
  286. FSSpec spec;
  287. return(fnameToFSSpec(fname, &spec) == noErr);
  288. } /* __PHYSFS_platformExists */
  289. int __PHYSFS_platformIsSymLink(const char *fname)
  290. {
  291. OSErr err;
  292. FSSpec spec;
  293. Boolean a = 0;
  294. Boolean f = 0;
  295. CInfoPBRec infoPB;
  296. char *ptr;
  297. char *dir = alloca(strlen(fname) + 1);
  298. BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
  299. strcpy(dir, fname);
  300. ptr = strrchr(dir, ':');
  301. if (ptr == NULL) /* just a volume name? Can't be a symlink. */
  302. return(0);
  303. /* resolve aliases up to the actual file... */
  304. *ptr = '\0';
  305. BAIL_IF_MACRO(fnameToFSSpec(dir, &spec) != noErr, ERR_OS_ERROR, 0);
  306. *ptr = strlen(ptr + 1); /* ptr is now a pascal string. Yikes! */
  307. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  308. infoPB.dirInfo.ioNamePtr = spec.name;
  309. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  310. infoPB.dirInfo.ioDrDirID = spec.parID;
  311. infoPB.dirInfo.ioFDirIndex = 0;
  312. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, 0);
  313. err = FSMakeFSSpec(spec.vRefNum, infoPB.dirInfo.ioDrDirID,
  314. (const unsigned char *) ptr, &spec);
  315. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  316. BAIL_IF_MACRO(IsAliasFile(&spec, &a, &f) != noErr, ERR_OS_ERROR, 0);
  317. return(a);
  318. } /* __PHYSFS_platformIsSymlink */
  319. int __PHYSFS_platformIsDirectory(const char *fname)
  320. {
  321. FSSpec spec;
  322. CInfoPBRec infoPB;
  323. OSErr err;
  324. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, 0);
  325. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  326. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  327. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  328. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  329. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  330. err = PBGetCatInfoSync(&infoPB);
  331. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  332. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  333. } /* __PHYSFS_platformIsDirectory */
  334. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  335. const char *dirName,
  336. const char *append)
  337. {
  338. int len = ((prepend) ? strlen(prepend) : 0) +
  339. ((append) ? strlen(append) : 0) +
  340. strlen(dirName) + 1;
  341. const char *src;
  342. char *dst;
  343. char *retval = malloc(len);
  344. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  345. if (prepend != NULL)
  346. {
  347. strcpy(retval, prepend);
  348. dst = retval + strlen(retval);
  349. } /* if */
  350. else
  351. {
  352. *retval = '\0';
  353. dst = retval;
  354. } /* else */
  355. for (src = dirName; *src; src++, dst++)
  356. *dst = ((*src == '/') ? ':' : *src);
  357. *dst = '\0';
  358. return(retval);
  359. } /* __PHYSFS_platformCvtToDependent */
  360. void __PHYSFS_platformTimeslice(void)
  361. {
  362. SystemTask();
  363. } /* __PHYSFS_platformTimeslice */
  364. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  365. int omitSymLinks)
  366. {
  367. LinkedStringList *retval = NULL;
  368. LinkedStringList *l = NULL;
  369. LinkedStringList *prev = NULL;
  370. UInt16 i;
  371. UInt16 max;
  372. FSSpec spec;
  373. CInfoPBRec infoPB;
  374. Str255 str255;
  375. long dirID;
  376. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
  377. /* get the dir ID of what we want to enumerate... */
  378. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  379. infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
  380. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  381. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
  382. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  383. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, NULL);
  384. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
  385. BAIL_MACRO(ERR_NOT_A_DIR, NULL);
  386. dirID = infoPB.dirInfo.ioDrDirID;
  387. max = infoPB.dirInfo.ioDrNmFls;
  388. for (i = 1; i <= max; i++)
  389. {
  390. FSSpec aliasspec;
  391. Boolean alias = 0;
  392. Boolean folder = 0;
  393. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  394. str255[0] = 0;
  395. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  396. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
  397. infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
  398. infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
  399. if (PBGetCatInfoSync(&infoPB) != noErr)
  400. continue; /* skip this file. Oh well. */
  401. if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
  402. continue; /* skip it. */
  403. if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
  404. continue; /* skip it. */
  405. if ((alias) && (omitSymLinks))
  406. continue;
  407. /* still here? Add it to the list. */
  408. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  409. if (l == NULL)
  410. break;
  411. l->str = (char *) malloc(str255[0] + 1);
  412. if (l->str == NULL)
  413. {
  414. free(l);
  415. break;
  416. } /* if */
  417. memcpy(l->str, &str255[1], str255[0]);
  418. l->str[str255[0]] = '\0';
  419. if (retval == NULL)
  420. retval = l;
  421. else
  422. prev->next = l;
  423. prev = l;
  424. l->next = NULL;
  425. } /* for */
  426. return(retval);
  427. } /* __PHYSFS_platformEnumerateFiles */
  428. char *__PHYSFS_platformCurrentDir(void)
  429. {
  430. /*
  431. * I don't think MacOS has a concept of "current directory", beyond
  432. * what is grafted on by a given standard C library implementation,
  433. * so just return the base dir.
  434. * We don't use this for anything crucial at the moment anyhow.
  435. */
  436. return(__PHYSFS_platformCalcBaseDir(NULL));
  437. } /* __PHYSFS_platformCurrentDir */
  438. char *__PHYSFS_platformRealPath(const char *path)
  439. {
  440. /* !!! FIXME: This isn't nearly right. */
  441. char *retval = (char *) malloc(strlen(path) + 1);
  442. strcpy(retval, path);
  443. return(retval);
  444. } /* __PHYSFS_platformRealPath */
  445. int __PHYSFS_platformMkDir(const char *path)
  446. {
  447. SInt32 val = 0;
  448. FSSpec spec;
  449. OSErr err = fnameToFSSpec(path, &spec);
  450. BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
  451. BAIL_IF_MACRO(err != fnfErr, ERR_OS_ERROR, 0);
  452. err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
  453. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  454. return(1);
  455. } /* __PHYSFS_platformMkDir */
  456. static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
  457. {
  458. int created = 0;
  459. SInt16 *retval = NULL;
  460. FSSpec spec;
  461. OSErr err = fnameToFSSpec(fname, &spec);
  462. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), ERR_OS_ERROR, NULL);
  463. if (err == fnfErr)
  464. {
  465. BAIL_IF_MACRO(!createIfMissing, ERR_FILE_NOT_FOUND, NULL);
  466. err = HCreate(spec.vRefNum, spec.parID, spec.name,
  467. procInfo.processSignature, 'BINA');
  468. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
  469. created = 1;
  470. } /* if */
  471. retval = (SInt16 *) malloc(sizeof (SInt16));
  472. if (retval == NULL)
  473. {
  474. if (created)
  475. HDelete(spec.vRefNum, spec.parID, spec.name);
  476. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  477. } /* if */
  478. if (HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval) != noErr)
  479. {
  480. free(retval);
  481. if (created)
  482. HDelete(spec.vRefNum, spec.parID, spec.name);
  483. BAIL_MACRO(ERR_OS_ERROR, NULL);
  484. } /* if */
  485. return(retval);
  486. } /* macDoOpen */
  487. void *__PHYSFS_platformOpenRead(const char *filename)
  488. {
  489. SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
  490. if (retval != NULL) /* got a file; seek to start. */
  491. {
  492. if (SetFPos(*retval, fsFromStart, 0) != noErr)
  493. {
  494. FSClose(*retval);
  495. BAIL_MACRO(ERR_OS_ERROR, NULL);
  496. } /* if */
  497. } /* if */
  498. return((void *) retval);
  499. } /* __PHYSFS_platformOpenRead */
  500. void *__PHYSFS_platformOpenWrite(const char *filename)
  501. {
  502. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  503. if (retval != NULL) /* got a file; truncate it. */
  504. {
  505. if ((SetEOF(*retval, 0) != noErr) ||
  506. (SetFPos(*retval, fsFromStart, 0) != noErr))
  507. {
  508. FSClose(*retval);
  509. BAIL_MACRO(ERR_OS_ERROR, NULL);
  510. } /* if */
  511. } /* if */
  512. return((void *) retval);
  513. } /* __PHYSFS_platformOpenWrite */
  514. void *__PHYSFS_platformOpenAppend(const char *filename)
  515. {
  516. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  517. if (retval != NULL) /* got a file; seek to end. */
  518. {
  519. if (SetFPos(*retval, fsFromLEOF, 0) != noErr)
  520. {
  521. FSClose(*retval);
  522. BAIL_MACRO(ERR_OS_ERROR, NULL);
  523. } /* if */
  524. } /* if */
  525. return(retval);
  526. } /* __PHYSFS_platformOpenAppend */
  527. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  528. PHYSFS_uint32 size, PHYSFS_uint32 count)
  529. {
  530. SInt16 ref = *((SInt16 *) opaque);
  531. SInt32 br;
  532. PHYSFS_uint32 i;
  533. for (i = 0; i < count; i++)
  534. {
  535. br = size;
  536. BAIL_IF_MACRO(FSRead(ref, &br, buffer) != noErr, ERR_OS_ERROR, i);
  537. BAIL_IF_MACRO(br != size, ERR_OS_ERROR, i);
  538. buffer = ((PHYSFS_uint8 *) buffer) + size;
  539. } /* for */
  540. return(count);
  541. } /* __PHYSFS_platformRead */
  542. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  543. PHYSFS_uint32 size, PHYSFS_uint32 count)
  544. {
  545. SInt16 ref = *((SInt16 *) opaque);
  546. SInt32 bw;
  547. PHYSFS_uint32 i;
  548. for (i = 0; i < count; i++)
  549. {
  550. bw = size;
  551. BAIL_IF_MACRO(FSWrite(ref, &bw, buffer) != noErr, ERR_OS_ERROR, i);
  552. BAIL_IF_MACRO(bw != size, ERR_OS_ERROR, i);
  553. buffer = ((PHYSFS_uint8 *) buffer) + size;
  554. } /* for */
  555. return(count);
  556. } /* __PHYSFS_platformWrite */
  557. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  558. {
  559. SInt16 ref = *((SInt16 *) opaque);
  560. OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
  561. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  562. return(1);
  563. } /* __PHYSFS_platformSeek */
  564. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  565. {
  566. SInt16 ref = *((SInt16 *) opaque);
  567. SInt32 curPos;
  568. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, -1);
  569. return((PHYSFS_sint64) curPos);
  570. } /* __PHYSFS_platformTell */
  571. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  572. {
  573. SInt16 ref = *((SInt16 *) opaque);
  574. SInt32 eofPos;
  575. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, -1);
  576. return((PHYSFS_sint64) eofPos);
  577. } /* __PHYSFS_platformFileLength */
  578. int __PHYSFS_platformEOF(void *opaque)
  579. {
  580. SInt16 ref = *((SInt16 *) opaque);
  581. SInt32 eofPos, curPos;
  582. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, 1);
  583. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, 1);
  584. return(curPos >= eofPos);
  585. } /* __PHYSFS_platformEOF */
  586. int __PHYSFS_platformFlush(void *opaque)
  587. {
  588. SInt16 ref = *((SInt16 *) opaque);
  589. ParamBlockRec pb;
  590. memset(&pb, '\0', sizeof (ParamBlockRec));
  591. pb.ioParam.ioRefNum = ref;
  592. BAIL_IF_MACRO(PBFlushFileSync(&pb) != noErr, ERR_OS_ERROR, 0);
  593. return(1);
  594. } /* __PHYSFS_platformFlush */
  595. int __PHYSFS_platformClose(void *opaque)
  596. {
  597. SInt16 ref = *((SInt16 *) opaque);
  598. SInt16 vRefNum;
  599. HParamBlockRec hpbr;
  600. Str63 volName;
  601. BAIL_IF_MACRO(GetVRefNum (ref, &vRefNum) != noErr, ERR_OS_ERROR, 0);
  602. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  603. hpbr.volumeParam.ioNamePtr = volName;
  604. hpbr.volumeParam.ioVRefNum = vRefNum;
  605. hpbr.volumeParam.ioVolIndex = 0;
  606. BAIL_IF_MACRO(PBHGetVInfoSync(&hpbr) != noErr, ERR_OS_ERROR, 0);
  607. BAIL_IF_MACRO(FSClose(ref) != noErr, ERR_OS_ERROR, 0);
  608. free(opaque);
  609. FlushVol(volName, vRefNum);
  610. return(1);
  611. } /* __PHYSFS_platformClose */
  612. int __PHYSFS_platformDelete(const char *path)
  613. {
  614. FSSpec spec;
  615. OSErr err;
  616. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, 0);
  617. err = HDelete(spec.vRefNum, spec.parID, spec.name);
  618. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  619. return(1);
  620. } /* __PHYSFS_platformDelete */
  621. void *__PHYSFS_platformCreateMutex(void)
  622. {
  623. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  624. } /* __PHYSFS_platformCreateMutex */
  625. void __PHYSFS_platformDestroyMutex(void *mutex)
  626. {
  627. /* no mutexes on MacOS Classic. */
  628. } /* __PHYSFS_platformDestroyMutex */
  629. int __PHYSFS_platformGrabMutex(void *mutex)
  630. {
  631. return(1); /* no mutexes on MacOS Classic. */
  632. } /* __PHYSFS_platformGrabMutex */
  633. void __PHYSFS_platformReleaseMutex(void *mutex)
  634. {
  635. /* no mutexes on MacOS Classic. */
  636. } /* __PHYSFS_platformReleaseMutex */
  637. /* end of macclassic.c ... */