physfs_platform_unix.c 9.8 KB

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