Fix manual pages for s/struct device */device_t/.
[dragonfly.git] / usr.bin / id / id.c
blob6818554798d513eb8f1f7b94dd88dad53c2800cd
1 /*-
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
7 * are met:
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
27 * SUCH DAMAGE.
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>
36 #include <err.h>
37 #include <grp.h>
38 #include <pwd.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.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 *
51 who(char *);
53 int isgroups, iswhoami;
55 int
56 main(int argc, char **argv)
58 struct group *gr;
59 struct passwd *pw;
60 int Gflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
61 const char *myname;
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) {
68 isgroups = 1;
69 Gflag = nflag = 1;
71 else if (strcmp(myname, "whoami") == 0) {
72 iswhoami = 1;
73 uflag = nflag = 1;
76 while ((ch = getopt(argc, argv,
77 (isgroups || iswhoami) ? "" : "PGgnpru")) != -1)
78 switch(ch) {
79 case 'G':
80 Gflag = 1;
81 break;
82 case 'P':
83 Pflag = 1;
84 break;
85 case 'g':
86 gflag = 1;
87 break;
88 case 'n':
89 nflag = 1;
90 break;
91 case 'p':
92 pflag = 1;
93 break;
94 case 'r':
95 rflag = 1;
96 break;
97 case 'u':
98 uflag = 1;
99 break;
100 case '?':
101 default:
102 usage();
104 argc -= optind;
105 argv += optind;
107 if (iswhoami && argc > 0)
108 usage();
110 switch(Gflag + Pflag + gflag + pflag + uflag) {
111 case 1:
112 break;
113 case 0:
114 if (!nflag && !rflag)
115 break;
116 /* FALLTHROUGH */
117 default:
118 usage();
121 pw = *argv ? who(*argv) : NULL;
123 if (gflag) {
124 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
125 if (nflag && (gr = getgrgid(id)))
126 printf("%s\n", gr->gr_name);
127 else
128 printf("%u\n", id);
129 exit(0);
132 if (uflag) {
133 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
134 if (nflag && (pw = getpwuid(id)))
135 printf("%s\n", pw->pw_name);
136 else
137 printf("%u\n", id);
138 exit(0);
141 if (Gflag) {
142 group(pw, nflag);
143 exit(0);
146 if (Pflag) {
147 pline(pw);
148 exit(0);
151 if (pflag) {
152 pretty(pw);
153 exit(0);
156 if (pw)
157 user(pw);
158 else
159 current();
160 exit(0);
163 static void
164 pretty(struct passwd *pw)
166 struct group *gr;
167 u_int eid, rid;
168 char *login;
170 if (pw) {
171 printf("uid\t%s\n", pw->pw_name);
172 printf("groups\t");
173 group(pw, 1);
174 } else {
175 if ((login = getlogin()) == NULL)
176 err(1, "getlogin");
178 pw = getpwuid(rid = getuid());
179 if (pw == NULL || strcmp(login, pw->pw_name))
180 printf("login\t%s\n", login);
181 if (pw)
182 printf("uid\t%s\n", pw->pw_name);
183 else
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);
189 else
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);
195 else
196 printf("rgid\t%u\n", rid);
198 printf("groups\t");
199 group(NULL, 1);
203 static void
204 current(void)
206 struct group *gr;
207 struct passwd *pw;
208 int cnt, id, eid, lastid, ngroups;
209 gid_t groups[NGROUPS];
210 const char *fmt;
212 id = getuid();
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);
221 id = getgid();
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) {
233 id = groups[cnt++];
234 if (lastid == id)
235 continue;
236 printf(fmt, id);
237 if ((gr = getgrgid(id)))
238 printf("(%s)", gr->gr_name);
241 printf("\n");
244 static void
245 user(struct passwd *pw)
247 struct group *gr;
248 const char *fmt;
249 int cnt, ngroups;
250 gid_t gid, lastgid, groups[NGROUPS + 1];
252 printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
253 gid = pw->pw_gid;
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);
259 fmt = " groups=%u";
260 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
261 if (lastgid == (gid = groups[cnt]))
262 continue;
263 printf(fmt, gid);
264 fmt = ", %u";
265 if ((gr = getgrgid(gid)))
266 printf("(%s)", gr->gr_name);
267 lastgid = gid;
269 printf("\n");
272 static void
273 group(struct passwd *pw, int nflag)
275 struct group *gr;
276 int cnt, id, lastid, ngroups;
277 gid_t groups[NGROUPS + 1];
278 const char *fmt;
280 if (pw) {
281 ngroups = NGROUPS + 1;
282 getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
283 } else {
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]))
290 continue;
291 if (nflag) {
292 if ((gr = getgrgid(id)))
293 printf(fmt, gr->gr_name);
294 else
295 printf(*fmt == ' ' ? " %u" : "%u",
296 id);
297 fmt = " %s";
298 } else {
299 printf(fmt, id);
300 fmt = " %u";
302 lastid = id;
304 printf("\n");
307 static struct passwd *
308 who(char *u)
310 struct passwd *pw;
311 long id;
312 char *ep;
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)))
319 return(pw);
320 id = strtol(u, &ep, 10);
321 if (*u && !*ep && (pw = getpwuid(id)))
322 return(pw);
323 errx(1, "%s: no such user", u);
324 /* NOTREACHED */
327 static void
328 pline(struct passwd *pw)
330 u_int rid;
332 if (!pw) {
333 if ((pw = getpwuid(rid = getuid())) == NULL)
334 err(1, "getpwuid");
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);
344 static void
345 usage(void)
348 if (isgroups)
349 fprintf(stderr, "usage: groups [user]\n");
350 else if (iswhoami)
351 fprintf(stderr, "usage: whoami\n");
352 else
353 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
354 "usage: id [user]",
355 " id -G [-n] [user]",
356 " id -P [user]",
357 " id -g [-nr] [user]",
358 " id -p [user]",
359 " id -u [-nr] [user]");
360 exit(1);