1998-02-27 07:54:42 +03:00
|
|
|
/* Various utilities - Unix variants
|
2007-09-25 19:33:35 +04:00
|
|
|
Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
|
|
|
|
2004, 2005, 2007 Free Software Foundation, Inc.
|
1998-02-27 07:54:42 +03:00
|
|
|
Written 1994, 1995, 1996 by:
|
|
|
|
Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
|
|
|
|
Jakub Jelinek, Mauricio Plaza.
|
|
|
|
|
2005-01-13 22:37:46 +03:00
|
|
|
The mc_realpath routine is mostly from uClibc package, written
|
|
|
|
by Rick Sladkey <jrs@world.std.com>
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-05 21:28:18 +03:00
|
|
|
/** \file utilunix.c
|
|
|
|
* \brief Source: various utilities - Unix variant
|
|
|
|
*/
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <stdio.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <fcntl.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
#include <sys/param.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2009-02-27 14:54:26 +03:00
|
|
|
#include <sys/wait.h>
|
2001-09-11 06:18:02 +04:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2001-09-07 21:52:19 +04:00
|
|
|
# include <sys/ioctl.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
2005-02-08 12:04:03 +03:00
|
|
|
#include <unistd.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2001-09-03 09:07:40 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "global.h"
|
2003-06-22 13:17:46 +04:00
|
|
|
#include "execute.h"
|
2002-11-14 10:25:18 +03:00
|
|
|
#include "wtools.h" /* message() */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
struct sigaction startup_handler;
|
|
|
|
|
|
|
|
#define UID_CACHE_SIZE 200
|
|
|
|
#define GID_CACHE_SIZE 30
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int index;
|
|
|
|
char *string;
|
|
|
|
} int_cache;
|
|
|
|
|
2001-07-14 10:13:29 +04:00
|
|
|
static int_cache uid_cache [UID_CACHE_SIZE];
|
|
|
|
static int_cache gid_cache [GID_CACHE_SIZE];
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
static char *i_cache_match (int id, int_cache *cache, int size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
if (cache [i].index == id)
|
|
|
|
return cache [i].string;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i_cache_add (int id, int_cache *cache, int size, char *text,
|
|
|
|
int *last)
|
|
|
|
{
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (cache [*last].string);
|
2009-02-06 01:40:32 +03:00
|
|
|
cache [*last].string = g_strdup (text);
|
1998-02-27 07:54:42 +03:00
|
|
|
cache [*last].index = id;
|
|
|
|
*last = ((*last)+1) % size;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_owner (int uid)
|
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
Glibing..... (2)
Wed Jan 27 03:17:44 1999 Timur Bakeyev <mc@bat.ru>
* Converted memory managment to Glib. Now we use g_new()/g_malloc()/
g_strdup()/g_free() routings. Also, copy_strings() replaced by
g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by
g_snprintf().
* Some sequences of malloc()/sprintf() changed to g_strdup_printf().
* mad.[ch]: Modified, to work with new GLib's memory managment. Fixed
a missing #undef for tempnam, which caused dead loop. Add several new
functions to emulate GLib memory managment.
*main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD
messages to the file.
* util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp()
and strdup() - we have g_ equivalences. Remove get_full_name() - it is
similar to concat_dir_and_file(). Some other tricks with g_* functions.
* global.h: Modified, extended. Now it is main memory mangment include -
i.e. all inclusions of <stdlib.h>, <malloc.h>, <glib.h>, "fs.h", "mem.h",
"util.h" and "mad.h" done there. This elimanates problem with proper or-
der of #include's.
* All around the source - changed order of #include's, most of them gone
to global.h (see above), minor changes, like "0" -> NULL in string func-
tions.
1999-01-27 04:08:30 +03:00
|
|
|
static char ibuf [10];
|
1998-02-27 07:54:42 +03:00
|
|
|
char *name;
|
1998-04-17 04:21:53 +04:00
|
|
|
static int uid_last;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if ((name = i_cache_match (uid, uid_cache, UID_CACHE_SIZE)) != NULL)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
pwd = getpwuid (uid);
|
|
|
|
if (pwd){
|
|
|
|
i_cache_add (uid, uid_cache, UID_CACHE_SIZE, pwd->pw_name, &uid_last);
|
|
|
|
return pwd->pw_name;
|
|
|
|
}
|
|
|
|
else {
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (ibuf, sizeof (ibuf), "%d", uid);
|
1998-02-27 07:54:42 +03:00
|
|
|
return ibuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_group (int gid)
|
|
|
|
{
|
|
|
|
struct group *grp;
|
Glibing..... (2)
Wed Jan 27 03:17:44 1999 Timur Bakeyev <mc@bat.ru>
* Converted memory managment to Glib. Now we use g_new()/g_malloc()/
g_strdup()/g_free() routings. Also, copy_strings() replaced by
g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by
g_snprintf().
* Some sequences of malloc()/sprintf() changed to g_strdup_printf().
* mad.[ch]: Modified, to work with new GLib's memory managment. Fixed
a missing #undef for tempnam, which caused dead loop. Add several new
functions to emulate GLib memory managment.
*main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD
messages to the file.
* util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp()
and strdup() - we have g_ equivalences. Remove get_full_name() - it is
similar to concat_dir_and_file(). Some other tricks with g_* functions.
* global.h: Modified, extended. Now it is main memory mangment include -
i.e. all inclusions of <stdlib.h>, <malloc.h>, <glib.h>, "fs.h", "mem.h",
"util.h" and "mad.h" done there. This elimanates problem with proper or-
der of #include's.
* All around the source - changed order of #include's, most of them gone
to global.h (see above), minor changes, like "0" -> NULL in string func-
tions.
1999-01-27 04:08:30 +03:00
|
|
|
static char gbuf [10];
|
1998-02-27 07:54:42 +03:00
|
|
|
char *name;
|
|
|
|
static int gid_last;
|
|
|
|
|
|
|
|
if ((name = i_cache_match (gid, gid_cache, GID_CACHE_SIZE)) != NULL)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
grp = getgrgid (gid);
|
|
|
|
if (grp){
|
|
|
|
i_cache_add (gid, gid_cache, GID_CACHE_SIZE, grp->gr_name, &gid_last);
|
|
|
|
return grp->gr_name;
|
|
|
|
} else {
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (gbuf, sizeof (gbuf), "%d", gid);
|
1998-02-27 07:54:42 +03:00
|
|
|
return gbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since ncurses uses a handler that automatically refreshes the */
|
|
|
|
/* screen after a SIGCONT, and we don't want this behavior when */
|
|
|
|
/* spawning a child, we save the original handler here */
|
|
|
|
void save_stop_handler (void)
|
|
|
|
{
|
|
|
|
sigaction (SIGTSTP, NULL, &startup_handler);
|
|
|
|
}
|
|
|
|
|
1998-04-24 05:08:06 +04:00
|
|
|
int my_system (int flags, const char *shell, const char *command)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sigaction ignore, save_intr, save_quit, save_stop;
|
|
|
|
pid_t pid;
|
|
|
|
int status = 0;
|
1998-04-24 05:08:06 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
ignore.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset (&ignore.sa_mask);
|
|
|
|
ignore.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction (SIGINT, &ignore, &save_intr);
|
|
|
|
sigaction (SIGQUIT, &ignore, &save_quit);
|
|
|
|
|
|
|
|
/* Restore the original SIGTSTP handler, we don't want ncurses' */
|
|
|
|
/* handler messing the screen after the SIGCONT */
|
|
|
|
sigaction (SIGTSTP, &startup_handler, &save_stop);
|
|
|
|
|
|
|
|
if ((pid = fork ()) < 0){
|
|
|
|
fprintf (stderr, "\n\nfork () = -1\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pid == 0){
|
1999-04-13 23:37:45 +04:00
|
|
|
signal (SIGINT, SIG_DFL);
|
|
|
|
signal (SIGQUIT, SIG_DFL);
|
|
|
|
signal (SIGTSTP, SIG_DFL);
|
|
|
|
signal (SIGCHLD, SIG_DFL);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-08-25 20:00:16 +04:00
|
|
|
if (flags & EXECUTE_AS_SHELL)
|
2004-09-24 18:02:39 +04:00
|
|
|
execl (shell, shell, "-c", command, (char *) NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
else
|
2004-09-24 18:02:39 +04:00
|
|
|
execlp (shell, shell, command, (char *) NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
_exit (127); /* Exec error */
|
|
|
|
} else {
|
|
|
|
while (waitpid (pid, &status, 0) < 0)
|
|
|
|
if (errno != EINTR){
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sigaction (SIGINT, &save_intr, NULL);
|
|
|
|
sigaction (SIGQUIT, &save_quit, NULL);
|
|
|
|
sigaction (SIGTSTP, &save_stop, NULL);
|
|
|
|
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
|
2003-11-27 12:45:22 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform tilde expansion if possible.
|
|
|
|
* Always return a newly allocated string, even if it's unchanged.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
tilde_expand (const char *directory)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct passwd *passwd;
|
2003-11-28 18:23:19 +03:00
|
|
|
const char *p, *q;
|
1998-02-27 07:54:42 +03:00
|
|
|
char *name;
|
2003-11-27 12:45:22 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (*directory != '~')
|
2009-02-06 01:40:32 +03:00
|
|
|
return g_strdup (directory);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-11-28 18:23:19 +03:00
|
|
|
p = directory + 1;
|
2003-11-27 12:45:22 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* d = "~" or d = "~/" */
|
2003-11-28 18:23:19 +03:00
|
|
|
if (!(*p) || (*p == PATH_SEP)) {
|
1998-02-27 07:54:42 +03:00
|
|
|
passwd = getpwuid (geteuid ());
|
2003-11-28 18:23:19 +03:00
|
|
|
q = (*p == PATH_SEP) ? p + 1 : "";
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
2004-09-14 21:23:16 +04:00
|
|
|
q = strchr (p, PATH_SEP);
|
2003-11-28 18:23:19 +03:00
|
|
|
if (!q) {
|
|
|
|
passwd = getpwnam (p);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
2004-08-21 01:28:05 +04:00
|
|
|
name = g_strndup (p, q - p);
|
1998-02-27 07:54:42 +03:00
|
|
|
passwd = getpwnam (name);
|
2004-09-14 21:23:16 +04:00
|
|
|
q++;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (name);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-27 12:45:22 +03:00
|
|
|
/* If we can't figure the user name, leave tilde unexpanded */
|
1998-02-27 07:54:42 +03:00
|
|
|
if (!passwd)
|
2009-02-06 01:40:32 +03:00
|
|
|
return g_strdup (directory);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2004-09-24 19:05:28 +04:00
|
|
|
return g_strconcat (passwd->pw_dir, PATH_SEP_STR, q, (char *) NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2004-11-01 15:03:45 +03:00
|
|
|
static void
|
|
|
|
mc_setenv (const char *name, const char *value, int overwrite_flag)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_SETENV)
|
|
|
|
setenv (name, value, overwrite_flag);
|
|
|
|
#else
|
|
|
|
if (overwrite_flag || getenv (name) == NULL)
|
|
|
|
putenv (g_strconcat (name, "=", value, (char *) NULL));
|
|
|
|
#endif
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-09-11 08:58:24 +04:00
|
|
|
/*
|
|
|
|
* Return the directory where mc should keep its temporary files.
|
2004-08-19 00:34:01 +04:00
|
|
|
* This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
|
2002-09-11 08:58:24 +04:00
|
|
|
* When called the first time, the directory is created if needed.
|
|
|
|
* The first call should be done early, since we are using fprintf()
|
|
|
|
* and not message() to report possible problems.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
mc_tmpdir (void)
|
|
|
|
{
|
2002-12-26 17:47:46 +03:00
|
|
|
static char buffer[64];
|
|
|
|
static const char *tmpdir;
|
2002-09-11 08:58:24 +04:00
|
|
|
const char *sys_tmp;
|
|
|
|
struct passwd *pwd;
|
2002-12-26 17:47:46 +03:00
|
|
|
struct stat st;
|
|
|
|
const char *error = NULL;
|
2002-09-11 08:58:24 +04:00
|
|
|
|
2005-07-27 19:03:24 +04:00
|
|
|
/* Check if already correctly initialized */
|
|
|
|
if (tmpdir && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
|
|
|
|
st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
|
2002-09-11 08:58:24 +04:00
|
|
|
return tmpdir;
|
|
|
|
|
|
|
|
sys_tmp = getenv ("TMPDIR");
|
2007-02-22 14:49:01 +03:00
|
|
|
if (!sys_tmp || sys_tmp[0] != '/') {
|
2002-09-11 08:58:24 +04:00
|
|
|
sys_tmp = TMPDIR_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwd = getpwuid (getuid ());
|
2004-08-17 22:45:47 +04:00
|
|
|
|
|
|
|
if (pwd)
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp,
|
2002-09-11 08:58:24 +04:00
|
|
|
pwd->pw_name);
|
2004-08-17 22:45:47 +04:00
|
|
|
else
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp,
|
2004-10-23 16:55:35 +04:00
|
|
|
(unsigned long) getuid ());
|
2004-08-17 22:45:47 +04:00
|
|
|
|
2002-12-26 17:47:46 +03:00
|
|
|
canonicalize_pathname (buffer);
|
|
|
|
|
|
|
|
if (lstat (buffer, &st) == 0) {
|
|
|
|
/* Sanity check for existing directory */
|
|
|
|
if (!S_ISDIR (st.st_mode))
|
|
|
|
error = _("%s is not a directory\n");
|
|
|
|
else if (st.st_uid != getuid ())
|
|
|
|
error = _("Directory %s is not owned by you\n");
|
|
|
|
else if (((st.st_mode & 0777) != 0700)
|
|
|
|
&& (chmod (buffer, 0700) != 0))
|
|
|
|
error = _("Cannot set correct permissions for directory %s\n");
|
|
|
|
} else {
|
|
|
|
/* Need to create directory */
|
|
|
|
if (mkdir (buffer, S_IRWXU) != 0) {
|
2002-10-20 12:34:31 +04:00
|
|
|
fprintf (stderr,
|
|
|
|
_("Cannot create temporary directory %s: %s\n"),
|
2002-12-26 17:47:46 +03:00
|
|
|
buffer, unix_error_string (errno));
|
|
|
|
error = "";
|
2002-10-20 12:34:31 +04:00
|
|
|
}
|
2002-09-11 08:58:24 +04:00
|
|
|
}
|
|
|
|
|
2007-01-02 18:55:01 +03:00
|
|
|
if (error != NULL) {
|
2002-12-26 17:47:46 +03:00
|
|
|
int test_fd;
|
2007-01-02 18:55:01 +03:00
|
|
|
char *test_fn, *fallback_prefix;
|
2002-12-26 17:47:46 +03:00
|
|
|
int fallback_ok = 0;
|
|
|
|
|
|
|
|
if (*error)
|
|
|
|
fprintf (stderr, error, buffer);
|
|
|
|
|
|
|
|
/* Test if sys_tmp is suitable for temporary files */
|
2007-01-02 18:55:01 +03:00
|
|
|
fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
|
|
|
|
test_fd = mc_mkstemps (&test_fn, fallback_prefix, NULL);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (fallback_prefix);
|
2002-12-26 17:47:46 +03:00
|
|
|
if (test_fd != -1) {
|
|
|
|
close (test_fd);
|
|
|
|
test_fd = open (test_fn, O_RDONLY);
|
|
|
|
if (test_fd != -1) {
|
|
|
|
close (test_fd);
|
|
|
|
unlink (test_fn);
|
|
|
|
fallback_ok = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fallback_ok) {
|
|
|
|
fprintf (stderr, _("Temporary files will be created in %s\n"),
|
|
|
|
sys_tmp);
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
|
2004-08-19 00:34:01 +04:00
|
|
|
error = NULL;
|
2002-12-26 17:47:46 +03:00
|
|
|
} else {
|
|
|
|
fprintf (stderr, _("Temporary files will not be created\n"));
|
2009-02-06 01:49:00 +03:00
|
|
|
g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
|
2002-12-26 17:47:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (stderr, "%s\n", _("Press any key to continue..."));
|
|
|
|
getc (stdin);
|
|
|
|
}
|
|
|
|
|
2007-01-02 18:55:01 +03:00
|
|
|
tmpdir = buffer;
|
|
|
|
|
2004-08-19 00:34:01 +04:00
|
|
|
if (!error)
|
2004-11-01 15:03:45 +03:00
|
|
|
mc_setenv ("MC_TMPDIR", tmpdir, 1);
|
2004-08-19 00:34:01 +04:00
|
|
|
|
2002-09-11 08:58:24 +04:00
|
|
|
return tmpdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Pipes are guaranteed to be able to hold at least 4096 bytes */
|
|
|
|
/* More than that would be unportable */
|
|
|
|
#define MAX_PIPE_SIZE 4096
|
|
|
|
|
|
|
|
static int error_pipe[2]; /* File descriptors of error pipe */
|
|
|
|
static int old_error; /* File descriptor of old standard error */
|
|
|
|
|
|
|
|
/* Creates a pipe to hold standard error for a later analysis. */
|
|
|
|
/* The pipe can hold 4096 bytes. Make sure no more is written */
|
|
|
|
/* or a deadlock might occur. */
|
|
|
|
void open_error_pipe (void)
|
|
|
|
{
|
|
|
|
if (pipe (error_pipe) < 0){
|
2009-01-10 16:13:56 +03:00
|
|
|
message (D_NORMAL, _("Warning"), _(" Pipe failed "));
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
old_error = dup (2);
|
|
|
|
if(old_error < 0 || close(2) || dup (error_pipe[1]) != 2){
|
2009-01-10 16:13:56 +03:00
|
|
|
message (D_NORMAL, _("Warning"), _(" Dup failed "));
|
2009-06-20 17:29:44 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
close (error_pipe[0]);
|
2009-06-20 17:29:44 +04:00
|
|
|
error_pipe[0] = -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2009-06-26 14:24:10 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Settng stderr in nonblocking mode as we close it earlier, than
|
|
|
|
* program stops. We try to read some error at program startup,
|
|
|
|
* but we should not block on it.
|
|
|
|
*
|
|
|
|
* TODO: make piped stdin/stderr poll()/select()able to get rid
|
|
|
|
* of following hack.
|
|
|
|
*/
|
|
|
|
int fd_flags;
|
|
|
|
fd_flags = fcntl (error_pipe[0], F_GETFL, NULL);
|
|
|
|
if (fd_flags != -1)
|
|
|
|
{
|
|
|
|
fd_flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(error_pipe[0], F_SETFL, fd_flags) == -1)
|
|
|
|
{
|
|
|
|
/* TODO: handle it somehow */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-20 17:29:44 +04:00
|
|
|
/* we never write there */
|
1998-02-27 07:54:42 +03:00
|
|
|
close (error_pipe[1]);
|
2009-06-20 17:29:44 +04:00
|
|
|
error_pipe[1] = -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-03-18 21:43:52 +03:00
|
|
|
/*
|
|
|
|
* Returns true if an error was displayed
|
2002-08-24 21:25:27 +04:00
|
|
|
* error: -1 - ignore errors, 0 - display warning, 1 - display error
|
|
|
|
* text is prepended to the error message from the pipe
|
1999-03-18 21:43:52 +03:00
|
|
|
*/
|
|
|
|
int
|
2004-08-30 03:20:01 +04:00
|
|
|
close_error_pipe (int error, const char *text)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2004-09-19 19:55:58 +04:00
|
|
|
const char *title;
|
1998-02-27 07:54:42 +03:00
|
|
|
char msg[MAX_PIPE_SIZE];
|
|
|
|
int len = 0;
|
|
|
|
|
2009-06-20 17:29:44 +04:00
|
|
|
/* already closed */
|
|
|
|
if (error_pipe[0] == -1)
|
2009-06-26 14:39:47 +04:00
|
|
|
return 0;
|
2009-06-20 17:29:44 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (error)
|
1998-03-25 08:16:00 +03:00
|
|
|
title = MSG_ERROR;
|
1998-02-27 07:54:42 +03:00
|
|
|
else
|
2002-10-21 08:13:49 +04:00
|
|
|
title = _("Warning");
|
1998-02-27 07:54:42 +03:00
|
|
|
if (old_error >= 0){
|
|
|
|
close (2);
|
|
|
|
dup (old_error);
|
|
|
|
close (old_error);
|
2004-10-12 01:53:30 +04:00
|
|
|
len = read (error_pipe[0], msg, MAX_PIPE_SIZE - 1);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (len >= 0)
|
|
|
|
msg[len] = 0;
|
|
|
|
close (error_pipe[0]);
|
2009-06-20 17:29:44 +04:00
|
|
|
error_pipe[0] = -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
if (error < 0)
|
1999-03-18 21:43:52 +03:00
|
|
|
return 0; /* Just ignore error message */
|
1998-02-27 07:54:42 +03:00
|
|
|
if (text == NULL){
|
2002-08-24 21:25:27 +04:00
|
|
|
if (len <= 0)
|
|
|
|
return 0; /* Nothing to show */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Show message from pipe */
|
2002-07-01 23:08:44 +04:00
|
|
|
message (error, title, "%s", msg);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
|
|
|
/* Show given text and possible message from pipe */
|
|
|
|
message (error, title, " %s \n %s ", text, msg);
|
|
|
|
}
|
1999-03-18 21:43:52 +03:00
|
|
|
return 1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:17:34 +03:00
|
|
|
/*
|
|
|
|
* Canonicalize path, and return a new path. Do everything in place.
|
|
|
|
* The new path differs from path in:
|
|
|
|
* Multiple `/'s are collapsed to a single `/'.
|
|
|
|
* Leading `./'s and trailing `/.'s are removed.
|
|
|
|
* Trailing `/'s are removed.
|
|
|
|
* Non-leading `../'s and trailing `..'s are handled by removing
|
|
|
|
* portions of the path.
|
|
|
|
* Well formed UNC paths are modified only in the local part.
|
|
|
|
*/
|
2004-01-24 02:53:37 +03:00
|
|
|
void
|
2003-03-10 09:52:57 +03:00
|
|
|
canonicalize_pathname (char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-03-10 09:52:57 +03:00
|
|
|
char *p, *s;
|
|
|
|
int len;
|
2004-01-24 03:17:34 +03:00
|
|
|
char *lpath = path; /* path without leading UNC part */
|
|
|
|
|
|
|
|
/* Detect and preserve UNC paths: //server/... */
|
|
|
|
if (path[0] == PATH_SEP && path[1] == PATH_SEP) {
|
|
|
|
p = path + 2;
|
|
|
|
while (p[0] && p[0] != '/')
|
|
|
|
p++;
|
|
|
|
if (p[0] == '/' && p > path + 2)
|
|
|
|
lpath = p;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2004-01-24 03:17:34 +03:00
|
|
|
if (!lpath[0] || !lpath[1])
|
2004-01-24 02:53:37 +03:00
|
|
|
return;
|
2002-07-27 01:51:16 +04:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Collapse multiple slashes */
|
2004-01-24 03:17:34 +03:00
|
|
|
p = lpath;
|
2003-03-10 09:52:57 +03:00
|
|
|
while (*p) {
|
|
|
|
if (p[0] == PATH_SEP && p[1] == PATH_SEP) {
|
|
|
|
s = p + 1;
|
|
|
|
while (*(++s) == PATH_SEP);
|
2009-02-06 15:01:28 +03:00
|
|
|
str_move (p + 1, s);
|
2003-03-10 09:52:57 +03:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Collapse "/./" -> "/" */
|
2004-01-24 03:17:34 +03:00
|
|
|
p = lpath;
|
2003-03-10 09:52:57 +03:00
|
|
|
while (*p) {
|
|
|
|
if (p[0] == PATH_SEP && p[1] == '.' && p[2] == PATH_SEP)
|
2009-02-06 15:01:28 +03:00
|
|
|
str_move (p, p + 2);
|
2003-03-10 09:52:57 +03:00
|
|
|
else
|
|
|
|
p++;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Remove trailing slashes */
|
2004-01-24 03:17:34 +03:00
|
|
|
p = lpath + strlen (lpath) - 1;
|
|
|
|
while (p > lpath && *p == PATH_SEP)
|
2003-03-10 09:52:57 +03:00
|
|
|
*p-- = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Remove leading "./" */
|
2004-01-24 03:17:34 +03:00
|
|
|
if (lpath[0] == '.' && lpath[1] == PATH_SEP) {
|
|
|
|
if (lpath[2] == 0) {
|
|
|
|
lpath[1] = 0;
|
2004-01-24 02:53:37 +03:00
|
|
|
return;
|
2003-03-10 09:52:57 +03:00
|
|
|
} else {
|
2009-02-06 15:01:28 +03:00
|
|
|
str_move (lpath, lpath + 2);
|
2003-03-10 09:52:57 +03:00
|
|
|
}
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Remove trailing "/" or "/." */
|
2004-01-24 03:17:34 +03:00
|
|
|
len = strlen (lpath);
|
2003-03-10 09:52:57 +03:00
|
|
|
if (len < 2)
|
2004-01-24 02:53:37 +03:00
|
|
|
return;
|
2004-01-24 03:17:34 +03:00
|
|
|
if (lpath[len - 1] == PATH_SEP) {
|
|
|
|
lpath[len - 1] = 0;
|
2003-03-10 09:52:57 +03:00
|
|
|
} else {
|
2004-01-24 03:17:34 +03:00
|
|
|
if (lpath[len - 1] == '.' && lpath[len - 2] == PATH_SEP) {
|
2003-03-10 09:52:57 +03:00
|
|
|
if (len == 2) {
|
2004-01-24 03:17:34 +03:00
|
|
|
lpath[1] = 0;
|
2004-01-24 02:53:37 +03:00
|
|
|
return;
|
2003-03-10 09:52:57 +03:00
|
|
|
} else {
|
2004-01-24 03:17:34 +03:00
|
|
|
lpath[len - 2] = 0;
|
2002-02-19 01:31:56 +03:00
|
|
|
}
|
|
|
|
}
|
2003-03-10 09:52:57 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* Collapse "/.." with the previous part of path */
|
2004-01-24 03:17:34 +03:00
|
|
|
p = lpath;
|
2003-03-10 09:52:57 +03:00
|
|
|
while (p[0] && p[1] && p[2]) {
|
|
|
|
if ((p[0] != PATH_SEP || p[1] != '.' || p[2] != '.')
|
|
|
|
|| (p[3] != PATH_SEP && p[3] != 0)) {
|
|
|
|
p++;
|
|
|
|
continue;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* search for the previous token */
|
|
|
|
s = p - 1;
|
2004-01-24 03:17:34 +03:00
|
|
|
while (s >= lpath && *s != PATH_SEP)
|
2003-03-10 09:52:57 +03:00
|
|
|
s--;
|
|
|
|
|
|
|
|
s++;
|
|
|
|
|
|
|
|
/* If the previous token is "..", we cannot collapse it */
|
|
|
|
if (s[0] == '.' && s[1] == '.' && s + 2 == p) {
|
|
|
|
p += 3;
|
|
|
|
continue;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
if (p[3] != 0) {
|
2004-01-24 03:17:34 +03:00
|
|
|
if (s == lpath && *s == PATH_SEP) {
|
2003-03-10 09:52:57 +03:00
|
|
|
/* "/../foo" -> "/foo" */
|
2009-02-06 15:01:28 +03:00
|
|
|
str_move (s + 1, p + 4);
|
2003-03-10 09:52:57 +03:00
|
|
|
} else {
|
|
|
|
/* "token/../foo" -> "foo" */
|
2009-02-06 15:01:28 +03:00
|
|
|
str_move (s, p + 4);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2004-01-24 03:17:34 +03:00
|
|
|
p = (s > lpath) ? s - 1 : s;
|
2003-03-10 09:52:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-03-10 09:52:57 +03:00
|
|
|
/* trailing ".." */
|
2004-01-24 03:17:34 +03:00
|
|
|
if (s == lpath) {
|
2003-03-10 09:52:57 +03:00
|
|
|
/* "token/.." -> "." */
|
2004-01-24 03:17:34 +03:00
|
|
|
if (lpath[0] != PATH_SEP) {
|
|
|
|
lpath[0] = '.';
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2004-01-24 03:17:34 +03:00
|
|
|
lpath[1] = 0;
|
2003-03-10 09:52:57 +03:00
|
|
|
} else {
|
|
|
|
/* "foo/token/.." -> "foo" */
|
2004-01-24 03:17:34 +03:00
|
|
|
if (s == lpath + 1)
|
2003-03-10 09:52:57 +03:00
|
|
|
s[0] = 0;
|
|
|
|
else
|
|
|
|
s[-1] = 0;
|
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 02:53:37 +03:00
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-30 03:12:31 +04:00
|
|
|
#ifdef HAVE_GET_PROCESS_STATS
|
|
|
|
# include <sys/procstats.h>
|
|
|
|
|
|
|
|
int gettimeofday (struct timeval *tp, void *tzp)
|
|
|
|
{
|
|
|
|
return get_process_stats(tp, PS_SELF, 0, 0);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GET_PROCESS_STATS */
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#ifndef HAVE_PUTENV
|
|
|
|
|
|
|
|
/* The following piece of code was copied from the GNU C Library */
|
|
|
|
/* And is provided here for nextstep who lacks putenv */
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
#ifndef HAVE_GNU_LD
|
|
|
|
#define __environ environ
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
|
|
|
|
int
|
2004-08-30 03:20:01 +04:00
|
|
|
putenv (char *string)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
const char *const name_end = strchr (string, '=');
|
|
|
|
register size_t size;
|
|
|
|
register char **ep;
|
|
|
|
|
|
|
|
if (name_end == NULL){
|
|
|
|
/* Remove the variable from the environment. */
|
|
|
|
size = strlen (string);
|
|
|
|
for (ep = __environ; *ep != NULL; ++ep)
|
|
|
|
if (!strncmp (*ep, string, size) && (*ep)[size] == '='){
|
|
|
|
while (ep[1] != NULL){
|
|
|
|
ep[0] = ep[1];
|
|
|
|
++ep;
|
|
|
|
}
|
|
|
|
*ep = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (ep = __environ; *ep != NULL; ++ep)
|
|
|
|
if (!strncmp (*ep, string, name_end - string) &&
|
|
|
|
(*ep)[name_end - string] == '=')
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++size;
|
|
|
|
|
|
|
|
if (*ep == NULL){
|
|
|
|
static char **last_environ = NULL;
|
Glibing..... (2)
Wed Jan 27 03:17:44 1999 Timur Bakeyev <mc@bat.ru>
* Converted memory managment to Glib. Now we use g_new()/g_malloc()/
g_strdup()/g_free() routings. Also, copy_strings() replaced by
g_strconcat(), strcasecmp() -> g_strcasecmp(),and sprintf() by
g_snprintf().
* Some sequences of malloc()/sprintf() changed to g_strdup_printf().
* mad.[ch]: Modified, to work with new GLib's memory managment. Fixed
a missing #undef for tempnam, which caused dead loop. Add several new
functions to emulate GLib memory managment.
*main.c, mad.[ch]: Add a new switch "-M", which allows to redirect MAD
messages to the file.
* util.[ch], utilunix.c: Modified, deleted our variants of strcasecmp()
and strdup() - we have g_ equivalences. Remove get_full_name() - it is
similar to concat_dir_and_file(). Some other tricks with g_* functions.
* global.h: Modified, extended. Now it is main memory mangment include -
i.e. all inclusions of <stdlib.h>, <malloc.h>, <glib.h>, "fs.h", "mem.h",
"util.h" and "mad.h" done there. This elimanates problem with proper or-
der of #include's.
* All around the source - changed order of #include's, most of them gone
to global.h (see above), minor changes, like "0" -> NULL in string func-
tions.
1999-01-27 04:08:30 +03:00
|
|
|
char **new_environ = g_new (char *, size + 2);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (new_environ == NULL)
|
|
|
|
return -1;
|
|
|
|
(void) memcpy ((void *) new_environ, (void *) __environ,
|
|
|
|
size * sizeof (char *));
|
|
|
|
new_environ[size] = (char *) string;
|
|
|
|
new_environ[size + 1] = NULL;
|
2009-02-06 01:18:38 +03:00
|
|
|
g_free ((void *) last_environ);
|
1998-02-27 07:54:42 +03:00
|
|
|
last_environ = new_environ;
|
|
|
|
__environ = new_environ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*ep = (char *) string;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_PUTENV */
|
|
|
|
|
2005-01-13 22:37:46 +03:00
|
|
|
char *
|
|
|
|
mc_realpath (const char *path, char resolved_path[])
|
|
|
|
{
|
2005-02-02 21:28:49 +03:00
|
|
|
#ifdef USE_SYSTEM_REALPATH
|
|
|
|
return realpath (path, resolved_path);
|
|
|
|
#else
|
2005-01-13 22:37:46 +03:00
|
|
|
char copy_path[PATH_MAX];
|
|
|
|
char link_path[PATH_MAX];
|
|
|
|
char got_path[PATH_MAX];
|
|
|
|
char *new_path = got_path;
|
|
|
|
char *max_path;
|
|
|
|
int readlinks = 0;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* Make a copy of the source path since we may need to modify it. */
|
|
|
|
if (strlen (path) >= PATH_MAX - 2) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy (copy_path, path);
|
|
|
|
path = copy_path;
|
|
|
|
max_path = copy_path + PATH_MAX - 2;
|
|
|
|
/* If it's a relative pathname use getwd for starters. */
|
|
|
|
if (*path != '/') {
|
|
|
|
/* Ohoo... */
|
|
|
|
#ifdef HAVE_GETCWD
|
|
|
|
getcwd (new_path, PATH_MAX - 1);
|
|
|
|
#else
|
|
|
|
getwd (new_path);
|
|
|
|
#endif
|
|
|
|
new_path += strlen (new_path);
|
|
|
|
if (new_path[-1] != '/')
|
|
|
|
*new_path++ = '/';
|
|
|
|
} else {
|
|
|
|
*new_path++ = '/';
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
/* Expand each slash-separated pathname component. */
|
|
|
|
while (*path != '\0') {
|
|
|
|
/* Ignore stray "/". */
|
|
|
|
if (*path == '/') {
|
|
|
|
path++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*path == '.') {
|
|
|
|
/* Ignore ".". */
|
|
|
|
if (path[1] == '\0' || path[1] == '/') {
|
|
|
|
path++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (path[1] == '.') {
|
|
|
|
if (path[2] == '\0' || path[2] == '/') {
|
|
|
|
path += 2;
|
|
|
|
/* Ignore ".." at root. */
|
|
|
|
if (new_path == got_path + 1)
|
|
|
|
continue;
|
|
|
|
/* Handle ".." by backing up. */
|
|
|
|
while ((--new_path)[-1] != '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Safely copy the next pathname component. */
|
|
|
|
while (*path != '\0' && *path != '/') {
|
|
|
|
if (path > max_path) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*new_path++ = *path++;
|
|
|
|
}
|
|
|
|
#ifdef S_IFLNK
|
|
|
|
/* Protect against infinite loops. */
|
|
|
|
if (readlinks++ > MAXSYMLINKS) {
|
|
|
|
errno = ELOOP;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* See if latest pathname component is a symlink. */
|
|
|
|
*new_path = '\0';
|
|
|
|
n = readlink (got_path, link_path, PATH_MAX - 1);
|
|
|
|
if (n < 0) {
|
|
|
|
/* EINVAL means the file exists but isn't a symlink. */
|
|
|
|
if (errno != EINVAL) {
|
|
|
|
/* Make sure it's null terminated. */
|
|
|
|
*new_path = '\0';
|
|
|
|
strcpy (resolved_path, got_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Note: readlink doesn't add the null byte. */
|
|
|
|
link_path[n] = '\0';
|
|
|
|
if (*link_path == '/')
|
|
|
|
/* Start over for an absolute symlink. */
|
|
|
|
new_path = got_path;
|
|
|
|
else
|
|
|
|
/* Otherwise back up over this component. */
|
|
|
|
while (*(--new_path) != '/');
|
|
|
|
/* Safe sex check. */
|
|
|
|
if (strlen (path) + n >= PATH_MAX - 2) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Insert symlink contents into path. */
|
|
|
|
strcat (link_path, path);
|
|
|
|
strcpy (copy_path, link_path);
|
|
|
|
path = copy_path;
|
|
|
|
}
|
|
|
|
#endif /* S_IFLNK */
|
|
|
|
*new_path++ = '/';
|
|
|
|
}
|
|
|
|
/* Delete trailing slash but don't whomp a lone slash. */
|
|
|
|
if (new_path != got_path + 1 && new_path[-1] == '/')
|
|
|
|
new_path--;
|
|
|
|
/* Make sure it's null terminated. */
|
|
|
|
*new_path = '\0';
|
|
|
|
strcpy (resolved_path, got_path);
|
|
|
|
return resolved_path;
|
2005-02-02 21:28:49 +03:00
|
|
|
#endif /* USE_SYSTEM_REALPATH */
|
2005-01-13 22:37:46 +03:00
|
|
|
}
|
2005-07-23 01:16:50 +04:00
|
|
|
|
|
|
|
/* Return the index of the permissions triplet */
|
|
|
|
int
|
|
|
|
get_user_permissions (struct stat *st) {
|
2009-02-06 01:46:07 +03:00
|
|
|
static gboolean initialized = FALSE;
|
2005-07-23 01:16:50 +04:00
|
|
|
static gid_t *groups;
|
|
|
|
static int ngroups;
|
|
|
|
static uid_t uid;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
uid = geteuid ();
|
|
|
|
|
|
|
|
ngroups = getgroups (0, NULL);
|
|
|
|
if (ngroups == -1)
|
|
|
|
ngroups = 0; /* ignore errors */
|
|
|
|
|
|
|
|
/* allocate space for one element in addition to what
|
|
|
|
* will be filled by getgroups(). */
|
|
|
|
groups = g_new (gid_t, ngroups + 1);
|
|
|
|
|
|
|
|
if (ngroups != 0) {
|
|
|
|
ngroups = getgroups (ngroups, groups);
|
|
|
|
if (ngroups == -1)
|
|
|
|
ngroups = 0; /* ignore errors */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getgroups() may or may not return the effective group ID,
|
|
|
|
* so we always include it at the end of the list. */
|
|
|
|
groups[ngroups++] = getegid ();
|
|
|
|
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st->st_uid == uid || uid == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ngroups; i++) {
|
|
|
|
if (st->st_gid == groups[i])
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|