unix.c 7.6 KB

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