unix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. /* add other mount types here */
  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. /* add other mount types here */
  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. /*
  151. * See where program (bin) resides in the $PATH specified by (envr).
  152. * returns a copy of the first element in envr that contains it, or NULL
  153. * if it doesn't exist or there were other problems. PHYSFS_SetError() is
  154. * called if we have a problem.
  155. *
  156. * (envr) will be scribbled over, and you are expected to free() the
  157. * return value when you're done with it.
  158. */
  159. static char *findBinaryInPath(const char *bin, char *envr)
  160. {
  161. size_t alloc_size = 0;
  162. char *exe = NULL;
  163. char *start = envr;
  164. char *ptr;
  165. BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
  166. BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
  167. do
  168. {
  169. size_t size;
  170. ptr = strchr(start, ':'); /* find next $PATH separator. */
  171. if (ptr)
  172. *ptr = '\0';
  173. size = strlen(start) + strlen(bin) + 2;
  174. if (size > alloc_size)
  175. {
  176. char *x = (char *) realloc(exe, size);
  177. if (x == NULL)
  178. {
  179. if (exe != NULL)
  180. free(exe);
  181. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  182. } /* if */
  183. alloc_size = size;
  184. exe = x;
  185. } /* if */
  186. /* build full binary path... */
  187. strcpy(exe, start);
  188. if (exe[strlen(exe) - 1] != '/')
  189. strcat(exe, "/");
  190. strcat(exe, bin);
  191. if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
  192. {
  193. strcpy(exe, start); /* i'm lazy. piss off. */
  194. return(exe);
  195. } /* if */
  196. start = ptr + 1; /* start points to beginning of next element. */
  197. } while (ptr != NULL);
  198. if (exe != NULL)
  199. free(exe);
  200. return(NULL); /* doesn't exist in path. */
  201. } /* findBinaryInPath */
  202. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  203. {
  204. /* If there isn't a path on argv0, then look through the $PATH for it. */
  205. char *retval;
  206. char *envr;
  207. if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
  208. return(NULL);
  209. envr = copyEnvironmentVariable("PATH");
  210. BAIL_IF_MACRO(!envr, NULL, NULL);
  211. retval = findBinaryInPath(argv0, envr);
  212. free(envr);
  213. return(retval);
  214. } /* __PHYSFS_platformCalcBaseDir */
  215. static char *getUserNameByUID(void)
  216. {
  217. uid_t uid = getuid();
  218. struct passwd *pw;
  219. char *retval = NULL;
  220. pw = getpwuid(uid);
  221. if ((pw != NULL) && (pw->pw_name != NULL))
  222. {
  223. retval = malloc(strlen(pw->pw_name) + 1);
  224. if (retval != NULL)
  225. strcpy(retval, pw->pw_name);
  226. } /* if */
  227. return(retval);
  228. } /* getUserNameByUID */
  229. static char *getUserDirByUID(void)
  230. {
  231. uid_t uid = getuid();
  232. struct passwd *pw;
  233. char *retval = NULL;
  234. pw = getpwuid(uid);
  235. if ((pw != NULL) && (pw->pw_dir != NULL))
  236. {
  237. retval = malloc(strlen(pw->pw_dir) + 1);
  238. if (retval != NULL)
  239. strcpy(retval, pw->pw_dir);
  240. } /* if */
  241. return(retval);
  242. } /* getUserDirByUID */
  243. char *__PHYSFS_platformGetUserName(void)
  244. {
  245. char *retval = getUserNameByUID();
  246. if (retval == NULL)
  247. retval = copyEnvironmentVariable("USER");
  248. return(retval);
  249. } /* __PHYSFS_platformGetUserName */
  250. char *__PHYSFS_platformGetUserDir(void)
  251. {
  252. char *retval = copyEnvironmentVariable("HOME");
  253. if (retval == NULL)
  254. retval = getUserDirByUID();
  255. return(retval);
  256. } /* __PHYSFS_platformGetUserDir */
  257. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  258. {
  259. return((PHYSFS_uint64) ((PHYSFS_uint32) pthread_self()));
  260. } /* __PHYSFS_platformGetThreadID */
  261. /* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */
  262. int __PHYSFS_platformStricmp(const char *x, const char *y)
  263. {
  264. int ux, uy;
  265. do
  266. {
  267. ux = toupper((int) *x);
  268. uy = toupper((int) *y);
  269. if (ux > uy)
  270. return(1);
  271. else if (ux < uy)
  272. return(-1);
  273. x++;
  274. y++;
  275. } while ((ux) && (uy));
  276. return(0);
  277. } /* __PHYSFS_platformStricmp */
  278. int __PHYSFS_platformExists(const char *fname)
  279. {
  280. struct stat statbuf;
  281. return(stat(fname, &statbuf) == 0);
  282. } /* __PHYSFS_platformExists */
  283. int __PHYSFS_platformIsSymLink(const char *fname)
  284. {
  285. #if (defined __PHYSFS_NO_SYMLINKS__)
  286. return(0);
  287. #else
  288. struct stat statbuf;
  289. int retval = 0;
  290. if (lstat(fname, &statbuf) == 0)
  291. {
  292. if (S_ISLNK(statbuf.st_mode))
  293. retval = 1;
  294. } /* if */
  295. return(retval);
  296. #endif
  297. } /* __PHYSFS_platformIsSymlink */
  298. int __PHYSFS_platformIsDirectory(const char *fname)
  299. {
  300. struct stat statbuf;
  301. int retval = 0;
  302. if (stat(fname, &statbuf) == 0)
  303. {
  304. if (S_ISDIR(statbuf.st_mode))
  305. retval = 1;
  306. } /* if */
  307. return(retval);
  308. } /* __PHYSFS_platformIsDirectory */
  309. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  310. const char *dirName,
  311. const char *append)
  312. {
  313. int len = ((prepend) ? strlen(prepend) : 0) +
  314. ((append) ? strlen(append) : 0) +
  315. strlen(dirName) + 1;
  316. char *retval = malloc(len);
  317. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  318. /* platform-independent notation is Unix-style already. :) */
  319. if (prepend)
  320. strcpy(retval, prepend);
  321. else
  322. retval[0] = '\0';
  323. strcat(retval, dirName);
  324. if (append)
  325. strcat(retval, append);
  326. return(retval);
  327. } /* __PHYSFS_platformCvtToDependent */
  328. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  329. void __PHYSFS_platformTimeslice(void)
  330. {
  331. usleep( 10 * 1000 ); /* don't care if it fails. */
  332. } /* __PHYSFS_platformTimeslice */
  333. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  334. int omitSymLinks)
  335. {
  336. LinkedStringList *retval = NULL;
  337. LinkedStringList *l = NULL;
  338. LinkedStringList *prev = NULL;
  339. DIR *dir;
  340. struct dirent *ent;
  341. int bufsize = 0;
  342. char *buf = NULL;
  343. int dlen = 0;
  344. if (omitSymLinks)
  345. {
  346. dlen = strlen(dirname);
  347. bufsize = dlen + 256;
  348. buf = malloc(bufsize);
  349. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
  350. strcpy(buf, dirname);
  351. if (buf[dlen - 1] != '/')
  352. {
  353. buf[dlen++] = '/';
  354. buf[dlen] = '\0';
  355. } /* if */
  356. } /* if */
  357. errno = 0;
  358. dir = opendir(dirname);
  359. if (dir == NULL)
  360. {
  361. if (buf != NULL)
  362. free(buf);
  363. BAIL_IF_MACRO(1, strerror(errno), NULL);
  364. } /* if */
  365. while (1)
  366. {
  367. ent = readdir(dir);
  368. if (ent == NULL) /* we're done. */
  369. break;
  370. if (strcmp(ent->d_name, ".") == 0)
  371. continue;
  372. if (strcmp(ent->d_name, "..") == 0)
  373. continue;
  374. if (omitSymLinks)
  375. {
  376. char *p;
  377. int len = strlen(ent->d_name) + dlen + 1;
  378. if (len > bufsize)
  379. {
  380. p = realloc(buf, len);
  381. if (p == NULL)
  382. continue;
  383. buf = p;
  384. bufsize = len;
  385. } /* if */
  386. strcpy(buf + dlen, ent->d_name);
  387. if (__PHYSFS_platformIsSymLink(buf))
  388. continue;
  389. } /* if */
  390. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  391. if (l == NULL)
  392. break;
  393. l->str = (char *) malloc(strlen(ent->d_name) + 1);
  394. if (l->str == NULL)
  395. {
  396. free(l);
  397. break;
  398. } /* if */
  399. strcpy(l->str, ent->d_name);
  400. if (retval == NULL)
  401. retval = l;
  402. else
  403. prev->next = l;
  404. prev = l;
  405. l->next = NULL;
  406. } /* while */
  407. if (buf != NULL)
  408. free(buf);
  409. closedir(dir);
  410. return(retval);
  411. } /* __PHYSFS_platformEnumerateFiles */
  412. char *__PHYSFS_platformCurrentDir(void)
  413. {
  414. int allocSize = 0;
  415. char *retval = NULL;
  416. char *ptr;
  417. do
  418. {
  419. allocSize += 100;
  420. ptr = (char *) realloc(retval, allocSize);
  421. if (ptr == NULL)
  422. {
  423. if (retval != NULL)
  424. free(retval);
  425. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  426. } /* if */
  427. retval = ptr;
  428. ptr = getcwd(retval, allocSize);
  429. } while (ptr == NULL && errno == ERANGE);
  430. if (ptr == NULL && errno)
  431. {
  432. /*
  433. * getcwd() failed for some reason, for example current
  434. * directory not existing.
  435. */
  436. if (retval != NULL)
  437. free(retval);
  438. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  439. } /* if */
  440. return(retval);
  441. } /* __PHYSFS_platformCurrentDir */
  442. char *__PHYSFS_platformRealPath(const char *path)
  443. {
  444. char resolved_path[MAXPATHLEN];
  445. char *retval = NULL;
  446. errno = 0;
  447. BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
  448. retval = malloc(strlen(resolved_path) + 1);
  449. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  450. strcpy(retval, resolved_path);
  451. return(retval);
  452. } /* __PHYSFS_platformRealPath */
  453. int __PHYSFS_platformMkDir(const char *path)
  454. {
  455. int rc;
  456. errno = 0;
  457. rc = mkdir(path, S_IRWXU);
  458. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  459. return(1);
  460. } /* __PHYSFS_platformMkDir */
  461. static void *doOpen(const char *filename, const char *mode)
  462. {
  463. FILE *retval;
  464. errno = 0;
  465. retval = fopen(filename, mode);
  466. if (retval == NULL)
  467. __PHYSFS_setError(strerror(errno));
  468. return((void *) retval);
  469. } /* doOpen */
  470. void *__PHYSFS_platformOpenRead(const char *filename)
  471. {
  472. return(doOpen(filename, "rb"));
  473. } /* __PHYSFS_platformOpenRead */
  474. void *__PHYSFS_platformOpenWrite(const char *filename)
  475. {
  476. return(doOpen(filename, "wb"));
  477. } /* __PHYSFS_platformOpenWrite */
  478. void *__PHYSFS_platformOpenAppend(const char *filename)
  479. {
  480. return(doOpen(filename, "ab"));
  481. } /* __PHYSFS_platformOpenAppend */
  482. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  483. PHYSFS_uint32 size, PHYSFS_uint32 count)
  484. {
  485. FILE *io = (FILE *) opaque;
  486. int rc = fread(buffer, size, count, io);
  487. if (rc < count)
  488. {
  489. int err = errno;
  490. BAIL_IF_MACRO(ferror(io), strerror(err), rc);
  491. BAIL_MACRO(ERR_PAST_EOF, rc);
  492. } /* if */
  493. return(rc);
  494. } /* __PHYSFS_platformRead */
  495. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  496. PHYSFS_uint32 size, PHYSFS_uint32 count)
  497. {
  498. FILE *io = (FILE *) opaque;
  499. int rc = fwrite((void *) buffer, size, count, io);
  500. if (rc < count)
  501. __PHYSFS_setError(strerror(errno));
  502. return(rc);
  503. } /* __PHYSFS_platformWrite */
  504. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  505. {
  506. FILE *io = (FILE *) opaque;
  507. /* !!! FIXME: Use llseek where available. */
  508. errno = 0;
  509. BAIL_IF_MACRO(fseek(io, pos, SEEK_SET) != 0, strerror(errno), 0);
  510. return(1);
  511. } /* __PHYSFS_platformSeek */
  512. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  513. {
  514. FILE *io = (FILE *) opaque;
  515. PHYSFS_sint64 retval = ftell(io);
  516. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  517. return(retval);
  518. } /* __PHYSFS_platformTell */
  519. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  520. {
  521. FILE *io = (FILE *) opaque;
  522. struct stat statbuf;
  523. errno = 0;
  524. BAIL_IF_MACRO(fstat(fileno(io), &statbuf) == -1, strerror(errno), -1);
  525. return((PHYSFS_sint64) statbuf.st_size);
  526. } /* __PHYSFS_platformFileLength */
  527. int __PHYSFS_platformEOF(void *opaque)
  528. {
  529. return(feof((FILE *) opaque));
  530. } /* __PHYSFS_platformEOF */
  531. int __PHYSFS_platformFlush(void *opaque)
  532. {
  533. errno = 0;
  534. BAIL_IF_MACRO(fflush((FILE *) opaque) == EOF, strerror(errno), 0);
  535. return(1);
  536. } /* __PHYSFS_platformFlush */
  537. int __PHYSFS_platformClose(void *opaque)
  538. {
  539. errno = 0;
  540. BAIL_IF_MACRO(fclose((FILE *) opaque) == EOF, strerror(errno), 0);
  541. return(1);
  542. } /* __PHYSFS_platformClose */
  543. int __PHYSFS_platformDelete(const char *path)
  544. {
  545. errno = 0;
  546. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  547. return(1);
  548. } /* __PHYSFS_platformDelete */
  549. void *__PHYSFS_platformCreateMutex(void)
  550. {
  551. int rc;
  552. pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
  553. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  554. rc = pthread_mutex_init(m, NULL);
  555. if (rc != 0)
  556. {
  557. free(m);
  558. BAIL_MACRO(strerror(rc), NULL);
  559. } /* if */
  560. return((void *) m);
  561. } /* __PHYSFS_platformCreateMutex */
  562. void __PHYSFS_platformDestroyMutex(void *mutex)
  563. {
  564. pthread_mutex_destroy((pthread_mutex_t *) mutex);
  565. free(mutex);
  566. } /* __PHYSFS_platformDestroyMutex */
  567. int __PHYSFS_platformGrabMutex(void *mutex)
  568. {
  569. return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
  570. } /* __PHYSFS_platformGrabMutex */
  571. void __PHYSFS_platformReleaseMutex(void *mutex)
  572. {
  573. pthread_mutex_unlock((pthread_mutex_t *) mutex);
  574. } /* __PHYSFS_platformReleaseMutex */
  575. /* end of unix.c ... */