xref: /csrg-svn/lib/libc/gen/pwcache.c (revision 40281)
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*40281Smarc static char sccsid[] = "@(#)pwcache.c	5.2 (Berkeley) 03/05/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>
2640263Sbostic 
2740263Sbostic #define	NCACHE	64			/* power of 2 */
2840263Sbostic #define	MASK	NCACHE - 1		/* bits to store with */
2940263Sbostic 
30*40281Smarc static	int pwopen = 0;
31*40281Smarc static	int gropen = 0;
32*40281Smarc 
3340263Sbostic char *
3440263Sbostic user_from_uid(uid)
3540263Sbostic 	uid_t uid;
3640263Sbostic {
3740263Sbostic 	static struct ncache {
3840263Sbostic 		uid_t	uid;
3940263Sbostic 		char	name[UT_NAMESIZE + 1];
4040263Sbostic 	} c_uid[NCACHE];
4140263Sbostic 	static char nbuf[15];		/* 32 bits == 10 digits */
4240263Sbostic 	register struct passwd *pw;
4340263Sbostic 	register struct ncache *cp;
4440263Sbostic 
4540263Sbostic 	cp = c_uid + (uid & MASK);
4640263Sbostic 	if (cp->uid != uid || !*cp->name) {
47*40281Smarc 		if (pwopen == 0) {
48*40281Smarc 			setpassent(1);
49*40281Smarc 			pwopen++;
50*40281Smarc 		}
5140263Sbostic 		/* if can't find owner, use user id instead */
5240263Sbostic 		if (!(pw = getpwuid(uid))) {
5340263Sbostic 			(void)sprintf(nbuf, "%u", uid);
5440263Sbostic 			return(nbuf);
5540263Sbostic 		}
5640263Sbostic 		cp->uid = uid;
5740263Sbostic 		(void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
5840263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
5940263Sbostic 	}
6040263Sbostic 	return(cp->name);
6140263Sbostic }
6240263Sbostic 
6340263Sbostic char *
6440263Sbostic group_from_gid(gid)
6540263Sbostic 	gid_t gid;
6640263Sbostic {
6740263Sbostic 	static struct ncache {
6840263Sbostic 		gid_t	gid;
6940263Sbostic 		char	name[UT_NAMESIZE];
7040263Sbostic 	} c_gid[NCACHE];
7140263Sbostic 	static char nbuf[15];		/* 32 bits == 10 digits */
7240263Sbostic 	register struct group *gr;
7340263Sbostic 	register struct ncache *cp;
7440263Sbostic 
7540263Sbostic 	cp = c_gid + (gid & MASK);
7640263Sbostic 	if (cp->gid != gid || !*cp->name) {
77*40281Smarc 		if (gropen == 0) {
78*40281Smarc 			setgroupent(1);
79*40281Smarc 			gropen++;
80*40281Smarc 		}
8140263Sbostic 		/* if can't find group, use group id instead */
8240263Sbostic 		if (!(gr = getgrgid(gid))) {
8340263Sbostic 			(void)sprintf(nbuf, "%u", gid);
8440263Sbostic 			return(nbuf);
8540263Sbostic 		}
8640263Sbostic 		cp->gid = gid;
8740263Sbostic 		(void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
8840263Sbostic 		cp->name[UT_NAMESIZE] = '\0';
8940263Sbostic 	}
9040263Sbostic 	return(cp->name);
9140263Sbostic }
92