]>
git.saurik.com Git - apple/shell_cmds.git/blob - id/id.c
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";
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/usr.bin/id/id.c,v 1.19 2002/09/04 23:29:02 dwmalone Exp $");
48 #include <sys/param.h>
59 void pline(struct passwd
*);
60 void pretty(struct passwd
*);
61 void group(struct passwd
*, int);
63 void user(struct passwd
*);
67 int isgroups
, iswhoami
;
70 main(int argc
, char *argv
[])
74 int Gflag
, Pflag
, ch
, gflag
, id
, nflag
, pflag
, rflag
, uflag
;
77 Gflag
= Pflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
79 myname
= strrchr(argv
[0], '/');
80 myname
= (myname
!= NULL
) ? myname
+ 1 : argv
[0];
81 if (strcmp(myname
, "groups") == 0) {
85 else if (strcmp(myname
, "whoami") == 0) {
90 while ((ch
= getopt(argc
, argv
,
91 (isgroups
|| iswhoami
) ? "" : "PGgnpru")) != -1)
121 if (iswhoami
&& argc
> 0)
124 switch(Gflag
+ Pflag
+ gflag
+ pflag
+ uflag
) {
128 if (!nflag
&& !rflag
)
135 pw
= *argv
? who(*argv
) : NULL
;
138 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
139 if (nflag
&& (gr
= getgrgid(id
)))
140 (void)printf("%s\n", gr
->gr_name
);
142 (void)printf("%u\n", id
);
147 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
148 if (nflag
&& (pw
= getpwuid(id
)))
149 (void)printf("%s\n", pw
->pw_name
);
151 (void)printf("%u\n", id
);
178 pretty(struct passwd
*pw
)
185 (void)printf("uid\t%s\n", pw
->pw_name
);
186 (void)printf("groups\t");
189 if ((login
= getlogin()) == NULL
)
192 pw
= getpwuid(rid
= getuid());
193 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
194 (void)printf("login\t%s\n", login
);
196 (void)printf("uid\t%s\n", pw
->pw_name
);
198 (void)printf("uid\t%u\n", rid
);
200 if ((eid
= geteuid()) != rid
) {
201 if ((pw
= getpwuid(eid
)))
202 (void)printf("euid\t%s\n", pw
->pw_name
);
204 (void)printf("euid\t%u\n", eid
);
206 if ((rid
= getgid()) != (eid
= getegid())) {
207 if ((gr
= getgrgid(rid
)))
208 (void)printf("rgid\t%s\n", gr
->gr_name
);
210 (void)printf("rgid\t%u\n", rid
);
212 (void)printf("groups\t");
222 int cnt
, id
, eid
, gid
, egid
, lastid
, ngroups
;
223 gid_t groups
[NGROUPS
];
227 (void)printf("uid=%u", id
);
228 if ((pw
= getpwuid(id
)))
229 (void)printf("(%s)", pw
->pw_name
);
231 (void)printf(" gid=%u", gid
);
232 if ((gr
= getgrgid(gid
)))
233 (void)printf("(%s)", gr
->gr_name
);
234 if ((eid
= geteuid()) != id
) {
235 (void)printf(" euid=%u", eid
);
236 if ((pw
= getpwuid(eid
)))
237 (void)printf("(%s)", pw
->pw_name
);
239 if ((egid
= getegid()) != gid
) {
240 (void)printf(" egid=%u", egid
);
241 if ((gr
= getgrgid(egid
)))
242 (void)printf("(%s)", gr
->gr_name
);
244 if ((ngroups
= getgroups(NGROUPS
, groups
))) {
245 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
246 fmt
= ", %u", lastid
= id
) {
250 (void)printf(fmt
, id
);
251 if ((gr
= getgrgid(id
)))
252 (void)printf("(%s)", gr
->gr_name
);
259 user(struct passwd
*pw
)
263 int cnt
, gid
, lastgid
, ngroups
, groups
[NGROUPS
+ 1];
265 (void)printf("uid=%u(%s)", pw
->pw_uid
, pw
->pw_name
);
267 (void)printf(" gid=%u", gid
);
268 if ((gr
= getgrgid(gid
)))
269 (void)printf("(%s)", gr
->gr_name
);
270 ngroups
= NGROUPS
+ 1;
271 (void) getgrouplist(pw
->pw_name
, gid
, groups
, &ngroups
);
273 for (lastgid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
274 if (lastgid
== (gid
= groups
[cnt
]))
276 (void)printf(fmt
, gid
);
278 if ((gr
= getgrgid(gid
)))
279 (void)printf("(%s)", gr
->gr_name
);
286 group(struct passwd
*pw
, int nflag
)
289 int cnt
, id
, lastid
, ngroups
;
290 gid_t groups
[NGROUPS
+ 1];
294 ngroups
= NGROUPS
+ 1;
295 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
);
297 groups
[0] = getgid();
298 ngroups
= getgroups(NGROUPS
, groups
+ 1) + 1;
300 fmt
= nflag
? "%s" : "%u";
301 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
302 if (lastid
== (id
= groups
[cnt
]))
305 if ((gr
= getgrgid(id
)))
306 (void)printf(fmt
, gr
->gr_name
);
308 (void)printf(*fmt
== ' ' ? " %u" : "%u",
312 (void)printf(fmt
, id
);
328 * Translate user argument into a pw pointer. First, try to
329 * get it as specified. If that fails, try it as a number.
331 if ((pw
= getpwnam(u
)))
333 id
= strtol(u
, &ep
, 10);
334 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
336 errx(1, "%s: no such user", u
);
341 pline(struct passwd
*pw
)
346 if ((pw
= getpwuid(rid
= getuid())) == NULL
)
350 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw
->pw_name
,
351 pw
->pw_passwd
, pw
->pw_uid
, pw
->pw_gid
, pw
->pw_class
,
352 (long)pw
->pw_change
, (long)pw
->pw_expire
, pw
->pw_gecos
,
353 pw
->pw_dir
, pw
->pw_shell
);
362 (void)fprintf(stderr
, "usage: groups [user]\n");
364 (void)fprintf(stderr
, "usage: whoami\n");
366 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n",
368 " id -G [-n] [user]",
370 " id -g [-nr] [user]",
372 " id -u [-nr] [user]");