unix.c 16 KB

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