macclassic.c 29 KB

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