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