macclassic.c 28 KB

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