unix.c 12 KB

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