1
0

macclassic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. Str255 str255;
  137. int len = strlen(fname);
  138. if (len > 255)
  139. return(bdNamErr);
  140. /* !!! FIXME: What happens with relative pathnames? */
  141. str255[0] = strlen(fname);
  142. memcpy(&str255[1], fname, str255[0]);
  143. return(FSMakeFSSpec(0, 0, str255, spec));
  144. } /* fnameToFSSpec */
  145. int __PHYSFS_platformExists(const char *fname)
  146. {
  147. FSSpec spec;
  148. return(fnameToFSSpec(fname, &spec) == noErr);
  149. } /* __PHYSFS_platformExists */
  150. int __PHYSFS_platformIsSymLink(const char *fname)
  151. {
  152. return(0); /* !!! FIXME: What happens if (fname) is an alias? */
  153. } /* __PHYSFS_platformIsSymlink */
  154. int __PHYSFS_platformIsDirectory(const char *fname)
  155. {
  156. FSSpec spec;
  157. CInfoPBRec infoPB;
  158. BAIL_IF_MACRO(fnameToFSSpec(fname, &spec) != noErr, ERR_OS_ERROR, 0);
  159. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  160. infoPB.dirInfo.ioNamePtr = spec.name; /* put name in here. */
  161. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  162. infoPB.dirInfo.ioDrParID = spec.parID; /* ID of bin's dir. */
  163. infoPB.dirInfo.ioFDirIndex = 0; /* file (not parent) info. */
  164. BAIL_IF_MACRO(PBGetCatInfoSync(&infoPB) != noErr, ERR_OS_ERROR, 0);
  165. return((infoPB.dirInfo.ioFlAttrib & kioFlAttribDirMask) != 0);
  166. } /* __PHYSFS_platformIsDirectory */
  167. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  168. const char *dirName,
  169. const char *append)
  170. {
  171. int len = ((prepend) ? strlen(prepend) : 0) +
  172. ((append) ? strlen(append) : 0) +
  173. strlen(dirName) + 1;
  174. const char *src;
  175. char *dst;
  176. char *retval = malloc(len);
  177. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  178. if (prepend != NULL)
  179. {
  180. strcpy(retval, prepend);
  181. dst = retval + strlen(retval);
  182. } /* if */
  183. else
  184. {
  185. *retval = '\0';
  186. dst = retval;
  187. } /* else */
  188. for (src = dirName; *src; src++, dst++)
  189. *dst = ((*src == '/') ? ':' : *src);
  190. *dst = '\0';
  191. return(retval);
  192. } /* __PHYSFS_platformCvtToDependent */
  193. void __PHYSFS_platformTimeslice(void)
  194. {
  195. SystemTask();
  196. } /* __PHYSFS_platformTimeslice */
  197. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  198. int omitSymLinks)
  199. {
  200. LinkedStringList *retval = NULL;
  201. LinkedStringList *l = NULL;
  202. LinkedStringList *prev = NULL;
  203. UInt16 i = 0;
  204. FSSpec spec;
  205. CInfoPBRec infoPB;
  206. Str255 str255;
  207. BAIL_IF_MACRO(fnameToFSSpec(dirname, &spec) != noErr, ERR_OS_ERROR, 0);
  208. while (1)
  209. {
  210. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  211. str255[0] = 0;
  212. infoPB.dirInfo.ioNamePtr = str255; /* store name in here. */
  213. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of file's volume. */
  214. infoPB.dirInfo.ioDrParID = spec.parID; /* ID of bin's dir. */
  215. infoPB.dirInfo.ioFDirIndex = ++i; /* file (not parent) info. */
  216. if (PBGetCatInfoSync(&infoPB) != noErr)
  217. break;
  218. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  219. if (l == NULL)
  220. break;
  221. l->str = (char *) malloc(str255[0] + 1);
  222. if (l->str == NULL)
  223. {
  224. free(l);
  225. break;
  226. } /* if */
  227. memcpy(l->str, &str255[1], str255[0]);
  228. l->str[str255[0]] = '\0';
  229. if (retval == NULL)
  230. retval = l;
  231. else
  232. prev->next = l;
  233. prev = l;
  234. l->next = NULL;
  235. } /* while */
  236. return(retval);
  237. } /* __PHYSFS_platformEnumerateFiles */
  238. char *__PHYSFS_platformCurrentDir(void)
  239. {
  240. /*
  241. * I don't think MacOS has a concept of "current directory", beyond
  242. * what is grafted on by a given standard C library implementation,
  243. * so just return the base dir.
  244. * We don't use this for anything crucial at the moment anyhow.
  245. */
  246. return(__PHYSFS_platformCalcBaseDir(NULL));
  247. } /* __PHYSFS_platformCurrentDir */
  248. char *__PHYSFS_platformRealPath(const char *path)
  249. {
  250. /* !!! FIXME: This isn't nearly right. */
  251. char *retval = (char *) malloc(strlen(path) + 1);
  252. strcpy(retval, path);
  253. return(retval);
  254. } /* __PHYSFS_platformRealPath */
  255. int __PHYSFS_platformMkDir(const char *path)
  256. {
  257. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  258. } /* __PHYSFS_platformMkDir */
  259. void *__PHYSFS_platformOpenRead(const char *filename)
  260. {
  261. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  262. } /* __PHYSFS_platformOpenRead */
  263. void *__PHYSFS_platformOpenWrite(const char *filename)
  264. {
  265. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  266. } /* __PHYSFS_platformOpenWrite */
  267. void *__PHYSFS_platformOpenAppend(const char *filename)
  268. {
  269. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  270. } /* __PHYSFS_platformOpenAppend */
  271. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  272. PHYSFS_uint32 size, PHYSFS_uint32 count)
  273. {
  274. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  275. } /* __PHYSFS_platformRead */
  276. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  277. PHYSFS_uint32 size, PHYSFS_uint32 count)
  278. {
  279. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  280. } /* __PHYSFS_platformWrite */
  281. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  282. {
  283. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  284. } /* __PHYSFS_platformSeek */
  285. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  286. {
  287. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  288. } /* __PHYSFS_platformTell */
  289. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  290. {
  291. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  292. } /* __PHYSFS_platformFileLength */
  293. int __PHYSFS_platformEOF(void *opaque)
  294. {
  295. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  296. } /* __PHYSFS_platformEOF */
  297. int __PHYSFS_platformFlush(void *opaque)
  298. {
  299. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  300. } /* __PHYSFS_platformFlush */
  301. int __PHYSFS_platformClose(void *opaque)
  302. {
  303. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  304. } /* __PHYSFS_platformClose */
  305. int __PHYSFS_platformDelete(const char *path)
  306. {
  307. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  308. } /* __PHYSFS_platformDelete */
  309. void *__PHYSFS_platformCreateMutex(void)
  310. {
  311. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  312. } /* __PHYSFS_platformCreateMutex */
  313. void __PHYSFS_platformDestroyMutex(void *mutex)
  314. {
  315. /* no mutexes on MacOS Classic. */
  316. } /* __PHYSFS_platformDestroyMutex */
  317. int __PHYSFS_platformGrabMutex(void *mutex)
  318. {
  319. return(1); /* no mutexes on MacOS Classic. */
  320. } /* __PHYSFS_platformGrabMutex */
  321. void __PHYSFS_platformReleaseMutex(void *mutex)
  322. {
  323. /* no mutexes on MacOS Classic. */
  324. } /* __PHYSFS_platformReleaseMutex */
  325. /* end of unix.c ... */