posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 <ctype.h>
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <pwd.h>
  36. #include <sys/stat.h>
  37. #include <sys/param.h>
  38. #include <dirent.h>
  39. #include <time.h>
  40. #include <errno.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 = 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 = 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 = 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 = 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 = 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, const char *mode)
  282. {
  283. FILE *retval;
  284. errno = 0;
  285. retval = fopen(filename, mode);
  286. if (retval == NULL)
  287. __PHYSFS_setError(strerror(errno));
  288. return((void *) retval);
  289. } /* doOpen */
  290. void *__PHYSFS_platformOpenRead(const char *filename)
  291. {
  292. return(doOpen(filename, "rb"));
  293. } /* __PHYSFS_platformOpenRead */
  294. void *__PHYSFS_platformOpenWrite(const char *filename)
  295. {
  296. return(doOpen(filename, "wb"));
  297. } /* __PHYSFS_platformOpenWrite */
  298. void *__PHYSFS_platformOpenAppend(const char *filename)
  299. {
  300. return(doOpen(filename, "ab"));
  301. } /* __PHYSFS_platformOpenAppend */
  302. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  303. PHYSFS_uint32 size, PHYSFS_uint32 count)
  304. {
  305. FILE *io = (FILE *) opaque;
  306. int rc = fread(buffer, size, count, io);
  307. if (rc < count)
  308. {
  309. int err = errno;
  310. BAIL_IF_MACRO(ferror(io), strerror(err), rc);
  311. BAIL_MACRO(ERR_PAST_EOF, rc);
  312. } /* if */
  313. return(rc);
  314. } /* __PHYSFS_platformRead */
  315. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  316. PHYSFS_uint32 size, PHYSFS_uint32 count)
  317. {
  318. FILE *io = (FILE *) opaque;
  319. int rc = fwrite((void *) buffer, size, count, io);
  320. if (rc < count)
  321. __PHYSFS_setError(strerror(errno));
  322. return(rc);
  323. } /* __PHYSFS_platformWrite */
  324. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  325. {
  326. FILE *io = (FILE *) opaque;
  327. /* !!! FIXME: Use llseek where available. */
  328. errno = 0;
  329. BAIL_IF_MACRO(fseek(io, pos, SEEK_SET) != 0, strerror(errno), 0);
  330. return(1);
  331. } /* __PHYSFS_platformSeek */
  332. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  333. {
  334. FILE *io = (FILE *) opaque;
  335. PHYSFS_sint64 retval = ftell(io);
  336. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  337. return(retval);
  338. } /* __PHYSFS_platformTell */
  339. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  340. {
  341. FILE *io = (FILE *) opaque;
  342. struct stat statbuf;
  343. errno = 0;
  344. BAIL_IF_MACRO(fstat(fileno(io), &statbuf) == -1, strerror(errno), -1);
  345. return((PHYSFS_sint64) statbuf.st_size);
  346. } /* __PHYSFS_platformFileLength */
  347. int __PHYSFS_platformEOF(void *opaque)
  348. {
  349. return(feof((FILE *) opaque));
  350. } /* __PHYSFS_platformEOF */
  351. int __PHYSFS_platformFlush(void *opaque)
  352. {
  353. errno = 0;
  354. BAIL_IF_MACRO(fflush((FILE *) opaque) == EOF, strerror(errno), 0);
  355. return(1);
  356. } /* __PHYSFS_platformFlush */
  357. int __PHYSFS_platformClose(void *opaque)
  358. {
  359. errno = 0;
  360. BAIL_IF_MACRO(fclose((FILE *) opaque) == EOF, strerror(errno), 0);
  361. return(1);
  362. } /* __PHYSFS_platformClose */
  363. int __PHYSFS_platformDelete(const char *path)
  364. {
  365. errno = 0;
  366. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  367. return(1);
  368. } /* __PHYSFS_platformDelete */
  369. /* end of posix.c ... */