platform_unix.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Unix support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_UNIX
  11. #include <ctype.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15. #include <sys/stat.h>
  16. #include <sys/param.h>
  17. #include <dirent.h>
  18. #include <time.h>
  19. #include <errno.h>
  20. #if PHYSFS_PLATFORM_LINUX && !defined(PHYSFS_HAVE_MNTENT_H)
  21. #define PHYSFS_HAVE_MNTENT_H 1
  22. #elif PHYSFS_PLATFORM_SOLARIS && !defined(PHYSFS_HAVE_SYS_MNTTAB_H)
  23. #define PHYSFS_HAVE_SYS_MNTTAB_H 1
  24. #elif PHYSFS_PLATFORM_BSD && !defined(PHYSFS_HAVE_SYS_UCRED_H)
  25. #define PHYSFS_HAVE_SYS_UCRED_H 1
  26. #endif
  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/mount.h>
  32. # include <sys/ucred.h>
  33. #endif
  34. #ifdef PHYSFS_HAVE_MNTENT_H
  35. #include <mntent.h>
  36. #endif
  37. #ifdef PHYSFS_HAVE_SYS_MNTTAB_H
  38. #include <sys/mnttab.h>
  39. #endif
  40. #include "physfs_internal.h"
  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. /* Stub version for platforms without CD-ROM support. */
  50. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  51. {
  52. #if (defined PHYSFS_NO_CDROM_SUPPORT)
  53. /* no-op. */
  54. #elif (defined PHYSFS_HAVE_SYS_UCRED_H)
  55. int i;
  56. struct statfs *mntbufp = NULL;
  57. int mounts = getmntinfo(&mntbufp, MNT_WAIT);
  58. for (i = 0; i < mounts; i++)
  59. {
  60. int add_it = 0;
  61. if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
  62. add_it = 1;
  63. else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
  64. add_it = 1;
  65. /* add other mount types here */
  66. if (add_it)
  67. cb(data, mntbufp[i].f_mntonname);
  68. } /* for */
  69. #elif (defined PHYSFS_HAVE_MNTENT_H)
  70. FILE *mounts = NULL;
  71. struct mntent *ent = NULL;
  72. mounts = setmntent("/etc/mtab", "r");
  73. BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
  74. while ( (ent = getmntent(mounts)) != NULL )
  75. {
  76. int add_it = 0;
  77. if (strcmp(ent->mnt_type, "iso9660") == 0)
  78. add_it = 1;
  79. else if (strcmp(ent->mnt_type, "udf") == 0)
  80. add_it = 1;
  81. /* !!! FIXME: these might pick up floppy drives, right? */
  82. else if (strcmp(ent->mnt_type, "auto") == 0)
  83. add_it = 1;
  84. else if (strcmp(ent->mnt_type, "supermount") == 0)
  85. add_it = 1;
  86. /* !!! FIXME: udf? automount? */
  87. /* add other mount types here */
  88. if (add_it)
  89. cb(data, ent->mnt_dir);
  90. } /* while */
  91. endmntent(mounts);
  92. #elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
  93. FILE *mounts = fopen(MNTTAB, "r");
  94. struct mnttab ent;
  95. BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
  96. while (getmntent(mounts, &ent) == 0)
  97. {
  98. int add_it = 0;
  99. if (strcmp(ent.mnt_fstype, "hsfs") == 0)
  100. add_it = 1;
  101. /* add other mount types here */
  102. if (add_it)
  103. cb(data, ent.mnt_mountp);
  104. } /* while */
  105. fclose(mounts);
  106. #else
  107. #error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
  108. #endif
  109. } /* __PHYSFS_platformDetectAvailableCDs */
  110. /*
  111. * See where program (bin) resides in the $PATH specified by (envr).
  112. * returns a copy of the first element in envr that contains it, or NULL
  113. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  114. * called if we have a problem.
  115. *
  116. * (envr) will be scribbled over, and you are expected to allocator.Free() the
  117. * return value when you're done with it.
  118. */
  119. static char *findBinaryInPath(const char *bin, char *envr)
  120. {
  121. size_t alloc_size = 0;
  122. char *exe = NULL;
  123. char *start = envr;
  124. char *ptr;
  125. assert(bin != NULL);
  126. assert(envr != NULL);
  127. do
  128. {
  129. size_t size;
  130. size_t binlen;
  131. ptr = strchr(start, ':'); /* find next $PATH separator. */
  132. if (ptr)
  133. *ptr = '\0';
  134. binlen = strlen(bin);
  135. size = strlen(start) + binlen + 2;
  136. if (size > alloc_size)
  137. {
  138. char *x = (char *) allocator.Realloc(exe, size);
  139. if (!x)
  140. {
  141. if (exe != NULL)
  142. allocator.Free(exe);
  143. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  144. } /* if */
  145. alloc_size = size;
  146. exe = x;
  147. } /* if */
  148. /* build full binary path... */
  149. strcpy(exe, start);
  150. if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
  151. strcat(exe, "/");
  152. strcat(exe, bin);
  153. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  154. {
  155. exe[size - binlen] = '\0'; /* chop off filename, leave '/' */
  156. return exe;
  157. } /* if */
  158. start = ptr + 1; /* start points to beginning of next element. */
  159. } while (ptr != NULL);
  160. if (exe != NULL)
  161. allocator.Free(exe);
  162. return NULL; /* doesn't exist in path. */
  163. } /* findBinaryInPath */
  164. static char *readSymLink(const char *path)
  165. {
  166. ssize_t len = 64;
  167. ssize_t rc = -1;
  168. char *retval = NULL;
  169. while (1)
  170. {
  171. char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
  172. if (ptr == NULL)
  173. break; /* out of memory. */
  174. retval = ptr;
  175. rc = readlink(path, retval, len);
  176. if (rc == -1)
  177. break; /* not a symlink, i/o error, etc. */
  178. else if (rc < len)
  179. {
  180. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  181. return retval; /* we're good to go. */
  182. } /* else if */
  183. len *= 2; /* grow buffer, try again. */
  184. } /* while */
  185. if (retval != NULL)
  186. allocator.Free(retval);
  187. return NULL;
  188. } /* readSymLink */
  189. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  190. {
  191. char *retval = NULL;
  192. const char *envr = NULL;
  193. /*
  194. * Try to avoid using argv0 unless forced to. If there's a Linux-like
  195. * /proc filesystem, you can get the full path to the current process from
  196. * the /proc/self/exe symlink.
  197. */
  198. retval = readSymLink("/proc/self/exe");
  199. if (retval == NULL)
  200. {
  201. /* older kernels don't have /proc/self ... try PID version... */
  202. const unsigned long long pid = (unsigned long long) getpid();
  203. char path[64];
  204. const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
  205. if ( (rc > 0) && (rc < sizeof(path)) )
  206. retval = readSymLink(path);
  207. } /* if */
  208. if (retval != NULL) /* chop off filename. */
  209. {
  210. char *ptr = strrchr(retval, '/');
  211. if (ptr != NULL)
  212. *(ptr+1) = '\0';
  213. else /* shouldn't happen, but just in case... */
  214. {
  215. allocator.Free(retval);
  216. retval = NULL;
  217. } /* else */
  218. } /* if */
  219. /* No /proc/self/exe, but we have an argv[0] we can parse? */
  220. if ((retval == NULL) && (argv0 != NULL))
  221. {
  222. /* fast path: default behaviour can handle this. */
  223. if (strchr(argv0, '/') != NULL)
  224. return NULL; /* higher level parses out real path from argv0. */
  225. /* If there's no dirsep on argv0, then look through $PATH for it. */
  226. envr = getenv("PATH");
  227. if (envr != NULL)
  228. {
  229. char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
  230. BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  231. strcpy(path, envr);
  232. retval = findBinaryInPath(argv0, path);
  233. __PHYSFS_smallFree(path);
  234. } /* if */
  235. } /* if */
  236. if (retval != NULL)
  237. {
  238. /* try to shrink buffer... */
  239. char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  240. if (ptr != NULL)
  241. retval = ptr; /* oh well if it failed. */
  242. } /* if */
  243. return retval;
  244. } /* __PHYSFS_platformCalcBaseDir */
  245. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  246. {
  247. /*
  248. * We use XDG's base directory spec, even if you're not on Linux.
  249. * This isn't strictly correct, but the results are relatively sane
  250. * in any case.
  251. *
  252. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  253. */
  254. const char *envr = getenv("XDG_DATA_HOME");
  255. const char *append = "/";
  256. char *retval = NULL;
  257. size_t len = 0;
  258. if (!envr)
  259. {
  260. /* You end up with "$HOME/.local/share/Game Name 2" */
  261. envr = __PHYSFS_getUserDir();
  262. BAIL_IF_MACRO(!envr, ERRPASS, NULL); /* oh well. */
  263. append = ".local/share/";
  264. } /* if */
  265. len = strlen(envr) + strlen(append) + strlen(app) + 2;
  266. retval = (char *) allocator.Malloc(len);
  267. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  268. snprintf(retval, len, "%s%s%s/", envr, append, app);
  269. return retval;
  270. } /* __PHYSFS_platformCalcPrefDir */
  271. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  272. {
  273. return 0; /* just use malloc() and friends. */
  274. } /* __PHYSFS_platformSetDefaultAllocator */
  275. #endif /* PHYSFS_PLATFORM_UNIX */
  276. /* end of unix.c ... */