macclassic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. /* !!! FIXME: This code is pretty heinous. */
  238. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  239. {
  240. Boolean alias = 0;
  241. Boolean folder = 0;
  242. OSErr err = fnameToFSSpecNoAlias(fname, spec);
  243. if (err == dirNFErr) /* might be an alias in the middle of the path. */
  244. {
  245. /*
  246. * Has to be at least two ':' chars, or we wouldn't get a
  247. * dir-not-found condition. (no ':' means it was just a volume,
  248. * just one ':' means we would have gotten a fnfErr, if anything.
  249. */
  250. char *ptr;
  251. char *start;
  252. char *path = alloca(strlen(fname) + 1);
  253. strcpy(path, fname);
  254. ptr = strchr(path, ':');
  255. BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
  256. ptr = strchr(ptr + 1, ':');
  257. BAIL_IF_MACRO(!ptr, ERR_FILE_NOT_FOUND, err); /* just in case */
  258. *ptr = '\0';
  259. err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
  260. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, err);
  261. start = ptr;
  262. ptr = strchr(start + 1, ':');
  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 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)
  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;
  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 *retval = NULL;
  383. LinkedStringList *l = NULL;
  384. LinkedStringList *prev = NULL;
  385. UInt16 i;
  386. UInt16 max;
  387. FSSpec spec;
  388. CInfoPBRec infoPB;
  389. Str255 str255;
  390. long dirID;
  391. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
  392. /* get the dir ID of what we want to enumerate... */
  393. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  394. infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
  395. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  396. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
  397. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  398. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, NULL);
  399. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
  400. BAIL_MACRO(ERR_NOT_A_DIR, NULL);
  401. dirID = infoPB.dirInfo.ioDrDirID;
  402. max = infoPB.dirInfo.ioDrNmFls;
  403. for (i = 1; i <= max; i++)
  404. {
  405. FSSpec aliasspec;
  406. Boolean alias = 0;
  407. Boolean folder = 0;
  408. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  409. str255[0] = 0;
  410. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  411. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
  412. infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
  413. infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
  414. if (PBGetCatInfoSync(&infoPB) != noErr)
  415. continue; /* skip this file. Oh well. */
  416. if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
  417. continue; /* skip it. */
  418. if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
  419. continue; /* skip it. */
  420. if ((alias) && (omitSymLinks))
  421. continue;
  422. /* still here? Add it to the list. */
  423. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  424. if (l == NULL)
  425. break;
  426. l->str = (char *) malloc(str255[0] + 1);
  427. if (l->str == NULL)
  428. {
  429. free(l);
  430. break;
  431. } /* if */
  432. memcpy(l->str, &str255[1], str255[0]);
  433. l->str[str255[0]] = '\0';
  434. if (retval == NULL)
  435. retval = l;
  436. else
  437. prev->next = l;
  438. prev = l;
  439. l->next = NULL;
  440. } /* for */
  441. return(retval);
  442. } /* __PHYSFS_platformEnumerateFiles */
  443. char *__PHYSFS_platformCurrentDir(void)
  444. {
  445. /*
  446. * I don't think MacOS has a concept of "current directory", beyond
  447. * what is grafted on by a given standard C library implementation,
  448. * so just return the base dir.
  449. * We don't use this for anything crucial at the moment anyhow.
  450. */
  451. return(__PHYSFS_platformCalcBaseDir(NULL));
  452. } /* __PHYSFS_platformCurrentDir */
  453. char *__PHYSFS_platformRealPath(const char *path)
  454. {
  455. /*
  456. * fnameToFSSpec() will resolve any symlinks to get to the real
  457. * file's FSSpec, which, when converted, will contain the real
  458. * direct path to a given file. convFSSpecToPath() mallocs a
  459. * return value buffer.
  460. */
  461. FSSpec spec;
  462. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, NULL);
  463. return(convFSSpecToPath(&spec, 1));
  464. } /* __PHYSFS_platformRealPath */
  465. int __PHYSFS_platformMkDir(const char *path)
  466. {
  467. SInt32 val = 0;
  468. FSSpec spec;
  469. OSErr err = fnameToFSSpec(path, &spec);
  470. BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
  471. BAIL_IF_MACRO(err != fnfErr, ERR_OS_ERROR, 0);
  472. err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
  473. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  474. return(1);
  475. } /* __PHYSFS_platformMkDir */
  476. static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
  477. {
  478. int created = 0;
  479. SInt16 *retval = NULL;
  480. FSSpec spec;
  481. OSErr err = fnameToFSSpec(fname, &spec);
  482. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), ERR_OS_ERROR, NULL);
  483. if (err == fnfErr)
  484. {
  485. BAIL_IF_MACRO(!createIfMissing, ERR_FILE_NOT_FOUND, NULL);
  486. err = HCreate(spec.vRefNum, spec.parID, spec.name,
  487. procInfo.processSignature, 'BINA');
  488. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
  489. created = 1;
  490. } /* if */
  491. retval = (SInt16 *) malloc(sizeof (SInt16));
  492. if (retval == NULL)
  493. {
  494. if (created)
  495. HDelete(spec.vRefNum, spec.parID, spec.name);
  496. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  497. } /* if */
  498. if (HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval) != noErr)
  499. {
  500. free(retval);
  501. if (created)
  502. HDelete(spec.vRefNum, spec.parID, spec.name);
  503. BAIL_MACRO(ERR_OS_ERROR, NULL);
  504. } /* if */
  505. return(retval);
  506. } /* macDoOpen */
  507. void *__PHYSFS_platformOpenRead(const char *filename)
  508. {
  509. SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
  510. if (retval != NULL) /* got a file; seek to start. */
  511. {
  512. if (SetFPos(*retval, fsFromStart, 0) != noErr)
  513. {
  514. FSClose(*retval);
  515. BAIL_MACRO(ERR_OS_ERROR, NULL);
  516. } /* if */
  517. } /* if */
  518. return((void *) retval);
  519. } /* __PHYSFS_platformOpenRead */
  520. void *__PHYSFS_platformOpenWrite(const char *filename)
  521. {
  522. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  523. if (retval != NULL) /* got a file; truncate it. */
  524. {
  525. if ((SetEOF(*retval, 0) != noErr) ||
  526. (SetFPos(*retval, fsFromStart, 0) != noErr))
  527. {
  528. FSClose(*retval);
  529. BAIL_MACRO(ERR_OS_ERROR, NULL);
  530. } /* if */
  531. } /* if */
  532. return((void *) retval);
  533. } /* __PHYSFS_platformOpenWrite */
  534. void *__PHYSFS_platformOpenAppend(const char *filename)
  535. {
  536. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  537. if (retval != NULL) /* got a file; seek to end. */
  538. {
  539. if (SetFPos(*retval, fsFromLEOF, 0) != noErr)
  540. {
  541. FSClose(*retval);
  542. BAIL_MACRO(ERR_OS_ERROR, NULL);
  543. } /* if */
  544. } /* if */
  545. return(retval);
  546. } /* __PHYSFS_platformOpenAppend */
  547. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  548. PHYSFS_uint32 size, PHYSFS_uint32 count)
  549. {
  550. SInt16 ref = *((SInt16 *) opaque);
  551. SInt32 br;
  552. PHYSFS_uint32 i;
  553. for (i = 0; i < count; i++)
  554. {
  555. br = size;
  556. BAIL_IF_MACRO(FSRead(ref, &br, buffer) != noErr, ERR_OS_ERROR, i);
  557. BAIL_IF_MACRO(br != size, ERR_OS_ERROR, i);
  558. buffer = ((PHYSFS_uint8 *) buffer) + size;
  559. } /* for */
  560. return(count);
  561. } /* __PHYSFS_platformRead */
  562. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  563. PHYSFS_uint32 size, PHYSFS_uint32 count)
  564. {
  565. SInt16 ref = *((SInt16 *) opaque);
  566. SInt32 bw;
  567. PHYSFS_uint32 i;
  568. for (i = 0; i < count; i++)
  569. {
  570. bw = size;
  571. BAIL_IF_MACRO(FSWrite(ref, &bw, buffer) != noErr, ERR_OS_ERROR, i);
  572. BAIL_IF_MACRO(bw != size, ERR_OS_ERROR, i);
  573. buffer = ((PHYSFS_uint8 *) buffer) + size;
  574. } /* for */
  575. return(count);
  576. } /* __PHYSFS_platformWrite */
  577. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  578. {
  579. SInt16 ref = *((SInt16 *) opaque);
  580. OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
  581. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  582. return(1);
  583. } /* __PHYSFS_platformSeek */
  584. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  585. {
  586. SInt16 ref = *((SInt16 *) opaque);
  587. SInt32 curPos;
  588. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, -1);
  589. return((PHYSFS_sint64) curPos);
  590. } /* __PHYSFS_platformTell */
  591. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  592. {
  593. SInt16 ref = *((SInt16 *) opaque);
  594. SInt32 eofPos;
  595. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, -1);
  596. return((PHYSFS_sint64) eofPos);
  597. } /* __PHYSFS_platformFileLength */
  598. int __PHYSFS_platformEOF(void *opaque)
  599. {
  600. SInt16 ref = *((SInt16 *) opaque);
  601. SInt32 eofPos, curPos;
  602. BAIL_IF_MACRO(GetEOF(ref, &eofPos) != noErr, ERR_OS_ERROR, 1);
  603. BAIL_IF_MACRO(GetFPos(ref, &curPos) != noErr, ERR_OS_ERROR, 1);
  604. return(curPos >= eofPos);
  605. } /* __PHYSFS_platformEOF */
  606. int __PHYSFS_platformFlush(void *opaque)
  607. {
  608. SInt16 ref = *((SInt16 *) opaque);
  609. ParamBlockRec pb;
  610. memset(&pb, '\0', sizeof (ParamBlockRec));
  611. pb.ioParam.ioRefNum = ref;
  612. BAIL_IF_MACRO(PBFlushFileSync(&pb) != noErr, ERR_OS_ERROR, 0);
  613. return(1);
  614. } /* __PHYSFS_platformFlush */
  615. int __PHYSFS_platformClose(void *opaque)
  616. {
  617. SInt16 ref = *((SInt16 *) opaque);
  618. SInt16 vRefNum;
  619. HParamBlockRec hpbr;
  620. Str63 volName;
  621. BAIL_IF_MACRO(GetVRefNum (ref, &vRefNum) != noErr, ERR_OS_ERROR, 0);
  622. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  623. hpbr.volumeParam.ioNamePtr = volName;
  624. hpbr.volumeParam.ioVRefNum = vRefNum;
  625. hpbr.volumeParam.ioVolIndex = 0;
  626. BAIL_IF_MACRO(PBHGetVInfoSync(&hpbr) != noErr, ERR_OS_ERROR, 0);
  627. BAIL_IF_MACRO(FSClose(ref) != noErr, ERR_OS_ERROR, 0);
  628. free(opaque);
  629. FlushVol(volName, vRefNum);
  630. return(1);
  631. } /* __PHYSFS_platformClose */
  632. int __PHYSFS_platformDelete(const char *path)
  633. {
  634. FSSpec spec;
  635. OSErr err;
  636. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, ERR_OS_ERROR, 0);
  637. err = HDelete(spec.vRefNum, spec.parID, spec.name);
  638. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, 0);
  639. return(1);
  640. } /* __PHYSFS_platformDelete */
  641. void *__PHYSFS_platformCreateMutex(void)
  642. {
  643. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  644. } /* __PHYSFS_platformCreateMutex */
  645. void __PHYSFS_platformDestroyMutex(void *mutex)
  646. {
  647. /* no mutexes on MacOS Classic. */
  648. } /* __PHYSFS_platformDestroyMutex */
  649. int __PHYSFS_platformGrabMutex(void *mutex)
  650. {
  651. return(1); /* no mutexes on MacOS Classic. */
  652. } /* __PHYSFS_platformGrabMutex */
  653. void __PHYSFS_platformReleaseMutex(void *mutex)
  654. {
  655. /* no mutexes on MacOS Classic. */
  656. } /* __PHYSFS_platformReleaseMutex */
  657. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  658. {
  659. FSSpec spec;
  660. CInfoPBRec infoPB;
  661. UInt32 modDate;
  662. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, -1);
  663. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  664. infoPB.dirInfo.ioNamePtr = spec.name;
  665. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  666. infoPB.dirInfo.ioDrDirID = spec.parID;
  667. infoPB.dirInfo.ioFDirIndex = 0;
  668. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, -1);
  669. modDate = ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0) ?
  670. infoPB.dirInfo.ioDrMdDat : infoPB.hFileInfo.ioFlMdDat;
  671. /* epoch is different on MacOS. They use Jan 1, 1904, apparently. */
  672. /* subtract seconds between those epochs, counting leap years. */
  673. modDate -= 2082844800;
  674. return((PHYSFS_sint64) modDate);
  675. } /* __PHYSFS_platformGetLastModTime */
  676. /* end of macclassic.c ... */