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
33 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)id.c 8.2 (Berkeley) 2/16/94
35 * $FreeBSD: src/usr.bin/id/id.c,v 1.12.2.3 2001/12/20 12:09:03 ru Exp $
36 * $DragonFly: src/usr.bin/id/id.c,v 1.4 2003/11/03 19:31:30 eirikn Exp $
39 #include <sys/param.h>
49 int main(int, char *[]);
51 void pline(struct passwd *);
52 void pretty(struct passwd *);
53 void group(struct passwd *, int);
55 void user(struct passwd *);
59 int isgroups, iswhoami;
62 main(int argc, char **argv)
66 int Gflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
69 Gflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
71 myname = strrchr(argv[0], '/');
72 myname = (myname != NULL) ? myname + 1 : argv[0];
73 if (strcmp(myname, "groups") == 0) {
77 else if (strcmp(myname, "whoami") == 0) {
82 while ((ch = getopt(argc, argv,
83 (isgroups || iswhoami) ? "" : "PGgnpru")) != -1)
113 if (iswhoami && argc > 0)
116 switch(Gflag + Pflag + gflag + pflag + uflag) {
120 if (!nflag && !rflag)
127 pw = *argv ? who(*argv) : NULL;
130 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
131 if (nflag && (gr = getgrgid(id)))
132 (void)printf("%s\n", gr->gr_name);
134 (void)printf("%u\n", id);
139 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
140 if (nflag && (pw = getpwuid(id)))
141 (void)printf("%s\n", pw->pw_name);
143 (void)printf("%u\n", id);
170 pretty(struct passwd *pw)
177 (void)printf("uid\t%s\n", pw->pw_name);
178 (void)printf("groups\t");
181 if ((login = getlogin()) == NULL)
184 pw = getpwuid(rid = getuid());
185 if (pw == NULL || strcmp(login, pw->pw_name))
186 (void)printf("login\t%s\n", login);
188 (void)printf("uid\t%s\n", pw->pw_name);
190 (void)printf("uid\t%u\n", rid);
192 if ((eid = geteuid()) != rid) {
193 if ((pw = getpwuid(eid)))
194 (void)printf("euid\t%s\n", pw->pw_name);
196 (void)printf("euid\t%u\n", eid);
198 if ((rid = getgid()) != (eid = getegid())) {
199 if ((gr = getgrgid(rid)))
200 (void)printf("rgid\t%s\n", gr->gr_name);
202 (void)printf("rgid\t%u\n", rid);
204 (void)printf("groups\t");
214 int cnt, id, eid, lastid, ngroups;
215 gid_t groups[NGROUPS];
219 (void)printf("uid=%u", id);
220 if ((pw = getpwuid(id)))
221 (void)printf("(%s)", pw->pw_name);
222 if ((eid = geteuid()) != id) {
223 (void)printf(" euid=%u", eid);
224 if ((pw = getpwuid(eid)))
225 (void)printf("(%s)", pw->pw_name);
228 (void)printf(" gid=%u", id);
229 if ((gr = getgrgid(id)))
230 (void)printf("(%s)", gr->gr_name);
231 if ((eid = getegid()) != id) {
232 (void)printf(" egid=%u", eid);
233 if ((gr = getgrgid(eid)))
234 (void)printf("(%s)", gr->gr_name);
236 if ((ngroups = getgroups(NGROUPS, groups))) {
237 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
238 fmt = ", %u", lastid = id) {
242 (void)printf(fmt, id);
243 if ((gr = getgrgid(id)))
244 (void)printf("(%s)", gr->gr_name);
251 user(struct passwd *pw)
255 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1];
257 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
259 (void)printf(" gid=%u", gid);
260 if ((gr = getgrgid(gid)))
261 (void)printf("(%s)", gr->gr_name);
262 ngroups = NGROUPS + 1;
263 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
265 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
266 if (lastgid == (gid = groups[cnt]))
268 (void)printf(fmt, gid);
270 if ((gr = getgrgid(gid)))
271 (void)printf("(%s)", gr->gr_name);
278 group(struct passwd *pw, int nflag)
281 int cnt, id, lastid, ngroups;
282 gid_t groups[NGROUPS + 1];
286 ngroups = NGROUPS + 1;
287 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
289 groups[0] = getgid();
290 ngroups = getgroups(NGROUPS, groups + 1) + 1;
292 fmt = nflag ? "%s" : "%u";
293 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
294 if (lastid == (id = groups[cnt]))
297 if ((gr = getgrgid(id)))
298 (void)printf(fmt, gr->gr_name);
300 (void)printf(*fmt == ' ' ? " %u" : "%u",
304 (void)printf(fmt, id);
320 * Translate user argument into a pw pointer. First, try to
321 * get it as specified. If that fails, try it as a number.
323 if ((pw = getpwnam(u)))
325 id = strtol(u, &ep, 10);
326 if (*u && !*ep && (pw = getpwuid(id)))
328 errx(1, "%s: no such user", u);
333 pline(struct passwd *pw)
338 if ((pw = getpwuid(rid = getuid())) == NULL)
342 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
343 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
344 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
345 pw->pw_dir, pw->pw_shell);
354 (void)fprintf(stderr, "usage: groups [user]\n");
356 (void)fprintf(stderr, "usage: whoami\n");
358 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
360 " id -G [-n] [user]",
362 " id -g [-nr] [user]",
364 " id -u [-nr] [user]");