posix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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) && (!defined OS2))
  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. #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 *) allocator.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 *) allocator.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 *) allocator.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((ux > uy) ? 1 : -1);
  110. x++;
  111. y++;
  112. } while ((ux) && (uy));
  113. return(0);
  114. } /* __PHYSFS_platformStricmp */
  115. int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
  116. {
  117. int ux, uy;
  118. if (!len)
  119. return(0);
  120. do
  121. {
  122. ux = toupper((int) *x);
  123. uy = toupper((int) *y);
  124. if (ux != uy)
  125. return((ux > uy) ? 1 : -1);
  126. x++;
  127. y++;
  128. len--;
  129. } while ((ux) && (uy) && (len));
  130. return(0);
  131. } /* __PHYSFS_platformStrnicmp */
  132. #if (defined __PHYSFS_NO_SYMLINKS__)
  133. #define doStat stat
  134. #else
  135. #define doStat lstat
  136. #endif
  137. int __PHYSFS_platformExists(const char *fname)
  138. {
  139. struct stat statbuf;
  140. BAIL_IF_MACRO(doStat(fname, &statbuf) == -1, strerror(errno), 0);
  141. return(1);
  142. } /* __PHYSFS_platformExists */
  143. int __PHYSFS_platformIsSymLink(const char *fname)
  144. {
  145. #if (defined __PHYSFS_NO_SYMLINKS__)
  146. return(0);
  147. #else
  148. struct stat statbuf;
  149. BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
  150. return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 );
  151. #endif
  152. } /* __PHYSFS_platformIsSymlink */
  153. int __PHYSFS_platformIsDirectory(const char *fname)
  154. {
  155. struct stat statbuf;
  156. BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0);
  157. return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 );
  158. } /* __PHYSFS_platformIsDirectory */
  159. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  160. const char *dirName,
  161. const char *append)
  162. {
  163. int len = ((prepend) ? strlen(prepend) : 0) +
  164. ((append) ? strlen(append) : 0) +
  165. strlen(dirName) + 1;
  166. char *retval = (char *) allocator.Malloc(len);
  167. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  168. /* platform-independent notation is Unix-style already. :) */
  169. if (prepend)
  170. strcpy(retval, prepend);
  171. else
  172. retval[0] = '\0';
  173. strcat(retval, dirName);
  174. if (append)
  175. strcat(retval, append);
  176. return(retval);
  177. } /* __PHYSFS_platformCvtToDependent */
  178. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  179. int omitSymLinks,
  180. PHYSFS_EnumFilesCallback callback,
  181. const char *origdir,
  182. void *callbackdata)
  183. {
  184. DIR *dir;
  185. struct dirent *ent;
  186. int bufsize = 0;
  187. char *buf = NULL;
  188. int dlen = 0;
  189. if (omitSymLinks) /* !!! FIXME: this malloc sucks. */
  190. {
  191. dlen = strlen(dirname);
  192. bufsize = dlen + 256;
  193. buf = (char *) allocator.Malloc(bufsize);
  194. if (buf == NULL)
  195. return;
  196. strcpy(buf, dirname);
  197. if (buf[dlen - 1] != '/')
  198. {
  199. buf[dlen++] = '/';
  200. buf[dlen] = '\0';
  201. } /* if */
  202. } /* if */
  203. errno = 0;
  204. dir = opendir(dirname);
  205. if (dir == NULL)
  206. {
  207. if (buf != NULL)
  208. allocator.Free(buf);
  209. return;
  210. } /* if */
  211. while ((ent = readdir(dir)) != NULL)
  212. {
  213. if (strcmp(ent->d_name, ".") == 0)
  214. continue;
  215. if (strcmp(ent->d_name, "..") == 0)
  216. continue;
  217. if (omitSymLinks)
  218. {
  219. char *p;
  220. int len = strlen(ent->d_name) + dlen + 1;
  221. if (len > bufsize)
  222. {
  223. p = (char *) allocator.Realloc(buf, len);
  224. if (p == NULL)
  225. continue;
  226. buf = p;
  227. bufsize = len;
  228. } /* if */
  229. strcpy(buf + dlen, ent->d_name);
  230. if (__PHYSFS_platformIsSymLink(buf))
  231. continue;
  232. } /* if */
  233. callback(callbackdata, origdir, ent->d_name);
  234. } /* while */
  235. if (buf != NULL)
  236. allocator.Free(buf);
  237. closedir(dir);
  238. } /* __PHYSFS_platformEnumerateFiles */
  239. char *__PHYSFS_platformCurrentDir(void)
  240. {
  241. int allocSize = 0;
  242. char *retval = NULL;
  243. char *ptr;
  244. do
  245. {
  246. allocSize += 100;
  247. ptr = (char *) allocator.Realloc(retval, allocSize);
  248. if (ptr == NULL)
  249. {
  250. if (retval != NULL)
  251. allocator.Free(retval);
  252. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  253. } /* if */
  254. retval = ptr;
  255. ptr = getcwd(retval, allocSize);
  256. } while (ptr == NULL && errno == ERANGE);
  257. if (ptr == NULL && errno)
  258. {
  259. /*
  260. * getcwd() failed for some reason, for example current
  261. * directory not existing.
  262. */
  263. if (retval != NULL)
  264. allocator.Free(retval);
  265. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  266. } /* if */
  267. return(retval);
  268. } /* __PHYSFS_platformCurrentDir */
  269. int __PHYSFS_platformMkDir(const char *path)
  270. {
  271. int rc;
  272. errno = 0;
  273. rc = mkdir(path, S_IRWXU);
  274. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  275. return(1);
  276. } /* __PHYSFS_platformMkDir */
  277. static void *doOpen(const char *filename, int mode)
  278. {
  279. int fd;
  280. int *retval;
  281. errno = 0;
  282. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  283. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  284. retval = (int *) allocator.Malloc(sizeof (int));
  285. if (retval == NULL)
  286. {
  287. close(fd);
  288. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  289. } /* if */
  290. *retval = fd;
  291. return((void *) retval);
  292. } /* doOpen */
  293. void *__PHYSFS_platformOpenRead(const char *filename)
  294. {
  295. return(doOpen(filename, O_RDONLY));
  296. } /* __PHYSFS_platformOpenRead */
  297. void *__PHYSFS_platformOpenWrite(const char *filename)
  298. {
  299. return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
  300. } /* __PHYSFS_platformOpenWrite */
  301. void *__PHYSFS_platformOpenAppend(const char *filename)
  302. {
  303. return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
  304. } /* __PHYSFS_platformOpenAppend */
  305. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  306. PHYSFS_uint32 size, PHYSFS_uint32 count)
  307. {
  308. int fd = *((int *) opaque);
  309. int max = size * count;
  310. int rc = read(fd, 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_platformRead */
  317. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  318. PHYSFS_uint32 size, PHYSFS_uint32 count)
  319. {
  320. int fd = *((int *) opaque);
  321. int max = size * count;
  322. int rc = write(fd, (void *) buffer, max);
  323. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  324. assert(rc <= max);
  325. if ((rc < max) && (size > 1))
  326. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  327. return(rc / size);
  328. } /* __PHYSFS_platformWrite */
  329. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  330. {
  331. int fd = *((int *) opaque);
  332. #ifdef PHYSFS_HAVE_LLSEEK
  333. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  334. unsigned long offset_low = (pos & 0xFFFFFFFF);
  335. loff_t retoffset;
  336. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  337. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  338. #else
  339. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  340. #endif
  341. return(1);
  342. } /* __PHYSFS_platformSeek */
  343. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  344. {
  345. int fd = *((int *) opaque);
  346. PHYSFS_sint64 retval;
  347. #ifdef PHYSFS_HAVE_LLSEEK
  348. loff_t retoffset;
  349. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  350. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  351. retval = (PHYSFS_sint64) retoffset;
  352. #else
  353. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  354. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  355. #endif
  356. return(retval);
  357. } /* __PHYSFS_platformTell */
  358. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  359. {
  360. int fd = *((int *) opaque);
  361. struct stat statbuf;
  362. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  363. return((PHYSFS_sint64) statbuf.st_size);
  364. } /* __PHYSFS_platformFileLength */
  365. int __PHYSFS_platformEOF(void *opaque)
  366. {
  367. PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
  368. PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
  369. return(pos >= len);
  370. } /* __PHYSFS_platformEOF */
  371. int __PHYSFS_platformFlush(void *opaque)
  372. {
  373. int fd = *((int *) opaque);
  374. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  375. return(1);
  376. } /* __PHYSFS_platformFlush */
  377. int __PHYSFS_platformClose(void *opaque)
  378. {
  379. int fd = *((int *) opaque);
  380. BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
  381. allocator.Free(opaque);
  382. return(1);
  383. } /* __PHYSFS_platformClose */
  384. int __PHYSFS_platformDelete(const char *path)
  385. {
  386. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  387. return(1);
  388. } /* __PHYSFS_platformDelete */
  389. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  390. {
  391. struct stat statbuf;
  392. BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
  393. return statbuf.st_mtime;
  394. } /* __PHYSFS_platformGetLastModTime */
  395. int __PHYSFS_platformAllocatorInit(void)
  396. {
  397. return(1); /* always succeeds. */
  398. } /* __PHYSFS_platformAllocatorInit */
  399. void __PHYSFS_platformAllocatorDeinit(void)
  400. {
  401. /* no-op */
  402. } /* __PHYSFS_platformAllocatorInit */
  403. void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
  404. {
  405. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  406. #undef malloc
  407. return(malloc((size_t) s));
  408. } /* __PHYSFS_platformMalloc */
  409. void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  410. {
  411. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  412. #undef realloc
  413. return(realloc(ptr, (size_t) s));
  414. } /* __PHYSFS_platformRealloc */
  415. void __PHYSFS_platformAllocatorFree(void *ptr)
  416. {
  417. #undef free
  418. free(ptr);
  419. } /* __PHYSFS_platformAllocatorFree */
  420. #endif /* !defined WIN32 */
  421. /* end of posix.c ... */