unix.c 16 KB

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