unix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Unix 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 ((defined __APPLE__) && (defined __MACH__))
  9. # if (!defined __DARWIN__)
  10. # define __DARWIN__
  11. # endif
  12. #endif
  13. #if (defined __STRICT_ANSI__)
  14. #define __PHYSFS_DOING_STRICT_ANSI__
  15. #endif
  16. /*
  17. * We cheat a little: I want the symlink version of stat() (lstat), and
  18. * GCC/Linux will not declare it if compiled with the -ansi flag.
  19. * If you are really lacking symlink support on your platform,
  20. * you should #define __PHYSFS_NO_SYMLINKS__ before compiling this
  21. * file. That will open a security hole, though, if you really DO have
  22. * symlinks on your platform; it renders PHYSFS_permitSymbolicLinks(0)
  23. * useless, since every symlink will be reported as a regular file/dir.
  24. */
  25. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  26. #undef __STRICT_ANSI__
  27. #endif
  28. #include <stdio.h>
  29. #if (defined __PHYSFS_DOING_STRICT_ANSI__)
  30. #define __STRICT_ANSI__
  31. #endif
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <pthread.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <pwd.h>
  39. #include <sys/stat.h>
  40. #include <sys/param.h>
  41. #include <dirent.h>
  42. #include <time.h>
  43. #include <errno.h>
  44. #if (!defined __DARWIN__)
  45. #include <mntent.h>
  46. #else
  47. #include <sys/ucred.h>
  48. #endif
  49. #include <sys/mount.h>
  50. #define __PHYSICSFS_INTERNAL__
  51. #include "physfs_internal.h"
  52. const char *__PHYSFS_platformDirSeparator = "/";
  53. int __PHYSFS_platformInit(void)
  54. {
  55. return(1); /* always succeed. */
  56. } /* __PHYSFS_platformInit */
  57. int __PHYSFS_platformDeinit(void)
  58. {
  59. return(1); /* always succeed. */
  60. } /* __PHYSFS_platformDeinit */
  61. #if (defined __DARWIN__)
  62. char **__PHYSFS_platformDetectAvailableCDs(void)
  63. {
  64. char **retval = (char **) malloc(sizeof (char *));
  65. int cd_count = 1; /* We count the NULL entry. */
  66. struct statfs* mntbufp = NULL;
  67. int mounts;
  68. int ii;
  69. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  70. mounts = getmntinfo( &mntbufp, MNT_WAIT );
  71. for ( ii=0; ii < mounts; ++ii ) {
  72. int add_it = 0;
  73. if ( strcmp( mntbufp[ii].f_fstypename, "iso9660") == 0 )
  74. add_it = 1;
  75. /* !!! other mount types? */
  76. if (add_it)
  77. {
  78. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  79. if (tmp)
  80. {
  81. retval = tmp;
  82. retval[cd_count-1] = (char *)
  83. malloc(strlen(mntbufp[ ii ].f_mntonname) + 1);
  84. if (retval[cd_count-1])
  85. {
  86. strcpy(retval[cd_count-1], mntbufp[ ii ].f_mntonname);
  87. cd_count++;
  88. } /* if */
  89. } /* if */
  90. } /* if */
  91. }
  92. free( mntbufp );
  93. retval[cd_count - 1] = NULL;
  94. return(retval);
  95. } /* __PHYSFS_platformDetectAvailableCDs */
  96. #else /* non-Darwin implementation... */
  97. char **__PHYSFS_platformDetectAvailableCDs(void)
  98. {
  99. char **retval = (char **) malloc(sizeof (char *));
  100. int cd_count = 1; /* We count the NULL entry. */
  101. FILE *mounts = NULL;
  102. struct mntent *ent = NULL;
  103. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  104. *retval = NULL;
  105. mounts = setmntent("/etc/mtab", "r");
  106. BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
  107. while ( (ent = getmntent(mounts)) != NULL )
  108. {
  109. int add_it = 0;
  110. if (strcmp(ent->mnt_type, "iso9660") == 0)
  111. add_it = 1;
  112. /* !!! other mount types? */
  113. if (add_it)
  114. {
  115. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  116. if (tmp)
  117. {
  118. retval = tmp;
  119. retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
  120. if (retval[cd_count-1])
  121. {
  122. strcpy(retval[cd_count-1], ent->mnt_dir);
  123. cd_count++;
  124. } /* if */
  125. } /* if */
  126. } /* if */
  127. } /* while */
  128. endmntent(mounts);
  129. retval[cd_count - 1] = NULL;
  130. return(retval);
  131. } /* __PHYSFS_platformDetectAvailableCDs */
  132. #endif
  133. static char *copyEnvironmentVariable(const char *varname)
  134. {
  135. const char *envr = getenv(varname);
  136. char *retval = NULL;
  137. if (envr != NULL)
  138. {
  139. retval = malloc(strlen(envr) + 1);
  140. if (retval != NULL)
  141. strcpy(retval, envr);
  142. } /* if */
  143. return(retval);
  144. } /* copyEnvironmentVariable */
  145. /* !!! this is ugly. */
  146. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  147. {
  148. /* If there isn't a path on argv0, then look through the $PATH for it. */
  149. char *retval = NULL;
  150. char *envr;
  151. char *start;
  152. char *ptr;
  153. char *exe;
  154. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  155. return(NULL);
  156. envr = copyEnvironmentVariable("PATH");
  157. BAIL_IF_MACRO(!envr, NULL, NULL);
  158. start = envr;
  159. do
  160. {
  161. ptr = strchr(start, ':');
  162. if (ptr)
  163. *ptr = '\0';
  164. exe = (char *) malloc(strlen(start) + strlen(argv0) + 2);
  165. if (!exe)
  166. {
  167. free(envr);
  168. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  169. } /* if */
  170. strcpy(exe, start);
  171. if (exe[strlen(exe) - 1] != '/')
  172. strcat(exe, "/");
  173. strcat(exe, argv0);
  174. if (access(exe, X_OK) != 0)
  175. free(exe);
  176. else
  177. {
  178. retval = exe;
  179. strcpy(retval, start); /* i'm lazy. piss off. */
  180. break;
  181. } /* else */
  182. start = ptr + 1;
  183. } while (ptr != NULL);
  184. free(envr);
  185. return(retval);
  186. } /* __PHYSFS_platformCalcBaseDir */
  187. static char *getUserNameByUID(void)
  188. {
  189. uid_t uid = getuid();
  190. struct passwd *pw;
  191. char *retval = NULL;
  192. pw = getpwuid(uid);
  193. if ((pw != NULL) && (pw->pw_name != NULL))
  194. {
  195. retval = malloc(strlen(pw->pw_name) + 1);
  196. if (retval != NULL)
  197. strcpy(retval, pw->pw_name);
  198. } /* if */
  199. return(retval);
  200. } /* getUserNameByUID */
  201. static char *getUserDirByUID(void)
  202. {
  203. uid_t uid = getuid();
  204. struct passwd *pw;
  205. char *retval = NULL;
  206. pw = getpwuid(uid);
  207. if ((pw != NULL) && (pw->pw_dir != NULL))
  208. {
  209. retval = malloc(strlen(pw->pw_dir) + 1);
  210. if (retval != NULL)
  211. strcpy(retval, pw->pw_dir);
  212. } /* if */
  213. return(retval);
  214. } /* getUserDirByUID */
  215. char *__PHYSFS_platformGetUserName(void)
  216. {
  217. char *retval = getUserNameByUID();
  218. if (retval == NULL)
  219. retval = copyEnvironmentVariable("USER");
  220. return(retval);
  221. } /* __PHYSFS_platformGetUserName */
  222. char *__PHYSFS_platformGetUserDir(void)
  223. {
  224. char *retval = copyEnvironmentVariable("HOME");
  225. if (retval == NULL)
  226. retval = getUserDirByUID();
  227. return(retval);
  228. } /* __PHYSFS_platformGetUserDir */
  229. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  230. {
  231. return((PHYSFS_uint64) ((PHYSFS_uint32) pthread_self()));
  232. } /* __PHYSFS_platformGetThreadID */
  233. /* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
  234. int __PHYSFS_platformStricmp(const char *x, const char *y)
  235. {
  236. int ux, uy;
  237. do
  238. {
  239. ux = toupper((int) *x);
  240. uy = toupper((int) *y);
  241. if (ux > uy)
  242. return(1);
  243. else if (ux < uy)
  244. return(-1);
  245. x++;
  246. y++;
  247. } while ((ux) && (uy));
  248. return(0);
  249. } /* __PHYSFS_platformStricmp */
  250. int __PHYSFS_platformExists(const char *fname)
  251. {
  252. struct stat statbuf;
  253. return(stat(fname, &statbuf) == 0);
  254. } /* __PHYSFS_platformExists */
  255. int __PHYSFS_platformIsSymLink(const char *fname)
  256. {
  257. #if (defined __PHYSFS_NO_SYMLINKS__)
  258. return(0);
  259. #else
  260. struct stat statbuf;
  261. int retval = 0;
  262. if (lstat(fname, &statbuf) == 0)
  263. {
  264. if (S_ISLNK(statbuf.st_mode))
  265. retval = 1;
  266. } /* if */
  267. return(retval);
  268. #endif
  269. } /* __PHYSFS_platformIsSymlink */
  270. int __PHYSFS_platformIsDirectory(const char *fname)
  271. {
  272. struct stat statbuf;
  273. int retval = 0;
  274. if (stat(fname, &statbuf) == 0)
  275. {
  276. if (S_ISDIR(statbuf.st_mode))
  277. retval = 1;
  278. } /* if */
  279. return(retval);
  280. } /* __PHYSFS_platformIsDirectory */
  281. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  282. const char *dirName,
  283. const char *append)
  284. {
  285. int len = ((prepend) ? strlen(prepend) : 0) +
  286. ((append) ? strlen(append) : 0) +
  287. strlen(dirName) + 1;
  288. char *retval = malloc(len);
  289. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  290. /* platform-independent notation is Unix-style already. :) */
  291. if (prepend)
  292. strcpy(retval, prepend);
  293. else
  294. retval[0] = '\0';
  295. strcat(retval, dirName);
  296. if (append)
  297. strcat(retval, append);
  298. return(retval);
  299. } /* __PHYSFS_platformCvtToDependent */
  300. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  301. void __PHYSFS_platformTimeslice(void)
  302. {
  303. usleep( 10 * 1000 ); /* don't care if it fails. */
  304. } /* __PHYSFS_platformTimeslice */
  305. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  306. int omitSymLinks)
  307. {
  308. LinkedStringList *retval = NULL;
  309. LinkedStringList *l = NULL;
  310. LinkedStringList *prev = NULL;
  311. DIR *dir;
  312. struct dirent *ent;
  313. int bufsize = 0;
  314. char *buf = NULL;
  315. int dlen = 0;
  316. if (omitSymLinks)
  317. {
  318. dlen = strlen(dirname);
  319. bufsize = dlen + 256;
  320. buf = malloc(bufsize);
  321. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  322. strcpy(buf, dirname);
  323. if (buf[dlen - 1] != '/')
  324. {
  325. buf[dlen++] = '/';
  326. buf[dlen] = '\0';
  327. } /* if */
  328. } /* if */
  329. errno = 0;
  330. dir = opendir(dirname);
  331. if (dir == NULL)
  332. {
  333. if (buf != NULL)
  334. free(buf);
  335. BAIL_IF_MACRO(1, strerror(errno), NULL);
  336. } /* if */
  337. while (1)
  338. {
  339. ent = readdir(dir);
  340. if (ent == NULL) /* we're done. */
  341. break;
  342. if (strcmp(ent->d_name, ".") == 0)
  343. continue;
  344. if (strcmp(ent->d_name, "..") == 0)
  345. continue;
  346. if (omitSymLinks)
  347. {
  348. char *p;
  349. int len = strlen(ent->d_name) + dlen + 1;
  350. if (len > bufsize)
  351. {
  352. p = realloc(buf, len);
  353. if (p == NULL)
  354. continue;
  355. buf = p;
  356. bufsize = len;
  357. } /* if */
  358. strcpy(buf + dlen, ent->d_name);
  359. if (__PHYSFS_platformIsSymLink(buf))
  360. continue;
  361. } /* if */
  362. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  363. if (l == NULL)
  364. break;
  365. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  366. if (l->str == NULL)
  367. {
  368. free(l);
  369. break;
  370. } /* if */
  371. strcpy(l->str, ent->d_name);
  372. if (retval == NULL)
  373. retval = l;
  374. else
  375. prev->next = l;
  376. prev = l;
  377. l->next = NULL;
  378. } /* while */
  379. if (buf != NULL)
  380. free(buf);
  381. closedir(dir);
  382. return(retval);
  383. } /* __PHYSFS_platformEnumerateFiles */
  384. char *__PHYSFS_platformCurrentDir(void)
  385. {
  386. int allocSize = 0;
  387. char *retval = NULL;
  388. char *ptr;
  389. do
  390. {
  391. allocSize += 100;
  392. ptr = (char *) realloc(retval, allocSize);
  393. if (ptr == NULL)
  394. {
  395. if (retval != NULL)
  396. free(retval);
  397. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  398. } /* if */
  399. retval = ptr;
  400. ptr = getcwd(retval, allocSize);
  401. } while (ptr == NULL && errno == ERANGE);
  402. if (ptr == NULL && errno)
  403. {
  404. /*
  405. * getcwd() failed for some reason, for example current
  406. * directory not existing.
  407. */
  408. if (retval != NULL)
  409. free(retval);
  410. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  411. } /* if */
  412. return(retval);
  413. } /* __PHYSFS_platformCurrentDir */
  414. char *__PHYSFS_platformRealPath(const char *path)
  415. {
  416. char resolved_path[MAXPATHLEN];
  417. char *retval = NULL;
  418. errno = 0;
  419. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  420. retval = malloc(strlen(resolved_path) + 1);
  421. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  422. strcpy(retval, resolved_path);
  423. return(retval);
  424. } /* __PHYSFS_platformRealPath */
  425. int __PHYSFS_platformMkDir(const char *path)
  426. {
  427. int rc;
  428. errno = 0;
  429. rc = mkdir(path, S_IRWXU);
  430. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  431. return(1);
  432. } /* __PHYSFS_platformMkDir */
  433. static void *doOpen(const char *filename, const char *mode)
  434. {
  435. FILE *retval;
  436. errno = 0;
  437. retval = fopen(filename, mode);
  438. if (retval == NULL)
  439. __PHYSFS_setError(strerror(errno));
  440. return((void *) retval);
  441. } /* doOpen */
  442. void *__PHYSFS_platformOpenRead(const char *filename)
  443. {
  444. return(doOpen(filename, "rb"));
  445. } /* __PHYSFS_platformOpenRead */
  446. void *__PHYSFS_platformOpenWrite(const char *filename)
  447. {
  448. return(doOpen(filename, "wb"));
  449. } /* __PHYSFS_platformOpenWrite */
  450. void *__PHYSFS_platformOpenAppend(const char *filename)
  451. {
  452. return(doOpen(filename, "wb+"));
  453. } /* __PHYSFS_platformOpenAppend */
  454. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  455. PHYSFS_uint32 size, PHYSFS_uint32 count)
  456. {
  457. FILE *io = (FILE *) opaque;
  458. int rc = fread(buffer, size, count, io);
  459. if (rc < count)
  460. {
  461. int err = errno;
  462. BAIL_IF_MACRO(ferror(io), strerror(err), rc);
  463. BAIL_MACRO(ERR_PAST_EOF, rc);
  464. } /* if */
  465. return(rc);
  466. } /* __PHYSFS_platformRead */
  467. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  468. PHYSFS_uint32 size, PHYSFS_uint32 count)
  469. {
  470. FILE *io = (FILE *) opaque;
  471. int rc = fwrite((void *) buffer, size, count, io);
  472. if (rc < count)
  473. __PHYSFS_setError(strerror(errno));
  474. return(rc);
  475. } /* __PHYSFS_platformWrite */
  476. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  477. {
  478. FILE *io = (FILE *) opaque;
  479. /* !!! FIXME: Use llseek where available. */
  480. errno = 0;
  481. BAIL_IF_MACRO(fseek(io, pos, SEEK_SET) != 0, strerror(errno), 0);
  482. return(1);
  483. } /* __PHYSFS_platformSeek */
  484. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  485. {
  486. FILE *io = (FILE *) opaque;
  487. PHYSFS_sint64 retval = ftell(io);
  488. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  489. return(retval);
  490. } /* __PHYSFS_platformTell */
  491. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  492. {
  493. FILE *io = (FILE *) opaque;
  494. struct stat statbuf;
  495. errno = 0;
  496. BAIL_IF_MACRO(fstat(fileno(io), &statbuf) == -1, strerror(errno), -1);
  497. return((PHYSFS_sint64) statbuf.st_size);
  498. } /* __PHYSFS_platformFileLength */
  499. int __PHYSFS_platformEOF(void *opaque)
  500. {
  501. return(feof((FILE *) opaque));
  502. } /* __PHYSFS_platformEOF */
  503. int __PHYSFS_platformFlush(void *opaque)
  504. {
  505. errno = 0;
  506. BAIL_IF_MACRO(fflush((FILE *) opaque) == EOF, strerror(errno), 0);
  507. return(1);
  508. } /* __PHYSFS_platformFlush */
  509. int __PHYSFS_platformClose(void *opaque)
  510. {
  511. errno = 0;
  512. BAIL_IF_MACRO(fclose((FILE *) opaque) == EOF, strerror(errno), 0);
  513. return(1);
  514. } /* __PHYSFS_platformClose */
  515. int __PHYSFS_platformDelete(const char *path)
  516. {
  517. errno = 0;
  518. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  519. return(1);
  520. } /* __PHYSFS_platformDelete */
  521. void *__PHYSFS_platformCreateMutex(void)
  522. {
  523. int rc;
  524. pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
  525. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  526. rc = pthread_mutex_init(m, NULL);
  527. if (rc != 0)
  528. {
  529. free(m);
  530. BAIL_MACRO(strerror(rc), NULL);
  531. } /* if */
  532. return((void *) m);
  533. } /* __PHYSFS_platformCreateMutex */
  534. void __PHYSFS_platformDestroyMutex(void *mutex)
  535. {
  536. pthread_mutex_destroy((pthread_mutex_t *) mutex);
  537. free(mutex);
  538. } /* __PHYSFS_platformDestroyMutex */
  539. int __PHYSFS_platformGrabMutex(void *mutex)
  540. {
  541. return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
  542. } /* __PHYSFS_platformGrabMutex */
  543. void __PHYSFS_platformReleaseMutex(void *mutex)
  544. {
  545. pthread_mutex_unlock((pthread_mutex_t *) mutex);
  546. } /* __PHYSFS_platformReleaseMutex */
  547. /* end of unix.c ... */