1 /* $Source: /u/mark/src/pax/RCS/names.c,v $
5 * names.c - Look up user and/or group names.
9 * These functions support UID and GID name lookup. The results are
10 * cached to improve performance.
14 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
16 * Sponsored by The USENIX Association for public distribution.
18 * Copyright (c) 1989 Mark H. Colburn.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that the above copyright notice is duplicated in all such
23 * forms and that any documentation, advertising materials, and other
24 * materials related to such distribution and use acknowledge that the
25 * software was developed * by Mark H. Colburn and sponsored by The
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33 * Revision 1.2 89/02/12 10:05:05 mark
36 * Revision 1.1 88/12/23 18:02:19 mark
42 static char *ident = "$Id: names.c,v 1.2 89/02/12 10:05:05 mark Exp $";
43 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
54 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
55 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
58 /* Internal Identifiers */
60 static int saveuid = -993;
61 static char saveuname[TUNMLEN];
62 static int my_uid = -993;
64 static int savegid = -993;
65 static char savegname[TGNMLEN];
66 static int my_gid = -993;
69 /* finduname - find a user or group name from a uid or gid
73 * Look up a user name from a uid/gid, maintaining a cache.
77 * char uname[] - name (to be returned to user)
78 * int uuid - id of name to find
83 * Returns a name which is associated with the user id given. If there
84 * is not name which corresponds to the user-id given, then a pointer
85 * to a string of zero length is returned.
89 * 1. for now it's a one-entry cache.
90 * 2. The "-993" is to reduce the chance of a hit on the first lookup.
95 char *finduname(int uuid)
106 if (uuid != saveuid) {
111 strncpy(saveuname, pw->pw_name, TUNMLEN);
118 /* finduid - get the uid for a given user name
122 * This does just the opposit of finduname. Given a user name it
123 * finds the corresponding UID for that name.
127 * char uname[] - username to find a UID for
131 * The UID which corresponds to the uname given, if any. If no UID
132 * could be found, then the UID which corrsponds the user running the
133 * program is returned.
139 int finduid(char *uname)
149 extern struct passwd *getpwnam();
151 if (uname[0] != saveuname[0]/* Quick test w/o proc call */
152 ||0 != strncmp(uname, saveuname, TUNMLEN)) {
153 strncpy(saveuname, uname, TUNMLEN);
154 pw = getpwnam(uname);
156 saveuid = pw->pw_uid;
165 /* findgname - look up a group name from a gid
169 * Look up a group name from a gid, maintaining a cache.
174 * int ggid - goupid of group to find
178 * A string which is associated with the group ID given. If no name
179 * can be found, a string of zero length is returned.
184 char *findgname(int ggid)
188 char *findgname(ggid)
195 if (ggid != savegid) {
198 #ifndef _POSIX_SOURCE
203 strncpy(savegname, gr->gr_name, TGNMLEN);
211 /* findgid - get the gid for a given group name
215 * This does just the opposit of finduname. Given a group name it
216 * finds the corresponding GID for that name.
220 * char uname[] - groupname to find a GID for
224 * The GID which corresponds to the uname given, if any. If no GID
225 * could be found, then the GID which corrsponds the group running the
226 * program is returned.
232 int findgid(char *gname)
243 /* Quick test w/o proc call */
244 if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) {
245 strncpy(savegname, gname, TUNMLEN);
246 gr = getgrnam(gname);
248 savegid = gr->gr_gid;