platform_unix.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #if 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. int __PHYSFS_platformDeinit(void)
  57. {
  58. return 1; /* always succeed. */
  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_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. 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. /* !!! FIXME: udf? automount? */
  98. /* add other mount types here */
  99. if (add_it)
  100. cb(data, ent->mnt_dir);
  101. } /* while */
  102. endmntent(mounts);
  103. #elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
  104. FILE *mounts = fopen(MNTTAB, "r");
  105. struct mnttab ent;
  106. BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
  107. while (getmntent(mounts, &ent) == 0)
  108. {
  109. int add_it = 0;
  110. if (strcmp(ent.mnt_fstype, "hsfs") == 0)
  111. add_it = 1;
  112. /* add other mount types here */
  113. if (add_it)
  114. cb(data, ent.mnt_mountp);
  115. } /* while */
  116. fclose(mounts);
  117. #endif
  118. } /* __PHYSFS_platformDetectAvailableCDs */
  119. /*
  120. * See where program (bin) resides in the $PATH specified by (envr).
  121. * returns a copy of the first element in envr that contains it, or NULL
  122. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  123. * called if we have a problem.
  124. *
  125. * (envr) will be scribbled over, and you are expected to allocator.Free() the
  126. * return value when you're done with it.
  127. */
  128. static char *findBinaryInPath(const char *bin, char *envr)
  129. {
  130. size_t alloc_size = 0;
  131. char *exe = NULL;
  132. char *start = envr;
  133. char *ptr;
  134. assert(bin != NULL);
  135. assert(envr != NULL);
  136. do
  137. {
  138. size_t size;
  139. size_t binlen;
  140. ptr = strchr(start, ':'); /* find next $PATH separator. */
  141. if (ptr)
  142. *ptr = '\0';
  143. binlen = strlen(bin);
  144. size = strlen(start) + binlen + 2;
  145. if (size >= alloc_size)
  146. {
  147. char *x = (char *) allocator.Realloc(exe, size);
  148. if (!x)
  149. {
  150. if (exe != NULL)
  151. allocator.Free(exe);
  152. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  153. } /* if */
  154. alloc_size = size;
  155. exe = x;
  156. } /* if */
  157. /* build full binary path... */
  158. strcpy(exe, start);
  159. if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
  160. strcat(exe, "/");
  161. strcat(exe, bin);
  162. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  163. {
  164. exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */
  165. return exe;
  166. } /* if */
  167. start = ptr + 1; /* start points to beginning of next element. */
  168. } while (ptr != NULL);
  169. if (exe != NULL)
  170. allocator.Free(exe);
  171. return NULL; /* doesn't exist in path. */
  172. } /* findBinaryInPath */
  173. static char *readSymLink(const char *path)
  174. {
  175. ssize_t len = 64;
  176. ssize_t rc = -1;
  177. char *retval = NULL;
  178. while (1)
  179. {
  180. char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
  181. if (ptr == NULL)
  182. break; /* out of memory. */
  183. retval = ptr;
  184. rc = readlink(path, retval, len);
  185. if (rc == -1)
  186. break; /* not a symlink, i/o error, etc. */
  187. else if (rc < len)
  188. {
  189. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  190. return retval; /* we're good to go. */
  191. } /* else if */
  192. len *= 2; /* grow buffer, try again. */
  193. } /* while */
  194. if (retval != NULL)
  195. allocator.Free(retval);
  196. return NULL;
  197. } /* readSymLink */
  198. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  199. {
  200. char *retval = NULL;
  201. const char *envr = NULL;
  202. /* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
  203. #if PHYSFS_PLATFORM_FREEBSD
  204. {
  205. char fullpath[PATH_MAX];
  206. size_t buflen = sizeof (fullpath);
  207. int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  208. if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1)
  209. retval = __PHYSFS_strdup(fullpath);
  210. }
  211. #elif PHYSFS_PLATFORM_SOLARIS
  212. {
  213. const char *path = getexecname();
  214. if ((path != NULL) && (path[0] == '/')) /* must be absolute path... */
  215. retval = __PHYSFS_strdup(path);
  216. }
  217. #endif
  218. if (retval)
  219. return retval; /* already got it. */
  220. /* If there's a Linux-like /proc filesystem, you can get the full path to
  221. * the current process from a symlink in there.
  222. */
  223. if (access("/proc", F_OK) == 0)
  224. {
  225. retval = readSymLink("/proc/self/exe");
  226. if (!retval) retval = readSymLink("/proc/curproc/file");
  227. if (!retval) retval = readSymLink("/proc/curproc/exe");
  228. if (retval == NULL)
  229. {
  230. /* older kernels don't have /proc/self ... try PID version... */
  231. const unsigned long long pid = (unsigned long long) getpid();
  232. char path[64];
  233. const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
  234. if ( (rc > 0) && (rc < sizeof(path)) )
  235. retval = readSymLink(path);
  236. } /* if */
  237. } /* if */
  238. if (retval != NULL) /* chop off filename. */
  239. {
  240. char *ptr = strrchr(retval, '/');
  241. if (ptr != NULL)
  242. *(ptr+1) = '\0';
  243. else /* shouldn't happen, but just in case... */
  244. {
  245. allocator.Free(retval);
  246. retval = NULL;
  247. } /* else */
  248. } /* if */
  249. /* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
  250. if ((retval == NULL) && (argv0 != NULL))
  251. {
  252. /* fast path: default behaviour can handle this. */
  253. if (strchr(argv0, '/') != NULL)
  254. return NULL; /* higher level parses out real path from argv0. */
  255. /* If there's no dirsep on argv0, then look through $PATH for it. */
  256. envr = getenv("PATH");
  257. if (envr != NULL)
  258. {
  259. char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
  260. BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  261. strcpy(path, envr);
  262. retval = findBinaryInPath(argv0, path);
  263. __PHYSFS_smallFree(path);
  264. } /* if */
  265. } /* if */
  266. if (retval != NULL)
  267. {
  268. /* try to shrink buffer... */
  269. char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  270. if (ptr != NULL)
  271. retval = ptr; /* oh well if it failed. */
  272. } /* if */
  273. return retval;
  274. } /* __PHYSFS_platformCalcBaseDir */
  275. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  276. {
  277. /*
  278. * We use XDG's base directory spec, even if you're not on Linux.
  279. * This isn't strictly correct, but the results are relatively sane
  280. * in any case.
  281. *
  282. * https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  283. */
  284. const char *envr = getenv("XDG_DATA_HOME");
  285. const char *append = "/";
  286. char *retval = NULL;
  287. size_t len = 0;
  288. if (!envr)
  289. {
  290. /* You end up with "$HOME/.local/share/Game Name 2" */
  291. envr = __PHYSFS_getUserDir();
  292. BAIL_IF_ERRPASS(!envr, NULL); /* oh well. */
  293. append = ".local/share/";
  294. } /* if */
  295. len = strlen(envr) + strlen(append) + strlen(app) + 2;
  296. retval = (char *) allocator.Malloc(len);
  297. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  298. snprintf(retval, len, "%s%s%s/", envr, append, app);
  299. return retval;
  300. } /* __PHYSFS_platformCalcPrefDir */
  301. #endif /* PHYSFS_PLATFORM_UNIX */
  302. /* end of unix.c ... */