posix.c 11 KB

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