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 passwd *pwd;
struct group *grp; struct group *grp;
uid_t ruid, euid; uid_t ruid, euid, uid;
gid_t rgid, egid; gid_t rgid, egid, gid;
#if __minix_vmd #if __minix_vmd
uid_t suid; uid_t suid;
gid_t sgid; gid_t sgid;
@ -36,7 +36,7 @@ int main(int argc, char *argv[])
#endif #endif
int g; int g;
int isug; int isug;
int c, uopt = 0; int c, uopt = 0, gopt = 0, nopt = 0, ropt = 0;
#if __minix_vmd #if __minix_vmd
get6id(&ruid, &euid, &suid, &rgid, &egid, &sgid); get6id(&ruid, &euid, &suid, &rgid, &egid, &sgid);
@ -52,19 +52,50 @@ int main(int argc, char *argv[])
ngroups = getgroups(NGROUPS_MAX, groups); ngroups = getgroups(NGROUPS_MAX, groups);
#endif #endif
while((c = getopt(argc, argv, "u")) != EOF) { while((c = getopt(argc, argv, "ugnr")) != EOF) {
switch(c) { switch(c) {
case 'u': case 'u':
uopt = 1; uopt = 1;
break; break;
case 'g':
gopt = 1;
break;
case 'n':
nopt = 1;
break;
case 'r':
ropt = 1;
break;
default: default:
fprintf(stderr, "%s: unrecognized option\n", argv[0]); fprintf(stderr, "%s: unrecognized option\n", argv[0]);
return(1); 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) { 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; return 0;
} }

View file

@ -3,6 +3,7 @@
id \- print the uid and gid id \- print the uid and gid
.SH SYNOPSIS .SH SYNOPSIS
\fBid\fR \fBid\fR
.RB [ \-ugnr ]
.br .br
.de FL .de FL
.TP .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 If the effective uid and gid are different from the real ones, all of them
are printed. are printed.
.PP .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" .SH "SEE ALSO"
.BR getuid (2), .BR getuid (2),
.BR getgid (2), .BR getgid (2),