summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/pwcache.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/gen/pwcache.c')
-rw-r--r--lib/libc/gen/pwcache.c41
1 files changed, 24 insertions, 17 deletions
diff --git a/lib/libc/gen/pwcache.c b/lib/libc/gen/pwcache.c
index 1dcbf5d..aea7f55 100644
--- a/lib/libc/gen/pwcache.c
+++ b/lib/libc/gen/pwcache.c
@@ -38,12 +38,13 @@ static char sccsid[] = "@(#)pwcache.c 8.1 (Berkeley) 6/4/93";
#include <sys/types.h>
#include <grp.h>
+#include <string.h>
#include <pwd.h>
#include <stdio.h>
#include <utmp.h>
#define NCACHE 64 /* power of 2 */
-#define MASK NCACHE - 1 /* bits to store with */
+#define MASK (NCACHE - 1) /* bits to store with */
char *
user_from_uid(uid, nouser)
@@ -52,10 +53,10 @@ user_from_uid(uid, nouser)
{
static struct ncache {
uid_t uid;
+ int found;
char name[UT_NAMESIZE + 1];
} c_uid[NCACHE];
static int pwopen;
- static char nbuf[15]; /* 32 bits == 10 digits */
register struct passwd *pw;
register struct ncache *cp;
@@ -65,17 +66,20 @@ user_from_uid(uid, nouser)
setpassent(1);
pwopen = 1;
}
- if ((pw = getpwuid(uid)) == NULL) {
+ pw = getpwuid(uid);
+ cp->uid = uid;
+ if (pw != NULL) {
+ cp->found = 1;
+ (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
+ cp->name[UT_NAMESIZE] = '\0';
+ } else {
+ cp->found = 0;
+ (void)snprintf(cp->name, UT_NAMESIZE, "%u", uid);
if (nouser)
return (NULL);
- (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
- return (nbuf);
}
- cp->uid = uid;
- (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
- cp->name[UT_NAMESIZE] = '\0';
}
- return (cp->name);
+ return ((nouser && !cp->found) ? NULL : cp->name);
}
char *
@@ -85,10 +89,10 @@ group_from_gid(gid, nogroup)
{
static struct ncache {
gid_t gid;
+ int found;
char name[UT_NAMESIZE + 1];
} c_gid[NCACHE];
static int gropen;
- static char nbuf[15]; /* 32 bits == 10 digits */
struct group *gr;
struct ncache *cp;
@@ -98,15 +102,18 @@ group_from_gid(gid, nogroup)
setgroupent(1);
gropen = 1;
}
- if ((gr = getgrgid(gid)) == NULL) {
+ gr = getgrgid(gid);
+ cp->gid = gid;
+ if (gr != NULL) {
+ cp->found = 1;
+ (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
+ cp->name[UT_NAMESIZE] = '\0';
+ } else {
+ cp->found = 0;
+ (void)snprintf(cp->name, UT_NAMESIZE, "%u", gid);
if (nogroup)
return (NULL);
- (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
- return (nbuf);
}
- cp->gid = gid;
- (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
- cp->name[UT_NAMESIZE] = '\0';
}
- return (cp->name);
+ return ((nogroup && !cp->found) ? NULL : cp->name);
}
OpenPOWER on IntegriCloud