From 899bf929460602235359a3d205d0e91fae3544e9 Mon Sep 17 00:00:00 2001 From: Okan Demirmen Date: Fri, 6 Sep 2013 19:48:47 +0000 Subject: teach id(1) about whoami(1) and groups(1) run modes; removes shell wrappers OK millert@ --- usr.bin/id/id.c | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'usr.bin/id/id.c') diff --git a/usr.bin/id/id.c b/usr.bin/id/id.c index 690a1e0f0a6..ec63a27e78d 100644 --- a/usr.bin/id/id.c +++ b/usr.bin/id/id.c @@ -1,4 +1,4 @@ -/* $OpenBSD: id.c,v 1.19 2009/10/27 23:59:39 deraadt Exp $ */ +/* $OpenBSD: id.c,v 1.20 2013/09/06 19:48:46 okan Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -38,7 +39,6 @@ #include #include #include -#include void current(void); void pretty(struct passwd *); @@ -56,9 +56,26 @@ main(int argc, char *argv[]) int Gflag, ch, gflag, nflag, pflag, rflag, uflag; uid_t uid; gid_t gid; + const char *opts; Gflag = gflag = nflag = pflag = rflag = uflag = 0; - while ((ch = getopt(argc, argv, "Ggnpru")) != -1) + + if (strcmp(getprogname(), "groups") == 0) { + Gflag = 1; + nflag = 1; + opts = ""; + if (argc > 2) + usage(); + } else if (strcmp(getprogname(), "whoami") == 0) { + uflag = 1; + nflag = 1; + opts = ""; + if (argc > 1) + usage(); + } else + opts = "Ggnpru"; + + while ((ch = getopt(argc, argv, opts)) != -1) switch(ch) { case 'G': Gflag = 1; @@ -85,7 +102,7 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - switch(Gflag + gflag + pflag + uflag) { + switch (Gflag + gflag + pflag + uflag) { case 1: break; case 0: @@ -96,6 +113,9 @@ main(int argc, char *argv[]) usage(); } + if (strcmp(opts, "") != 0 && argc > 1) + usage(); + pw = *argv ? who(*argv) : NULL; if (gflag) { @@ -155,7 +175,7 @@ pretty(struct passwd *pw) (void)printf("uid\t%s\n", pw->pw_name); else (void)printf("uid\t%u\n", rid); - + if ((eid = geteuid()) != rid) { if ((pw = getpwuid(eid))) (void)printf("euid\t%s\n", pw->pw_name); @@ -302,10 +322,16 @@ who(char *u) void usage(void) { - (void)fprintf(stderr, "usage: id [user]\n" - " id -G [-n] [user]\n" - " id -g [-nr] [user]\n" - " id -p [user]\n" - " id -u [-nr] [user]\n"); + if (strcmp(getprogname(), "groups") == 0) { + (void)fprintf(stderr, "usage: groups [user]\n"); + } else if (strcmp(getprogname(), "whoami") == 0) { + (void)fprintf(stderr, "usage: whoami\n"); + } else { + (void)fprintf(stderr, "usage: id [user]\n"); + (void)fprintf(stderr, " id -G [-n] [user]\n"); + (void)fprintf(stderr, " id -g [-nr] [user]\n"); + (void)fprintf(stderr, " id -p [user]\n"); + (void)fprintf(stderr, " id -u [-nr] [user]\n"); + } exit(1); } -- cgit v1.2.3