posix.c 11 KB

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