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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)id.c 8.2 (Berkeley) 2/16/94
31 * $FreeBSD: src/usr.bin/id/id.c,v 1.12.2.3 2001/12/20 12:09:03 ru Exp $
34 #include <sys/param.h>
44 static void current(void);
45 static void pline(struct passwd
*);
46 static void pretty(struct passwd
*);
47 static void group(struct passwd
*, int);
48 static void usage(void);
49 static void user(struct passwd
*);
50 static struct passwd
*
53 int isgroups
, iswhoami
;
56 main(int argc
, char **argv
)
60 int Gflag
, Pflag
, ch
, gflag
, id
, nflag
, pflag
, rflag
, uflag
;
63 Gflag
= Pflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
65 myname
= strrchr(argv
[0], '/');
66 myname
= (myname
!= NULL
) ? myname
+ 1 : argv
[0];
67 if (strcmp(myname
, "groups") == 0) {
71 else if (strcmp(myname
, "whoami") == 0) {
76 while ((ch
= getopt(argc
, argv
,
77 (isgroups
|| iswhoami
) ? "" : "PGgnpru")) != -1)
107 if (iswhoami
&& argc
> 0)
110 switch(Gflag
+ Pflag
+ gflag
+ pflag
+ uflag
) {
114 if (!nflag
&& !rflag
)
121 pw
= *argv
? who(*argv
) : NULL
;
124 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
125 if (nflag
&& (gr
= getgrgid(id
)))
126 printf("%s\n", gr
->gr_name
);
133 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
134 if (nflag
&& (pw
= getpwuid(id
)))
135 printf("%s\n", pw
->pw_name
);
164 pretty(struct passwd
*pw
)
171 printf("uid\t%s\n", pw
->pw_name
);
175 if ((login
= getlogin()) == NULL
)
178 pw
= getpwuid(rid
= getuid());
179 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
180 printf("login\t%s\n", login
);
182 printf("uid\t%s\n", pw
->pw_name
);
184 printf("uid\t%u\n", rid
);
186 if ((eid
= geteuid()) != rid
) {
187 if ((pw
= getpwuid(eid
)))
188 printf("euid\t%s\n", pw
->pw_name
);
190 printf("euid\t%u\n", eid
);
192 if ((rid
= getgid()) != (eid
= getegid())) {
193 if ((gr
= getgrgid(rid
)))
194 printf("rgid\t%s\n", gr
->gr_name
);
196 printf("rgid\t%u\n", rid
);
208 int cnt
, id
, eid
, lastid
, ngroups
;
209 gid_t groups
[NGROUPS
];
213 printf("uid=%u", id
);
214 if ((pw
= getpwuid(id
)))
215 printf("(%s)", pw
->pw_name
);
216 if ((eid
= geteuid()) != id
) {
217 printf(" euid=%u", eid
);
218 if ((pw
= getpwuid(eid
)))
219 printf("(%s)", pw
->pw_name
);
222 printf(" gid=%u", id
);
223 if ((gr
= getgrgid(id
)))
224 printf("(%s)", gr
->gr_name
);
225 if ((eid
= getegid()) != id
) {
226 printf(" egid=%u", eid
);
227 if ((gr
= getgrgid(eid
)))
228 printf("(%s)", gr
->gr_name
);
230 if ((ngroups
= getgroups(NGROUPS
, groups
))) {
231 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
232 fmt
= ", %u", lastid
= id
) {
237 if ((gr
= getgrgid(id
)))
238 printf("(%s)", gr
->gr_name
);
245 user(struct passwd
*pw
)
250 gid_t gid
, lastgid
, groups
[NGROUPS
+ 1];
252 printf("uid=%u(%s)", pw
->pw_uid
, pw
->pw_name
);
254 printf(" gid=%u", gid
);
255 if ((gr
= getgrgid(gid
)))
256 printf("(%s)", gr
->gr_name
);
257 ngroups
= NGROUPS
+ 1;
258 getgrouplist(pw
->pw_name
, gid
, groups
, &ngroups
);
260 for (lastgid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
261 if (lastgid
== (gid
= groups
[cnt
]))
265 if ((gr
= getgrgid(gid
)))
266 printf("(%s)", gr
->gr_name
);
273 group(struct passwd
*pw
, int nflag
)
276 int cnt
, id
, lastid
, ngroups
;
277 gid_t groups
[NGROUPS
+ 1];
281 ngroups
= NGROUPS
+ 1;
282 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 printf(fmt
, gr
->gr_name
);
295 printf(*fmt
== ' ' ? " %u" : "%u",
307 static struct passwd
*
315 * Translate user argument into a pw pointer. First, try to
316 * get it as specified. If that fails, try it as a number.
318 if ((pw
= getpwnam(u
)))
320 id
= strtol(u
, &ep
, 10);
321 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
323 errx(1, "%s: no such user", u
);
328 pline(struct passwd
*pw
)
333 if ((pw
= getpwuid(rid
= getuid())) == NULL
)
337 printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw
->pw_name
,
338 pw
->pw_passwd
, pw
->pw_uid
, pw
->pw_gid
, pw
->pw_class
,
339 (long)pw
->pw_change
, (long)pw
->pw_expire
, pw
->pw_gecos
,
340 pw
->pw_dir
, pw
->pw_shell
);
349 fprintf(stderr
, "usage: groups [user]\n");
351 fprintf(stderr
, "usage: whoami\n");
353 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n",
355 " id -G [-n] [user]",
357 " id -g [-nr] [user]",
359 " id -u [-nr] [user]");