File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #ifdef HAVE_CONFIG_H
0031 #include "config.h"
0032 #endif
0033
0034 #if defined(LIBC_SCCS) && !defined(lint)
0035 static char sccsid[] = "@(#)pwcache.c 8.1 (Berkeley) 6/4/93";
0036 #endif
0037 #include <sys/cdefs.h>
0038 __FBSDID("$FreeBSD: src/lib/libc/gen/pwcache.c,v 1.11 2007/01/09 00:27:55 imp Exp $");
0039
0040 #include <sys/types.h>
0041
0042 #include <grp.h>
0043 #include <pwd.h>
0044 #include <stdio.h>
0045 #include <string.h>
0046
0047 #include "internal.h"
0048
0049 #define UT_NAMESIZE 64
0050
0051 #define NCACHE 64
0052 #define MASK (NCACHE - 1)
0053
0054 const char *
0055 user_from_uid(uid_t uid, int nouser)
0056 {
0057 static struct ncache {
0058 uid_t uid;
0059 int found;
0060 char name[UT_NAMESIZE + 1];
0061 } c_uid[NCACHE];
0062 static int pwopen;
0063 struct passwd *pw;
0064 struct ncache *cp;
0065
0066 cp = c_uid + (uid & MASK);
0067 if (cp->uid != uid || !*cp->name) {
0068 if (pwopen == 0) {
0069
0070 pwopen = 1;
0071 }
0072 pw = getpwuid(uid);
0073 cp->uid = uid;
0074 if (pw != NULL) {
0075 cp->found = 1;
0076 (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
0077 cp->name[UT_NAMESIZE] = '\0';
0078 } else {
0079 cp->found = 0;
0080 (void)snprintf(cp->name, UT_NAMESIZE, "%u", uid);
0081 if (nouser)
0082 return (NULL);
0083 }
0084 }
0085 return ((nouser && !cp->found) ? NULL : cp->name);
0086 }
0087
0088 char *
0089 group_from_gid(gid_t gid, int nogroup)
0090 {
0091 static struct ncache {
0092 gid_t gid;
0093 int found;
0094 char name[UT_NAMESIZE + 1];
0095 } c_gid[NCACHE];
0096 static int gropen;
0097 struct group *gr;
0098 struct ncache *cp;
0099
0100 cp = c_gid + (gid & MASK);
0101 if (cp->gid != gid || !*cp->name) {
0102 if (gropen == 0) {
0103
0104 gropen = 1;
0105 }
0106 gr = getgrgid(gid);
0107 cp->gid = gid;
0108 if (gr != NULL) {
0109 cp->found = 1;
0110 (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
0111 cp->name[UT_NAMESIZE] = '\0';
0112 } else {
0113 cp->found = 0;
0114 (void)snprintf(cp->name, UT_NAMESIZE, "%u", gid);
0115 if (nogroup)
0116 return (NULL);
0117 }
0118 }
0119 return ((nogroup && !cp->found) ? NULL : cp->name);
0120 }
0121