Mercurial > hg > toybox
comparison toys/posix/id.c @ 1503:96bdf979d9e6 draft
Untangle id/groups/logname so single.sh can build each one standalone.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 27 Sep 2014 20:28:33 -0500 |
parents | 855728acd1ad |
children | 4bfbd8b96f66 |
comparison
equal
deleted
inserted
replaced
1502:84922376d15c | 1503:96bdf979d9e6 |
---|---|
5 * by Tim Bird <tim.bird@am.sony.com> | 5 * by Tim Bird <tim.bird@am.sony.com> |
6 * | 6 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html | 7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html |
8 | 8 |
9 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN)) | 9 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN)) |
10 USE_ID_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN)) | 10 USE_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN)) |
11 USE_ID_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN)) | 11 USE_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN)) |
12 USE_ID_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN)) | 12 USE_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN)) |
13 | 13 |
14 config ID | 14 config ID |
15 bool "id" | 15 bool "id" |
16 default y | 16 default y |
17 help | 17 help |
23 -G Show only the group IDs | 23 -G Show only the group IDs |
24 -g Show only the effective group ID | 24 -g Show only the effective group ID |
25 -r Show real ID instead of effective ID | 25 -r Show real ID instead of effective ID |
26 -u Show only the effective user ID | 26 -u Show only the effective user ID |
27 | 27 |
28 config ID_GROUPS | 28 config GROUPS |
29 bool "groups" | 29 bool "groups" |
30 default y | 30 default y |
31 depends on ID | |
32 help | 31 help |
33 usage: groups [user] | 32 usage: groups [user] |
34 | 33 |
35 Print the groups a user is in. | 34 Print the groups a user is in. |
36 | 35 |
37 config ID_LOGNAME | 36 config LOGNAME |
38 bool "logname" | 37 bool "logname" |
39 default y | 38 default y |
40 depends on ID | |
41 help | 39 help |
42 usage: logname | 40 usage: logname |
43 | 41 |
44 Print the current user name. | 42 Print the current user name. |
45 | 43 |
44 config WHOAMI | |
45 bool "whoami" | |
46 default y | |
47 help | |
48 usage: whoami | |
49 | |
50 Print the current user name. | |
46 */ | 51 */ |
47 | 52 |
48 #define FOR_id | 53 #define FOR_id |
49 #include "toys.h" | 54 #include "toys.h" |
50 | 55 |
56 GLOBALS( | |
57 int do_u, do_n, do_G, is_groups; | |
58 ) | |
59 | |
51 static void s_or_u(char *s, unsigned u, int done) | 60 static void s_or_u(char *s, unsigned u, int done) |
52 { | 61 { |
53 if (toys.optflags & FLAG_n) printf("%s", s); | 62 if (TT.do_n) printf("%s", s); |
54 else printf("%u", u); | 63 else printf("%u", u); |
55 if (done) { | 64 if (done) { |
56 xputc('\n'); | 65 xputc('\n'); |
57 exit(0); | 66 exit(0); |
58 } | 67 } |
63 printf("%s%u(%s)", header, u, s); | 72 printf("%s%u(%s)", header, u, s); |
64 } | 73 } |
65 | 74 |
66 void do_id(char *username) | 75 void do_id(char *username) |
67 { | 76 { |
68 int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g'; | 77 int flags, i, ngroups; |
69 struct passwd *pw; | 78 struct passwd *pw; |
70 struct group *grp; | 79 struct group *grp; |
71 uid_t uid = getuid(), euid = geteuid(); | 80 uid_t uid = getuid(), euid = geteuid(); |
72 gid_t gid = getgid(), egid = getegid(), *groups; | 81 gid_t gid = getgid(), egid = getegid(), *groups; |
73 | |
74 if (cmd_groups) | |
75 toys.optflags |= FLAG_G | FLAG_n; | |
76 | 82 |
77 flags = toys.optflags; | 83 flags = toys.optflags; |
78 | 84 |
79 // check if a username is given | 85 // check if a username is given |
80 if (username) { | 86 if (username) { |
81 pw = xgetpwnam(username); | 87 pw = xgetpwnam(username); |
82 uid = euid = pw->pw_uid; | 88 uid = euid = pw->pw_uid; |
83 gid = egid = pw->pw_gid; | 89 gid = egid = pw->pw_gid; |
84 if (cmd_groups) printf("%s : ", pw->pw_name); | 90 if (TT.is_groups) printf("%s : ", pw->pw_name); |
85 } | 91 } |
86 | 92 |
87 i = flags & FLAG_r; | 93 i = flags & FLAG_r; |
88 pw = xgetpwuid(i ? uid : euid); | 94 pw = xgetpwuid(i ? uid : euid); |
89 if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1); | 95 if (TT.do_u) s_or_u(pw->pw_name, pw->pw_uid, 1); |
90 | 96 |
91 grp = xgetgrgid(i ? gid : egid); | 97 grp = xgetgrgid(i ? gid : egid); |
92 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1); | 98 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1); |
93 | 99 |
94 if (!(flags & FLAG_G)) { | 100 if (!TT.do_G) { |
95 showid("uid=", pw->pw_uid, pw->pw_name); | 101 showid("uid=", pw->pw_uid, pw->pw_name); |
96 showid(" gid=", grp->gr_gid, grp->gr_name); | 102 showid(" gid=", grp->gr_gid, grp->gr_name); |
97 | 103 |
98 if (!i) { | 104 if (!i) { |
99 if (uid != euid) { | 105 if (uid != euid) { |
114 ngroups = username ? getgrouplist(username, gid, groups, &i) | 120 ngroups = username ? getgrouplist(username, gid, groups, &i) |
115 : getgroups(i, groups); | 121 : getgroups(i, groups); |
116 if (ngroups<0) perror_exit(0); | 122 if (ngroups<0) perror_exit(0); |
117 | 123 |
118 for (i = 0; i<ngroups; i++) { | 124 for (i = 0; i<ngroups; i++) { |
119 if (i) xputc(' '); | 125 if (i || !TT.do_G) xputc(' '); |
120 if (!(grp = getgrgid(groups[i]))) perror_msg(0); | 126 if (!(grp = getgrgid(groups[i]))) perror_msg(0); |
121 else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0); | 127 else if (TT.do_G) s_or_u(grp->gr_name, grp->gr_gid, 0); |
122 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name); | 128 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name); |
123 } | 129 } |
124 xputc('\n'); | 130 xputc('\n'); |
125 } | 131 } |
126 | 132 |
127 void id_main(void) | 133 void id_main(void) |
128 { | 134 { |
129 if (toys.which->name[0] > 'i') toys.optflags = (FLAG_u | FLAG_n); | 135 // FLAG macros can be 0 if "id" command enabled, so snapshot them here. |
136 if (FLAG_u) TT.do_u = toys.optflags & FLAG_u; | |
137 if (FLAG_n) TT.do_n = toys.optflags & FLAG_n; | |
138 if (FLAG_G) TT.do_G = toys.optflags & FLAG_G; | |
139 | |
140 // And set the variables for non-id commands. | |
141 TT.is_groups = toys.which->name[0] == 'g'; | |
142 if (TT.is_groups) TT.do_G = TT.do_n = 1; | |
143 else if (toys.which->name[0] != 'i') TT.do_u = TT.do_n = 1; | |
144 | |
130 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); | 145 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); |
131 else do_id(NULL); | 146 else do_id(NULL); |
132 } | 147 } |