unix.c 7.9 KB

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