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