unix.c 7.5 KB

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