posix.c 13 KB

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