unix.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #if ((defined __APPLE__) && (defined __MACH__))
  14. # if (!defined __DARWIN__)
  15. # define __DARWIN__
  16. # endif
  17. #endif
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <pthread.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <pwd.h>
  26. #include <sys/stat.h>
  27. #include <sys/param.h>
  28. #include <dirent.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #if (!defined __DARWIN__)
  32. #include <mntent.h>
  33. #else
  34. #include <sys/ucred.h>
  35. #endif
  36. #include <sys/mount.h>
  37. #define __PHYSICSFS_INTERNAL__
  38. #include "physfs_internal.h"
  39. const char *__PHYSFS_platformDirSeparator = "/";
  40. int __PHYSFS_platformInit(void)
  41. {
  42. return(1); /* always succeed. */
  43. } /* __PHYSFS_platformInit */
  44. int __PHYSFS_platformDeinit(void)
  45. {
  46. return(1); /* always succeed. */
  47. } /* __PHYSFS_platformDeinit */
  48. #if (defined __DARWIN__)
  49. char **__PHYSFS_platformDetectAvailableCDs(void)
  50. {
  51. char **retval = (char **) malloc(sizeof (char *));
  52. int cd_count = 1; /* We count the NULL entry. */
  53. struct statfs* mntbufp = NULL;
  54. int mounts;
  55. int ii;
  56. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  57. mounts = getmntinfo( &mntbufp, MNT_WAIT );
  58. for ( ii=0; ii < mounts; ++ii ) {
  59. int add_it = 0;
  60. if ( strcmp( mntbufp[ii].f_fstypename, "iso9660") == 0 )
  61. add_it = 1;
  62. else if ( strcmp( mntbufp[ii].f_fstypename, "cd9660") == 0 )
  63. add_it = 1;
  64. /* add other mount types here */
  65. if (add_it)
  66. {
  67. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  68. if (tmp)
  69. {
  70. retval = tmp;
  71. retval[cd_count - 1] = (char *)
  72. malloc(strlen(mntbufp[ii].f_mntonname) + 1);
  73. if (retval[cd_count - 1])
  74. {
  75. strcpy(retval[cd_count - 1], mntbufp[ii].f_mntonname);
  76. cd_count++;
  77. } /* if */
  78. } /* if */
  79. } /* if */
  80. }
  81. retval[cd_count - 1] = NULL;
  82. return(retval);
  83. } /* __PHYSFS_platformDetectAvailableCDs */
  84. #else /* non-Darwin implementation... */
  85. char **__PHYSFS_platformDetectAvailableCDs(void)
  86. {
  87. char **retval = (char **) malloc(sizeof (char *));
  88. int cd_count = 1; /* We count the NULL entry. */
  89. FILE *mounts = NULL;
  90. struct mntent *ent = NULL;
  91. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  92. *retval = NULL;
  93. mounts = setmntent("/etc/mtab", "r");
  94. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
  95. while ( (ent = getmntent(mounts)) != NULL )
  96. {
  97. int add_it = 0;
  98. if (strcmp(ent->mnt_type, "iso9660") == 0)
  99. add_it = 1;
  100. /* add other mount types here */
  101. if (add_it)
  102. {
  103. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  104. if (tmp)
  105. {
  106. retval = tmp;
  107. retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
  108. if (retval[cd_count - 1])
  109. {
  110. strcpy(retval[cd_count - 1], ent->mnt_dir);
  111. cd_count++;
  112. } /* if */
  113. } /* if */
  114. } /* if */
  115. } /* while */
  116. endmntent(mounts);
  117. retval[cd_count - 1] = NULL;
  118. return(retval);
  119. } /* __PHYSFS_platformDetectAvailableCDs */
  120. #endif
  121. /* this is in posix.c ... */
  122. extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
  123. /*
  124. * See where program (bin) resides in the $PATH specified by (envr).
  125. * returns a copy of the first element in envr that contains it, or NULL
  126. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  127. * called if we have a problem.
  128. *
  129. * (envr) will be scribbled over, and you are expected to free() the
  130. * return value when you're done with it.
  131. */
  132. static char *findBinaryInPath(const char *bin, char *envr)
  133. {
  134. size_t alloc_size = 0;
  135. char *exe = NULL;
  136. char *start = envr;
  137. char *ptr;
  138. BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
  139. BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
  140. do
  141. {
  142. size_t size;
  143. ptr = strchr(start, ':'); /* find next $PATH separator. */
  144. if (ptr)
  145. *ptr = '\0';
  146. size = strlen(start) + strlen(bin) + 2;
  147. if (size > alloc_size)
  148. {
  149. char *x = (char *) realloc(exe, size);
  150. if (x == NULL)
  151. {
  152. if (exe != NULL)
  153. free(exe);
  154. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  155. } /* if */
  156. alloc_size = size;
  157. exe = x;
  158. } /* if */
  159. /* build full binary path... */
  160. strcpy(exe, start);
  161. if (exe[strlen(exe) - 1] != '/')
  162. strcat(exe, "/");
  163. strcat(exe, bin);
  164. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  165. {
  166. strcpy(exe, start); /* i'm lazy. piss off. */
  167. return(exe);
  168. } /* if */
  169. start = ptr + 1; /* start points to beginning of next element. */
  170. } while (ptr != NULL);
  171. if (exe != NULL)
  172. free(exe);
  173. return(NULL); /* doesn't exist in path. */
  174. } /* findBinaryInPath */
  175. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  176. {
  177. /* If there isn't a path on argv0, then look through the $PATH for it. */
  178. char *retval;
  179. char *envr;
  180. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  181. return(NULL);
  182. envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
  183. BAIL_IF_MACRO(!envr, NULL, NULL);
  184. retval = findBinaryInPath(argv0, envr);
  185. free(envr);
  186. return(retval);
  187. } /* __PHYSFS_platformCalcBaseDir */
  188. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  189. {
  190. return((PHYSFS_uint64) ((PHYSFS_uint32) pthread_self()));
  191. } /* __PHYSFS_platformGetThreadID */
  192. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  193. void __PHYSFS_platformTimeslice(void)
  194. {
  195. usleep( 10 * 1000 ); /* don't care if it fails. */
  196. } /* __PHYSFS_platformTimeslice */
  197. char *__PHYSFS_platformRealPath(const char *path)
  198. {
  199. char resolved_path[MAXPATHLEN];
  200. char *retval = NULL;
  201. errno = 0;
  202. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  203. retval = (char *) malloc(strlen(resolved_path) + 1);
  204. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  205. strcpy(retval, resolved_path);
  206. return(retval);
  207. } /* __PHYSFS_platformRealPath */
  208. void *__PHYSFS_platformCreateMutex(void)
  209. {
  210. int rc;
  211. pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
  212. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  213. rc = pthread_mutex_init(m, NULL);
  214. if (rc != 0)
  215. {
  216. free(m);
  217. BAIL_MACRO(strerror(rc), NULL);
  218. } /* if */
  219. return((void *) m);
  220. } /* __PHYSFS_platformCreateMutex */
  221. void __PHYSFS_platformDestroyMutex(void *mutex)
  222. {
  223. pthread_mutex_destroy((pthread_mutex_t *) mutex);
  224. free(mutex);
  225. } /* __PHYSFS_platformDestroyMutex */
  226. int __PHYSFS_platformGrabMutex(void *mutex)
  227. {
  228. return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
  229. } /* __PHYSFS_platformGrabMutex */
  230. void __PHYSFS_platformReleaseMutex(void *mutex)
  231. {
  232. pthread_mutex_unlock((pthread_mutex_t *) mutex);
  233. } /* __PHYSFS_platformReleaseMutex */
  234. #endif /* !defined __BEOS__ && !defined WIN32 */
  235. /* end of unix.c ... */