posix.c 11 KB

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