unix.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #define __PHYSICSFS_INTERNAL__
  40. #include "physfs_internal.h"
  41. const char *__PHYSFS_platformDirSeparator = "/";
  42. char **__PHYSFS_platformDetectAvailableCDs(void)
  43. {
  44. /* !!! write me. */
  45. char **retval = malloc(sizeof (char *));
  46. if (retval != NULL)
  47. *retval = NULL;
  48. return(retval);
  49. } /* __PHYSFS_detectAvailableCDs */
  50. static char *copyEnvironmentVariable(const char *varname)
  51. {
  52. const char *envr = getenv(varname);
  53. char *retval = NULL;
  54. if (envr != NULL)
  55. {
  56. retval = malloc(strlen(envr) + 1);
  57. if (retval != NULL)
  58. strcpy(retval, envr);
  59. } /* if */
  60. return(retval);
  61. } /* copyEnvironmentVariable */
  62. /* !!! this is ugly. */
  63. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  64. {
  65. /* If there isn't a path on argv0, then look through the $PATH for it. */
  66. char *retval = NULL;
  67. char *envr;
  68. char *start;
  69. char *ptr;
  70. char *exe;
  71. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  72. return(NULL);
  73. envr = copyEnvironmentVariable("PATH");
  74. BAIL_IF_MACRO(!envr, NULL, NULL);
  75. start = envr;
  76. do
  77. {
  78. ptr = strchr(start, ':');
  79. if (ptr)
  80. *ptr = '\0';
  81. exe = (char *) malloc(strlen(start) + strlen(argv0) + 2);
  82. if (!exe)
  83. {
  84. free(envr);
  85. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  86. } /* if */
  87. strcpy(exe, start);
  88. if (start[strlen(start) - 1] != '/')
  89. strcat(start, "/");
  90. strcat(start, argv0);
  91. if (access(exe, X_OK) != 0)
  92. free(exe);
  93. else
  94. {
  95. retval = exe;
  96. strcpy(retval, start); /* i'm lazy. piss off. */
  97. break;
  98. } /* else */
  99. start = ptr + 1;
  100. } while (ptr != NULL);
  101. free(envr);
  102. return(retval);
  103. } /* __PHYSFS_platformCalcBaseDir */
  104. static char *getUserNameByUID(void)
  105. {
  106. uid_t uid = getuid();
  107. struct passwd *pw;
  108. char *retval = NULL;
  109. pw = getpwuid(uid);
  110. if ((pw != NULL) && (pw->pw_name != NULL))
  111. {
  112. retval = malloc(strlen(pw->pw_name) + 1);
  113. if (retval != NULL)
  114. strcpy(retval, pw->pw_name);
  115. } /* if */
  116. return(retval);
  117. } /* getUserNameByUID */
  118. static char *getUserDirByUID(void)
  119. {
  120. uid_t uid = getuid();
  121. struct passwd *pw;
  122. char *retval = NULL;
  123. pw = getpwuid(uid);
  124. if ((pw != NULL) && (pw->pw_dir != NULL))
  125. {
  126. retval = malloc(strlen(pw->pw_dir) + 1);
  127. if (retval != NULL)
  128. strcpy(retval, pw->pw_dir);
  129. } /* if */
  130. return(retval);
  131. } /* getUserDirByUID */
  132. char *__PHYSFS_platformGetUserName(void)
  133. {
  134. char *retval = getUserNameByUID();
  135. if (retval == NULL)
  136. retval = copyEnvironmentVariable("USER");
  137. return(retval);
  138. } /* __PHYSFS_platformGetUserName */
  139. char *__PHYSFS_platformGetUserDir(void)
  140. {
  141. char *retval = copyEnvironmentVariable("HOME");
  142. if (retval == NULL)
  143. retval = getUserDirByUID();
  144. return(retval);
  145. } /* __PHYSFS_platformGetUserDir */
  146. int __PHYSFS_platformGetThreadID(void)
  147. {
  148. return((int) pthread_self());
  149. } /* __PHYSFS_platformGetThreadID */
  150. /* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
  151. int __PHYSFS_platformStricmp(const char *x, const char *y)
  152. {
  153. int ux, uy;
  154. do
  155. {
  156. ux = toupper((int) *x);
  157. uy = toupper((int) *y);
  158. if (ux > uy)
  159. return(1);
  160. else if (ux < uy)
  161. return(-1);
  162. x++;
  163. y++;
  164. } while ((ux) && (uy));
  165. return(0);
  166. } /* __PHYSFS_platformStricmp */
  167. int __PHYSFS_platformExists(const char *fname)
  168. {
  169. struct stat statbuf;
  170. return(stat(fname, &statbuf) == 0);
  171. } /* __PHYSFS_platformExists */
  172. int __PHYSFS_platformIsSymLink(const char *fname)
  173. {
  174. #if (defined __PHYSFS_NO_SYMLINKS__)
  175. return(0);
  176. #else
  177. struct stat statbuf;
  178. int retval = 0;
  179. if (lstat(fname, &statbuf) == 0)
  180. {
  181. if (S_ISLNK(statbuf.st_mode))
  182. retval = 1;
  183. } /* if */
  184. return(retval);
  185. #endif
  186. } /* __PHYSFS_platformIsSymlink */
  187. int __PHYSFS_platformIsDirectory(const char *fname)
  188. {
  189. struct stat statbuf;
  190. int retval = 0;
  191. if (stat(fname, &statbuf) == 0)
  192. {
  193. if (S_ISDIR(statbuf.st_mode))
  194. retval = 1;
  195. } /* if */
  196. return(retval);
  197. } /* __PHYSFS_platformIsDirectory */
  198. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  199. const char *dirName,
  200. const char *append)
  201. {
  202. int len = ((prepend) ? strlen(prepend) : 0) +
  203. ((append) ? strlen(append) : 0) +
  204. strlen(dirName) + 1;
  205. char *retval = malloc(len);
  206. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  207. /* platform-independent notation is Unix-style already. :) */
  208. if (prepend)
  209. strcpy(retval, prepend);
  210. else
  211. retval[0] = '\0';
  212. strcat(retval, dirName);
  213. if (append)
  214. strcat(retval, append);
  215. return(retval);
  216. } /* __PHYSFS_platformCvtToDependent */
  217. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  218. void __PHYSFS_platformTimeslice(void)
  219. {
  220. struct timespec napTime;
  221. napTime.tv_sec = 0;
  222. napTime.tv_nsec = 10 * 1000 * 1000; /* specified in nanoseconds. */
  223. nanosleep(&napTime, NULL); /* don't care if it fails. */
  224. } /* __PHYSFS_platformTimeslice */
  225. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  226. int omitSymLinks)
  227. {
  228. LinkedStringList *retval = NULL;
  229. LinkedStringList *l = NULL;
  230. LinkedStringList *prev = NULL;
  231. DIR *dir;
  232. struct dirent *ent;
  233. int bufsize = 0;
  234. char *buf = NULL;
  235. int dlen = 0;
  236. if (omitSymLinks)
  237. {
  238. dlen = strlen(dirname);
  239. bufsize = dlen + 256;
  240. buf = malloc(bufsize);
  241. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  242. strcpy(buf, dirname);
  243. if (buf[dlen - 1] != '/')
  244. {
  245. buf[dlen++] = '/';
  246. buf[dlen] = '\0';
  247. } /* if */
  248. } /* if */
  249. errno = 0;
  250. dir = opendir(dirname);
  251. if (dir == NULL)
  252. {
  253. if (buf != NULL)
  254. free(buf);
  255. BAIL_IF_MACRO(1, strerror(errno), NULL);
  256. } /* if */
  257. while (1)
  258. {
  259. ent = readdir(dir);
  260. if (ent == NULL) /* we're done. */
  261. break;
  262. if (strcmp(ent->d_name, ".") == 0)
  263. continue;
  264. if (strcmp(ent->d_name, "..") == 0)
  265. continue;
  266. if (omitSymLinks)
  267. {
  268. char *p;
  269. int len = strlen(ent->d_name) + dlen + 1;
  270. if (len > bufsize)
  271. {
  272. p = realloc(buf, len);
  273. if (p == NULL)
  274. continue;
  275. buf = p;
  276. bufsize = len;
  277. } /* if */
  278. strcpy(buf + dlen, ent->d_name);
  279. if (__PHYSFS_platformIsSymLink(buf))
  280. continue;
  281. } /* if */
  282. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  283. if (l == NULL)
  284. break;
  285. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  286. if (l->str == NULL)
  287. {
  288. free(l);
  289. break;
  290. } /* if */
  291. strcpy(l->str, ent->d_name);
  292. if (retval == NULL)
  293. retval = l;
  294. else
  295. prev->next = l;
  296. prev = l;
  297. l->next = NULL;
  298. } /* while */
  299. closedir(dir);
  300. free(buf);
  301. return(retval);
  302. } /* __PHYSFS_platformEnumerateFiles */
  303. int __PHYSFS_platformFileLength(FILE *handle)
  304. {
  305. struct stat statbuf;
  306. errno = 0;
  307. BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
  308. return(statbuf.st_size);
  309. } /* __PHYSFS_platformFileLength */
  310. char *__PHYSFS_platformCurrentDir(void)
  311. {
  312. int allocSize = 0;
  313. char *retval = NULL;
  314. char *ptr;
  315. do
  316. {
  317. allocSize += 100;
  318. ptr = (char *) realloc(retval, allocSize);
  319. if (ptr == NULL)
  320. {
  321. if (retval != NULL)
  322. free(retval);
  323. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  324. } /* if */
  325. retval = ptr;
  326. ptr = getcwd(retval, allocSize);
  327. } while (ptr == NULL);
  328. return(retval);
  329. } /* __PHYSFS_platformCurrentDir */
  330. char *__PHYSFS_platformRealPath(const char *path)
  331. {
  332. char resolved_path[MAXPATHLEN];
  333. char *retval = NULL;
  334. errno = 0;
  335. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  336. retval = malloc(strlen(resolved_path) + 1);
  337. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  338. strcpy(retval, resolved_path);
  339. return(retval);
  340. } /* __PHYSFS_platformRealPath */
  341. /* end of unix.c ... */