xref: /csrg-svn/lib/libc/gen/pwcache.c (revision 42662)
140263Sbostic /*
240263Sbostic  * Copyright (c) 1989 The Regents of the University of California.
340263Sbostic  * All rights reserved.
440263Sbostic  *
5*42662Sbostic  * %sccs.include.redist.c%
640263Sbostic  */
740263Sbostic 
840263Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*42662Sbostic static char sccsid[] = "@(#)pwcache.c	5.4 (Berkeley) 06/01/90";
1040263Sbostic #endif /* LIBC_SCCS and not lint */
1140263Sbostic 
1240263Sbostic #include <sys/types.h>
1340263Sbostic #include <utmp.h>
1440263Sbostic #include <pwd.h>
1540263Sbostic #include <grp.h>
1640478Sbostic #include <stdio.h>
1740263Sbostic 
1840263Sbostic #define	NCACHE	64			/* power of 2 */
1940263Sbostic #define	MASK	NCACHE - 1		/* bits to store with */
2040263Sbostic 
2140281Smarc static	int pwopen = 0;
2240281Smarc static	int gropen = 0;
2340281Smarc 
2440263Sbostic char *
2540478Sbostic user_from_uid(uid, nouser)
2640263Sbostic 	uid_t uid;
2740478Sbostic 	int nouser;
2840263Sbostic {
2940263Sbostic 	static struct ncache {
3040263Sbostic 		uid_t	uid;
3140263Sbostic 		char	name[UT_NAMESIZE + 1];
3240263Sbostic 	} c_uid[NCACHE];
3340263Sbostic 	static char nbuf[15];		/* 32 bits == 10 digits */
3440263Sbostic 	register struct passwd *pw;
3540263Sbostic 	register struct ncache *cp;
3640263Sbostic 
3740263Sbostic 	cp = c_uid + (uid & MASK);
3840263Sbostic 	if (cp->uid != uid || !*cp->name) {
3940281Smarc 		if (pwopen == 0) {
4040281Smarc 			setpassent(1);
4140281Smarc 			pwopen++;
4240281Smarc 		}
4340263Sbostic 		if (!(pw = getpwuid(uid))) {
4440478Sbostic 			if (nouser)
4540478Sbostic 				return((char *)NULL);
4640263Sbostic 			(void)sprintf(nbuf, "%u", uid);
4740263Sbostic 			return(nbuf);
4840263Sbostic 		}
4940263Sbostic 		cp->uid = uid;
5040263Sbostic 		(void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
5140263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
5240263Sbostic 	}
5340263Sbostic 	return(cp->name);
5440263Sbostic }
5540263Sbostic 
5640263Sbostic char *
5740478Sbostic group_from_gid(gid, nogroup)
5840263Sbostic 	gid_t gid;
5940478Sbostic 	int nogroup;
6040263Sbostic {
6140263Sbostic 	static struct ncache {
6240263Sbostic 		gid_t	gid;
6340263Sbostic 		char	name[UT_NAMESIZE];
6440263Sbostic 	} c_gid[NCACHE];
6540263Sbostic 	static char nbuf[15];		/* 32 bits == 10 digits */
6640263Sbostic 	register struct group *gr;
6740263Sbostic 	register struct ncache *cp;
6840263Sbostic 
6940263Sbostic 	cp = c_gid + (gid & MASK);
7040263Sbostic 	if (cp->gid != gid || !*cp->name) {
7140281Smarc 		if (gropen == 0) {
7240281Smarc 			setgroupent(1);
7340281Smarc 			gropen++;
7440281Smarc 		}
7540263Sbostic 		if (!(gr = getgrgid(gid))) {
7640478Sbostic 			if (nogroup)
7740478Sbostic 				return((char *)NULL);
7840263Sbostic 			(void)sprintf(nbuf, "%u", gid);
7940263Sbostic 			return(nbuf);
8040263Sbostic 		}
8140263Sbostic 		cp->gid = gid;
8240263Sbostic 		(void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
8340263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
8440263Sbostic 	}
8540263Sbostic 	return(cp->name);
8640263Sbostic }
87