macclassic.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. char **__PHYSFS_platformDetectAvailableCDs(void)
  156. {
  157. DriverGestaltParam pb;
  158. DrvQEl *dqp;
  159. OSErr status;
  160. char **retval = (char **) malloc(sizeof (char *));
  161. int cd_count = 1;
  162. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  163. *retval = NULL;
  164. pb.csCode = kDriverGestaltCode;
  165. pb.driverGestaltSelector = kdgDeviceType;
  166. dqp = (DrvQEl *) GetDrvQHdr()->qHead;
  167. while (dqp != NULL)
  168. {
  169. pb.ioCRefNum = dqp->dQRefNum;
  170. pb.ioVRefNum = dqp->dQDrive;
  171. status = PBStatusSync((ParmBlkPtr) &pb);
  172. if ((status == noErr) && (pb.driverGestaltResponse == kdgCDType))
  173. {
  174. Str63 volName;
  175. HParamBlockRec hpbr;
  176. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  177. hpbr.volumeParam.ioNamePtr = volName;
  178. hpbr.volumeParam.ioVRefNum = dqp->dQDrive;
  179. hpbr.volumeParam.ioVolIndex = 0;
  180. if (PBHGetVInfoSync(&hpbr) == noErr)
  181. {
  182. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  183. if (tmp)
  184. {
  185. char *str = (char *) malloc(volName[0] + 1);
  186. retval = tmp;
  187. if (str != NULL)
  188. {
  189. memcpy(str, &volName[1], volName[0]);
  190. str[volName[0]] = '\0';
  191. retval[cd_count-1] = str;
  192. cd_count++;
  193. } /* if */
  194. } /* if */
  195. } /* if */
  196. } /* if */
  197. dqp = (DrvQEl *) dqp->qLink;
  198. } /* while */
  199. retval[cd_count - 1] = NULL;
  200. return(retval);
  201. } /* __PHYSFS_platformDetectAvailableCDs */
  202. static char *convFSSpecToPath(FSSpec *spec, int includeFile)
  203. {
  204. char *ptr;
  205. char *retval = NULL;
  206. UInt32 retLength = 0;
  207. CInfoPBRec infoPB;
  208. Str255 str255;
  209. str255[0] = spec->name[0];
  210. memcpy(&str255[1], &spec->name[1], str255[0]);
  211. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  212. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  213. infoPB.dirInfo.ioVRefNum = spec->vRefNum; /* ID of bin's volume. */
  214. infoPB.dirInfo.ioDrParID = spec->parID; /* ID of bin's dir. */
  215. infoPB.dirInfo.ioFDirIndex = (includeFile) ? 0 : -1;
  216. /* walk the tree back to the root dir (volume), building path string... */
  217. do
  218. {
  219. /* check parent dir of what we last looked at... */
  220. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  221. if (oserr(PBGetCatInfoSync(&infoPB)) != noErr)
  222. {
  223. if (retval != NULL)
  224. free(retval);
  225. return(NULL);
  226. } /* if */
  227. infoPB.dirInfo.ioFDirIndex = -1; /* look at parent dir next time. */
  228. /* allocate more space for the retval... */
  229. retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
  230. ptr = (char *) malloc(retLength);
  231. if (ptr == NULL)
  232. {
  233. if (retval != NULL)
  234. free(retval);
  235. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  236. } /* if */
  237. /* prepend new dir to retval and cleanup... */
  238. memcpy(ptr, &str255[1], str255[0]);
  239. ptr[str255[0]] = '\0'; /* null terminate it. */
  240. if (retval != NULL)
  241. {
  242. strcat(ptr, ":");
  243. strcat(ptr, retval);
  244. free(retval);
  245. } /* if */
  246. retval = ptr;
  247. } while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
  248. return(retval);
  249. } /* convFSSpecToPath */
  250. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  251. {
  252. FSSpec spec;
  253. /* Get the name of the binary's parent directory. */
  254. FSMakeFSSpec(procfsspec.vRefNum, procfsspec.parID, procfsspec.name, &spec);
  255. return(convFSSpecToPath(&spec, 0));
  256. } /* __PHYSFS_platformCalcBaseDir */
  257. char *__PHYSFS_platformGetUserName(void)
  258. {
  259. char *retval = NULL;
  260. StringHandle strHandle;
  261. short origResourceFile = CurResFile();
  262. /* use the System resource file. */
  263. UseResFile(0);
  264. /* apparently, -16096 specifies the username. */
  265. strHandle = GetString(-16096);
  266. UseResFile(origResourceFile);
  267. BAIL_IF_MACRO(strHandle == NULL, NULL, NULL);
  268. HLock((Handle) strHandle);
  269. retval = (char *) malloc((*strHandle)[0] + 1);
  270. if (retval == NULL)
  271. {
  272. HUnlock((Handle) strHandle);
  273. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  274. } /* if */
  275. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  276. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  277. HUnlock((Handle) strHandle);
  278. return(retval);
  279. } /* __PHYSFS_platformGetUserName */
  280. char *__PHYSFS_platformGetUserDir(void)
  281. {
  282. #if 0
  283. return(NULL); /* bah...use default behaviour, I guess. */
  284. #else
  285. /* (Hmm. Default behaviour is broken in the base library. :) ) */
  286. return(__PHYSFS_platformCalcBaseDir(NULL));
  287. #endif
  288. } /* __PHYSFS_platformGetUserDir */
  289. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  290. {
  291. return(1); /* single threaded. */
  292. } /* __PHYSFS_platformGetThreadID */
  293. int __PHYSFS_platformStricmp(const char *x, const char *y)
  294. {
  295. int ux, uy;
  296. do
  297. {
  298. ux = toupper((int) *x);
  299. uy = toupper((int) *y);
  300. if (ux != uy)
  301. return((ux > uy) ? 1 : -1);
  302. x++;
  303. y++;
  304. } while ((ux) && (uy));
  305. return(0);
  306. } /* __PHYSFS_platformStricmp */
  307. int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
  308. {
  309. int ux, uy;
  310. if (!len)
  311. return(0);
  312. do
  313. {
  314. ux = toupper((int) *x);
  315. uy = toupper((int) *y);
  316. if (ux != uy)
  317. return((ux > uy) ? 1 : -1);
  318. x++;
  319. y++;
  320. len--;
  321. } while ((ux) && (uy) && (len));
  322. return(0);
  323. } /* __PHYSFS_platformStrnicmp */
  324. static OSErr fnameToFSSpecNoAlias(const char *fname, FSSpec *spec)
  325. {
  326. OSErr err;
  327. Str255 str255;
  328. int needColon = (strchr(fname, ':') == NULL);
  329. int len = strlen(fname) + ((needColon) ? 1 : 0);
  330. if (len > 255)
  331. return(bdNamErr);
  332. /* !!! FIXME: What happens with relative pathnames? */
  333. str255[0] = len;
  334. memcpy(&str255[1], fname, len);
  335. /* probably just a volume name, which seems to need a ':' at the end. */
  336. if (needColon)
  337. str255[len] = ':';
  338. err = oserr(FSMakeFSSpec(0, 0, str255, spec));
  339. return(err);
  340. } /* fnameToFSSpecNoAlias */
  341. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  342. {
  343. Boolean alias = 0;
  344. Boolean folder = 0;
  345. OSErr err = fnameToFSSpecNoAlias(fname, spec);
  346. if (err == dirNFErr) /* might be an alias in the middle of the path. */
  347. {
  348. /*
  349. * Has to be at least two ':' chars, or we wouldn't get a
  350. * dir-not-found condition. (no ':' means it was just a volume,
  351. * just one ':' means we would have gotten a fnfErr, if anything.
  352. */
  353. char *ptr;
  354. char *start;
  355. char *path = alloca(strlen(fname) + 1);
  356. strcpy(path, fname);
  357. ptr = strchr(path, ':');
  358. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  359. ptr = strchr(ptr + 1, ':');
  360. BAIL_IF_MACRO(!ptr, ERR_NO_SUCH_FILE, err); /* just in case */
  361. *ptr = '\0';
  362. err = fnameToFSSpecNoAlias(path, spec); /* get first dir. */
  363. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  364. start = ptr;
  365. ptr = strchr(start + 1, ':');
  366. /* Now check each element of the path for aliases... */
  367. do
  368. {
  369. CInfoPBRec infoPB;
  370. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  371. infoPB.dirInfo.ioNamePtr = spec->name;
  372. infoPB.dirInfo.ioVRefNum = spec->vRefNum;
  373. infoPB.dirInfo.ioDrDirID = spec->parID;
  374. infoPB.dirInfo.ioFDirIndex = 0;
  375. err = PBGetCatInfoSync(&infoPB);
  376. if (err != noErr) /* not an alias, really just a bogus path. */
  377. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  378. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0)
  379. spec->parID = infoPB.dirInfo.ioDrDirID;
  380. if (ptr != NULL) /* terminate string after next element. */
  381. *ptr = '\0';
  382. *start = strlen(start + 1); /* make it a pstring. */
  383. err = FSMakeFSSpec(spec->vRefNum, spec->parID,
  384. (const unsigned char *) start, spec);
  385. if (err != noErr) /* not an alias, really a bogus path. */
  386. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  387. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  388. if (err != noErr) /* not an alias, really a bogus path. */
  389. return(fnameToFSSpecNoAlias(fname, spec)); /* reset */
  390. start = ptr; /* move to the next element. */
  391. if (ptr != NULL)
  392. ptr = strchr(start + 1, ':');
  393. } while (start != NULL);
  394. } /* if */
  395. else /* there's something there; make sure final file is not an alias. */
  396. {
  397. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  398. err = ResolveAliasFileWithMountFlags(spec, 1, &folder, &alias, 0);
  399. BAIL_IF_MACRO(oserr(err) != noErr, NULL, err);
  400. } /* else */
  401. return(noErr); /* w00t. */
  402. } /* fnameToFSSpec */
  403. int __PHYSFS_platformExists(const char *fname)
  404. {
  405. FSSpec spec;
  406. return(fnameToFSSpec(fname, &spec) == noErr);
  407. } /* __PHYSFS_platformExists */
  408. int __PHYSFS_platformIsSymLink(const char *fname)
  409. {
  410. OSErr err;
  411. FSSpec spec;
  412. Boolean a = 0;
  413. Boolean f = 0;
  414. CInfoPBRec infoPB;
  415. char *ptr;
  416. char *dir = alloca(strlen(fname) + 1);
  417. BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
  418. strcpy(dir, fname);
  419. ptr = strrchr(dir, ':');
  420. if (ptr == NULL) /* just a volume name? Can't be a symlink. */
  421. return(0);
  422. /* resolve aliases up to the actual file... */
  423. *ptr = '\0';
  424. BAIL_IF_MACRO(fnameToFSSpec(dir, &spec) != noErr, NULL, 0);
  425. *ptr = strlen(ptr + 1); /* ptr is now a pascal string. Yikes! */
  426. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  427. infoPB.dirInfo.ioNamePtr = spec.name;
  428. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  429. infoPB.dirInfo.ioDrDirID = spec.parID;
  430. infoPB.dirInfo.ioFDirIndex = 0;
  431. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, 0);
  432. err = FSMakeFSSpec(spec.vRefNum, infoPB.dirInfo.ioDrDirID,
  433. (const unsigned char *) ptr, &spec);
  434. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  435. BAIL_IF_MACRO(oserr(IsAliasFile(&spec, &a, &f)) != noErr, NULL, 0);
  436. return(a);
  437. } /* __PHYSFS_platformIsSymlink */
  438. int __PHYSFS_platformIsDirectory(const char *fname)
  439. {
  440. FSSpec spec;
  441. CInfoPBRec infoPB;
  442. OSErr err;
  443. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, NULL, 0);
  444. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  445. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  446. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  447. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  448. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  449. err = PBGetCatInfoSync(&infoPB);
  450. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  451. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  452. } /* __PHYSFS_platformIsDirectory */
  453. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  454. const char *dirName,
  455. const char *append)
  456. {
  457. int len = ((prepend) ? strlen(prepend) : 0) +
  458. ((append) ? strlen(append) : 0) +
  459. strlen(dirName) + 1;
  460. const char *src;
  461. char *dst;
  462. char *retval = malloc(len);
  463. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  464. if (prepend != NULL)
  465. {
  466. strcpy(retval, prepend);
  467. dst = retval + strlen(retval);
  468. } /* if */
  469. else
  470. {
  471. *retval = '\0';
  472. dst = retval;
  473. } /* else */
  474. for (src = dirName; *src; src++, dst++)
  475. *dst = ((*src == '/') ? ':' : *src);
  476. *dst = '\0';
  477. return(retval);
  478. } /* __PHYSFS_platformCvtToDependent */
  479. void __PHYSFS_platformTimeslice(void)
  480. {
  481. SystemTask();
  482. } /* __PHYSFS_platformTimeslice */
  483. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  484. int omitSymLinks)
  485. {
  486. LinkedStringList *ret = NULL, *p = NULL;
  487. UInt16 i;
  488. UInt16 max;
  489. FSSpec spec;
  490. CInfoPBRec infoPB;
  491. Str255 str255;
  492. long dirID;
  493. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, NULL, 0);
  494. /* get the dir ID of what we want to enumerate... */
  495. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  496. infoPB.dirInfo.ioNamePtr = spec.name; /* name of dir to enum. */
  497. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  498. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of dir. */
  499. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  500. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, NULL);
  501. if ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) == 0)
  502. BAIL_MACRO(ERR_NOT_A_DIR, NULL);
  503. dirID = infoPB.dirInfo.ioDrDirID;
  504. max = infoPB.dirInfo.ioDrNmFls;
  505. for (i = 1; i <= max; i++)
  506. {
  507. FSSpec aliasspec;
  508. Boolean alias = 0;
  509. Boolean folder = 0;
  510. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  511. str255[0] = 0;
  512. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  513. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of dir's volume. */
  514. infoPB.dirInfo.ioDrDirID = dirID; /* ID of dir. */
  515. infoPB.dirInfo.ioFDirIndex = i; /* next file's info. */
  516. if (PBGetCatInfoSync(&infoPB) != noErr)
  517. continue; /* skip this file. Oh well. */
  518. if (FSMakeFSSpec(spec.vRefNum, dirID, str255, &aliasspec) != noErr)
  519. continue; /* skip it. */
  520. if (IsAliasFile(&aliasspec, &alias, &folder) != noErr)
  521. continue; /* skip it. */
  522. if ((alias) && (omitSymLinks))
  523. continue;
  524. /* still here? Add it to the list. */
  525. ret = __PHYSFS_addToLinkedStringList(ret, &p, (const char *) &str255[1], str255[0]);
  526. } /* for */
  527. return(ret);
  528. } /* __PHYSFS_platformEnumerateFiles */
  529. char *__PHYSFS_platformCurrentDir(void)
  530. {
  531. /*
  532. * I don't think MacOS has a concept of "current directory", beyond
  533. * what is grafted on by a given standard C library implementation,
  534. * so just return the base dir.
  535. * We don't use this for anything crucial at the moment anyhow.
  536. */
  537. return(__PHYSFS_platformCalcBaseDir(NULL));
  538. } /* __PHYSFS_platformCurrentDir */
  539. char *__PHYSFS_platformRealPath(const char *path)
  540. {
  541. /*
  542. * fnameToFSSpec() will resolve any symlinks to get to the real
  543. * file's FSSpec, which, when converted, will contain the real
  544. * direct path to a given file. convFSSpecToPath() mallocs a
  545. * return value buffer.
  546. */
  547. FSSpec spec;
  548. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, NULL, NULL);
  549. return(convFSSpecToPath(&spec, 1));
  550. } /* __PHYSFS_platformRealPath */
  551. int __PHYSFS_platformMkDir(const char *path)
  552. {
  553. SInt32 val = 0;
  554. FSSpec spec;
  555. OSErr err = fnameToFSSpec(path, &spec);
  556. BAIL_IF_MACRO(err == noErr, ERR_FILE_EXISTS, 0);
  557. BAIL_IF_MACRO(err != fnfErr, NULL, 0);
  558. err = DirCreate(spec.vRefNum, spec.parID, spec.name, &val);
  559. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  560. return(1);
  561. } /* __PHYSFS_platformMkDir */
  562. static SInt16 *macDoOpen(const char *fname, SInt8 perm, int createIfMissing)
  563. {
  564. int created = 0;
  565. SInt16 *retval = NULL;
  566. FSSpec spec;
  567. OSErr err = fnameToFSSpec(fname, &spec);
  568. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), NULL, NULL);
  569. if (err == fnfErr)
  570. {
  571. BAIL_IF_MACRO(!createIfMissing, ERR_NO_SUCH_FILE, NULL);
  572. err = HCreate(spec.vRefNum, spec.parID, spec.name,
  573. procInfo.processSignature, 'BINA');
  574. BAIL_IF_MACRO(oserr(err) != noErr, NULL, NULL);
  575. created = 1;
  576. } /* if */
  577. retval = (SInt16 *) malloc(sizeof (SInt16));
  578. if (retval == NULL)
  579. {
  580. if (created)
  581. HDelete(spec.vRefNum, spec.parID, spec.name);
  582. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  583. } /* if */
  584. err = HOpenDF(spec.vRefNum, spec.parID, spec.name, perm, retval);
  585. if (oserr(err) != noErr)
  586. {
  587. free(retval);
  588. if (created)
  589. HDelete(spec.vRefNum, spec.parID, spec.name);
  590. return(NULL);
  591. } /* if */
  592. return(retval);
  593. } /* macDoOpen */
  594. void *__PHYSFS_platformOpenRead(const char *filename)
  595. {
  596. SInt16 *retval = macDoOpen(filename, fsRdPerm, 0);
  597. if (retval != NULL) /* got a file; seek to start. */
  598. {
  599. if (oserr(SetFPos(*retval, fsFromStart, 0)) != noErr)
  600. {
  601. FSClose(*retval);
  602. return(NULL);
  603. } /* if */
  604. } /* if */
  605. return((void *) retval);
  606. } /* __PHYSFS_platformOpenRead */
  607. void *__PHYSFS_platformOpenWrite(const char *filename)
  608. {
  609. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  610. if (retval != NULL) /* got a file; truncate it. */
  611. {
  612. if ((oserr(SetEOF(*retval, 0)) != noErr) ||
  613. (oserr(SetFPos(*retval, fsFromStart, 0)) != noErr))
  614. {
  615. FSClose(*retval);
  616. return(NULL);
  617. } /* if */
  618. } /* if */
  619. return((void *) retval);
  620. } /* __PHYSFS_platformOpenWrite */
  621. void *__PHYSFS_platformOpenAppend(const char *filename)
  622. {
  623. SInt16 *retval = macDoOpen(filename, fsRdWrPerm, 1);
  624. if (retval != NULL) /* got a file; seek to end. */
  625. {
  626. if (oserr(SetFPos(*retval, fsFromLEOF, 0)) != noErr)
  627. {
  628. FSClose(*retval);
  629. return(NULL);
  630. } /* if */
  631. } /* if */
  632. return(retval);
  633. } /* __PHYSFS_platformOpenAppend */
  634. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  635. PHYSFS_uint32 size, PHYSFS_uint32 count)
  636. {
  637. SInt16 ref = *((SInt16 *) opaque);
  638. SInt32 br;
  639. PHYSFS_uint32 i;
  640. for (i = 0; i < count; i++)
  641. {
  642. br = size;
  643. BAIL_IF_MACRO(oserr(FSRead(ref, &br, buffer)) != noErr, NULL, i);
  644. BAIL_IF_MACRO(br != size, NULL, i); /* !!! FIXME: seek back if only read part of an object! */
  645. buffer = ((PHYSFS_uint8 *) buffer) + size;
  646. } /* for */
  647. return(count);
  648. } /* __PHYSFS_platformRead */
  649. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  650. PHYSFS_uint32 size, PHYSFS_uint32 count)
  651. {
  652. SInt16 ref = *((SInt16 *) opaque);
  653. SInt32 bw;
  654. PHYSFS_uint32 i;
  655. for (i = 0; i < count; i++)
  656. {
  657. bw = size;
  658. BAIL_IF_MACRO(oserr(FSWrite(ref, &bw, buffer)) != noErr, NULL, i);
  659. BAIL_IF_MACRO(bw != size, NULL, i); /* !!! FIXME: seek back if only wrote part of an object! */
  660. buffer = ((PHYSFS_uint8 *) buffer) + size;
  661. } /* for */
  662. return(count);
  663. } /* __PHYSFS_platformWrite */
  664. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  665. {
  666. SInt16 ref = *((SInt16 *) opaque);
  667. OSErr err = SetFPos(ref, fsFromStart, (SInt32) pos);
  668. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  669. return(1);
  670. } /* __PHYSFS_platformSeek */
  671. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  672. {
  673. SInt16 ref = *((SInt16 *) opaque);
  674. SInt32 curPos;
  675. BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, -1);
  676. return((PHYSFS_sint64) curPos);
  677. } /* __PHYSFS_platformTell */
  678. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  679. {
  680. SInt16 ref = *((SInt16 *) opaque);
  681. SInt32 eofPos;
  682. BAIL_IF_MACRO(oserr(GetEOF(ref, &eofPos)) != noErr, NULL, -1);
  683. return((PHYSFS_sint64) eofPos);
  684. } /* __PHYSFS_platformFileLength */
  685. int __PHYSFS_platformEOF(void *opaque)
  686. {
  687. SInt16 ref = *((SInt16 *) opaque);
  688. SInt32 eofPos, curPos;
  689. BAIL_IF_MACRO(oserr(GetEOF(ref, &eofPos)) != noErr, NULL, 1);
  690. BAIL_IF_MACRO(oserr(GetFPos(ref, &curPos)) != noErr, NULL, 1);
  691. return(curPos >= eofPos);
  692. } /* __PHYSFS_platformEOF */
  693. int __PHYSFS_platformFlush(void *opaque)
  694. {
  695. SInt16 ref = *((SInt16 *) opaque);
  696. ParamBlockRec pb;
  697. memset(&pb, '\0', sizeof (ParamBlockRec));
  698. pb.ioParam.ioRefNum = ref;
  699. BAIL_IF_MACRO(oserr(PBFlushFileSync(&pb)) != noErr, NULL, 0);
  700. return(1);
  701. } /* __PHYSFS_platformFlush */
  702. int __PHYSFS_platformClose(void *opaque)
  703. {
  704. SInt16 ref = *((SInt16 *) opaque);
  705. SInt16 vRefNum;
  706. Str63 volName;
  707. int flushVol = 0;
  708. if (GetVRefNum(ref, &vRefNum) == noErr)
  709. {
  710. HParamBlockRec hpbr;
  711. memset(&hpbr, '\0', sizeof (HParamBlockRec));
  712. hpbr.volumeParam.ioNamePtr = volName;
  713. hpbr.volumeParam.ioVRefNum = vRefNum;
  714. hpbr.volumeParam.ioVolIndex = 0;
  715. if (PBHGetVInfoSync(&hpbr) == noErr)
  716. flushVol = 1;
  717. } /* if */
  718. BAIL_IF_MACRO(oserr(FSClose(ref)) != noErr, NULL, 0);
  719. free(opaque);
  720. if (flushVol)
  721. FlushVol(volName, vRefNum); /* update catalog info, etc. */
  722. return(1);
  723. } /* __PHYSFS_platformClose */
  724. int __PHYSFS_platformDelete(const char *path)
  725. {
  726. FSSpec spec;
  727. OSErr err;
  728. BAIL_IF_MACRO(fnameToFSSpec(path, &spec) != noErr, NULL, 0);
  729. err = HDelete(spec.vRefNum, spec.parID, spec.name);
  730. BAIL_IF_MACRO(oserr(err) != noErr, NULL, 0);
  731. return(1);
  732. } /* __PHYSFS_platformDelete */
  733. void *__PHYSFS_platformCreateMutex(void)
  734. {
  735. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  736. } /* __PHYSFS_platformCreateMutex */
  737. void __PHYSFS_platformDestroyMutex(void *mutex)
  738. {
  739. /* no mutexes on MacOS Classic. */
  740. } /* __PHYSFS_platformDestroyMutex */
  741. int __PHYSFS_platformGrabMutex(void *mutex)
  742. {
  743. return(1); /* no mutexes on MacOS Classic. */
  744. } /* __PHYSFS_platformGrabMutex */
  745. void __PHYSFS_platformReleaseMutex(void *mutex)
  746. {
  747. /* no mutexes on MacOS Classic. */
  748. } /* __PHYSFS_platformReleaseMutex */
  749. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  750. {
  751. FSSpec spec;
  752. CInfoPBRec infoPB;
  753. UInt32 modDate;
  754. if (fnameToFSSpec(fname, &spec) != noErr)
  755. return(-1); /* fnameToFSSpec() sets physfs error message. */
  756. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  757. infoPB.dirInfo.ioNamePtr = spec.name;
  758. infoPB.dirInfo.ioVRefNum = spec.vRefNum;
  759. infoPB.dirInfo.ioDrDirID = spec.parID;
  760. infoPB.dirInfo.ioFDirIndex = 0;
  761. BAIL_IF_MACRO(oserr(PBGetCatInfoSync(&infoPB)) != noErr, NULL, -1);
  762. modDate = ((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0) ?
  763. infoPB.dirInfo.ioDrMdDat : infoPB.hFileInfo.ioFlMdDat;
  764. /* epoch is different on MacOS. They use Jan 1, 1904, apparently. */
  765. /* subtract seconds between those epochs, counting leap years. */
  766. modDate -= 2082844800;
  767. return((PHYSFS_sint64) modDate);
  768. } /* __PHYSFS_platformGetLastModTime */
  769. /* end of macclassic.c ... */