unix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Unix 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. /* BeOS uses beos.cpp and posix.c ... Cygwin and such use win32.c ... */
  12. #if ((!defined __BEOS__) && (!defined WIN32))
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <pwd.h>
  20. #include <sys/stat.h>
  21. #include <sys/param.h>
  22. #include <dirent.h>
  23. #include <time.h>
  24. #include <errno.h>
  25. #include <sys/mount.h>
  26. #if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
  27. #include <pthread.h>
  28. #endif
  29. #ifdef PHYSFS_HAVE_SYS_UCRED_H
  30. # ifdef PHYSFS_HAVE_MNTENT_H
  31. # undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
  32. # endif
  33. # include <sys/ucred.h>
  34. #endif
  35. #ifdef PHYSFS_HAVE_MNTENT_H
  36. #include <mntent.h>
  37. #endif
  38. #define __PHYSICSFS_INTERNAL__
  39. #include "physfs_internal.h"
  40. const char *__PHYSFS_platformDirSeparator = "/";
  41. int __PHYSFS_platformInit(void)
  42. {
  43. return(1); /* always succeed. */
  44. } /* __PHYSFS_platformInit */
  45. int __PHYSFS_platformDeinit(void)
  46. {
  47. return(1); /* always succeed. */
  48. } /* __PHYSFS_platformDeinit */
  49. #ifdef PHYSFS_NO_CDROM_SUPPORT
  50. /* Stub version for platforms without CD-ROM support. */
  51. char **__PHYSFS_platformDetectAvailableCDs(void)
  52. {
  53. char **retval = (char **) malloc(sizeof (char *));
  54. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  55. *retval = NULL;
  56. return(retval);
  57. } /* __PHYSFS_platformDetectAvailableCDs */
  58. #else
  59. #ifdef PHYSFS_HAVE_SYS_UCRED_H
  60. char **__PHYSFS_platformDetectAvailableCDs(void)
  61. {
  62. char **retval = (char **) malloc(sizeof (char *));
  63. int cd_count = 1; /* We count the NULL entry. */
  64. struct statfs *mntbufp = NULL;
  65. int mounts;
  66. int i;
  67. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  68. mounts = getmntinfo(&mntbufp, MNT_WAIT);
  69. for (i = 0; i < mounts; i++)
  70. {
  71. int add_it = 0;
  72. if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
  73. add_it = 1;
  74. else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
  75. add_it = 1;
  76. /* add other mount types here */
  77. if (add_it)
  78. {
  79. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  80. if (tmp)
  81. {
  82. retval = tmp;
  83. retval[cd_count - 1] = (char *)
  84. malloc(strlen(mntbufp[i].f_mntonname) + 1);
  85. if (retval[cd_count - 1])
  86. {
  87. strcpy(retval[cd_count - 1], mntbufp[i].f_mntonname);
  88. cd_count++;
  89. } /* if */
  90. } /* if */
  91. } /* if */
  92. } /* for */
  93. retval[cd_count - 1] = NULL;
  94. return(retval);
  95. } /* __PHYSFS_platformDetectAvailableCDs */
  96. #endif
  97. #ifdef PHYSFS_HAVE_MNTENT_H
  98. char **__PHYSFS_platformDetectAvailableCDs(void)
  99. {
  100. char **retval = (char **) malloc(sizeof (char *));
  101. int cd_count = 1; /* We count the NULL entry. */
  102. FILE *mounts = NULL;
  103. struct mntent *ent = NULL;
  104. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  105. *retval = NULL;
  106. mounts = setmntent("/etc/mtab", "r");
  107. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
  108. while ( (ent = getmntent(mounts)) != NULL )
  109. {
  110. int add_it = 0;
  111. if (strcmp(ent->mnt_type, "iso9660") == 0)
  112. add_it = 1;
  113. /* add other mount types here */
  114. if (add_it)
  115. {
  116. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  117. if (tmp)
  118. {
  119. retval = tmp;
  120. retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
  121. if (retval[cd_count - 1])
  122. {
  123. strcpy(retval[cd_count - 1], ent->mnt_dir);
  124. cd_count++;
  125. } /* if */
  126. } /* if */
  127. } /* if */
  128. } /* while */
  129. endmntent(mounts);
  130. retval[cd_count - 1] = NULL;
  131. return(retval);
  132. } /* __PHYSFS_platformDetectAvailableCDs */
  133. #endif
  134. #endif /* !PHYSFS_NO_CDROM_SUPPORT */
  135. /* this is in posix.c ... */
  136. extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
  137. /*
  138. * See where program (bin) resides in the $PATH specified by (envr).
  139. * returns a copy of the first element in envr that contains it, or NULL
  140. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  141. * called if we have a problem.
  142. *
  143. * (envr) will be scribbled over, and you are expected to free() the
  144. * return value when you're done with it.
  145. */
  146. static char *findBinaryInPath(const char *bin, char *envr)
  147. {
  148. size_t alloc_size = 0;
  149. char *exe = NULL;
  150. char *start = envr;
  151. char *ptr;
  152. BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
  153. BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
  154. do
  155. {
  156. size_t size;
  157. ptr = strchr(start, ':'); /* find next $PATH separator. */
  158. if (ptr)
  159. *ptr = '\0';
  160. size = strlen(start) + strlen(bin) + 2;
  161. if (size > alloc_size)
  162. {
  163. char *x = (char *) realloc(exe, size);
  164. if (x == NULL)
  165. {
  166. if (exe != NULL)
  167. free(exe);
  168. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  169. } /* if */
  170. alloc_size = size;
  171. exe = x;
  172. } /* if */
  173. /* build full binary path... */
  174. strcpy(exe, start);
  175. if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
  176. strcat(exe, "/");
  177. strcat(exe, bin);
  178. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  179. {
  180. strcpy(exe, start); /* i'm lazy. piss off. */
  181. return(exe);
  182. } /* if */
  183. start = ptr + 1; /* start points to beginning of next element. */
  184. } while (ptr != NULL);
  185. if (exe != NULL)
  186. free(exe);
  187. return(NULL); /* doesn't exist in path. */
  188. } /* findBinaryInPath */
  189. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  190. {
  191. /* If there isn't a path on argv0, then look through the $PATH for it. */
  192. char *retval;
  193. char *envr;
  194. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  195. return(NULL);
  196. envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
  197. BAIL_IF_MACRO(!envr, NULL, NULL);
  198. retval = findBinaryInPath(argv0, envr);
  199. free(envr);
  200. return(retval);
  201. } /* __PHYSFS_platformCalcBaseDir */
  202. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  203. void __PHYSFS_platformTimeslice(void)
  204. {
  205. usleep( 10 * 1000 ); /* don't care if it fails. */
  206. } /* __PHYSFS_platformTimeslice */
  207. #if defined(__MACH__) && defined(__APPLE__)
  208. /*
  209. * This function is only for OSX. The problem is that Apple's applications
  210. * can actually be directory structures with the actual executable nested
  211. * several levels down. PhysFS computes the base directory from the Unix
  212. * executable, but this may not be the correct directory. Apple tries to
  213. * hide everything from the user, so from Finder, the user never sees the
  214. * Unix executable, and the directory package (bundle) is considered the
  215. * "executable". This means that the correct base directory is at the
  216. * level where the directory structure starts.
  217. * A typical bundle seems to look like this:
  218. * MyApp.app/ <-- top level...this is what the user sees in Finder
  219. * Contents/
  220. * MacOS/
  221. * MyApp <-- the actual executable
  222. *
  223. * Since anything below the app folder is considered hidden, most
  224. * application files need to be at the top level if you intend to
  225. * write portable software. Thus if the application resides in:
  226. * /Applications/MyProgram
  227. * and the executable is the bundle MyApp.app,
  228. * PhysFS computes the following as the base directory:
  229. * /Applications/MyProgram/MyApp.app/Contents/MacOS/
  230. * We need to strip off the MyApp.app/Contents/MacOS/
  231. *
  232. * However, there are corner cases. OSX applications can be traditional
  233. * Unix executables without the bundle. Also, it is not entirely clear
  234. * to me what kinds of permutations bundle structures can have.
  235. *
  236. * For now, this is a temporary hack until a better solution
  237. * can be made. This function will try to find a "/Contents/MacOS"
  238. * inside the path. If it succeeds, then the path will be truncated
  239. * to correct the directory. If it is not found, the path will be
  240. * left alone and will presume it is a traditional Unix execuatable.
  241. * Most programs also include the .app extention in the top level
  242. * folder, but it doesn't seem to be a requirement (Acrobat doesn't
  243. * have it). MacOS looks like it can also be MacOSClassic.
  244. * This function will test for MacOS and hope it captures any
  245. * other permutations.
  246. */
  247. static void stripAppleBundle(char *path)
  248. {
  249. char *sub_str = "/contents/macos";
  250. char *found_ptr = NULL;
  251. char *tempbuf = NULL;
  252. int i;
  253. /* Calloc will place the \0 character in the proper place for us */
  254. tempbuf = (char*)calloc( (strlen(path)+1), sizeof(char) );
  255. /* Unlike other Unix filesystems, HFS is case insensitive
  256. * It wouldn be nice to use strcasestr, but it doesn't seem
  257. * to be available in the OSX gcc library right now.
  258. * So we should make a lower case copy of the path to
  259. * compare against
  260. */
  261. for(i=0; i<strlen(path); i++)
  262. {
  263. /* convert to lower case */
  264. tempbuf[i] = tolower(path[i]);
  265. }
  266. /* See if we can find "/contents/macos" in the path */
  267. found_ptr = strstr(tempbuf, sub_str);
  268. if(NULL == found_ptr)
  269. {
  270. /* It doesn't look like a bundle so we can keep the
  271. * original path. Just return */
  272. free(tempbuf);
  273. return;
  274. }
  275. /* We have a bundle, so let's backstep character by character
  276. * to erase the extra parts of the path. Quit when we hit
  277. * the preceding '/' character.
  278. */
  279. for(i=strlen(path)-strlen(found_ptr)-1; i>=0; i--)
  280. {
  281. if('/' == path[i])
  282. {
  283. break;
  284. }
  285. }
  286. /* Safety check */
  287. if(i<1)
  288. {
  289. /* This probably shouldn't happen. */
  290. path[0] = '\0';
  291. }
  292. else
  293. {
  294. /* Back up one more to remove trailing '/' and set the '\0' */
  295. path[i] = '\0';
  296. }
  297. free(tempbuf);
  298. return;
  299. }
  300. #endif /* defined __MACH__ && defined __APPLE__ */
  301. char *__PHYSFS_platformRealPath(const char *path)
  302. {
  303. char resolved_path[MAXPATHLEN];
  304. char *retval = NULL;
  305. errno = 0;
  306. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  307. retval = (char *) malloc(strlen(resolved_path) + 1);
  308. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  309. strcpy(retval, resolved_path);
  310. #if defined(__MACH__) && defined(__APPLE__)
  311. stripAppleBundle(retval);
  312. #endif /* defined __MACH__ && defined __APPLE__ */
  313. return(retval);
  314. } /* __PHYSFS_platformRealPath */
  315. #if (defined PHYSFS_NO_PTHREADS_SUPPORT)
  316. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
  317. void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
  318. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  319. int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
  320. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  321. #else
  322. /* Just in case; this is a panic value. */
  323. #if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
  324. # define SIZEOF_INT 4
  325. #endif
  326. #if (SIZEOF_INT == 4)
  327. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
  328. #elif (SIZEOF_INT == 2)
  329. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
  330. #elif (SIZEOF_INT == 1)
  331. # define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
  332. #else
  333. # define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
  334. #endif
  335. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  336. {
  337. return(PHTREAD_TO_UI64(pthread_self()));
  338. } /* __PHYSFS_platformGetThreadID */
  339. void *__PHYSFS_platformCreateMutex(void)
  340. {
  341. int rc;
  342. pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
  343. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  344. rc = pthread_mutex_init(m, NULL);
  345. if (rc != 0)
  346. {
  347. free(m);
  348. BAIL_MACRO(strerror(rc), NULL);
  349. } /* if */
  350. return((void *) m);
  351. } /* __PHYSFS_platformCreateMutex */
  352. void __PHYSFS_platformDestroyMutex(void *mutex)
  353. {
  354. pthread_mutex_destroy((pthread_mutex_t *) mutex);
  355. free(mutex);
  356. } /* __PHYSFS_platformDestroyMutex */
  357. int __PHYSFS_platformGrabMutex(void *mutex)
  358. {
  359. return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
  360. } /* __PHYSFS_platformGrabMutex */
  361. void __PHYSFS_platformReleaseMutex(void *mutex)
  362. {
  363. pthread_mutex_unlock((pthread_mutex_t *) mutex);
  364. } /* __PHYSFS_platformReleaseMutex */
  365. #endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
  366. #endif /* !defined __BEOS__ && !defined WIN32 */
  367. /* end of unix.c ... */