unix.c 7.6 KB

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