1
0

macclassic.c 28 KB

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