summaryrefslogtreecommitdiff
path: root/usr.bin/id/id.c
diff options
context:
space:
mode:
authorOkan Demirmen <okan@cvs.openbsd.org>2013-09-06 19:48:47 +0000
committerOkan Demirmen <okan@cvs.openbsd.org>2013-09-06 19:48:47 +0000
commit899bf929460602235359a3d205d0e91fae3544e9 (patch)
tree495c0d833b859df1420193daf4bc8706796b0505 /usr.bin/id/id.c
parentbc23a9b3e6bdf0d746f65ad457049b06e8709f37 (diff)
teach id(1) about whoami(1) and groups(1) run modes; removes shell wrappers
OK millert@
Diffstat (limited to 'usr.bin/id/id.c')
-rw-r--r--usr.bin/id/id.c46
1 files changed, 36 insertions, 10 deletions
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 <sys/param.h>
+#include <err.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
@@ -38,7 +39,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <err.h>
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);
}