posix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Posix-esque 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 HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #if (defined __STRICT_ANSI__)
  12. #define __PHYSFS_DOING_STRICT_ANSI__
  13. #endif
  14. /*
  15. * We cheat a little: I want the symlink version of stat() (lstat), and
  16. * GCC/Linux will not declare it if compiled with the -ansi flag.
  17. * If you are really lacking symlink support on your platform,
  18. * you should #define __PHYSFS_NO_SYMLINKS__ before compiling this
  19. * file. That will open a security hole, though, if you really DO have
  20. * symlinks on your platform; it renders PHYSFS_permitSymbolicLinks(0)
  21. * useless, since every symlink will be reported as a regular file/dir.
  22. */
  23. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  24. #undef __STRICT_ANSI__
  25. #endif
  26. #include <stdio.h>
  27. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  28. #define __STRICT_ANSI__
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <ctype.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <pwd.h>
  37. #include <dirent.h>
  38. #include <errno.h>
  39. #include <fcntl.h>
  40. #include <assert.h>
  41. #ifdef PHYSFS_HAVE_LLSEEK
  42. #include <linux/unistd.h>
  43. #endif
  44. #define __PHYSICSFS_INTERNAL__
  45. #include "physfs_internal.h"
  46. char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
  47. {
  48. const char *envr = getenv(varname);
  49. char *retval = NULL;
  50. if (envr != NULL)
  51. {
  52. retval = (char *) malloc(strlen(envr) + 1);
  53. if (retval != NULL)
  54. strcpy(retval, envr);
  55. } /* if */
  56. return(retval);
  57. } /* __PHYSFS_platformCopyEnvironmentVariable */
  58. static char *getUserNameByUID(void)
  59. {
  60. uid_t uid = getuid();
  61. struct passwd *pw;
  62. char *retval = NULL;
  63. pw = getpwuid(uid);
  64. if ((pw != NULL) && (pw->pw_name != NULL))
  65. {
  66. retval = (char *) malloc(strlen(pw->pw_name) + 1);
  67. if (retval != NULL)
  68. strcpy(retval, pw->pw_name);
  69. } /* if */
  70. return(retval);
  71. } /* getUserNameByUID */
  72. static char *getUserDirByUID(void)
  73. {
  74. uid_t uid = getuid();
  75. struct passwd *pw;
  76. char *retval = NULL;
  77. pw = getpwuid(uid);
  78. if ((pw != NULL) && (pw->pw_dir != NULL))
  79. {
  80. retval = (char *) malloc(strlen(pw->pw_dir) + 1);
  81. if (retval != NULL)
  82. strcpy(retval, pw->pw_dir);
  83. } /* if */
  84. return(retval);
  85. } /* getUserDirByUID */
  86. char *__PHYSFS_platformGetUserName(void)
  87. {
  88. char *retval = getUserNameByUID();
  89. if (retval == NULL)
  90. retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
  91. return(retval);
  92. } /* __PHYSFS_platformGetUserName */
  93. char *__PHYSFS_platformGetUserDir(void)
  94. {
  95. char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
  96. if (retval == NULL)
  97. retval = getUserDirByUID();
  98. return(retval);
  99. } /* __PHYSFS_platformGetUserDir */
  100. /* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
  101. int __PHYSFS_platformStricmp(const char *x, const char *y)
  102. {
  103. int ux, uy;
  104. do
  105. {
  106. ux = toupper((int) *x);
  107. uy = toupper((int) *y);
  108. if (ux > uy)
  109. return(1);
  110. else if (ux < uy)
  111. return(-1);
  112. x++;
  113. y++;
  114. } while ((ux) && (uy));
  115. return(0);
  116. } /* __PHYSFS_platformStricmp */
  117. int __PHYSFS_platformExists(const char *fname)
  118. {
  119. struct stat statbuf;
  120. return(stat(fname, &statbuf) == 0);
  121. } /* __PHYSFS_platformExists */
  122. int __PHYSFS_platformIsSymLink(const char *fname)
  123. {
  124. #if (defined __PHYSFS_NO_SYMLINKS__)
  125. return(0);
  126. #else
  127. struct stat statbuf;
  128. int retval = 0;
  129. if (lstat(fname, &statbuf) == 0)
  130. {
  131. if (S_ISLNK(statbuf.st_mode))
  132. retval = 1;
  133. } /* if */
  134. return(retval);
  135. #endif
  136. } /* __PHYSFS_platformIsSymlink */
  137. int __PHYSFS_platformIsDirectory(const char *fname)
  138. {
  139. struct stat statbuf;
  140. int retval = 0;
  141. if (stat(fname, &statbuf) == 0)
  142. {
  143. if (S_ISDIR(statbuf.st_mode))
  144. retval = 1;
  145. } /* if */
  146. return(retval);
  147. } /* __PHYSFS_platformIsDirectory */
  148. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  149. const char *dirName,
  150. const char *append)
  151. {
  152. int len = ((prepend) ? strlen(prepend) : 0) +
  153. ((append) ? strlen(append) : 0) +
  154. strlen(dirName) + 1;
  155. char *retval = (char *) malloc(len);
  156. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  157. /* platform-independent notation is Unix-style already. :) */
  158. if (prepend)
  159. strcpy(retval, prepend);
  160. else
  161. retval[0] = '\0';
  162. strcat(retval, dirName);
  163. if (append)
  164. strcat(retval, append);
  165. return(retval);
  166. } /* __PHYSFS_platformCvtToDependent */
  167. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  168. int omitSymLinks)
  169. {
  170. LinkedStringList *retval = NULL;
  171. LinkedStringList *l = NULL;
  172. LinkedStringList *prev = NULL;
  173. DIR *dir;
  174. struct dirent *ent;
  175. int bufsize = 0;
  176. char *buf = NULL;
  177. int dlen = 0;
  178. if (omitSymLinks)
  179. {
  180. dlen = strlen(dirname);
  181. bufsize = dlen + 256;
  182. buf = (char *) malloc(bufsize);
  183. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  184. strcpy(buf, dirname);
  185. if (buf[dlen - 1] != '/')
  186. {
  187. buf[dlen++] = '/';
  188. buf[dlen] = '\0';
  189. } /* if */
  190. } /* if */
  191. errno = 0;
  192. dir = opendir(dirname);
  193. if (dir == NULL)
  194. {
  195. if (buf != NULL)
  196. free(buf);
  197. BAIL_IF_MACRO(1, strerror(errno), NULL);
  198. } /* if */
  199. while (1)
  200. {
  201. ent = readdir(dir);
  202. if (ent == NULL) /* we're done. */
  203. break;
  204. if (strcmp(ent->d_name, ".") == 0)
  205. continue;
  206. if (strcmp(ent->d_name, "..") == 0)
  207. continue;
  208. if (omitSymLinks)
  209. {
  210. char *p;
  211. int len = strlen(ent->d_name) + dlen + 1;
  212. if (len > bufsize)
  213. {
  214. p = realloc(buf, len);
  215. if (p == NULL)
  216. continue;
  217. buf = p;
  218. bufsize = len;
  219. } /* if */
  220. strcpy(buf + dlen, ent->d_name);
  221. if (__PHYSFS_platformIsSymLink(buf))
  222. continue;
  223. } /* if */
  224. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  225. if (l == NULL)
  226. break;
  227. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  228. if (l->str == NULL)
  229. {
  230. free(l);
  231. break;
  232. } /* if */
  233. strcpy(l->str, ent->d_name);
  234. if (retval == NULL)
  235. retval = l;
  236. else
  237. prev->next = l;
  238. prev = l;
  239. l->next = NULL;
  240. } /* while */
  241. if (buf != NULL)
  242. free(buf);
  243. closedir(dir);
  244. return(retval);
  245. } /* __PHYSFS_platformEnumerateFiles */
  246. char *__PHYSFS_platformCurrentDir(void)
  247. {
  248. int allocSize = 0;
  249. char *retval = NULL;
  250. char *ptr;
  251. do
  252. {
  253. allocSize += 100;
  254. ptr = (char *) realloc(retval, allocSize);
  255. if (ptr == NULL)
  256. {
  257. if (retval != NULL)
  258. 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. free(retval);
  272. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  273. } /* if */
  274. return(retval);
  275. } /* __PHYSFS_platformCurrentDir */
  276. int __PHYSFS_platformMkDir(const char *path)
  277. {
  278. int rc;
  279. errno = 0;
  280. rc = mkdir(path, S_IRWXU);
  281. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  282. return(1);
  283. } /* __PHYSFS_platformMkDir */
  284. static void *doOpen(const char *filename, int mode)
  285. {
  286. int fd;
  287. int *retval;
  288. errno = 0;
  289. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  290. if (fd < 0)
  291. __PHYSFS_setError(strerror(errno));
  292. retval = (int *) malloc(sizeof (int));
  293. if (retval == NULL)
  294. {
  295. close(fd);
  296. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  297. } /* if */
  298. *retval = fd;
  299. return((void *) retval);
  300. } /* doOpen */
  301. void *__PHYSFS_platformOpenRead(const char *filename)
  302. {
  303. return(doOpen(filename, O_RDONLY));
  304. } /* __PHYSFS_platformOpenRead */
  305. void *__PHYSFS_platformOpenWrite(const char *filename)
  306. {
  307. return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
  308. } /* __PHYSFS_platformOpenWrite */
  309. void *__PHYSFS_platformOpenAppend(const char *filename)
  310. {
  311. return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
  312. } /* __PHYSFS_platformOpenAppend */
  313. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  314. PHYSFS_uint32 size, PHYSFS_uint32 count)
  315. {
  316. int fd = *((int *) opaque);
  317. int max = size * count;
  318. int rc = read(fd, buffer, max);
  319. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  320. assert(rc <= max);
  321. if ((rc < max) && (size > 1))
  322. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  323. return(rc / size);
  324. } /* __PHYSFS_platformRead */
  325. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  326. PHYSFS_uint32 size, PHYSFS_uint32 count)
  327. {
  328. int fd = *((int *) opaque);
  329. int max = size * count;
  330. int rc = write(fd, (void *) buffer, max);
  331. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  332. assert(rc <= max);
  333. if ((rc < max) && (size > 1))
  334. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  335. return(rc / size);
  336. } /* __PHYSFS_platformWrite */
  337. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  338. {
  339. int fd = *((int *) opaque);
  340. #ifdef PHYSFS_HAVE_LLSEEK
  341. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  342. unsigned long offset_low = (pos & 0xFFFFFFFF);
  343. loff_t retoffset;
  344. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  345. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  346. #else
  347. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  348. #endif
  349. return(1);
  350. } /* __PHYSFS_platformSeek */
  351. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  352. {
  353. int fd = *((int *) opaque);
  354. PHYSFS_sint64 retval;
  355. #ifdef PHYSFS_HAVE_LLSEEK
  356. loff_t retoffset;
  357. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  358. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  359. retval = (PHYSFS_sint64) retoffset;
  360. #else
  361. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  362. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  363. #endif
  364. return(retval);
  365. } /* __PHYSFS_platformTell */
  366. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  367. {
  368. int fd = *((int *) opaque);
  369. struct stat statbuf;
  370. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  371. return((PHYSFS_sint64) statbuf.st_size);
  372. } /* __PHYSFS_platformFileLength */
  373. int __PHYSFS_platformEOF(void *opaque)
  374. {
  375. PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
  376. PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
  377. return(pos >= len);
  378. } /* __PHYSFS_platformEOF */
  379. int __PHYSFS_platformFlush(void *opaque)
  380. {
  381. int fd = *((int *) opaque);
  382. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  383. return(1);
  384. } /* __PHYSFS_platformFlush */
  385. int __PHYSFS_platformClose(void *opaque)
  386. {
  387. int fd = *((int *) opaque);
  388. BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
  389. return(1);
  390. } /* __PHYSFS_platformClose */
  391. int __PHYSFS_platformDelete(const char *path)
  392. {
  393. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  394. return(1);
  395. } /* __PHYSFS_platformDelete */
  396. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  397. {
  398. struct stat statbuf;
  399. BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
  400. return statbuf.st_mtime;
  401. } /* __PHYSFS_platformGetLastModTime */
  402. /* end of posix.c ... */