140263Sbostic /* 240263Sbostic * Copyright (c) 1989 The Regents of the University of California. 340263Sbostic * All rights reserved. 440263Sbostic * 540263Sbostic * Redistribution and use in source and binary forms are permitted 640263Sbostic * provided that the above copyright notice and this paragraph are 740263Sbostic * duplicated in all such forms and that any documentation, 840263Sbostic * advertising materials, and other materials related to such 940263Sbostic * distribution and use acknowledge that the software was developed 1040263Sbostic * by the University of California, Berkeley. The name of the 1140263Sbostic * University may not be used to endorse or promote products derived 1240263Sbostic * from this software without specific prior written permission. 1340263Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1440263Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1540263Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1640263Sbostic */ 1740263Sbostic 1840263Sbostic #if defined(LIBC_SCCS) && !defined(lint) 19*40478Sbostic static char sccsid[] = "@(#)pwcache.c 5.3 (Berkeley) 03/13/90"; 2040263Sbostic #endif /* LIBC_SCCS and not lint */ 2140263Sbostic 2240263Sbostic #include <sys/types.h> 2340263Sbostic #include <utmp.h> 2440263Sbostic #include <pwd.h> 2540263Sbostic #include <grp.h> 26*40478Sbostic #include <stdio.h> 2740263Sbostic 2840263Sbostic #define NCACHE 64 /* power of 2 */ 2940263Sbostic #define MASK NCACHE - 1 /* bits to store with */ 3040263Sbostic 3140281Smarc static int pwopen = 0; 3240281Smarc static int gropen = 0; 3340281Smarc 3440263Sbostic char * 35*40478Sbostic user_from_uid(uid, nouser) 3640263Sbostic uid_t uid; 37*40478Sbostic int nouser; 3840263Sbostic { 3940263Sbostic static struct ncache { 4040263Sbostic uid_t uid; 4140263Sbostic char name[UT_NAMESIZE + 1]; 4240263Sbostic } c_uid[NCACHE]; 4340263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 4440263Sbostic register struct passwd *pw; 4540263Sbostic register struct ncache *cp; 4640263Sbostic 4740263Sbostic cp = c_uid + (uid & MASK); 4840263Sbostic if (cp->uid != uid || !*cp->name) { 4940281Smarc if (pwopen == 0) { 5040281Smarc setpassent(1); 5140281Smarc pwopen++; 5240281Smarc } 5340263Sbostic if (!(pw = getpwuid(uid))) { 54*40478Sbostic if (nouser) 55*40478Sbostic return((char *)NULL); 5640263Sbostic (void)sprintf(nbuf, "%u", uid); 5740263Sbostic return(nbuf); 5840263Sbostic } 5940263Sbostic cp->uid = uid; 6040263Sbostic (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE); 6140263Sbostic cp->name[UT_NAMESIZE] = '\0'; 6240263Sbostic } 6340263Sbostic return(cp->name); 6440263Sbostic } 6540263Sbostic 6640263Sbostic char * 67*40478Sbostic group_from_gid(gid, nogroup) 6840263Sbostic gid_t gid; 69*40478Sbostic int nogroup; 7040263Sbostic { 7140263Sbostic static struct ncache { 7240263Sbostic gid_t gid; 7340263Sbostic char name[UT_NAMESIZE]; 7440263Sbostic } c_gid[NCACHE]; 7540263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 7640263Sbostic register struct group *gr; 7740263Sbostic register struct ncache *cp; 7840263Sbostic 7940263Sbostic cp = c_gid + (gid & MASK); 8040263Sbostic if (cp->gid != gid || !*cp->name) { 8140281Smarc if (gropen == 0) { 8240281Smarc setgroupent(1); 8340281Smarc gropen++; 8440281Smarc } 8540263Sbostic if (!(gr = getgrgid(gid))) { 86*40478Sbostic if (nogroup) 87*40478Sbostic return((char *)NULL); 8840263Sbostic (void)sprintf(nbuf, "%u", gid); 8940263Sbostic return(nbuf); 9040263Sbostic } 9140263Sbostic cp->gid = gid; 9240263Sbostic (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE); 9340263Sbostic cp->name[UT_NAMESIZE] = '\0'; 9440263Sbostic } 9540263Sbostic return(cp->name); 9640263Sbostic } 97