1998-02-27 07:54:42 +03:00
|
|
|
/* Look up user and/or group names.
|
|
|
|
Copyright (C) 1988, 1992 Free Software Foundation
|
|
|
|
|
|
|
|
From GNU Tar.
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
GNU Tar is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Library General Public License as published
|
|
|
|
by the Free Software Foundation; either version 2, or (at your option)
|
1998-02-27 07:54:42 +03:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
GNU Tar 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
|
1998-09-27 23:27:58 +04:00
|
|
|
GNU Library General Public License for more details.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
* Namespace: finduname, finduid, findgname, findgid.
|
1998-09-27 23:27:58 +04:00
|
|
|
*/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/*
|
2002-11-15 09:19:34 +03:00
|
|
|
* Look up a user or group name from a uid/gid, maintaining a cache.
|
|
|
|
* FIXME, for now it's a one-entry cache.
|
|
|
|
* FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
|
1998-02-27 07:54:42 +03:00
|
|
|
* This file should be modified for non-unix systems to do something
|
|
|
|
* reasonable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2002-11-15 09:19:34 +03:00
|
|
|
#include "names.h"
|
|
|
|
|
|
|
|
#ifndef TUNMLEN
|
1998-05-26 04:53:24 +04:00
|
|
|
#define TUNMLEN 256
|
|
|
|
#endif
|
|
|
|
#ifndef TGNMLEN
|
|
|
|
#define TGNMLEN 256
|
|
|
|
#endif
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
static int saveuid = -993;
|
|
|
|
static char saveuname[TUNMLEN];
|
|
|
|
static int my_uid = -993;
|
|
|
|
|
|
|
|
static int savegid = -993;
|
|
|
|
static char savegname[TGNMLEN];
|
|
|
|
static int my_gid = -993;
|
|
|
|
|
|
|
|
#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
|
|
|
|
#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
|
|
|
|
|
2002-11-15 09:19:34 +03:00
|
|
|
int
|
|
|
|
finduid (char *uname)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
2002-11-15 09:19:34 +03:00
|
|
|
if (uname[0] != saveuname[0] /* Quick test w/o proc call */
|
1998-02-27 07:54:42 +03:00
|
|
|
||0 != strncmp (uname, saveuname, TUNMLEN)) {
|
|
|
|
strncpy (saveuname, uname, TUNMLEN);
|
|
|
|
pw = getpwnam (uname);
|
|
|
|
if (pw) {
|
|
|
|
saveuid = pw->pw_uid;
|
|
|
|
} else {
|
|
|
|
saveuid = myuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return saveuid;
|
|
|
|
}
|
|
|
|
|
2002-11-15 09:19:34 +03:00
|
|
|
int
|
|
|
|
findgid (char *gname)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
|
2002-11-15 09:19:34 +03:00
|
|
|
if (gname[0] != savegname[0] /* Quick test w/o proc call */
|
1998-02-27 07:54:42 +03:00
|
|
|
||0 != strncmp (gname, savegname, TUNMLEN)) {
|
|
|
|
strncpy (savegname, gname, TUNMLEN);
|
|
|
|
gr = getgrnam (gname);
|
|
|
|
if (gr) {
|
|
|
|
savegid = gr->gr_gid;
|
|
|
|
} else {
|
|
|
|
savegid = mygid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return savegid;
|
|
|
|
}
|