unix.c 16 KB

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