platform_unix.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #ifdef PHYSFS_HAVE_SYS_UCRED_H
  21. # ifdef PHYSFS_HAVE_MNTENT_H
  22. # undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
  23. # endif
  24. # include <sys/mount.h>
  25. # include <sys/ucred.h>
  26. #endif
  27. #ifdef PHYSFS_HAVE_MNTENT_H
  28. #include <mntent.h>
  29. #endif
  30. #ifdef PHYSFS_HAVE_SYS_MNTTAB_H
  31. #include <sys/mnttab.h>
  32. #endif
  33. #include "physfs_internal.h"
  34. /* !!! FIXME: we should probably remove MAXPATHLEN entirely, if we can. */
  35. #ifndef MAXPATHLEN
  36. #define MAXPATHLEN 1024
  37. #endif
  38. int __PHYSFS_platformInit(void)
  39. {
  40. return 1; /* always succeed. */
  41. } /* __PHYSFS_platformInit */
  42. int __PHYSFS_platformDeinit(void)
  43. {
  44. return 1; /* always succeed. */
  45. } /* __PHYSFS_platformDeinit */
  46. /* Stub version for platforms without CD-ROM support. */
  47. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  48. {
  49. #if (defined PHYSFS_NO_CDROM_SUPPORT)
  50. /* no-op. */
  51. #elif (defined PHYSFS_HAVE_SYS_UCRED_H)
  52. int i;
  53. struct statfs *mntbufp = NULL;
  54. int mounts = getmntinfo(&mntbufp, MNT_WAIT);
  55. for (i = 0; i < mounts; i++)
  56. {
  57. int add_it = 0;
  58. if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
  59. add_it = 1;
  60. else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
  61. add_it = 1;
  62. /* add other mount types here */
  63. if (add_it)
  64. cb(data, mntbufp[i].f_mntonname);
  65. } /* for */
  66. #elif (defined PHYSFS_HAVE_MNTENT_H)
  67. FILE *mounts = NULL;
  68. struct mntent *ent = NULL;
  69. mounts = setmntent("/etc/mtab", "r");
  70. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
  71. while ( (ent = getmntent(mounts)) != NULL )
  72. {
  73. int add_it = 0;
  74. if (strcmp(ent->mnt_type, "iso9660") == 0)
  75. add_it = 1;
  76. else if (strcmp(ent->mnt_type, "udf") == 0)
  77. add_it = 1;
  78. /* !!! FIXME: these might pick up floppy drives, right? */
  79. else if (strcmp(ent->mnt_type, "auto") == 0)
  80. add_it = 1;
  81. else if (strcmp(ent->mnt_type, "supermount") == 0)
  82. add_it = 1;
  83. /* !!! FIXME: udf? automount? */
  84. /* add other mount types here */
  85. if (add_it)
  86. cb(data, ent->mnt_dir);
  87. } /* while */
  88. endmntent(mounts);
  89. #elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
  90. FILE *mounts = fopen(MNTTAB, "r");
  91. struct mnttab ent;
  92. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
  93. while (getmntent(mounts, &ent) == 0)
  94. {
  95. int add_it = 0;
  96. if (strcmp(ent.mnt_fstype, "hsfs") == 0)
  97. add_it = 1;
  98. /* add other mount types here */
  99. if (add_it)
  100. cb(data, ent.mnt_mountp);
  101. } /* while */
  102. fclose(mounts);
  103. #else
  104. #error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
  105. #endif
  106. } /* __PHYSFS_platformDetectAvailableCDs */
  107. /* this is in posix.c ... */
  108. extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
  109. /*
  110. * See where program (bin) resides in the $PATH specified by (envr).
  111. * returns a copy of the first element in envr that contains it, or NULL
  112. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  113. * called if we have a problem.
  114. *
  115. * (envr) will be scribbled over, and you are expected to allocator.Free() the
  116. * return value when you're done with it.
  117. */
  118. static char *findBinaryInPath(const char *bin, char *envr)
  119. {
  120. size_t alloc_size = 0;
  121. char *exe = NULL;
  122. char *start = envr;
  123. char *ptr;
  124. BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
  125. BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
  126. do
  127. {
  128. size_t size;
  129. ptr = strchr(start, ':'); /* find next $PATH separator. */
  130. if (ptr)
  131. *ptr = '\0';
  132. size = strlen(start) + strlen(bin) + 2;
  133. if (size > alloc_size)
  134. {
  135. char *x = (char *) allocator.Realloc(exe, size);
  136. if (x == NULL)
  137. {
  138. if (exe != NULL)
  139. allocator.Free(exe);
  140. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  141. } /* if */
  142. alloc_size = size;
  143. exe = x;
  144. } /* if */
  145. /* build full binary path... */
  146. strcpy(exe, start);
  147. if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
  148. strcat(exe, "/");
  149. strcat(exe, bin);
  150. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  151. {
  152. strcpy(exe, start); /* i'm lazy. piss off. */
  153. return exe;
  154. } /* if */
  155. start = ptr + 1; /* start points to beginning of next element. */
  156. } while (ptr != NULL);
  157. if (exe != NULL)
  158. allocator.Free(exe);
  159. return NULL; /* doesn't exist in path. */
  160. } /* findBinaryInPath */
  161. static char *readSymLink(const char *path)
  162. {
  163. ssize_t len = 64;
  164. ssize_t rc = -1;
  165. char *retval = NULL;
  166. while (1)
  167. {
  168. char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
  169. if (ptr == NULL)
  170. break; /* out of memory. */
  171. retval = ptr;
  172. rc = readlink(path, retval, len);
  173. if (rc == -1)
  174. break; /* not a symlink, i/o error, etc. */
  175. else if (rc < len)
  176. {
  177. retval[rc] = '\0'; /* readlink doesn't null-terminate. */
  178. return retval; /* we're good to go. */
  179. } /* else if */
  180. len *= 2; /* grow buffer, try again. */
  181. } /* while */
  182. if (retval != NULL)
  183. allocator.Free(retval);
  184. return NULL;
  185. } /* readSymLink */
  186. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  187. {
  188. char *retval = NULL;
  189. char *envr = NULL;
  190. /* fast path: default behaviour can handle this. */
  191. if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
  192. return NULL; /* higher level will parse out real path from argv0. */
  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 = '\0';
  213. } /* if */
  214. if ((retval == NULL) && (argv0 != NULL))
  215. {
  216. /* If there's no dirsep on argv0, then look through $PATH for it. */
  217. envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
  218. BAIL_IF_MACRO(!envr, NULL, NULL);
  219. retval = findBinaryInPath(argv0, envr);
  220. allocator.Free(envr);
  221. } /* if */
  222. if (retval != NULL)
  223. {
  224. /* try to shrink buffer... */
  225. char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  226. if (ptr != NULL)
  227. retval = ptr; /* oh well if it failed. */
  228. } /* if */
  229. return retval;
  230. } /* __PHYSFS_platformCalcBaseDir */
  231. char *__PHYSFS_platformRealPath(const char *path)
  232. {
  233. char resolved_path[MAXPATHLEN];
  234. char *retval = NULL;
  235. errno = 0;
  236. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  237. retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  238. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  239. strcpy(retval, resolved_path);
  240. return retval;
  241. } /* __PHYSFS_platformRealPath */
  242. char *__PHYSFS_platformCurrentDir(void)
  243. {
  244. /*
  245. * This can't just do platformRealPath("."), since that would eventually
  246. * just end up calling back into here.
  247. */
  248. int allocSize = 0;
  249. char *retval = NULL;
  250. char *ptr;
  251. do
  252. {
  253. allocSize += 100;
  254. ptr = (char *) allocator.Realloc(retval, allocSize);
  255. if (ptr == NULL)
  256. {
  257. if (retval != NULL)
  258. allocator.Free(retval);
  259. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  260. } /* if */
  261. retval = ptr;
  262. ptr = getcwd(retval, allocSize);
  263. } while (ptr == NULL && errno == ERANGE);
  264. if (ptr == NULL && errno)
  265. {
  266. /*
  267. * getcwd() failed for some reason, for example current
  268. * directory not existing.
  269. */
  270. if (retval != NULL)
  271. allocator.Free(retval);
  272. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  273. } /* if */
  274. return retval;
  275. } /* __PHYSFS_platformCurrentDir */
  276. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  277. {
  278. return 0; /* just use malloc() and friends. */
  279. } /* __PHYSFS_platformSetDefaultAllocator */
  280. #endif /* PHYSFS_PLATFORM_UNIX */
  281. /* end of unix.c ... */