2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94";
44 static const char rcsid[] =
48 #include <sys/param.h>
58 void current __P((void));
59 void pline __P((struct passwd *));
60 void pretty __P((struct passwd *));
61 void group __P((struct passwd *, int));
62 void usage __P((void));
63 void user __P((struct passwd *));
74 int Gflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
76 Gflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
77 while ((ch = getopt(argc, argv, "PGgnpru")) != -1)
107 switch(Gflag + Pflag + gflag + pflag + uflag) {
111 if (!nflag && !rflag)
118 pw = *argv ? who(*argv) : NULL;
121 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
122 if (nflag && (gr = getgrgid(id)))
123 (void)printf("%s\n", gr->gr_name);
125 (void)printf("%u\n", id);
130 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
131 if (nflag && (pw = getpwuid(id)))
132 (void)printf("%s\n", pw->pw_name);
134 (void)printf("%u\n", id);
169 (void)printf("uid\t%s\n", pw->pw_name);
170 (void)printf("groups\t");
173 if ((login = getlogin()) == NULL)
176 pw = getpwuid(rid = getuid());
177 if (pw == NULL || strcmp(login, pw->pw_name))
178 (void)printf("login\t%s\n", login);
180 (void)printf("uid\t%s\n", pw->pw_name);
182 (void)printf("uid\t%u\n", rid);
184 if ((eid = geteuid()) != rid) {
185 if ((pw = getpwuid(eid)))
186 (void)printf("euid\t%s\n", pw->pw_name);
188 (void)printf("euid\t%u\n", eid);
190 if ((rid = getgid()) != (eid = getegid())) {
191 if ((gr = getgrgid(rid)))
192 (void)printf("rgid\t%s\n", gr->gr_name);
194 (void)printf("rgid\t%u\n", rid);
196 (void)printf("groups\t");
206 int cnt, id, eid, lastid, ngroups;
207 gid_t groups[NGROUPS];
211 (void)printf("uid=%u", id);
212 if ((pw = getpwuid(id)))
213 (void)printf("(%s)", pw->pw_name);
214 if ((eid = geteuid()) != id) {
215 (void)printf(" euid=%u", eid);
216 if ((pw = getpwuid(eid)))
217 (void)printf("(%s)", pw->pw_name);
220 (void)printf(" gid=%u", id);
221 if ((gr = getgrgid(id)))
222 (void)printf("(%s)", gr->gr_name);
223 if ((eid = getegid()) != id) {
224 (void)printf(" egid=%u", eid);
225 if ((gr = getgrgid(eid)))
226 (void)printf("(%s)", gr->gr_name);
228 if ((ngroups = getgroups(NGROUPS, groups))) {
229 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
230 fmt = ", %u", lastid = id) {
234 (void)printf(fmt, id);
235 if ((gr = getgrgid(id)))
236 (void)printf("(%s)", gr->gr_name);
244 register struct passwd *pw;
246 register struct group *gr;
248 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1];
250 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
252 (void)printf(" gid=%u", gid);
253 if ((gr = getgrgid(gid)))
254 (void)printf("(%s)", gr->gr_name);
255 ngroups = NGROUPS + 1;
256 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
258 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
259 if (lastgid == (gid = groups[cnt]))
261 (void)printf(fmt, gid);
263 if ((gr = getgrgid(gid)))
264 (void)printf("(%s)", gr->gr_name);
276 int cnt, id, lastid, ngroups;
277 gid_t groups[NGROUPS + 1];
281 ngroups = NGROUPS + 1;
282 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
284 groups[0] = getgid();
285 ngroups = getgroups(NGROUPS, groups + 1) + 1;
287 fmt = nflag ? "%s" : "%u";
288 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
289 if (lastid == (id = groups[cnt]))
292 if ((gr = getgrgid(id)))
293 (void)printf(fmt, gr->gr_name);
295 (void)printf(*fmt == ' ' ? " %u" : "%u",
299 (void)printf(fmt, id);
316 * Translate user argument into a pw pointer. First, try to
317 * get it as specified. If that fails, try it as a number.
319 if ((pw = getpwnam(u)))
321 id = strtol(u, &ep, 10);
322 if (*u && !*ep && (pw = getpwuid(id)))
324 errx(1, "%s: no such user", u);
335 if ((pw = getpwuid(rid = getuid())) == NULL)
339 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
340 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
341 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
342 pw->pw_dir, pw->pw_shell);
349 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
351 " id -G [-n] [user]",
353 " id -g [-nr] [user]",
355 " id -u [-nr] [user]");