Mercurial > hg > toybox
comparison toys/posix/id.c @ 903:159b84d04b33
Make groups handle multiple usernames on command line.
author | Ivo van Poorten <ivopvp@gmail.com> |
---|---|
date | Sat, 18 May 2013 22:33:40 -0500 |
parents | 7b2e8f9db29b |
children | c644f85444d0 |
comparison
equal
deleted
inserted
replaced
902:36993c59a3d3 | 903:159b84d04b33 |
---|---|
64 struct group *group = getgrgid(gid); | 64 struct group *group = getgrgid(gid); |
65 if (!group) error_exit(NULL); | 65 if (!group) error_exit(NULL); |
66 return group; | 66 return group; |
67 } | 67 } |
68 | 68 |
69 void id_main(void) | 69 void do_id(char *username) |
70 { | 70 { |
71 int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g'; | 71 int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g'; |
72 struct passwd *pw; | 72 struct passwd *pw; |
73 struct group *grp; | 73 struct group *grp; |
74 uid_t uid = getuid(), euid = geteuid(); | 74 uid_t uid = getuid(), euid = geteuid(); |
78 toys.optflags |= FLAG_G | FLAG_n; | 78 toys.optflags |= FLAG_G | FLAG_n; |
79 | 79 |
80 flags = toys.optflags; | 80 flags = toys.optflags; |
81 | 81 |
82 // check if a username is given | 82 // check if a username is given |
83 if (*toys.optargs) { | 83 if (username) { |
84 if (!(pw = getpwnam(*toys.optargs))) | 84 if (!(pw = getpwnam(username))) |
85 error_exit("no such user '%s'", *toys.optargs); | 85 error_exit("no such user '%s'", username); |
86 uid = euid = pw->pw_uid; | 86 uid = euid = pw->pw_uid; |
87 gid = egid = pw->pw_gid; | 87 gid = egid = pw->pw_gid; |
88 if (cmd_groups) | 88 if (cmd_groups) |
89 printf("%s : ", pw->pw_name); | 89 printf("%s : ", pw->pw_name); |
90 } | 90 } |
114 showid(" groups=", grp->gr_gid, grp->gr_name); | 114 showid(" groups=", grp->gr_gid, grp->gr_name); |
115 } | 115 } |
116 | 116 |
117 groups = (gid_t *)toybuf; | 117 groups = (gid_t *)toybuf; |
118 i = sizeof(toybuf)/sizeof(gid_t); | 118 i = sizeof(toybuf)/sizeof(gid_t); |
119 ngroups = *toys.optargs ? getgrouplist(*toys.optargs, gid, groups, &i) : getgroups(i, groups); | 119 ngroups = username ? getgrouplist(username, gid, groups, &i) |
120 : getgroups(i, groups); | |
120 if (0 >= ngroups) perror_exit(0); | 121 if (0 >= ngroups) perror_exit(0); |
121 | 122 |
122 for (i = 0;;) { | 123 for (i = 0;;) { |
123 if (!(grp = getgrgid(groups[i]))) perror_msg(0); | 124 if (!(grp = getgrgid(groups[i]))) perror_msg(0); |
124 else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0); | 125 else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0); |
126 if (++i >= ngroups) break; | 127 if (++i >= ngroups) break; |
127 xputc(' '); | 128 xputc(' '); |
128 } | 129 } |
129 xputc('\n'); | 130 xputc('\n'); |
130 } | 131 } |
132 | |
133 void id_main(void) | |
134 { | |
135 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); | |
136 else do_id(NULL); | |
137 } |