unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 (defined __STRICT_ANSI__)
  9. #define __PHYSFS_DOING_STRICT_ANSI__
  10. #endif
  11. /*
  12. * We cheat a little: I want the symlink version of stat() (lstat), and
  13. * GCC/Linux will not declare it if compiled with the -ansi flag.
  14. * If you are really lacking symlink support on your platform,
  15. * you should #define __PHYSFS_NO_SYMLINKS__ before compiling this
  16. * file. That will open a security hole, though, if you really DO have
  17. * symlinks on your platform; it renders PHYSFS_permitSymbolicLinks(0)
  18. * useless, since every symlink will be reported as a regular file/dir.
  19. */
  20. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  21. #undef __STRICT_ANSI__
  22. #endif
  23. #include <stdio.h>
  24. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  25. #define __STRICT_ANSI__
  26. #endif
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <pthread.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <pwd.h>
  34. #include <sys/stat.h>
  35. #include <sys/param.h>
  36. #include <dirent.h>
  37. #include <time.h>
  38. #include <errno.h>
  39. #include <mntent.h>
  40. #define __PHYSICSFS_INTERNAL__
  41. #include "physfs_internal.h"
  42. const char *__PHYSFS_platformDirSeparator = "/";
  43. char **__PHYSFS_platformDetectAvailableCDs(void)
  44. {
  45. char **retval = (char **) malloc(sizeof (char *));
  46. int cd_count = 1; /* We count the NULL entry. */
  47. FILE *mounts = NULL;
  48. struct mntent *ent = NULL;
  49. *retval = NULL;
  50. mounts = setmntent("/etc/mtab", "r");
  51. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
  52. while ( (ent = getmntent(mounts)) != NULL )
  53. {
  54. int add_it = 0;
  55. if (strcmp(ent->mnt_type, "iso9660") == 0)
  56. add_it = 1;
  57. /* !!! other mount types? */
  58. if (add_it)
  59. {
  60. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  61. if (tmp)
  62. {
  63. retval = tmp;
  64. retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
  65. if (retval[cd_count-1])
  66. {
  67. strcpy(retval[cd_count-1], ent->mnt_dir);
  68. cd_count++;
  69. } /* if */
  70. } /* if */
  71. } /* if */
  72. } /* while */
  73. endmntent(mounts);
  74. retval[cd_count - 1] = NULL;
  75. return(retval);
  76. } /* __PHYSFS_detectAvailableCDs */
  77. static char *copyEnvironmentVariable(const char *varname)
  78. {
  79. const char *envr = getenv(varname);
  80. char *retval = NULL;
  81. if (envr != NULL)
  82. {
  83. retval = malloc(strlen(envr) + 1);
  84. if (retval != NULL)
  85. strcpy(retval, envr);
  86. } /* if */
  87. return(retval);
  88. } /* copyEnvironmentVariable */
  89. /* !!! this is ugly. */
  90. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  91. {
  92. /* If there isn't a path on argv0, then look through the $PATH for it. */
  93. char *retval = NULL;
  94. char *envr;
  95. char *start;
  96. char *ptr;
  97. char *exe;
  98. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  99. return(NULL);
  100. envr = copyEnvironmentVariable("PATH");
  101. BAIL_IF_MACRO(!envr, NULL, NULL);
  102. start = envr;
  103. do
  104. {
  105. ptr = strchr(start, ':');
  106. if (ptr)
  107. *ptr = '\0';
  108. exe = (char *) malloc(strlen(start) + strlen(argv0) + 2);
  109. if (!exe)
  110. {
  111. free(envr);
  112. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  113. } /* if */
  114. strcpy(exe, start);
  115. if (exe[strlen(exe) - 1] != '/')
  116. strcat(exe, "/");
  117. strcat(exe, argv0);
  118. if (access(exe, X_OK) != 0)
  119. free(exe);
  120. else
  121. {
  122. retval = exe;
  123. strcpy(retval, start); /* i'm lazy. piss off. */
  124. break;
  125. } /* else */
  126. start = ptr + 1;
  127. } while (ptr != NULL);
  128. free(envr);
  129. return(retval);
  130. } /* __PHYSFS_platformCalcBaseDir */
  131. static char *getUserNameByUID(void)
  132. {
  133. uid_t uid = getuid();
  134. struct passwd *pw;
  135. char *retval = NULL;
  136. pw = getpwuid(uid);
  137. if ((pw != NULL) && (pw->pw_name != NULL))
  138. {
  139. retval = malloc(strlen(pw->pw_name) + 1);
  140. if (retval != NULL)
  141. strcpy(retval, pw->pw_name);
  142. } /* if */
  143. return(retval);
  144. } /* getUserNameByUID */
  145. static char *getUserDirByUID(void)
  146. {
  147. uid_t uid = getuid();
  148. struct passwd *pw;
  149. char *retval = NULL;
  150. pw = getpwuid(uid);
  151. if ((pw != NULL) && (pw->pw_dir != NULL))
  152. {
  153. retval = malloc(strlen(pw->pw_dir) + 1);
  154. if (retval != NULL)
  155. strcpy(retval, pw->pw_dir);
  156. } /* if */
  157. return(retval);
  158. } /* getUserDirByUID */
  159. char *__PHYSFS_platformGetUserName(void)
  160. {
  161. char *retval = getUserNameByUID();
  162. if (retval == NULL)
  163. retval = copyEnvironmentVariable("USER");
  164. return(retval);
  165. } /* __PHYSFS_platformGetUserName */
  166. char *__PHYSFS_platformGetUserDir(void)
  167. {
  168. char *retval = copyEnvironmentVariable("HOME");
  169. if (retval == NULL)
  170. retval = getUserDirByUID();
  171. return(retval);
  172. } /* __PHYSFS_platformGetUserDir */
  173. int __PHYSFS_platformGetThreadID(void)
  174. {
  175. return((int) pthread_self());
  176. } /* __PHYSFS_platformGetThreadID */
  177. /* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
  178. int __PHYSFS_platformStricmp(const char *x, const char *y)
  179. {
  180. int ux, uy;
  181. do
  182. {
  183. ux = toupper((int) *x);
  184. uy = toupper((int) *y);
  185. if (ux > uy)
  186. return(1);
  187. else if (ux < uy)
  188. return(-1);
  189. x++;
  190. y++;
  191. } while ((ux) && (uy));
  192. return(0);
  193. } /* __PHYSFS_platformStricmp */
  194. int __PHYSFS_platformExists(const char *fname)
  195. {
  196. struct stat statbuf;
  197. return(stat(fname, &statbuf) == 0);
  198. } /* __PHYSFS_platformExists */
  199. int __PHYSFS_platformIsSymLink(const char *fname)
  200. {
  201. #if (defined __PHYSFS_NO_SYMLINKS__)
  202. return(0);
  203. #else
  204. struct stat statbuf;
  205. int retval = 0;
  206. if (lstat(fname, &statbuf) == 0)
  207. {
  208. if (S_ISLNK(statbuf.st_mode))
  209. retval = 1;
  210. } /* if */
  211. return(retval);
  212. #endif
  213. } /* __PHYSFS_platformIsSymlink */
  214. int __PHYSFS_platformIsDirectory(const char *fname)
  215. {
  216. struct stat statbuf;
  217. int retval = 0;
  218. if (stat(fname, &statbuf) == 0)
  219. {
  220. if (S_ISDIR(statbuf.st_mode))
  221. retval = 1;
  222. } /* if */
  223. return(retval);
  224. } /* __PHYSFS_platformIsDirectory */
  225. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  226. const char *dirName,
  227. const char *append)
  228. {
  229. int len = ((prepend) ? strlen(prepend) : 0) +
  230. ((append) ? strlen(append) : 0) +
  231. strlen(dirName) + 1;
  232. char *retval = malloc(len);
  233. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  234. /* platform-independent notation is Unix-style already. :) */
  235. if (prepend)
  236. strcpy(retval, prepend);
  237. else
  238. retval[0] = '\0';
  239. strcat(retval, dirName);
  240. if (append)
  241. strcat(retval, append);
  242. return(retval);
  243. } /* __PHYSFS_platformCvtToDependent */
  244. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  245. void __PHYSFS_platformTimeslice(void)
  246. {
  247. struct timespec napTime;
  248. napTime.tv_sec = 0;
  249. napTime.tv_nsec = 10 * 1000 * 1000; /* specified in nanoseconds. */
  250. nanosleep(&napTime, NULL); /* don't care if it fails. */
  251. } /* __PHYSFS_platformTimeslice */
  252. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  253. int omitSymLinks)
  254. {
  255. LinkedStringList *retval = NULL;
  256. LinkedStringList *l = NULL;
  257. LinkedStringList *prev = NULL;
  258. DIR *dir;
  259. struct dirent *ent;
  260. int bufsize = 0;
  261. char *buf = NULL;
  262. int dlen = 0;
  263. if (omitSymLinks)
  264. {
  265. dlen = strlen(dirname);
  266. bufsize = dlen + 256;
  267. buf = malloc(bufsize);
  268. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  269. strcpy(buf, dirname);
  270. if (buf[dlen - 1] != '/')
  271. {
  272. buf[dlen++] = '/';
  273. buf[dlen] = '\0';
  274. } /* if */
  275. } /* if */
  276. errno = 0;
  277. dir = opendir(dirname);
  278. if (dir == NULL)
  279. {
  280. if (buf != NULL)
  281. free(buf);
  282. BAIL_IF_MACRO(1, strerror(errno), NULL);
  283. } /* if */
  284. while (1)
  285. {
  286. ent = readdir(dir);
  287. if (ent == NULL) /* we're done. */
  288. break;
  289. if (strcmp(ent->d_name, ".") == 0)
  290. continue;
  291. if (strcmp(ent->d_name, "..") == 0)
  292. continue;
  293. if (omitSymLinks)
  294. {
  295. char *p;
  296. int len = strlen(ent->d_name) + dlen + 1;
  297. if (len > bufsize)
  298. {
  299. p = realloc(buf, len);
  300. if (p == NULL)
  301. continue;
  302. buf = p;
  303. bufsize = len;
  304. } /* if */
  305. strcpy(buf + dlen, ent->d_name);
  306. if (__PHYSFS_platformIsSymLink(buf))
  307. continue;
  308. } /* if */
  309. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  310. if (l == NULL)
  311. break;
  312. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  313. if (l->str == NULL)
  314. {
  315. free(l);
  316. break;
  317. } /* if */
  318. strcpy(l->str, ent->d_name);
  319. if (retval == NULL)
  320. retval = l;
  321. else
  322. prev->next = l;
  323. prev = l;
  324. l->next = NULL;
  325. } /* while */
  326. if (buf != NULL)
  327. free(buf);
  328. closedir(dir);
  329. return(retval);
  330. } /* __PHYSFS_platformEnumerateFiles */
  331. int __PHYSFS_platformFileLength(FILE *handle)
  332. {
  333. struct stat statbuf;
  334. errno = 0;
  335. BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
  336. return(statbuf.st_size);
  337. } /* __PHYSFS_platformFileLength */
  338. char *__PHYSFS_platformCurrentDir(void)
  339. {
  340. int allocSize = 0;
  341. char *retval = NULL;
  342. char *ptr;
  343. do
  344. {
  345. allocSize += 100;
  346. ptr = (char *) realloc(retval, allocSize);
  347. if (ptr == NULL)
  348. {
  349. if (retval != NULL)
  350. free(retval);
  351. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  352. } /* if */
  353. retval = ptr;
  354. ptr = getcwd(retval, allocSize);
  355. } while (ptr == NULL && errno == ERANGE);
  356. if(ptr == NULL && errno) {
  357. /* getcwd() failed for some reason, for example current
  358. * directory not existing.
  359. */
  360. if (retval != NULL)
  361. free(retval);
  362. BAIL_IF_MACRO(1, ERR_NO_SUCH_FILE, NULL);
  363. }
  364. return(retval);
  365. } /* __PHYSFS_platformCurrentDir */
  366. char *__PHYSFS_platformRealPath(const char *path)
  367. {
  368. char resolved_path[MAXPATHLEN];
  369. char *retval = NULL;
  370. errno = 0;
  371. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  372. retval = malloc(strlen(resolved_path) + 1);
  373. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  374. strcpy(retval, resolved_path);
  375. return(retval);
  376. } /* __PHYSFS_platformRealPath */
  377. int __PHYSFS_platformMkDir(const char *path)
  378. {
  379. int rc;
  380. errno = 0;
  381. rc = mkdir(path, S_IRWXU);
  382. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  383. return(1);
  384. } /* __PHYSFS_platformMkDir */
  385. /* end of unix.c ... */