annotate toys/posix/id.c @ 1130:6df194c6de88 draft

Add xgetpwnam() to lib/xwrap.c.
author Rob Landley <rob@landley.net>
date Thu, 28 Nov 2013 21:06:15 -0600
parents c644f85444d0
children 0f42d83199a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* id.c - print real and effective user and group IDs
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Sony Network Entertainment, Inc.
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * by Tim Bird <tim.bird@am.sony.com>
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
796
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
9 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
10 USE_ID_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
11
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 config ID
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 bool "id"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
14 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 usage: id [-nGgru]
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 Print user and group ID.
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
20 -n print names instead of numeric IDs (to be used with -Ggu)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
21 -G Show only the group IDs
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
22 -g Show only the effective group ID
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
23 -r Show real ID instead of effective ID
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
24 -u Show only the effective user ID
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
25
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
26 config ID_GROUPS
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
27 bool "groups"
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
28 default y
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
29 depends on ID
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
30 help
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
31 usage: groups [user]
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
32
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
33 Print the groups a user is in.
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
34
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 */
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
37 #define FOR_id
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 #include "toys.h"
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
39
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
40 static void s_or_u(char *s, unsigned u, int done)
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
41 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
42 if (toys.optflags & FLAG_n) printf("%s", s);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
43 else printf("%u", u);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 if (done) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 xputc('\n');
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
46 exit(0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
47 }
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
48 }
542
06763d1a2f9d Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
Rob Landley <rob@landley.net>
parents: 533
diff changeset
49
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
50 static void showid(char *header, unsigned u, char *s)
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
51 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
52 printf("%s%u(%s)", header, u, s);
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
53 }
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
54
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
55 void do_id(char *username)
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 {
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
57 int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
58 struct passwd *pw;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
59 struct group *grp;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
60 uid_t uid = getuid(), euid = geteuid();
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
61 gid_t gid = getgid(), egid = getegid(), *groups;
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
63 if (cmd_groups)
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
64 toys.optflags |= FLAG_G | FLAG_n;
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
65
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
66 flags = toys.optflags;
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
67
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
68 // check if a username is given
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
69 if (username) {
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
70 pw = xgetpwnam(username);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
71 uid = euid = pw->pw_uid;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
72 gid = egid = pw->pw_gid;
1130
6df194c6de88 Add xgetpwnam() to lib/xwrap.c.
Rob Landley <rob@landley.net>
parents: 1129
diff changeset
73 if (cmd_groups) printf("%s : ", pw->pw_name);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
74 }
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
75
899
7b2e8f9db29b add groups implementation to id.c
Ivo van poorten <ivopvp@gmail.com>
parents: 796
diff changeset
76 i = flags & FLAG_r;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
77 pw = xgetpwuid(i ? uid : euid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
78 if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
79
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
80 grp = xgetgrgid(i ? gid : egid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
81 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
82
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
83 if (!(flags & FLAG_G)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
84 showid("uid=", pw->pw_uid, pw->pw_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
85 showid(" gid=", grp->gr_gid, grp->gr_name);
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
86
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
87 if (!i) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
88 if (uid != euid) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
89 pw = xgetpwuid(euid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
90 showid(" euid=", pw->pw_uid, pw->pw_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
91 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
92 if (gid != egid) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
93 grp = xgetgrgid(egid);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
94 showid(" egid=", grp->gr_gid, grp->gr_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
95 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
96 }
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
97
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
98 showid(" groups=", grp->gr_gid, grp->gr_name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
99 }
543
60b97ba66a70 Extensive semi-gratuitous refactoring: factor out common code, handle euid!=uid and egid!=gid cases. (Note: test suite requires root access, possibly container support.)
Rob Landley <rob@landley.net>
parents: 542
diff changeset
100
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
101 groups = (gid_t *)toybuf;
796
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
102 i = sizeof(toybuf)/sizeof(gid_t);
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
103 ngroups = username ? getgrouplist(username, gid, groups, &i)
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
104 : getgroups(i, groups);
796
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
105 if (0 >= ngroups) perror_exit(0);
493
42a322adbd17 update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents: 430
diff changeset
106
796
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
107 for (i = 0;;) {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
108 if (!(grp = getgrgid(groups[i]))) perror_msg(0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
109 else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
110 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
796
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
111 if (++i >= ngroups) break;
50052f1368b9 id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
112 xputc(' ');
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
113 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
114 xputc('\n');
413
12add511705e Add id command from Tim Bird.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 }
903
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
116
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
117 void id_main(void)
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
118 {
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
119 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
120 else do_id(NULL);
159b84d04b33 Make groups handle multiple usernames on command line.
Ivo van Poorten <ivopvp@gmail.com>
parents: 899
diff changeset
121 }