unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. usleep( 10 * 1000 ); /* don't care if it fails. */
  248. } /* __PHYSFS_platformTimeslice */
  249. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  250. int omitSymLinks)
  251. {
  252. LinkedStringList *retval = NULL;
  253. LinkedStringList *l = NULL;
  254. LinkedStringList *prev = NULL;
  255. DIR *dir;
  256. struct dirent *ent;
  257. int bufsize = 0;
  258. char *buf = NULL;
  259. int dlen = 0;
  260. if (omitSymLinks)
  261. {
  262. dlen = strlen(dirname);
  263. bufsize = dlen + 256;
  264. buf = malloc(bufsize);
  265. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  266. strcpy(buf, dirname);
  267. if (buf[dlen - 1] != '/')
  268. {
  269. buf[dlen++] = '/';
  270. buf[dlen] = '\0';
  271. } /* if */
  272. } /* if */
  273. errno = 0;
  274. dir = opendir(dirname);
  275. if (dir == NULL)
  276. {
  277. if (buf != NULL)
  278. free(buf);
  279. BAIL_IF_MACRO(1, strerror(errno), NULL);
  280. } /* if */
  281. while (1)
  282. {
  283. ent = readdir(dir);
  284. if (ent == NULL) /* we're done. */
  285. break;
  286. if (strcmp(ent->d_name, ".") == 0)
  287. continue;
  288. if (strcmp(ent->d_name, "..") == 0)
  289. continue;
  290. if (omitSymLinks)
  291. {
  292. char *p;
  293. int len = strlen(ent->d_name) + dlen + 1;
  294. if (len > bufsize)
  295. {
  296. p = realloc(buf, len);
  297. if (p == NULL)
  298. continue;
  299. buf = p;
  300. bufsize = len;
  301. } /* if */
  302. strcpy(buf + dlen, ent->d_name);
  303. if (__PHYSFS_platformIsSymLink(buf))
  304. continue;
  305. } /* if */
  306. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  307. if (l == NULL)
  308. break;
  309. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  310. if (l->str == NULL)
  311. {
  312. free(l);
  313. break;
  314. } /* if */
  315. strcpy(l->str, ent->d_name);
  316. if (retval == NULL)
  317. retval = l;
  318. else
  319. prev->next = l;
  320. prev = l;
  321. l->next = NULL;
  322. } /* while */
  323. if (buf != NULL)
  324. free(buf);
  325. closedir(dir);
  326. return(retval);
  327. } /* __PHYSFS_platformEnumerateFiles */
  328. int __PHYSFS_platformFileLength(FILE *handle)
  329. {
  330. struct stat statbuf;
  331. errno = 0;
  332. BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
  333. return(statbuf.st_size);
  334. } /* __PHYSFS_platformFileLength */
  335. char *__PHYSFS_platformCurrentDir(void)
  336. {
  337. int allocSize = 0;
  338. char *retval = NULL;
  339. char *ptr;
  340. do
  341. {
  342. allocSize += 100;
  343. ptr = (char *) realloc(retval, allocSize);
  344. if (ptr == NULL)
  345. {
  346. if (retval != NULL)
  347. free(retval);
  348. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  349. } /* if */
  350. retval = ptr;
  351. ptr = getcwd(retval, allocSize);
  352. } while (ptr == NULL && errno == ERANGE);
  353. if(ptr == NULL && errno) {
  354. /* getcwd() failed for some reason, for example current
  355. * directory not existing.
  356. */
  357. if (retval != NULL)
  358. free(retval);
  359. BAIL_IF_MACRO(1, ERR_NO_SUCH_FILE, NULL);
  360. }
  361. return(retval);
  362. } /* __PHYSFS_platformCurrentDir */
  363. char *__PHYSFS_platformRealPath(const char *path)
  364. {
  365. char resolved_path[MAXPATHLEN];
  366. char *retval = NULL;
  367. errno = 0;
  368. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  369. retval = malloc(strlen(resolved_path) + 1);
  370. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  371. strcpy(retval, resolved_path);
  372. return(retval);
  373. } /* __PHYSFS_platformRealPath */
  374. int __PHYSFS_platformMkDir(const char *path)
  375. {
  376. int rc;
  377. errno = 0;
  378. rc = mkdir(path, S_IRWXU);
  379. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  380. return(1);
  381. } /* __PHYSFS_platformMkDir */
  382. /* end of unix.c ... */