From: David van Moolenbroek Date: Fri, 2 Oct 2009 12:23:00 +0000 (+0000) Subject: Add id(1) -g, -n, -r options (Bug#302, reported by Aleksey Cheusov) X-Git-Tag: v3.1.5~37 X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=e7dea9c6095321013f36eac1842d1d60d04a1cad;p=minix.git Add id(1) -g, -n, -r options (Bug#302, reported by Aleksey Cheusov) --- diff --git a/commands/simple/id.c b/commands/simple/id.c index b2e6ae3fc..b1b044f97 100755 --- a/commands/simple/id.c +++ b/commands/simple/id.c @@ -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; } diff --git a/man/man1/id.1 b/man/man1/id.1 index ba3d6a311..4cfbd8184 100644 --- a/man/man1/id.1 +++ b/man/man1/id.1 @@ -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),