unix.c 8.8 KB

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