macclassic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #include <stdlib.h>
  9. #include <string.h>
  10. /*
  11. * Please note that I haven't tried this code with CarbonLib or under
  12. * MacOS X at all. The code in unix.c is known to work with Darwin,
  13. * and you may or may not be better off using that.
  14. */
  15. #ifdef __PHYSFS_CARBONIZED__
  16. #include <Carbon.h>
  17. #else
  18. #include <OSUtils.h>
  19. #include <Processes.h>
  20. #include <Files.h>
  21. #include <TextUtils.h>
  22. #include <Resources.h>
  23. #include <MacMemory.h>
  24. #include <Events.h>
  25. #endif
  26. #define __PHYSICSFS_INTERNAL__
  27. #include "physfs_internal.h"
  28. const char *__PHYSFS_platformDirSeparator = ":";
  29. int __PHYSFS_platformInit(void)
  30. {
  31. return(1); /* always succeeds. */
  32. } /* __PHYSFS_platformInit */
  33. int __PHYSFS_platformDeinit(void)
  34. {
  35. return(1); /* always succeed. */
  36. } /* __PHYSFS_platformDeinit */
  37. char **__PHYSFS_platformDetectAvailableCDs(void)
  38. {
  39. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  40. } /* __PHYSFS_platformDetectAvailableCDs */
  41. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  42. {
  43. char *ptr;
  44. char *retval = NULL;
  45. UInt32 retLength = 0;
  46. ProcessSerialNumber psn;
  47. struct ProcessInfoRec procInfo;
  48. FSSpec spec;
  49. CInfoPBRec infoPB;
  50. OSErr err;
  51. Str255 str255;
  52. /* Get the FSSpecPtr of the current process's binary... */
  53. BAIL_IF_MACRO(GetCurrentProcess(&psn) != noErr, ERR_OS_ERROR, NULL);
  54. memset(&procInfo, '\0', sizeof (procInfo));
  55. procInfo.processInfoLength = sizeof (procInfo);
  56. procInfo.processAppSpec = &spec;
  57. err = GetProcessInformation(&psn, &procInfo);
  58. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
  59. /* Get the name of the binary's parent directory. */
  60. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  61. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  62. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of bin's volume. */
  63. infoPB.dirInfo.ioDrParID = spec.parID; /* ID of bin's dir. */
  64. infoPB.dirInfo.ioFDirIndex = -1; /* get dir (not file) info. */
  65. /* walk the tree back to the root dir (volume), building path string... */
  66. do
  67. {
  68. /* check parent dir of what we last looked at... */
  69. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  70. if (PBGetCatInfoSync(&infoPB) != noErr)
  71. {
  72. if (retval != NULL)
  73. free(retval);
  74. BAIL_MACRO(ERR_OS_ERROR, NULL);
  75. } /* if */
  76. /* allocate more space for the retval... */
  77. retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
  78. ptr = (char *) malloc(retLength);
  79. if (ptr == NULL)
  80. {
  81. if (retval != NULL)
  82. free(retval);
  83. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  84. } /* if */
  85. /* prepend new dir to retval and cleanup... */
  86. memcpy(ptr, &str255[1], str255[0]);
  87. ptr[str255[0]] = '\0'; /* null terminate it. */
  88. if (retval != NULL)
  89. {
  90. strcat(ptr, ":");
  91. strcat(ptr, retval);
  92. free(retval);
  93. } /* if */
  94. retval = ptr;
  95. } while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
  96. return(retval);
  97. } /* __PHYSFS_platformCalcBaseDir */
  98. char *__PHYSFS_platformGetUserName(void)
  99. {
  100. char *retval = NULL;
  101. StringHandle strHandle;
  102. short origResourceFile = CurResFile();
  103. /* use the System resource file. */
  104. UseResFile(0);
  105. /* apparently, -16096 specifies the username. */
  106. strHandle = GetString(-16096);
  107. UseResFile(origResourceFile);
  108. BAIL_IF_MACRO(strHandle == NULL, ERR_OS_ERROR, NULL);
  109. HLock((Handle) strHandle);
  110. retval = (char *) malloc((*strHandle)[0] + 1);
  111. if (retval == NULL)
  112. {
  113. HUnlock((Handle) strHandle);
  114. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  115. } /* if */
  116. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  117. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  118. HUnlock((Handle) strHandle);
  119. return(retval);
  120. } /* __PHYSFS_platformGetUserName */
  121. char *__PHYSFS_platformGetUserDir(void)
  122. {
  123. return(NULL); /* bah...use default behaviour, I guess. */
  124. } /* __PHYSFS_platformGetUserDir */
  125. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  126. {
  127. return(1); /* single threaded. */
  128. } /* __PHYSFS_platformGetThreadID */
  129. int __PHYSFS_platformStricmp(const char *x, const char *y)
  130. {
  131. extern int _stricmp(const char *, const char *);
  132. return(_stricmp(x, y)); /* (*shrug*) */
  133. } /* __PHYSFS_platformStricmp */
  134. static OSErr fnameToFSSpec(const char *fname, FSSpec *spec)
  135. {
  136. OSErr err;
  137. Str255 str255;
  138. int len = strlen(fname);
  139. if (len > 255)
  140. return(bdNamErr);
  141. /* !!! FIXME: What happens with relative pathnames? */
  142. str255[0] = strlen(fname);
  143. memcpy(&str255[1], fname, str255[0]);
  144. err = FSMakeFSSpec(0, 0, str255, spec);
  145. return(err);
  146. } /* fnameToFSSpec */
  147. int __PHYSFS_platformExists(const char *fname)
  148. {
  149. FSSpec spec;
  150. return(fnameToFSSpec(fname, &spec) == noErr);
  151. } /* __PHYSFS_platformExists */
  152. int __PHYSFS_platformIsSymLink(const char *fname)
  153. {
  154. return(0); /* !!! FIXME: What happens if (fname) is an alias? */
  155. } /* __PHYSFS_platformIsSymlink */
  156. int __PHYSFS_platformIsDirectory(const char *fname)
  157. {
  158. FSSpec spec;
  159. CInfoPBRec infoPB;
  160. OSErr err;
  161. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, 0);
  162. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  163. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  164. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  165. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  166. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  167. err = PBGetCatInfoSync(&infoPB);
  168. BAIL_IF_MACRO((err != noErr) && (err != fnfErr), ERR_OS_ERROR, 0);
  169. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  170. } /* __PHYSFS_platformIsDirectory */
  171. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  172. const char *dirName,
  173. const char *append)
  174. {
  175. int len = ((prepend) ? strlen(prepend) : 0) +
  176. ((append) ? strlen(append) : 0) +
  177. strlen(dirName) + 1;
  178. const char *src;
  179. char *dst;
  180. char *retval = malloc(len);
  181. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  182. if (prepend != NULL)
  183. {
  184. strcpy(retval, prepend);
  185. dst = retval + strlen(retval);
  186. } /* if */
  187. else
  188. {
  189. *retval = '\0';
  190. dst = retval;
  191. } /* else */
  192. for (src = dirName; *src; src++, dst++)
  193. *dst = ((*src == '/') ? ':' : *src);
  194. *dst = '\0';
  195. return(retval);
  196. } /* __PHYSFS_platformCvtToDependent */
  197. void __PHYSFS_platformTimeslice(void)
  198. {
  199. SystemTask();
  200. } /* __PHYSFS_platformTimeslice */
  201. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  202. int omitSymLinks)
  203. {
  204. LinkedStringList *retval = NULL;
  205. LinkedStringList *l = NULL;
  206. LinkedStringList *prev = NULL;
  207. UInt16 i = 0;
  208. FSSpec spec;
  209. CInfoPBRec infoPB;
  210. Str255 str255;
  211. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
  212. while (1)
  213. {
  214. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  215. str255[0] = 0;
  216. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  217. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  218. infoPB.dirInfo.ioDrDirID = spec.parID; /* ID of bin's dir. */
  219. infoPB.dirInfo.ioFDirIndex = ++i; /* file (not parent) info. */
  220. if (PBGetCatInfoSync(&infoPB) != noErr)
  221. break;
  222. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  223. if (l == NULL)
  224. break;
  225. l->str = (char *) malloc(str255[0] + 1);
  226. if (l->str == NULL)
  227. {
  228. free(l);
  229. break;
  230. } /* if */
  231. memcpy(l->str, &str255[1], str255[0]);
  232. l->str[str255[0]] = '\0';
  233. if (retval == NULL)
  234. retval = l;
  235. else
  236. prev->next = l;
  237. prev = l;
  238. l->next = NULL;
  239. } /* while */
  240. return(retval);
  241. } /* __PHYSFS_platformEnumerateFiles */
  242. char *__PHYSFS_platformCurrentDir(void)
  243. {
  244. /*
  245. * I don't think MacOS has a concept of "current directory", beyond
  246. * what is grafted on by a given standard C library implementation,
  247. * so just return the base dir.
  248. * We don't use this for anything crucial at the moment anyhow.
  249. */
  250. return(__PHYSFS_platformCalcBaseDir(NULL));
  251. } /* __PHYSFS_platformCurrentDir */
  252. char *__PHYSFS_platformRealPath(const char *path)
  253. {
  254. /* !!! FIXME: This isn't nearly right. */
  255. char *retval = (char *) malloc(strlen(path) + 1);
  256. strcpy(retval, path);
  257. return(retval);
  258. } /* __PHYSFS_platformRealPath */
  259. int __PHYSFS_platformMkDir(const char *path)
  260. {
  261. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  262. } /* __PHYSFS_platformMkDir */
  263. void *__PHYSFS_platformOpenRead(const char *filename)
  264. {
  265. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  266. } /* __PHYSFS_platformOpenRead */
  267. void *__PHYSFS_platformOpenWrite(const char *filename)
  268. {
  269. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  270. } /* __PHYSFS_platformOpenWrite */
  271. void *__PHYSFS_platformOpenAppend(const char *filename)
  272. {
  273. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  274. } /* __PHYSFS_platformOpenAppend */
  275. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  276. PHYSFS_uint32 size, PHYSFS_uint32 count)
  277. {
  278. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  279. } /* __PHYSFS_platformRead */
  280. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  281. PHYSFS_uint32 size, PHYSFS_uint32 count)
  282. {
  283. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  284. } /* __PHYSFS_platformWrite */
  285. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  286. {
  287. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  288. } /* __PHYSFS_platformSeek */
  289. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  290. {
  291. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  292. } /* __PHYSFS_platformTell */
  293. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  294. {
  295. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  296. } /* __PHYSFS_platformFileLength */
  297. int __PHYSFS_platformEOF(void *opaque)
  298. {
  299. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  300. } /* __PHYSFS_platformEOF */
  301. int __PHYSFS_platformFlush(void *opaque)
  302. {
  303. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  304. } /* __PHYSFS_platformFlush */
  305. int __PHYSFS_platformClose(void *opaque)
  306. {
  307. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  308. } /* __PHYSFS_platformClose */
  309. int __PHYSFS_platformDelete(const char *path)
  310. {
  311. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  312. } /* __PHYSFS_platformDelete */
  313. void *__PHYSFS_platformCreateMutex(void)
  314. {
  315. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  316. } /* __PHYSFS_platformCreateMutex */
  317. void __PHYSFS_platformDestroyMutex(void *mutex)
  318. {
  319. /* no mutexes on MacOS Classic. */
  320. } /* __PHYSFS_platformDestroyMutex */
  321. int __PHYSFS_platformGrabMutex(void *mutex)
  322. {
  323. return(1); /* no mutexes on MacOS Classic. */
  324. } /* __PHYSFS_platformGrabMutex */
  325. void __PHYSFS_platformReleaseMutex(void *mutex)
  326. {
  327. /* no mutexes on MacOS Classic. */
  328. } /* __PHYSFS_platformReleaseMutex */
  329. /* end of unix.c ... */