utilunix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* Various utilities - Unix variants
  2. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2007 Free Software Foundation, Inc.
  4. Written 1994, 1995, 1996 by:
  5. Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
  6. Jakub Jelinek, Mauricio Plaza.
  7. The mc_realpath routine is mostly from uClibc package, written
  8. by Rick Sladkey <jrs@world.std.com>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  20. /** \file utilunix.c
  21. * \brief Source: various utilities - Unix variant
  22. */
  23. #include <config.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <signal.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include <sys/param.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <sys/wait.h>
  37. #ifdef HAVE_SYS_IOCTL_H
  38. # include <sys/ioctl.h>
  39. #endif
  40. #include <unistd.h>
  41. #include <pwd.h>
  42. #include <grp.h>
  43. #include "lib/global.h"
  44. #include "src/execute.h"
  45. #include "src/wtools.h" /* message() */
  46. struct sigaction startup_handler;
  47. #define UID_CACHE_SIZE 200
  48. #define GID_CACHE_SIZE 30
  49. typedef struct {
  50. int index;
  51. char *string;
  52. } int_cache;
  53. static int_cache uid_cache [UID_CACHE_SIZE];
  54. static int_cache gid_cache [GID_CACHE_SIZE];
  55. static char *i_cache_match (int id, int_cache *cache, int size)
  56. {
  57. int i;
  58. for (i = 0; i < size; i++)
  59. if (cache [i].index == id)
  60. return cache [i].string;
  61. return 0;
  62. }
  63. static void i_cache_add (int id, int_cache *cache, int size, char *text,
  64. int *last)
  65. {
  66. g_free (cache [*last].string);
  67. cache [*last].string = g_strdup (text);
  68. cache [*last].index = id;
  69. *last = ((*last)+1) % size;
  70. }
  71. char *get_owner (int uid)
  72. {
  73. struct passwd *pwd;
  74. static char ibuf [10];
  75. char *name;
  76. static int uid_last;
  77. if ((name = i_cache_match (uid, uid_cache, UID_CACHE_SIZE)) != NULL)
  78. return name;
  79. pwd = getpwuid (uid);
  80. if (pwd){
  81. i_cache_add (uid, uid_cache, UID_CACHE_SIZE, pwd->pw_name, &uid_last);
  82. return pwd->pw_name;
  83. }
  84. else {
  85. g_snprintf (ibuf, sizeof (ibuf), "%d", uid);
  86. return ibuf;
  87. }
  88. }
  89. char *get_group (int gid)
  90. {
  91. struct group *grp;
  92. static char gbuf [10];
  93. char *name;
  94. static int gid_last;
  95. if ((name = i_cache_match (gid, gid_cache, GID_CACHE_SIZE)) != NULL)
  96. return name;
  97. grp = getgrgid (gid);
  98. if (grp){
  99. i_cache_add (gid, gid_cache, GID_CACHE_SIZE, grp->gr_name, &gid_last);
  100. return grp->gr_name;
  101. } else {
  102. g_snprintf (gbuf, sizeof (gbuf), "%d", gid);
  103. return gbuf;
  104. }
  105. }
  106. /* Since ncurses uses a handler that automatically refreshes the */
  107. /* screen after a SIGCONT, and we don't want this behavior when */
  108. /* spawning a child, we save the original handler here */
  109. void save_stop_handler (void)
  110. {
  111. sigaction (SIGTSTP, NULL, &startup_handler);
  112. }
  113. int my_system (int flags, const char *shell, const char *command)
  114. {
  115. struct sigaction ignore, save_intr, save_quit, save_stop;
  116. pid_t pid;
  117. int status = 0;
  118. ignore.sa_handler = SIG_IGN;
  119. sigemptyset (&ignore.sa_mask);
  120. ignore.sa_flags = 0;
  121. sigaction (SIGINT, &ignore, &save_intr);
  122. sigaction (SIGQUIT, &ignore, &save_quit);
  123. /* Restore the original SIGTSTP handler, we don't want ncurses' */
  124. /* handler messing the screen after the SIGCONT */
  125. sigaction (SIGTSTP, &startup_handler, &save_stop);
  126. if ((pid = fork ()) < 0){
  127. fprintf (stderr, "\n\nfork () = -1\n");
  128. return -1;
  129. }
  130. if (pid == 0){
  131. signal (SIGINT, SIG_DFL);
  132. signal (SIGQUIT, SIG_DFL);
  133. signal (SIGTSTP, SIG_DFL);
  134. signal (SIGCHLD, SIG_DFL);
  135. if (flags & EXECUTE_AS_SHELL)
  136. execl (shell, shell, "-c", command, (char *) NULL);
  137. else
  138. {
  139. gchar **shell_tokens;
  140. const gchar *only_cmd;
  141. shell_tokens = g_strsplit(shell," ", 2);
  142. if (shell_tokens == NULL)
  143. only_cmd = shell;
  144. else
  145. only_cmd = (*shell_tokens) ? *shell_tokens: shell;
  146. execlp (only_cmd, shell, command, (char *) NULL);
  147. /*
  148. execlp will replace current process,
  149. therefore no sence in call of g_strfreev().
  150. But this keeped for estetic reason :)
  151. */
  152. g_strfreev(shell_tokens);
  153. }
  154. _exit (127); /* Exec error */
  155. } else {
  156. while (waitpid (pid, &status, 0) < 0)
  157. if (errno != EINTR){
  158. status = -1;
  159. break;
  160. }
  161. }
  162. sigaction (SIGINT, &save_intr, NULL);
  163. sigaction (SIGQUIT, &save_quit, NULL);
  164. sigaction (SIGTSTP, &save_stop, NULL);
  165. return WEXITSTATUS(status);
  166. }
  167. /*
  168. * Perform tilde expansion if possible.
  169. * Always return a newly allocated string, even if it's unchanged.
  170. */
  171. char *
  172. tilde_expand (const char *directory)
  173. {
  174. struct passwd *passwd;
  175. const char *p, *q;
  176. char *name;
  177. if (*directory != '~')
  178. return g_strdup (directory);
  179. p = directory + 1;
  180. /* d = "~" or d = "~/" */
  181. if (!(*p) || (*p == PATH_SEP)) {
  182. passwd = getpwuid (geteuid ());
  183. q = (*p == PATH_SEP) ? p + 1 : "";
  184. } else {
  185. q = strchr (p, PATH_SEP);
  186. if (!q) {
  187. passwd = getpwnam (p);
  188. } else {
  189. name = g_strndup (p, q - p);
  190. passwd = getpwnam (name);
  191. q++;
  192. g_free (name);
  193. }
  194. }
  195. /* If we can't figure the user name, leave tilde unexpanded */
  196. if (!passwd)
  197. return g_strdup (directory);
  198. return g_strconcat (passwd->pw_dir, PATH_SEP_STR, q, (char *) NULL);
  199. }
  200. /*
  201. * Return the directory where mc should keep its temporary files.
  202. * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
  203. * When called the first time, the directory is created if needed.
  204. * The first call should be done early, since we are using fprintf()
  205. * and not message() to report possible problems.
  206. */
  207. const char *
  208. mc_tmpdir (void)
  209. {
  210. static char buffer[64];
  211. static const char *tmpdir;
  212. const char *sys_tmp;
  213. struct passwd *pwd;
  214. struct stat st;
  215. const char *error = NULL;
  216. /* Check if already correctly initialized */
  217. if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
  218. st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
  219. return tmpdir;
  220. sys_tmp = getenv ("TMPDIR");
  221. if (!sys_tmp || sys_tmp[0] != '/') {
  222. sys_tmp = TMPDIR_DEFAULT;
  223. }
  224. pwd = getpwuid (getuid ());
  225. if (pwd)
  226. g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp,
  227. pwd->pw_name);
  228. else
  229. g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp,
  230. (unsigned long) getuid ());
  231. canonicalize_pathname (buffer);
  232. if (lstat (buffer, &st) == 0) {
  233. /* Sanity check for existing directory */
  234. if (!S_ISDIR (st.st_mode))
  235. error = _("%s is not a directory\n");
  236. else if (st.st_uid != getuid ())
  237. error = _("Directory %s is not owned by you\n");
  238. else if (((st.st_mode & 0777) != 0700)
  239. && (chmod (buffer, 0700) != 0))
  240. error = _("Cannot set correct permissions for directory %s\n");
  241. } else {
  242. /* Need to create directory */
  243. if (mkdir (buffer, S_IRWXU) != 0) {
  244. fprintf (stderr,
  245. _("Cannot create temporary directory %s: %s\n"),
  246. buffer, unix_error_string (errno));
  247. error = "";
  248. }
  249. }
  250. if (error != NULL) {
  251. int test_fd;
  252. char *test_fn, *fallback_prefix;
  253. int fallback_ok = 0;
  254. if (*error)
  255. fprintf (stderr, error, buffer);
  256. /* Test if sys_tmp is suitable for temporary files */
  257. fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
  258. test_fd = mc_mkstemps (&test_fn, fallback_prefix, NULL);
  259. g_free (fallback_prefix);
  260. if (test_fd != -1) {
  261. close (test_fd);
  262. test_fd = open (test_fn, O_RDONLY);
  263. if (test_fd != -1) {
  264. close (test_fd);
  265. unlink (test_fn);
  266. fallback_ok = 1;
  267. }
  268. }
  269. if (fallback_ok) {
  270. fprintf (stderr, _("Temporary files will be created in %s\n"),
  271. sys_tmp);
  272. g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
  273. error = NULL;
  274. } else {
  275. fprintf (stderr, _("Temporary files will not be created\n"));
  276. g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
  277. }
  278. fprintf (stderr, "%s\n", _("Press any key to continue..."));
  279. getc (stdin);
  280. }
  281. tmpdir = buffer;
  282. if (!error)
  283. g_setenv ("MC_TMPDIR", tmpdir, TRUE);
  284. return tmpdir;
  285. }
  286. /* Pipes are guaranteed to be able to hold at least 4096 bytes */
  287. /* More than that would be unportable */
  288. #define MAX_PIPE_SIZE 4096
  289. static int error_pipe[2]; /* File descriptors of error pipe */
  290. static int old_error; /* File descriptor of old standard error */
  291. /* Creates a pipe to hold standard error for a later analysis. */
  292. /* The pipe can hold 4096 bytes. Make sure no more is written */
  293. /* or a deadlock might occur. */
  294. void open_error_pipe (void)
  295. {
  296. if (pipe (error_pipe) < 0){
  297. message (D_NORMAL, _("Warning"), _(" Pipe failed "));
  298. }
  299. old_error = dup (2);
  300. if(old_error < 0 || close(2) || dup (error_pipe[1]) != 2){
  301. message (D_NORMAL, _("Warning"), _(" Dup failed "));
  302. close (error_pipe[0]);
  303. error_pipe[0] = -1;
  304. }
  305. else
  306. {
  307. /*
  308. * Settng stderr in nonblocking mode as we close it earlier, than
  309. * program stops. We try to read some error at program startup,
  310. * but we should not block on it.
  311. *
  312. * TODO: make piped stdin/stderr poll()/select()able to get rid
  313. * of following hack.
  314. */
  315. int fd_flags;
  316. fd_flags = fcntl (error_pipe[0], F_GETFL, NULL);
  317. if (fd_flags != -1)
  318. {
  319. fd_flags |= O_NONBLOCK;
  320. if (fcntl(error_pipe[0], F_SETFL, fd_flags) == -1)
  321. {
  322. /* TODO: handle it somehow */
  323. }
  324. }
  325. }
  326. /* we never write there */
  327. close (error_pipe[1]);
  328. error_pipe[1] = -1;
  329. }
  330. /*
  331. * Returns true if an error was displayed
  332. * error: -1 - ignore errors, 0 - display warning, 1 - display error
  333. * text is prepended to the error message from the pipe
  334. */
  335. int
  336. close_error_pipe (int error, const char *text)
  337. {
  338. const char *title;
  339. char msg[MAX_PIPE_SIZE];
  340. int len = 0;
  341. /* already closed */
  342. if (error_pipe[0] == -1)
  343. return 0;
  344. if (error)
  345. title = MSG_ERROR;
  346. else
  347. title = _("Warning");
  348. if (old_error >= 0){
  349. close (2);
  350. dup (old_error);
  351. close (old_error);
  352. len = read (error_pipe[0], msg, MAX_PIPE_SIZE - 1);
  353. if (len >= 0)
  354. msg[len] = 0;
  355. close (error_pipe[0]);
  356. error_pipe[0] = -1;
  357. }
  358. if (error < 0)
  359. return 0; /* Just ignore error message */
  360. if (text == NULL){
  361. if (len <= 0)
  362. return 0; /* Nothing to show */
  363. /* Show message from pipe */
  364. message (error, title, "%s", msg);
  365. } else {
  366. /* Show given text and possible message from pipe */
  367. message (error, title, " %s \n %s ", text, msg);
  368. }
  369. return 1;
  370. }
  371. /*
  372. * Canonicalize path, and return a new path. Do everything in place.
  373. * The new path differs from path in:
  374. * Multiple `/'s are collapsed to a single `/'.
  375. * Leading `./'s and trailing `/.'s are removed.
  376. * Trailing `/'s are removed.
  377. * Non-leading `../'s and trailing `..'s are handled by removing
  378. * portions of the path.
  379. * Well formed UNC paths are modified only in the local part.
  380. */
  381. void
  382. custom_canonicalize_pathname (char *path, CANON_PATH_FLAGS flags)
  383. {
  384. char *p, *s;
  385. int len;
  386. char *lpath = path; /* path without leading UNC part */
  387. /* Detect and preserve UNC paths: //server/... */
  388. if ( ( flags & CANON_PATH_GUARDUNC ) && path[0] == PATH_SEP && path[1] == PATH_SEP) {
  389. p = path + 2;
  390. while (p[0] && p[0] != '/')
  391. p++;
  392. if (p[0] == '/' && p > path + 2)
  393. lpath = p;
  394. }
  395. if (!lpath[0] || !lpath[1])
  396. return;
  397. if ( flags & CANON_PATH_JOINSLASHES ) {
  398. /* Collapse multiple slashes */
  399. p = lpath;
  400. while (*p) {
  401. if (p[0] == PATH_SEP && p[1] == PATH_SEP) {
  402. s = p + 1;
  403. while (*(++s) == PATH_SEP);
  404. str_move (p + 1, s);
  405. }
  406. p++;
  407. }
  408. }
  409. if ( flags & CANON_PATH_JOINSLASHES ) {
  410. /* Collapse "/./" -> "/" */
  411. p = lpath;
  412. while (*p) {
  413. if (p[0] == PATH_SEP && p[1] == '.' && p[2] == PATH_SEP)
  414. str_move (p, p + 2);
  415. else
  416. p++;
  417. }
  418. }
  419. if ( flags & CANON_PATH_REMSLASHDOTS ) {
  420. /* Remove trailing slashes */
  421. p = lpath + strlen (lpath) - 1;
  422. while (p > lpath && *p == PATH_SEP)
  423. *p-- = 0;
  424. /* Remove leading "./" */
  425. if (lpath[0] == '.' && lpath[1] == PATH_SEP) {
  426. if (lpath[2] == 0) {
  427. lpath[1] = 0;
  428. return;
  429. } else {
  430. str_move (lpath, lpath + 2);
  431. }
  432. }
  433. /* Remove trailing "/" or "/." */
  434. len = strlen (lpath);
  435. if (len < 2)
  436. return;
  437. if (lpath[len - 1] == PATH_SEP) {
  438. lpath[len - 1] = 0;
  439. } else {
  440. if (lpath[len - 1] == '.' && lpath[len - 2] == PATH_SEP) {
  441. if (len == 2) {
  442. lpath[1] = 0;
  443. return;
  444. } else {
  445. lpath[len - 2] = 0;
  446. }
  447. }
  448. }
  449. }
  450. if ( flags & CANON_PATH_REMDOUBLEDOTS ) {
  451. /* Collapse "/.." with the previous part of path */
  452. p = lpath;
  453. while (p[0] && p[1] && p[2]) {
  454. if ((p[0] != PATH_SEP || p[1] != '.' || p[2] != '.')
  455. || (p[3] != PATH_SEP && p[3] != 0)) {
  456. p++;
  457. continue;
  458. }
  459. /* search for the previous token */
  460. s = p - 1;
  461. while (s >= lpath && *s != PATH_SEP)
  462. s--;
  463. s++;
  464. /* If the previous token is "..", we cannot collapse it */
  465. if (s[0] == '.' && s[1] == '.' && s + 2 == p) {
  466. p += 3;
  467. continue;
  468. }
  469. if (p[3] != 0) {
  470. if (s == lpath && *s == PATH_SEP) {
  471. /* "/../foo" -> "/foo" */
  472. str_move (s + 1, p + 4);
  473. } else {
  474. /* "token/../foo" -> "foo" */
  475. str_move (s, p + 4);
  476. }
  477. p = (s > lpath) ? s - 1 : s;
  478. continue;
  479. }
  480. /* trailing ".." */
  481. if (s == lpath) {
  482. /* "token/.." -> "." */
  483. if (lpath[0] != PATH_SEP) {
  484. lpath[0] = '.';
  485. }
  486. lpath[1] = 0;
  487. } else {
  488. /* "foo/token/.." -> "foo" */
  489. if (s == lpath + 1)
  490. s[0] = 0;
  491. else
  492. s[-1] = 0;
  493. break;
  494. }
  495. break;
  496. }
  497. }
  498. }
  499. void
  500. canonicalize_pathname (char *path)
  501. {
  502. custom_canonicalize_pathname (path, CANON_PATH_ALL);
  503. }
  504. #ifdef HAVE_GET_PROCESS_STATS
  505. # include <sys/procstats.h>
  506. int gettimeofday (struct timeval *tp, void *tzp)
  507. {
  508. return get_process_stats(tp, PS_SELF, 0, 0);
  509. }
  510. #endif /* HAVE_GET_PROCESS_STATS */
  511. char *
  512. mc_realpath (const char *path, char resolved_path[])
  513. {
  514. #ifdef USE_SYSTEM_REALPATH
  515. return realpath (path, resolved_path);
  516. #else
  517. char copy_path[PATH_MAX];
  518. char link_path[PATH_MAX];
  519. char got_path[PATH_MAX];
  520. char *new_path = got_path;
  521. char *max_path;
  522. int readlinks = 0;
  523. int n;
  524. /* Make a copy of the source path since we may need to modify it. */
  525. if (strlen (path) >= PATH_MAX - 2) {
  526. errno = ENAMETOOLONG;
  527. return NULL;
  528. }
  529. strcpy (copy_path, path);
  530. path = copy_path;
  531. max_path = copy_path + PATH_MAX - 2;
  532. /* If it's a relative pathname use getwd for starters. */
  533. if (*path != '/') {
  534. /* Ohoo... */
  535. #ifdef HAVE_GETCWD
  536. getcwd (new_path, PATH_MAX - 1);
  537. #else
  538. getwd (new_path);
  539. #endif
  540. new_path += strlen (new_path);
  541. if (new_path[-1] != '/')
  542. *new_path++ = '/';
  543. } else {
  544. *new_path++ = '/';
  545. path++;
  546. }
  547. /* Expand each slash-separated pathname component. */
  548. while (*path != '\0') {
  549. /* Ignore stray "/". */
  550. if (*path == '/') {
  551. path++;
  552. continue;
  553. }
  554. if (*path == '.') {
  555. /* Ignore ".". */
  556. if (path[1] == '\0' || path[1] == '/') {
  557. path++;
  558. continue;
  559. }
  560. if (path[1] == '.') {
  561. if (path[2] == '\0' || path[2] == '/') {
  562. path += 2;
  563. /* Ignore ".." at root. */
  564. if (new_path == got_path + 1)
  565. continue;
  566. /* Handle ".." by backing up. */
  567. while ((--new_path)[-1] != '/');
  568. continue;
  569. }
  570. }
  571. }
  572. /* Safely copy the next pathname component. */
  573. while (*path != '\0' && *path != '/') {
  574. if (path > max_path) {
  575. errno = ENAMETOOLONG;
  576. return NULL;
  577. }
  578. *new_path++ = *path++;
  579. }
  580. #ifdef S_IFLNK
  581. /* Protect against infinite loops. */
  582. if (readlinks++ > MAXSYMLINKS) {
  583. errno = ELOOP;
  584. return NULL;
  585. }
  586. /* See if latest pathname component is a symlink. */
  587. *new_path = '\0';
  588. n = readlink (got_path, link_path, PATH_MAX - 1);
  589. if (n < 0) {
  590. /* EINVAL means the file exists but isn't a symlink. */
  591. if (errno != EINVAL) {
  592. /* Make sure it's null terminated. */
  593. *new_path = '\0';
  594. strcpy (resolved_path, got_path);
  595. return NULL;
  596. }
  597. } else {
  598. /* Note: readlink doesn't add the null byte. */
  599. link_path[n] = '\0';
  600. if (*link_path == '/')
  601. /* Start over for an absolute symlink. */
  602. new_path = got_path;
  603. else
  604. /* Otherwise back up over this component. */
  605. while (*(--new_path) != '/');
  606. /* Safe sex check. */
  607. if (strlen (path) + n >= PATH_MAX - 2) {
  608. errno = ENAMETOOLONG;
  609. return NULL;
  610. }
  611. /* Insert symlink contents into path. */
  612. strcat (link_path, path);
  613. strcpy (copy_path, link_path);
  614. path = copy_path;
  615. }
  616. #endif /* S_IFLNK */
  617. *new_path++ = '/';
  618. }
  619. /* Delete trailing slash but don't whomp a lone slash. */
  620. if (new_path != got_path + 1 && new_path[-1] == '/')
  621. new_path--;
  622. /* Make sure it's null terminated. */
  623. *new_path = '\0';
  624. strcpy (resolved_path, got_path);
  625. return resolved_path;
  626. #endif /* USE_SYSTEM_REALPATH */
  627. }
  628. /* Return the index of the permissions triplet */
  629. int
  630. get_user_permissions (struct stat *st) {
  631. static gboolean initialized = FALSE;
  632. static gid_t *groups;
  633. static int ngroups;
  634. static uid_t uid;
  635. int i;
  636. if (!initialized) {
  637. uid = geteuid ();
  638. ngroups = getgroups (0, NULL);
  639. if (ngroups == -1)
  640. ngroups = 0; /* ignore errors */
  641. /* allocate space for one element in addition to what
  642. * will be filled by getgroups(). */
  643. groups = g_new (gid_t, ngroups + 1);
  644. if (ngroups != 0) {
  645. ngroups = getgroups (ngroups, groups);
  646. if (ngroups == -1)
  647. ngroups = 0; /* ignore errors */
  648. }
  649. /* getgroups() may or may not return the effective group ID,
  650. * so we always include it at the end of the list. */
  651. groups[ngroups++] = getegid ();
  652. initialized = TRUE;
  653. }
  654. if (st->st_uid == uid || uid == 0)
  655. return 0;
  656. for (i = 0; i < ngroups; i++) {
  657. if (st->st_gid == groups[i])
  658. return 1;
  659. }
  660. return 2;
  661. }