|
|
1.1 root 1: /*
2: * Copyright (c) 1989 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)pwcache.c 5.4 (Berkeley) 6/1/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <sys/types.h>
25: #include <utmp.h>
26: #include <pwd.h>
27: #include <grp.h>
28: #include <stdio.h>
29:
30: #define NCACHE 64 /* power of 2 */
31: #define MASK NCACHE - 1 /* bits to store with */
32:
33: static int pwopen = 0;
34: static int gropen = 0;
35:
36: char *
37: user_from_uid(uid, nouser)
38: uid_t uid;
39: int nouser;
40: {
41: static struct ncache {
42: uid_t uid;
43: char name[UT_NAMESIZE + 1];
44: } c_uid[NCACHE];
45: static char nbuf[15]; /* 32 bits == 10 digits */
46: register struct passwd *pw;
47: register struct ncache *cp;
48:
49: cp = c_uid + (uid & MASK);
50: if (cp->uid != uid || !*cp->name) {
51: if (pwopen == 0) {
52: setpassent(1);
53: pwopen++;
54: }
55: if (!(pw = getpwuid(uid))) {
56: if (nouser)
57: return((char *)NULL);
58: (void)sprintf(nbuf, "%u", uid);
59: return(nbuf);
60: }
61: cp->uid = uid;
62: (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
63: cp->name[UT_NAMESIZE] = '\0';
64: }
65: return(cp->name);
66: }
67:
68: char *
69: group_from_gid(gid, nogroup)
70: gid_t gid;
71: int nogroup;
72: {
73: static struct ncache {
74: gid_t gid;
75: char name[UT_NAMESIZE];
76: } c_gid[NCACHE];
77: static char nbuf[15]; /* 32 bits == 10 digits */
78: register struct group *gr;
79: register struct ncache *cp;
80:
81: cp = c_gid + (gid & MASK);
82: if (cp->gid != gid || !*cp->name) {
83: if (gropen == 0) {
84: setgroupent(1);
85: gropen++;
86: }
87: if (!(gr = getgrgid(gid))) {
88: if (nogroup)
89: return((char *)NULL);
90: (void)sprintf(nbuf, "%u", gid);
91: return(nbuf);
92: }
93: cp->gid = gid;
94: (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
95: cp->name[UT_NAMESIZE] = '\0';
96: }
97: return(cp->name);
98: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.