Add id(1) -g, -n, -r options (Bug#302, reported by Aleksey Cheusov)

This commit is contained in:
David van Moolenbroek 2009-10-02 12:23:00 +00:00
parent 982f4a0cbb
commit e7dea9c609
2 changed files with 42 additions and 6 deletions

View file

@ -18,8 +18,8 @@ int main(int argc, char *argv[])
{
struct passwd *pwd;
struct group *grp;
uid_t ruid, euid;
gid_t rgid, egid;
uid_t ruid, euid, uid;
gid_t rgid, egid, gid;
#if __minix_vmd
uid_t suid;
gid_t sgid;
@ -36,7 +36,7 @@ int main(int argc, char *argv[])
#endif
int g;
int isug;
int c, uopt = 0;
int c, uopt = 0, gopt = 0, nopt = 0, ropt = 0;
#if __minix_vmd
get6id(&ruid, &euid, &suid, &rgid, &egid, &sgid);
@ -52,19 +52,50 @@ int main(int argc, char *argv[])
ngroups = getgroups(NGROUPS_MAX, groups);
#endif
while((c = getopt(argc, argv, "u")) != EOF) {
while((c = getopt(argc, argv, "ugnr")) != EOF) {
switch(c) {
case 'u':
uopt = 1;
break;
case 'g':
gopt = 1;
break;
case 'n':
nopt = 1;
break;
case 'r':
ropt = 1;
break;
default:
fprintf(stderr, "%s: unrecognized option\n", argv[0]);
return(1);
}
}
if(uopt && gopt) {
fprintf(stderr, "%s: cannot combine -u and -g\n", argv[0]);
return 1;
}
if((nopt || ropt) && !uopt && !gopt) {
fprintf(stderr, "%s: cannot use -n or -r without -u or -g\n", argv[0]);
return 1;
}
if(uopt) {
printf("%d\n", euid);
uid = ropt ? ruid : euid;
if (!nopt || (pwd = getpwuid(uid)) == NULL)
printf("%u\n", uid);
else
printf("%s\n", pwd->pw_name);
return 0;
}
if(gopt) {
gid = ropt ? rgid : egid;
if (!nopt || (grp = getgrgid(gid)) == NULL)
printf("%u\n", gid);
else
printf("%s\n", grp->gr_name);
return 0;
}

View file

@ -3,6 +3,7 @@
id \- print the uid and gid
.SH SYNOPSIS
\fBid\fR
.RB [ \-ugnr ]
.br
.de FL
.TP
@ -22,7 +23,11 @@ id \- print the uid and gid
If the effective uid and gid are different from the real ones, all of them
are printed.
.PP
Under Minix-vmd the supplementary group IDs are also printed.
If the \fB\-u\fP or \fB\-g\fP option is specified, then \fBid\fP will print
the effective uid or guid number, respectively, on a single line. Combined
with either of those options, the \fB\-n\fP option prints the name instead of
the number, and the \fB\-r\fP option prints the real instead of the effective
uid or gid.
.SH "SEE ALSO"
.BR getuid (2),
.BR getgid (2),