unix.c 16 KB

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