macclassic.c 28 KB

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