posix.c 13 KB

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