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 int main __P((int, char *[]));
59 void current __P((void));
60 void pline __P((struct passwd *));
61 void pretty __P((struct passwd *));
62 void group __P((struct passwd *, int));
63 void usage __P((void));
64 void user __P((struct passwd *));
75 int Gflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
77 Gflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
78 while ((ch = getopt(argc, argv, "PGgnpru")) != -1)
108 switch(Gflag + Pflag + gflag + pflag + uflag) {
112 if (!nflag && !rflag)
119 pw = *argv ? who(*argv) : NULL;
122 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
123 if (nflag && (gr = getgrgid(id)))
124 (void)printf("%s\n", gr->gr_name);
126 (void)printf("%u\n", id);
131 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
132 if (nflag && (pw = getpwuid(id)))
133 (void)printf("%s\n", pw->pw_name);
135 (void)printf("%u\n", id);
170 (void)printf("uid\t%s\n", pw->pw_name);
171 (void)printf("groups\t");
174 if ((login = getlogin()) == NULL)
177 pw = getpwuid(rid = getuid());
178 if (pw == NULL || strcmp(login, pw->pw_name))
179 (void)printf("login\t%s\n", login);
181 (void)printf("uid\t%s\n", pw->pw_name);
183 (void)printf("uid\t%u\n", rid);
185 if ((eid = geteuid()) != rid) {
186 if ((pw = getpwuid(eid)))
187 (void)printf("euid\t%s\n", pw->pw_name);
189 (void)printf("euid\t%u\n", eid);
191 if ((rid = getgid()) != (eid = getegid())) {
192 if ((gr = getgrgid(rid)))
193 (void)printf("rgid\t%s\n", gr->gr_name);
195 (void)printf("rgid\t%u\n", rid);
197 (void)printf("groups\t");
207 int cnt, id, eid, lastid, ngroups;
208 gid_t groups[NGROUPS];
212 (void)printf("uid=%u", id);
213 if ((pw = getpwuid(id)))
214 (void)printf("(%s)", pw->pw_name);
215 if ((eid = geteuid()) != id) {
216 (void)printf(" euid=%u", eid);
217 if ((pw = getpwuid(eid)))
218 (void)printf("(%s)", pw->pw_name);
221 (void)printf(" gid=%u", id);
222 if ((gr = getgrgid(id)))
223 (void)printf("(%s)", gr->gr_name);
224 if ((eid = getegid()) != id) {
225 (void)printf(" egid=%u", eid);
226 if ((gr = getgrgid(eid)))
227 (void)printf("(%s)", gr->gr_name);
229 if ((ngroups = getgroups(NGROUPS, groups))) {
230 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
231 fmt = ", %u", lastid = id) {
235 (void)printf(fmt, id);
236 if ((gr = getgrgid(id)))
237 (void)printf("(%s)", gr->gr_name);
249 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1];
251 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
253 (void)printf(" gid=%u", gid);
254 if ((gr = getgrgid(gid)))
255 (void)printf("(%s)", gr->gr_name);
256 ngroups = NGROUPS + 1;
257 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
259 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
260 if (lastgid == (gid = groups[cnt]))
262 (void)printf(fmt, gid);
264 if ((gr = getgrgid(gid)))
265 (void)printf("(%s)", gr->gr_name);
277 int cnt, id, lastid, ngroups;
278 gid_t groups[NGROUPS + 1];
282 ngroups = NGROUPS + 1;
283 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
285 groups[0] = getgid();
286 ngroups = getgroups(NGROUPS, groups + 1) + 1;
288 fmt = nflag ? "%s" : "%u";
289 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
290 if (lastid == (id = groups[cnt]))
293 if ((gr = getgrgid(id)))
294 (void)printf(fmt, gr->gr_name);
296 (void)printf(*fmt == ' ' ? " %u" : "%u",
300 (void)printf(fmt, id);
317 * Translate user argument into a pw pointer. First, try to
318 * get it as specified. If that fails, try it as a number.
320 if ((pw = getpwnam(u)))
322 id = strtol(u, &ep, 10);
323 if (*u && !*ep && (pw = getpwuid(id)))
325 errx(1, "%s: no such user", u);
336 if ((pw = getpwuid(rid = getuid())) == NULL)
340 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
341 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
342 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
343 pw->pw_dir, pw->pw_shell);
350 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
352 " id -G [-n] [user]",
354 " id -g [-nr] [user]",
356 " id -u [-nr] [user]");