140263Sbostic /* 240263Sbostic * Copyright (c) 1989 The Regents of the University of California. 340263Sbostic * All rights reserved. 440263Sbostic * 542662Sbostic * %sccs.include.redist.c% 640263Sbostic */ 740263Sbostic 840263Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*59427Sbostic static char sccsid[] = "@(#)pwcache.c 5.6 (Berkeley) 04/27/93"; 1040263Sbostic #endif /* LIBC_SCCS and not lint */ 1140263Sbostic 1240263Sbostic #include <sys/types.h> 13*59427Sbostic 14*59427Sbostic #include <grp.h> 1540263Sbostic #include <pwd.h> 1640478Sbostic #include <stdio.h> 17*59427Sbostic #include <utmp.h> 1840263Sbostic 1940263Sbostic #define NCACHE 64 /* power of 2 */ 2040263Sbostic #define MASK NCACHE - 1 /* bits to store with */ 2140263Sbostic 2240263Sbostic char * 2340478Sbostic user_from_uid(uid, nouser) 2440263Sbostic uid_t uid; 2540478Sbostic int nouser; 2640263Sbostic { 2740263Sbostic static struct ncache { 2840263Sbostic uid_t uid; 2940263Sbostic char name[UT_NAMESIZE + 1]; 3040263Sbostic } c_uid[NCACHE]; 31*59427Sbostic static int pwopen; 3240263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 3340263Sbostic register struct passwd *pw; 3440263Sbostic register struct ncache *cp; 3540263Sbostic 3640263Sbostic cp = c_uid + (uid & MASK); 3740263Sbostic if (cp->uid != uid || !*cp->name) { 3840281Smarc if (pwopen == 0) { 3940281Smarc setpassent(1); 40*59427Sbostic pwopen = 1; 4140281Smarc } 42*59427Sbostic if ((pw = getpwuid(uid)) == NULL) { 4340478Sbostic if (nouser) 44*59427Sbostic return (NULL); 45*59427Sbostic (void)snprintf(nbuf, sizeof(nbuf), "%u", uid); 46*59427Sbostic return (nbuf); 4740263Sbostic } 4840263Sbostic cp->uid = uid; 4940263Sbostic (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE); 5040263Sbostic cp->name[UT_NAMESIZE] = '\0'; 5140263Sbostic } 52*59427Sbostic return (cp->name); 5340263Sbostic } 5440263Sbostic 5540263Sbostic char * 5640478Sbostic group_from_gid(gid, nogroup) 5740263Sbostic gid_t gid; 5840478Sbostic int nogroup; 5940263Sbostic { 6040263Sbostic static struct ncache { 6140263Sbostic gid_t gid; 6259308Sbostic char name[UT_NAMESIZE + 1]; 6340263Sbostic } c_gid[NCACHE]; 64*59427Sbostic static int gropen; 6540263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 66*59427Sbostic struct group *gr; 67*59427Sbostic struct ncache *cp; 6840263Sbostic 6940263Sbostic cp = c_gid + (gid & MASK); 7040263Sbostic if (cp->gid != gid || !*cp->name) { 7140281Smarc if (gropen == 0) { 7240281Smarc setgroupent(1); 73*59427Sbostic gropen = 1; 7440281Smarc } 75*59427Sbostic if ((gr = getgrgid(gid)) == NULL) { 7640478Sbostic if (nogroup) 77*59427Sbostic return (NULL); 78*59427Sbostic (void)snprintf(nbuf, sizeof(nbuf), "%u", gid); 79*59427Sbostic return (nbuf); 8040263Sbostic } 8140263Sbostic cp->gid = gid; 8240263Sbostic (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE); 8340263Sbostic cp->name[UT_NAMESIZE] = '\0'; 8440263Sbostic } 85*59427Sbostic return (cp->name); 8640263Sbostic } 87