macclassic.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*
  2. * Mac OS classic support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_MACCLASSIC
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. /*
  16. * Most of the API calls in here are, according to ADC, available since
  17. * Mac OS 8.1. I don't think I used any Mac OS 9 or CarbonLib-specific
  18. * functions. There might be one or two 8.5 calls, and perhaps when the
  19. * ADC docs say "Available in Mac OS 8.1" they really mean "this works
  20. * with System 6, but we don't want to hear about it at this point."
  21. *
  22. * IsAliasFile() showed up in Mac OS 8.5. You can duplicate this code with
  23. * PBGetCatInfoSync(), which is an older API, if you hope the bits in the
  24. * catalog info never change (which they won't for, say, Mac OS 8.1).
  25. * See Apple Technote FL-30:
  26. * http://developer.apple.com/technotes/fl/fl_30.html
  27. *
  28. * If you want to use weak pointers and Gestalt, and choose the correct
  29. * code to use during __PHYSFS_platformInit(), I'll accept a patch, but
  30. * chances are, it wasn't worth the time it took to write this, let alone
  31. * implement that.
  32. */
  33. #include <OSUtils.h>
  34. #include <Processes.h>
  35. #include <Files.h>
  36. #include <TextUtils.h>
  37. #include <Resources.h>
  38. #include <MacMemory.h>
  39. #include <Events.h>
  40. #include <DriverGestalt.h>
  41. #include <Aliases.h>
  42. #include "physfs_internal.h"
  43. const char *__PHYSFS_platformDirSeparator = ":";
  44. static const char *get_macos_error_string(OSErr err)
  45. {
  46. if (err == noErr)
  47. return(NULL);
  48. switch (err)
  49. {
  50. case fnfErr: return(ERR_NO_SUCH_FILE);
  51. case notOpenErr: return(ERR_NO_SUCH_VOLUME);
  52. case dirFulErr: return(ERR_DIRECTORY_FULL);
  53. case dskFulErr: return(ERR_DISK_FULL);
  54. case nsvErr: return(ERR_NO_SUCH_VOLUME);
  55. case ioErr: return(ERR_IO_ERROR);
  56. case bdNamErr: return(ERR_BAD_FILENAME);
  57. case fnOpnErr: return(ERR_NOT_A_HANDLE);
  58. case eofErr: return(ERR_PAST_EOF);
  59. case posErr: return(ERR_SEEK_OUT_OF_RANGE);
  60. case tmfoErr: return(ERR_TOO_MANY_HANDLES);
  61. case wPrErr: return(ERR_VOL_LOCKED_HW);
  62. case fLckdErr: return(ERR_FILE_LOCKED);
  63. case vLckdErr: return(ERR_VOL_LOCKED_SW);
  64. case fBsyErr: return(ERR_FILE_OR_DIR_BUSY);
  65. case dupFNErr: return(ERR_FILE_EXISTS);
  66. case opWrErr: return(ERR_FILE_ALREADY_OPEN_W);
  67. case rfNumErr: return(ERR_INVALID_REFNUM);
  68. case gfpErr: return(ERR_GETTING_FILE_POS);
  69. case volOffLinErr: return(ERR_VOLUME_OFFLINE);
  70. case permErr: return(ERR_PERMISSION_DENIED);
  71. case volOnLinErr: return(ERR_VOL_ALREADY_ONLINE);
  72. case nsDrvErr: return(ERR_NO_SUCH_DRIVE);
  73. case noMacDskErr: return(ERR_NOT_MAC_DISK);
  74. case extFSErr: return(ERR_VOL_EXTERNAL_FS);
  75. case fsRnErr: return(ERR_PROBLEM_RENAME);
  76. case badMDBErr: return(ERR_BAD_MASTER_BLOCK);
  77. case wrPermErr: return(ERR_PERMISSION_DENIED);
  78. case memFullErr: return(ERR_OUT_OF_MEMORY);
  79. case dirNFErr: return(ERR_NO_SUCH_PATH);
  80. case tmwdoErr: return(ERR_TOO_MANY_HANDLES);
  81. case badMovErr: return(ERR_CANT_MOVE_FORBIDDEN);
  82. case wrgVolTypErr: return(ERR_WRONG_VOL_TYPE);
  83. case volGoneErr: return(ERR_SERVER_VOL_LOST);
  84. case errFSNameTooLong: return(ERR_BAD_FILENAME);
  85. case errFSNotAFolder: return(ERR_NOT_A_DIR);
  86. /*case errFSNotAFile: return(ERR_NOT_A_FILE);*/
  87. case fidNotFound: return(ERR_FILE_ID_NOT_FOUND);
  88. case fidExists: return(ERR_FILE_ID_EXISTS);
  89. case afpAccessDenied: return(ERR_ACCESS_DENIED);
  90. case afpNoServer: return(ERR_SERVER_NO_RESPOND);
  91. case afpUserNotAuth: return(ERR_USER_AUTH_FAILED);
  92. case afpPwdExpiredErr: return(ERR_PWORD_EXPIRED);
  93. case paramErr:
  94. case errFSBadFSRef:
  95. case errFSBadBuffer:
  96. case errFSMissingName:
  97. case errFSBadPosMode:
  98. case errFSBadAllocFlags:
  99. case errFSBadItemCount:
  100. case errFSBadSearchParams:
  101. case afpDenyConflict:
  102. return(ERR_PHYSFS_BAD_OS_CALL);
  103. default: return(ERR_MACOS_GENERIC);
  104. } /* switch */
  105. return(NULL);
  106. } /* get_macos_error_string */
  107. static OSErr oserr(OSErr retval)
  108. {
  109. char buf[sizeof (ERR_MACOS_GENERIC) + 32];
  110. const char *errstr = get_macos_error_string(retval);
  111. if (errstr != NULL)
  112. {
  113. if (strcmp(errstr, ERR_MACOS_GENERIC) == 0)
  114. {
  115. sprintf(buf, ERR_MACOS_GENERIC, (int) retval);
  116. errstr = buf;
  117. } /* if */
  118. __PHYSFS_setError(errstr);
  119. } /* if */
  120. return(retval);
  121. } /* oserr */
  122. static struct ProcessInfoRec procInfo;
  123. static FSSpec procfsspec;
  124. int __PHYSFS_platformInit(void)
  125. {
  126. OSErr err;
  127. ProcessSerialNumber psn;
  128. BAIL_IF_MACRO(oserr(GetCurrentProcess(&psn)) != noErr, NULL, 0);
  129. memset(&procInfo, '\0', sizeof (ProcessInfoRec));
  130. memset(&procfsspec, '\0', sizeof (FSSpec));
  131. procInfo.processInfoLength = sizeof (ProcessInfoRec);
  132. procInfo.processAppSpec = &procfsspec;
  133. err = GetProcessInformation(&psn, &procInfo);
  134. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  135. return(1); /* we're golden. */
  136. } /* __PHYSFS_platformInit */
  137. int __PHYSFS_platformDeinit(void)
  138. {
  139. return(1); /* always succeed. */
  140. } /* __PHYSFS_platformDeinit */
  141. /*
  142. * CD detection code is borrowed from Apple Technical Q&A DV18.
  143. * http://developer.apple.com/qa/dv/dv18.html
  144. */
  145. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  146. {
  147. DriverGestaltParam pb;
  148. DrvQEl *dqp;
  149. OSErr status;
  150. pb.csCode = kDriverGestaltCode;
  151. pb.driverGestaltSelector = kdgDeviceType;
  152. dqp = (DrvQEl *) GetDrvQHdr()->qHead;
  153. while (dqp != NULL)
  154. {
  155. pb.ioCRefNum = dqp->dQRefNum;
  156. pb.ioVRefNum = dqp->dQDrive;
  157. status = PBStatusSync((ParmBlkPtr) &pb);
  158. if ((status == noErr) && (pb.driverGestaltResponse == kdgCDType))
  159. {
  160. Str63 volName;
  161. size_t size;
  162. HParamBlockRec hpbr;
  163. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  164. hpbr.volumeParam.ioNamePtr = volName;
  165. hpbr.volumeParam.ioVRefNum = dqp->dQDrive;
  166. hpbr.volumeParam.ioVolIndex = 0;
  167. if (PBHGetVInfoSync(&hpbr) == noErr)
  168. {
  169. size = (size_t) volName[0]; /* convert to ASCIZ string... */
  170. memmove(&volName[0], &volName[1], size);
  171. volName[size] = '\0';
  172. cb(data, (const char *) volName);
  173. } /* if */
  174. } /* if */
  175. dqp = (DrvQEl *) dqp->qLink;
  176. } /* while */
  177. } /* __PHYSFS_platformDetectAvailableCDs */
  178. static char *convFSSpecToPath(FSSpec *spec, int includeFile)
  179. {
  180. char *ptr;
  181. char *retval = NULL;
  182. UInt32 retLength = 0;
  183. CInfoPBRec infoPB;
  184. Str255 str255;
  185. str255[0] = spec->name[0];
  186. memcpy(&str255[1], &spec->name[1], str255[0]);
  187. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  188. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  189. infoPB.dirInfo.ioVRefNum = spec->vRefNum; /* ID of bin's volume. */
  190. infoPB.dirInfo.ioDrParID = spec->parID; /* ID of bin's dir. */
  191. infoPB.dirInfo.ioFDirIndex = (includeFile) ? 0 : -1;
  192. /* walk the tree back to the root dir (volume), building path string... */
  193. do
  194. {
  195. /* check parent dir of what we last looked at... */
  196. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  197. if (oserr(PBGetCatInfoSync(&infoPB)) != noErr)
  198. {
  199. if (retval != NULL)
  200. allocator.Free(retval);
  201. return(NULL);
  202. } /* if */
  203. infoPB.dirInfo.ioFDirIndex = -1; /* look at parent dir next time. */
  204. /* allocate more space for the retval... */
  205. retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
  206. ptr = (char *) allocator.Malloc(retLength);
  207. if (ptr == NULL)
  208. {
  209. if (retval != NULL)
  210. allocator.Free(retval);
  211. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  212. } /* if */
  213. /* prepend new dir to retval and cleanup... */
  214. memcpy(ptr, &str255[1], str255[0]);
  215. ptr[str255[0]] = '\0'; /* null terminate it. */
  216. if (retval != NULL)
  217. {
  218. strcat(ptr, ":");
  219. strcat(ptr, retval);
  220. allocator.Free(retval);
  221. } /* if */
  222. retval = ptr;
  223. } while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
  224. return(retval);
  225. } /* convFSSpecToPath */
  226. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  227. {
  228. FSSpec spec;
  229. /* Get the name of the binary's parent directory. */
  230. FSMakeFSSpec(procfsspec.vRefNum, procfsspec.parID, procfsspec.name, &spec);
  231. return(convFSSpecToPath(&spec, 0));
  232. } /* __PHYSFS_platformCalcBaseDir */
  233. char *__PHYSFS_platformGetUserName(void)
  234. {
  235. char *retval = NULL;
  236. StringHandle strHandle;
  237. short origResourceFile = CurResFile();
  238. /* use the System resource file. */
  239. UseResFile(0);
  240. /* apparently, -16096 specifies the username. */
  241. strHandle = GetString(-16096);
  242. UseResFile(origResourceFile);
  243. BAIL_IF_MACRO(strHandle == NULL, NULL, NULL);
  244. HLock((Handle) strHandle);
  245. retval = (char *) allocator.Malloc((*strHandle)[0] + 1);
  246. if (retval == NULL)
  247. {
  248. HUnlock((Handle) strHandle);
  249. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  250. } /* if */
  251. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  252. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  253. HUnlock((Handle) strHandle);
  254. return(retval);
  255. } /* __PHYSFS_platformGetUserName */
  256. char *__PHYSFS_platformGetUserDir(void)
  257. {
  258. #if 0
  259. return(NULL); /* bah...use default behaviour, I guess. */
  260. #else
  261. /* (Hmm. Default behaviour is broken in the base library. :) ) */
  262. return(__PHYSFS_platformCalcBaseDir(NULL));
  263. #endif
  264. } /* __PHYSFS_platformGetUserDir */
  265. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  266. {
  267. return(1); /* single threaded. */
  268. } /* __PHYSFS_platformGetThreadID */
  269. int __PHYSFS_platformStricmp(const char *x, const char *y)
  270. {
  271. int ux, uy;
  272. do
  273. {
  274. ux = toupper((int) *x);
  275. uy = toupper((int) *y);
  276. if (ux != uy)
  277. return((ux > uy) ? 1 : -1);
  278. x++;
  279. y++;
  280. } while ((ux) && (uy));
  281. return(0);
  282. } /* __PHYSFS_platformStricmp */
  283. int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
  284. {
  285. int ux, uy;
  286. if (!len)
  287. return(0);
  288. do
  289. {
  290. ux = toupper((int) *x);
  291. uy = toupper((int) *y);
  292. if (ux != uy)
  293. return((ux > uy) ? 1 : -1);
  294. x++;
  295. y++;
  296. len--;
  297. } while ((ux) && (uy) && (len));
  298. return(0);
  299. } /* __PHYSFS_platformStrnicmp */
  300. static OSErr fnameToFSSpecNoAlias(const char *fname, FSSpec *spec)
  301. {
  302. OSErr err;
  303. Str255 str255;
  304. int needColon = (strchr(fname, ':') == NULL);
  305. int len = strlen(fname) + ((needColon) ? 1 : 0);
  306. if (len > 255)
  307. return(bdNamErr);
  308. /* !!! FIXME: What happens with relative pathnames? */
  309. str255[0] = len;
  310. memcpy(&str255[1], fname, len);
  311. /* probably just a volume name, which seems to need a ':' at the end. */
  312. if (needColon)
  313. str255[len] = ':';
  314. err = oserr(FSMakeFSSpec(0, 0, str255, spec));
  315. return(err);
  316. } /* fnameToFSSpecNoAlias */
  317. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  318. {
  319. Boolean alias = 0;
  320. Boolean folder = 0;
  321. OSErr err = fnameToFSSpecNoAlias(fname, spec);
  322. if (err == dirNFErr) /* might be an alias in the middle of the path. */
  323. {
  324. /*
  325. * Has to be at least two ':' chars, or we wouldn't get a
  326. * dir-not-found condition. (no ':' means it was just a volume,
  327. * just one ':' means we would have gotten a fnfErr, if anything.
  328. */
  329. char *ptr;
  330. char *start;
  331. char *path = alloca(strlen(fname) + 1);
  332. strcpy(path, fname);
  333. ptr = strchr(path, ':');
  334. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  335. ptr = strchr(ptr + 1, ':');
  336. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  337. *ptr = '\0';
  338. err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
  339. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  340. start = ptr;
  341. ptr = strchr(start + 1, ':');
  342. /* Now check each element of the path for aliases... */
  343. do
  344. {
  345. CInfoPBRec infoPB;
  346. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  347. infoPB.dirInfo.ioNamePtr = spec->name;
  348. infoPB.dirInfo.ioVRefNum = spec->vRefNum;
  349. infoPB.dirInfo.ioDrDirID = spec->parID;
  350. infoPB.dirInfo.ioFDirIndex = 0;
  351. err = PBGetCatInfoSync(&infoPB);
  352. if (err != noErr) /* not an alias, really just a bogus path. */
  353. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  354. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0)
  355. spec->parID = infoPB.dirInfo.ioDrDirID;
  356. if (ptr != NULL) /* terminate string after next element. */
  357. *ptr = '\0';
  358. *start = strlen(start + 1); /* make it a pstring. */
  359. err = FSMakeFSSpec(spec->vRefNum, spec->parID,
  360. (const unsigned char *) start, spec);
  361. if (err != noErr) /* not an alias, really a bogus path. */
  362. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  363. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  364. if (err != noErr) /* not an alias, really a bogus path. */
  365. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  366. start = ptr; /* move to the next element. */
  367. if (ptr != NULL)
  368. ptr = strchr(start + 1, ':');
  369. } while (start != NULL);
  370. } /* if */
  371. else /* there's something there; make sure final file is not an alias. */
  372. {
  373. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  374. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  375. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  376. } /* else */
  377. return(noErr); /* w00t. */
  378. } /* fnameToFSSpec */
  379. int __PHYSFS_platformExists(const char *fname)
  380. {
  381. FSSpec spec;
  382. return(fnameToFSSpec(fname, &spec) == noErr);
  383. } /* __PHYSFS_platformExists */
  384. int __PHYSFS_platformIsSymLink(const char *fname)
  385. {
  386. OSErr err;
  387. FSSpec spec;
  388. Boolean a = 0;
  389. Boolean f = 0;
  390. CInfoPBRec infoPB;
  391. char *ptr;
  392. char *dir = alloca(strlen(fname) + 1);
  393. BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
  394. strcpy(dir, fname);
  395. ptr = strrchr(dir, ':');
  396. if (ptr == NULL) /* just a volume name? Can't be a symlink. */
  397. return(0);
  398. /* resolve aliases up to the actual file... */
  399. *ptr = '\0';
  400. BAIL_IF_MACRO(fnameToFSSpec(dir, &spec) != noErr, NULL, 0);
  401. *ptr = strlen(ptr + 1); /* ptr is now a pascal string. Yikes! */
  402. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  403. infoPB.dirInfo.ioNamePtr = spec.name;
  404. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  405. infoPB.dirInfo.ioDrDirID = spec.parID;
  406. infoPB.dirInfo.ioFDirIndex = 0;
  407. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, 0);
  408. err = FSMakeFSSpec(spec.vRefNum, infoPB.dirInfo.ioDrDirID,
  409. (const unsigned char *) ptr, &spec);
  410. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  411. BAIL_IF_MACRO(oserr(IsAliasFile(&spec, &a, &f)) != noErr, NULL, 0);
  412. return(a);
  413. } /* __PHYSFS_platformIsSymlink */
  414. int __PHYSFS_platformIsDirectory(const char *fname)
  415. {
  416. FSSpec spec;
  417. CInfoPBRec infoPB;
  418. OSErr err;
  419. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, NULL, 0);
  420. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  421. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  422. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  423. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  424. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  425. err = PBGetCatInfoSync(&infoPB);
  426. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  427. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  428. } /* __PHYSFS_platformIsDirectory */
  429. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  430. const char *dirName,
  431. const char *append)
  432. {
  433. int len = ((prepend) ? strlen(prepend) : 0) +
  434. ((append) ? strlen(append) : 0) +
  435. strlen(dirName) + 1;
  436. const char *src;
  437. char *dst;
  438. char *retval = (char *) allocator.Malloc(len);
  439. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  440. if (prepend != NULL)
  441. {
  442. strcpy(retval, prepend);
  443. dst = retval + strlen(retval);
  444. } /* if */
  445. else
  446. {
  447. *retval = '\0';
  448. dst = retval;
  449. } /* else */
  450. for (src = dirName; *src; src++, dst++)
  451. *dst = ((*src == '/') ? ':' : *src);
  452. *dst = '\0';
  453. return(retval);
  454. } /* __PHYSFS_platformCvtToDependent */
  455. void __PHYSFS_platformTimeslice(void)
  456. {
  457. SystemTask();
  458. } /* __PHYSFS_platformTimeslice */
  459. /* returns int so we can use BAIL*MACRO... */
  460. static int macClassicEnumerateFiles(const char *dirname,
  461. int omitSymLinks,
  462. PHYSFS_EnumFilesCallback callback,
  463. const char *origdir,
  464. void *callbackdata)
  465. {
  466. UInt16 i;
  467. UInt16 max;
  468. FSSpec spec;
  469. CInfoPBRec infoPB;
  470. Str255 str255;
  471. long dirID;
  472. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, NULL, 0);
  473. /* get the dir ID of what we want to enumerate... */
  474. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  475. infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
  476. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  477. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
  478. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  479. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, NULL);
  480. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
  481. BAIL_MACRO(ERR_NOT_A_DIR, NULL);
  482. dirID = infoPB.dirInfo.ioDrDirID;
  483. max = infoPB.dirInfo.ioDrNmFls;
  484. for (i = 1; i <= max; i++)
  485. {
  486. size_t size;
  487. FSSpec aliasspec;
  488. Boolean alias = 0;
  489. Boolean folder = 0;
  490. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  491. str255[0] = 0;
  492. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  493. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
  494. infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
  495. infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
  496. if (PBGetCatInfoSync(&infoPB) != noErr)
  497. continue; /* skip this file. Oh well. */
  498. if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
  499. continue; /* skip it. */
  500. if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
  501. continue; /* skip it. */
  502. if ((alias) && (omitSymLinks))
  503. continue;
  504. /* still here? Add it to the list. */
  505. size = (size_t) str255[0]; /* (convert to ASCIZ string...) */
  506. memmove(&str255[0], &str255[1], size);
  507. str255[size] = '\0';
  508. callback(callbackdata, origdir, (const char *) str255);
  509. } /* for */
  510. return(1);
  511. } /* macClassicEnumerateFiles */
  512. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  513. int omitSymLinks,
  514. PHYSFS_EnumFilesCallback callback,
  515. const char *origdir,
  516. void *callbackdata)
  517. {
  518. macClassicEnumerateFiles(dirname, omitSymLinks, callback,
  519. origdir, callbackdata);
  520. } /* __PHYSFS_platformEnumerateFiles */
  521. char *__PHYSFS_platformCurrentDir(void)
  522. {
  523. /*
  524. * I don't think Mac OS has a concept of "current directory", beyond
  525. * what is grafted on by a given standard C library implementation,
  526. * so just return the base dir.
  527. * We don't use this for anything crucial at the moment anyhow.
  528. */
  529. return(__PHYSFS_platformCalcBaseDir(NULL));
  530. } /* __PHYSFS_platformCurrentDir */
  531. char *__PHYSFS_platformRealPath(const char *path)
  532. {
  533. /*
  534. * fnameToFSSpec() will resolve any symlinks to get to the real
  535. * file's FSSpec, which, when converted, will contain the real
  536. * direct path to a given file. convFSSpecToPath() mallocs a
  537. * return value buffer.
  538. */
  539. FSSpec spec;
  540. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, NULL, NULL);
  541. return(convFSSpecToPath(&spec, 1));
  542. } /* __PHYSFS_platformRealPath */
  543. int __PHYSFS_platformMkDir(const char *path)
  544. {
  545. SInt32 val = 0;
  546. FSSpec spec;
  547. OSErr err = fnameToFSSpec(path, &spec);
  548. BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
  549. BAIL_IF_MACRO(err != fnfErr, NULL, 0);
  550. err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
  551. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  552. return(1);
  553. } /* __PHYSFS_platformMkDir */
  554. static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
  555. {
  556. int created = 0;
  557. SInt16 *retval = NULL;
  558. FSSpec spec;
  559. OSErr err = fnameToFSSpec(fname, &spec);
  560. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), NULL, NULL);
  561. if (err == fnfErr)
  562. {
  563. BAIL_IF_MACRO(!createIfMissing, ERR_NO_SUCH_FILE, NULL);
  564. err = HCreate(spec.vRefNum, spec.parID, spec.name,
  565. procInfo.processSignature, 'BINA');
  566. BAIL_IF_MACRO(oserr(err) != noErr, NULL, NULL);
  567. created = 1;
  568. } /* if */
  569. retval = (SInt16 *) allocator.Malloc(sizeof (SInt16));
  570. if (retval == NULL)
  571. {
  572. if (created)
  573. HDelete(spec.vRefNum, spec.parID, spec.name);
  574. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  575. } /* if */
  576. err = HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval);
  577. if (oserr(err) != noErr)
  578. {
  579. allocator.Free(retval);
  580. if (created)
  581. HDelete(spec.vRefNum, spec.parID, spec.name);
  582. return(NULL);
  583. } /* if */
  584. return(retval);
  585. } /* macDoOpen */
  586. void *__PHYSFS_platformOpenRead(const char *filename)
  587. {
  588. SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
  589. if (retval != NULL) /* got a file; seek to start. */
  590. {
  591. if (oserr(SetFPos(*retval, fsFromStart, 0)) != noErr)
  592. {
  593. FSClose(*retval);
  594. return(NULL);
  595. } /* if */
  596. } /* if */
  597. return((void *) retval);
  598. } /* __PHYSFS_platformOpenRead */
  599. void *__PHYSFS_platformOpenWrite(const char *filename)
  600. {
  601. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  602. if (retval != NULL) /* got a file; truncate it. */
  603. {
  604. if ((oserr(SetEOF(*retval, 0)) != noErr) ||
  605. (oserr(SetFPos(*retval, fsFromStart, 0)) != noErr))
  606. {
  607. FSClose(*retval);
  608. return(NULL);
  609. } /* if */
  610. } /* if */
  611. return((void *) retval);
  612. } /* __PHYSFS_platformOpenWrite */
  613. void *__PHYSFS_platformOpenAppend(const char *filename)
  614. {
  615. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  616. if (retval != NULL) /* got a file; seek to end. */
  617. {
  618. if (oserr(SetFPos(*retval, fsFromLEOF, 0)) != noErr)
  619. {
  620. FSClose(*retval);
  621. return(NULL);
  622. } /* if */
  623. } /* if */
  624. return(retval);
  625. } /* __PHYSFS_platformOpenAppend */
  626. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  627. PHYSFS_uint32 size, PHYSFS_uint32 count)
  628. {
  629. SInt16 ref = *((SInt16 *) opaque);
  630. SInt32 br = size*count;
  631. BAIL_IF_MACRO(oserr(FSRead(ref, &br, buffer)) != noErr, NULL, br/size);
  632. BAIL_IF_MACRO(br != size*count, NULL, br/size); /* !!! FIXME: seek back if only read part of an object! */
  633. return(count);
  634. } /* __PHYSFS_platformRead */
  635. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  636. PHYSFS_uint32 size, PHYSFS_uint32 count)
  637. {
  638. SInt16 ref = *((SInt16 *) opaque);
  639. SInt32 bw = size*count;
  640. BAIL_IF_MACRO(oserr(FSWrite(ref, &bw, buffer)) != noErr, NULL, bw/size);
  641. BAIL_IF_MACRO(bw != size*count, NULL, bw/size); /* !!! FIXME: seek back if only wrote part of an object! */
  642. return(count);
  643. } /* __PHYSFS_platformWrite */
  644. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  645. {
  646. SInt16 ref = *((SInt16 *) opaque);
  647. OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
  648. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  649. return(1);
  650. } /* __PHYSFS_platformSeek */
  651. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  652. {
  653. SInt16 ref = *((SInt16 *) opaque);
  654. SInt32 curPos;
  655. BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, -1);
  656. return((PHYSFS_sint64) curPos);
  657. } /* __PHYSFS_platformTell */
  658. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  659. {
  660. SInt16 ref = *((SInt16 *) opaque);
  661. SInt32 eofPos;
  662. BAIL_IF_MACRO(oserr(GetEOF(ref, &eofPos)) != noErr, NULL, -1);
  663. return((PHYSFS_sint64) eofPos);
  664. } /* __PHYSFS_platformFileLength */
  665. int __PHYSFS_platformEOF(void *opaque)
  666. {
  667. SInt16 ref = *((SInt16 *) opaque);
  668. SInt32 eofPos, curPos;
  669. BAIL_IF_MACRO(oserr(GetEOF(ref, &eofPos)) != noErr, NULL, 1);
  670. BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, 1);
  671. return(curPos >= eofPos);
  672. } /* __PHYSFS_platformEOF */
  673. int __PHYSFS_platformFlush(void *opaque)
  674. {
  675. SInt16 ref = *((SInt16 *) opaque);
  676. ParamBlockRec pb;
  677. memset(&pb, '\0', sizeof (ParamBlockRec));
  678. pb.ioParam.ioRefNum = ref;
  679. BAIL_IF_MACRO(oserr(PBFlushFileSync(&pb)) != noErr, NULL, 0);
  680. return(1);
  681. } /* __PHYSFS_platformFlush */
  682. int __PHYSFS_platformClose(void *opaque)
  683. {
  684. SInt16 ref = *((SInt16 *) opaque);
  685. SInt16 vRefNum;
  686. Str63 volName;
  687. int flushVol = 0;
  688. if (GetVRefNum(ref, &vRefNum) == noErr)
  689. {
  690. HParamBlockRec hpbr;
  691. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  692. hpbr.volumeParam.ioNamePtr = volName;
  693. hpbr.volumeParam.ioVRefNum = vRefNum;
  694. hpbr.volumeParam.ioVolIndex = 0;
  695. if (PBHGetVInfoSync(&hpbr) == noErr)
  696. flushVol = 1;
  697. } /* if */
  698. BAIL_IF_MACRO(oserr(FSClose(ref)) != noErr, NULL, 0);
  699. allocator.Free(opaque);
  700. if (flushVol)
  701. FlushVol(volName, vRefNum); /* update catalog info, etc. */
  702. return(1);
  703. } /* __PHYSFS_platformClose */
  704. int __PHYSFS_platformDelete(const char *path)
  705. {
  706. FSSpec spec;
  707. OSErr err;
  708. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, NULL, 0);
  709. err = HDelete(spec.vRefNum, spec.parID, spec.name);
  710. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  711. return(1);
  712. } /* __PHYSFS_platformDelete */
  713. void *__PHYSFS_platformCreateMutex(void)
  714. {
  715. return((void *) 0x0001); /* no mutexes on Mac OS classic. */
  716. } /* __PHYSFS_platformCreateMutex */
  717. void __PHYSFS_platformDestroyMutex(void *mutex)
  718. {
  719. /* no mutexes on Mac OS classic. */
  720. } /* __PHYSFS_platformDestroyMutex */
  721. int __PHYSFS_platformGrabMutex(void *mutex)
  722. {
  723. return(1); /* no mutexes on Mac OS classic. */
  724. } /* __PHYSFS_platformGrabMutex */
  725. void __PHYSFS_platformReleaseMutex(void *mutex)
  726. {
  727. /* no mutexes on Mac OS classic. */
  728. } /* __PHYSFS_platformReleaseMutex */
  729. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  730. {
  731. FSSpec spec;
  732. CInfoPBRec infoPB;
  733. UInt32 modDate;
  734. if (fnameToFSSpec(fname, &spec) != noErr)
  735. return(-1); /* fnameToFSSpec() sets physfs error message. */
  736. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  737. infoPB.dirInfo.ioNamePtr = spec.name;
  738. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  739. infoPB.dirInfo.ioDrDirID = spec.parID;
  740. infoPB.dirInfo.ioFDirIndex = 0;
  741. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, -1);
  742. modDate = ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0) ?
  743. infoPB.dirInfo.ioDrMdDat : infoPB.hFileInfo.ioFlMdDat;
  744. /* epoch is different on Mac OS. They use Jan 1, 1904, apparently. */
  745. /* subtract seconds between those epochs, counting leap years. */
  746. modDate -= 2082844800;
  747. return((PHYSFS_sint64) modDate);
  748. } /* __PHYSFS_platformGetLastModTime */
  749. /* !!! FIXME: Don't use C runtime for allocators? */
  750. int __PHYSFS_platformAllocatorInit(void)
  751. {
  752. return(1); /* always succeeds. */
  753. } /* __PHYSFS_platformAllocatorInit */
  754. void __PHYSFS_platformAllocatorDeinit(void)
  755. {
  756. /* no-op */
  757. } /* __PHYSFS_platformAllocatorInit */
  758. void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
  759. {
  760. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  761. #undef malloc
  762. return(malloc((size_t) s));
  763. } /* __PHYSFS_platformMalloc */
  764. void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  765. {
  766. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  767. #undef realloc
  768. return(realloc(ptr, (size_t) s));
  769. } /* __PHYSFS_platformRealloc */
  770. void __PHYSFS_platformAllocatorFree(void *ptr)
  771. {
  772. #undef free
  773. free(ptr);
  774. } /* __PHYSFS_platformAllocatorFree */
  775. #endif /* PHYSFS_PLATFORM_MACCLASSIC */
  776. /* end of macclassic.c ... */