xref: /csrg-svn/lib/libc/gen/pwcache.c (revision 61111)
140263Sbostic /*
2*61111Sbostic  * Copyright (c) 1989, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
440263Sbostic  *
542662Sbostic  * %sccs.include.redist.c%
640263Sbostic  */
740263Sbostic 
840263Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)pwcache.c	8.1 (Berkeley) 06/04/93";
1040263Sbostic #endif /* LIBC_SCCS and not lint */
1140263Sbostic 
1240263Sbostic #include <sys/types.h>
1359427Sbostic 
1459427Sbostic #include <grp.h>
1540263Sbostic #include <pwd.h>
1640478Sbostic #include <stdio.h>
1759427Sbostic #include <utmp.h>
1840263Sbostic 
1940263Sbostic #define	NCACHE	64			/* power of 2 */
2040263Sbostic #define	MASK	NCACHE - 1		/* bits to store with */
2140263Sbostic 
2240263Sbostic char *
user_from_uid(uid,nouser)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];
3159427Sbostic 	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);
4059427Sbostic 			pwopen = 1;
4140281Smarc 		}
4259427Sbostic 		if ((pw = getpwuid(uid)) == NULL) {
4340478Sbostic 			if (nouser)
4459427Sbostic 				return (NULL);
4559427Sbostic 			(void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
4659427Sbostic 			return (nbuf);
4740263Sbostic 		}
4840263Sbostic 		cp->uid = uid;
4940263Sbostic 		(void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
5040263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
5140263Sbostic 	}
5259427Sbostic 	return (cp->name);
5340263Sbostic }
5440263Sbostic 
5540263Sbostic char *
group_from_gid(gid,nogroup)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];
6459427Sbostic 	static int gropen;
6540263Sbostic 	static char nbuf[15];		/* 32 bits == 10 digits */
6659427Sbostic 	struct group *gr;
6759427Sbostic 	struct ncache *cp;
6840263Sbostic 
6940263Sbostic 	cp = c_gid + (gid & MASK);
7040263Sbostic 	if (cp->gid != gid || !*cp->name) {
7140281Smarc 		if (gropen == 0) {
7240281Smarc 			setgroupent(1);
7359427Sbostic 			gropen = 1;
7440281Smarc 		}
7559427Sbostic 		if ((gr = getgrgid(gid)) == NULL) {
7640478Sbostic 			if (nogroup)
7759427Sbostic 				return (NULL);
7859427Sbostic 			(void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
7959427Sbostic 			return (nbuf);
8040263Sbostic 		}
8140263Sbostic 		cp->gid = gid;
8240263Sbostic 		(void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
8340263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
8440263Sbostic 	}
8559427Sbostic 	return (cp->name);
8640263Sbostic }
87