X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fnames.c;h=e37c8af553302def9aa904bfa01ab71624945964;hb=bc88be110bc8d49c4e8ec5530b8238607afdb1fc;hp=b339ef13a90172fb2e041c1dd01783928f89e997;hpb=f3531f64fb63f785eac9bbd80fecd2912e535566;p=chaz%2Ftar diff --git a/src/names.c b/src/names.c index b339ef1..e37c8af 100644 --- a/src/names.c +++ b/src/names.c @@ -1,5 +1,5 @@ /* Various processing of names. - Copyright (C) 1988, 1992, 1994, 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1988, 92, 94, 96, 97, 98, 1999 Free Software Foundation, Inc. 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 @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 59 Place - Suite 330, Boston, MA 02111-1307, USA. */ + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "system.h" @@ -48,6 +48,15 @@ static char cached_gname[GNAME_FIELD_SIZE] = ""; static uid_t cached_uid; /* valid only if cached_uname is not empty */ static gid_t cached_gid; /* valid only if cached_gname is not empty */ +/* These variables are valid only if nonempty. */ +static char cached_no_such_uname[UNAME_FIELD_SIZE] = ""; +static char cached_no_such_gname[GNAME_FIELD_SIZE] = ""; + +/* These variables are valid only if nonzero. It's not worth optimizing + the case for weird systems where 0 is not a valid uid or gid. */ +static uid_t cached_no_such_uid = 0; +static gid_t cached_no_such_gid = 0; + /*------------------------------------------. | Given UID, find the corresponding UNAME. | `------------------------------------------*/ @@ -57,6 +66,12 @@ uid_to_uname (uid_t uid, char uname[UNAME_FIELD_SIZE]) { struct passwd *passwd; + if (uid != 0 && uid == cached_no_such_uid) + { + *uname = '\0'; + return; + } + if (!cached_uname[0] || uid != cached_uid) { passwd = getpwuid (uid); @@ -66,7 +81,11 @@ uid_to_uname (uid_t uid, char uname[UNAME_FIELD_SIZE]) strncpy (cached_uname, passwd->pw_name, UNAME_FIELD_SIZE); } else - *uname = '\0'; + { + cached_no_such_uid = uid; + *uname = '\0'; + return; + } } strncpy (uname, cached_uname, UNAME_FIELD_SIZE); } @@ -80,6 +99,12 @@ gid_to_gname (gid_t gid, char gname[GNAME_FIELD_SIZE]) { struct group *group; + if (gid != 0 && gid == cached_no_such_gid) + { + *gname = '\0'; + return; + } + if (!cached_gname[0] || gid != cached_gid) { setgrent (); /* FIXME: why?! */ @@ -90,7 +115,11 @@ gid_to_gname (gid_t gid, char gname[GNAME_FIELD_SIZE]) strncpy (cached_gname, group->gr_name, GNAME_FIELD_SIZE); } else - *gname = '\0'; + { + cached_no_such_gid = gid; + *gname = '\0'; + return; + } } strncpy (gname, cached_gname, GNAME_FIELD_SIZE); } @@ -104,6 +133,10 @@ uname_to_uid (char uname[UNAME_FIELD_SIZE], uid_t *uidp) { struct passwd *passwd; + if (cached_no_such_uname[0] + && strncmp (uname, cached_no_such_uname, UNAME_FIELD_SIZE) == 0) + return 0; + if (!cached_uname[0] || uname[0] != cached_uname[0] || strncmp (uname, cached_uname, UNAME_FIELD_SIZE) != 0) @@ -115,7 +148,10 @@ uname_to_uid (char uname[UNAME_FIELD_SIZE], uid_t *uidp) strncpy (cached_uname, uname, UNAME_FIELD_SIZE); } else - return 0; + { + strncpy (cached_no_such_uname, uname, UNAME_FIELD_SIZE); + return 0; + } } *uidp = cached_uid; return 1; @@ -130,6 +166,10 @@ gname_to_gid (char gname[GNAME_FIELD_SIZE], gid_t *gidp) { struct group *group; + if (cached_no_such_gname[0] + && strncmp (gname, cached_no_such_gname, GNAME_FIELD_SIZE) == 0) + return 0; + if (!cached_gname[0] || gname[0] != cached_gname[0] || strncmp (gname, cached_gname, GNAME_FIELD_SIZE) != 0) @@ -141,7 +181,10 @@ gname_to_gid (char gname[GNAME_FIELD_SIZE], gid_t *gidp) strncpy (cached_gname, gname, GNAME_FIELD_SIZE); } else - return 0; + { + strncpy (cached_no_such_gname, gname, GNAME_FIELD_SIZE); + return 0; + } } *gidp = cached_gid; return 1;