1*40263Sbostic /* 2*40263Sbostic * Copyright (c) 1989 The Regents of the University of California. 3*40263Sbostic * All rights reserved. 4*40263Sbostic * 5*40263Sbostic * Redistribution and use in source and binary forms are permitted 6*40263Sbostic * provided that the above copyright notice and this paragraph are 7*40263Sbostic * duplicated in all such forms and that any documentation, 8*40263Sbostic * advertising materials, and other materials related to such 9*40263Sbostic * distribution and use acknowledge that the software was developed 10*40263Sbostic * by the University of California, Berkeley. The name of the 11*40263Sbostic * University may not be used to endorse or promote products derived 12*40263Sbostic * from this software without specific prior written permission. 13*40263Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*40263Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*40263Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16*40263Sbostic */ 17*40263Sbostic 18*40263Sbostic #if defined(LIBC_SCCS) && !defined(lint) 19*40263Sbostic static char sccsid[] = "@(#)pwcache.c 5.1 (Berkeley) 03/05/90"; 20*40263Sbostic #endif /* LIBC_SCCS and not lint */ 21*40263Sbostic 22*40263Sbostic #include <sys/types.h> 23*40263Sbostic #include <utmp.h> 24*40263Sbostic #include <pwd.h> 25*40263Sbostic #include <grp.h> 26*40263Sbostic 27*40263Sbostic #define NCACHE 64 /* power of 2 */ 28*40263Sbostic #define MASK NCACHE - 1 /* bits to store with */ 29*40263Sbostic 30*40263Sbostic char * 31*40263Sbostic user_from_uid(uid) 32*40263Sbostic uid_t uid; 33*40263Sbostic { 34*40263Sbostic static struct ncache { 35*40263Sbostic uid_t uid; 36*40263Sbostic char name[UT_NAMESIZE + 1]; 37*40263Sbostic } c_uid[NCACHE]; 38*40263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 39*40263Sbostic register struct passwd *pw; 40*40263Sbostic register struct ncache *cp; 41*40263Sbostic 42*40263Sbostic cp = c_uid + (uid & MASK); 43*40263Sbostic if (cp->uid != uid || !*cp->name) { 44*40263Sbostic /* if can't find owner, use user id instead */ 45*40263Sbostic if (!(pw = getpwuid(uid))) { 46*40263Sbostic (void)sprintf(nbuf, "%u", uid); 47*40263Sbostic return(nbuf); 48*40263Sbostic } 49*40263Sbostic cp->uid = uid; 50*40263Sbostic (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE); 51*40263Sbostic cp->name[UT_NAMESIZE] = '\0'; 52*40263Sbostic } 53*40263Sbostic return(cp->name); 54*40263Sbostic } 55*40263Sbostic 56*40263Sbostic char * 57*40263Sbostic group_from_gid(gid) 58*40263Sbostic gid_t gid; 59*40263Sbostic { 60*40263Sbostic static struct ncache { 61*40263Sbostic gid_t gid; 62*40263Sbostic char name[UT_NAMESIZE]; 63*40263Sbostic } c_gid[NCACHE]; 64*40263Sbostic static char nbuf[15]; /* 32 bits == 10 digits */ 65*40263Sbostic register struct group *gr; 66*40263Sbostic register struct ncache *cp; 67*40263Sbostic 68*40263Sbostic cp = c_gid + (gid & MASK); 69*40263Sbostic if (cp->gid != gid || !*cp->name) { 70*40263Sbostic /* if can't find group, use group id instead */ 71*40263Sbostic if (!(gr = getgrgid(gid))) { 72*40263Sbostic (void)sprintf(nbuf, "%u", gid); 73*40263Sbostic return(nbuf); 74*40263Sbostic } 75*40263Sbostic cp->gid = gid; 76*40263Sbostic (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE); 77*40263Sbostic cp->name[UT_NAMESIZE] = '\0'; 78*40263Sbostic } 79*40263Sbostic return(cp->name); 80*40263Sbostic } 81