posix.c 11 KB

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