]> Dogcows Code - chaz/tar/blobdiff - src/names.c
Don't include <ctype.h>; system.h already does this.
[chaz/tar] / src / names.c
index b339ef13a90172fb2e041c1dd01783928f89e997..e37c8af553302def9aa904bfa01ab71624945964 100644 (file)
@@ -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;
This page took 0.025302 seconds and 4 git commands to generate.