1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)pwcache.c 5.4 (Berkeley) 06/01/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/types.h> 13 #include <utmp.h> 14 #include <pwd.h> 15 #include <grp.h> 16 #include <stdio.h> 17 18 #define NCACHE 64 /* power of 2 */ 19 #define MASK NCACHE - 1 /* bits to store with */ 20 21 static int pwopen = 0; 22 static int gropen = 0; 23 24 char * 25 user_from_uid(uid, nouser) 26 uid_t uid; 27 int nouser; 28 { 29 static struct ncache { 30 uid_t uid; 31 char name[UT_NAMESIZE + 1]; 32 } c_uid[NCACHE]; 33 static char nbuf[15]; /* 32 bits == 10 digits */ 34 register struct passwd *pw; 35 register struct ncache *cp; 36 37 cp = c_uid + (uid & MASK); 38 if (cp->uid != uid || !*cp->name) { 39 if (pwopen == 0) { 40 setpassent(1); 41 pwopen++; 42 } 43 if (!(pw = getpwuid(uid))) { 44 if (nouser) 45 return((char *)NULL); 46 (void)sprintf(nbuf, "%u", uid); 47 return(nbuf); 48 } 49 cp->uid = uid; 50 (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE); 51 cp->name[UT_NAMESIZE] = '\0'; 52 } 53 return(cp->name); 54 } 55 56 char * 57 group_from_gid(gid, nogroup) 58 gid_t gid; 59 int nogroup; 60 { 61 static struct ncache { 62 gid_t gid; 63 char name[UT_NAMESIZE]; 64 } c_gid[NCACHE]; 65 static char nbuf[15]; /* 32 bits == 10 digits */ 66 register struct group *gr; 67 register struct ncache *cp; 68 69 cp = c_gid + (gid & MASK); 70 if (cp->gid != gid || !*cp->name) { 71 if (gropen == 0) { 72 setgroupent(1); 73 gropen++; 74 } 75 if (!(gr = getgrgid(gid))) { 76 if (nogroup) 77 return((char *)NULL); 78 (void)sprintf(nbuf, "%u", gid); 79 return(nbuf); 80 } 81 cp->gid = gid; 82 (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE); 83 cp->name[UT_NAMESIZE] = '\0'; 84 } 85 return(cp->name); 86 } 87