Libinfo-173.tar.gz
[apple/libinfo.git] / util.subproj / pwcache.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 *
28 * Copyright (c) 1989, 1993
29 * The Regents of the University of California. All rights reserved.
30 *
31 * The NEXTSTEP Software License Agreement specifies the terms
32 * and conditions for redistribution.
33 *
34 * @(#)pwcache.c 8.1 (Berkeley) 6/4/93
35 */
36
37
38 #include <sys/types.h>
39
40 #include <grp.h>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <utmp.h>
46
47 #define NCACHE 64 /* power of 2 */
48 #define MASK (NCACHE - 1) /* bits to store with */
49
50 char *
51 user_from_uid(uid, nouser)
52 uid_t uid;
53 int nouser;
54 {
55 static struct ncache {
56 uid_t uid;
57 char name[UT_NAMESIZE + 1];
58 } *c_uid[NCACHE];
59 static int pwopen;
60 static char nbuf[15]; /* 32 bits == 10 digits */
61 register struct passwd *pw;
62 register struct ncache **cp;
63
64 cp = &c_uid[uid & MASK];
65 if (*cp == NULL || (*cp)->uid != uid || !*(*cp)->name) {
66 if (pwopen == 0) {
67 setpassent(1);
68 pwopen = 1;
69 }
70 if ((pw = getpwuid(uid)) == NULL) {
71 err:
72 if (nouser)
73 return (NULL);
74 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
75 return (nbuf);
76 }
77 if (*cp == NULL) {
78 *cp = malloc(sizeof(struct ncache));
79 if (*cp == NULL)
80 goto err;
81 }
82 (*cp)->uid = uid;
83 (void)strncpy((*cp)->name, pw->pw_name, UT_NAMESIZE);
84 (*cp)->name[UT_NAMESIZE] = '\0';
85 }
86 return ((*cp)->name);
87 }
88
89 char *
90 group_from_gid(gid, nogroup)
91 gid_t gid;
92 int nogroup;
93 {
94 static struct ncache {
95 gid_t gid;
96 char name[UT_NAMESIZE + 1];
97 } *c_gid[NCACHE];
98 static int gropen;
99 static char nbuf[15]; /* 32 bits == 10 digits */
100 struct group *gr;
101 struct ncache **cp = NULL;
102
103 cp = &c_gid[gid & MASK];
104 if (*cp == NULL || (*cp)->gid != gid || !*(*cp)->name) {
105 if (gropen == 0) {
106 setgroupent(1);
107 gropen = 1;
108 }
109 if ((gr = getgrgid(gid)) == NULL) {
110 err:
111 if (nogroup)
112 return (NULL);
113 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
114 return (nbuf);
115 }
116 if (*cp == NULL) {
117 *cp = malloc(sizeof(struct ncache));
118 if (*cp == NULL)
119 goto err;
120 }
121 (*cp)->gid = gid;
122 (void)strncpy((*cp)->name, gr->gr_name, UT_NAMESIZE);
123 (*cp)->name[UT_NAMESIZE] = '\0';
124 }
125 return ((*cp)->name);
126 }