]> git.saurik.com Git - apple/libinfo.git/blob - util.subproj/pwcache.c
ce31e7a9fb3a4d573fe4536403ad50b0b4c4ee10
[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) 1980, 1988, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
31 */
32
33 #include <sys/types.h>
34
35 #include <grp.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <utmp.h>
41 #include <utmpx.h>
42
43 #define NCACHE 64 /* power of 2 */
44 #define MASK (NCACHE - 1) /* bits to store with */
45
46 char *
47 user_from_uid(uid, nouser)
48 uid_t uid;
49 int nouser;
50 {
51 static struct ncache {
52 uid_t uid;
53 char name[_UTX_USERSIZE + 1];
54 } *c_uid[NCACHE];
55 static int pwopen;
56 static char nbuf[15]; /* 32 bits == 10 digits */
57 register struct passwd *pw;
58 register struct ncache **cp;
59
60 cp = &c_uid[uid & MASK];
61 if (*cp == NULL || (*cp)->uid != uid || !*(*cp)->name) {
62 if (pwopen == 0) {
63 setpassent(1);
64 pwopen = 1;
65 }
66 if ((pw = getpwuid(uid)) == NULL) {
67 err:
68 if (nouser)
69 return (NULL);
70 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
71 return (nbuf);
72 }
73 if (*cp == NULL) {
74 *cp = malloc(sizeof(struct ncache));
75 if (*cp == NULL)
76 goto err;
77 }
78 (*cp)->uid = uid;
79 (void)strncpy((*cp)->name, pw->pw_name, _UTX_USERSIZE);
80 (*cp)->name[_UTX_USERSIZE] = '\0';
81 }
82 return ((*cp)->name);
83 }
84
85 char *
86 group_from_gid(gid, nogroup)
87 gid_t gid;
88 int nogroup;
89 {
90 static struct ncache {
91 gid_t gid;
92 char name[_UTX_USERSIZE + 1];
93 } *c_gid[NCACHE];
94 static int gropen;
95 static char nbuf[15]; /* 32 bits == 10 digits */
96 struct group *gr;
97 struct ncache **cp = NULL;
98
99 cp = &c_gid[gid & MASK];
100 if (*cp == NULL || (*cp)->gid != gid || !*(*cp)->name) {
101 if (gropen == 0) {
102 setgroupent(1);
103 gropen = 1;
104 }
105 if ((gr = getgrgid(gid)) == NULL) {
106 err:
107 if (nogroup)
108 return (NULL);
109 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
110 return (nbuf);
111 }
112 if (*cp == NULL) {
113 *cp = malloc(sizeof(struct ncache));
114 if (*cp == NULL)
115 goto err;
116 }
117 (*cp)->gid = gid;
118 (void)strncpy((*cp)->name, gr->gr_name, _UTX_USERSIZE);
119 (*cp)->name[_UTX_USERSIZE] = '\0';
120 }
121 return ((*cp)->name);
122 }