macclassic.c 29 KB

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