Hi,

Attached are two commits for id(1), the first removes curproc() as we
can reuse the now-present user() function. The second adds support for
uid arguments.

uid arguments were mentioned in a previous commit, and it's trivial to
support them, but GNU id(1) doesn't, so it might not be worth the
hassle.

Patch is attached anyway.

Cheers,
Rob
From fad6b54c1c84031291b76644aa9d944ec1dcd091 Mon Sep 17 00:00:00 2001
From: Rob Pilling <robpill...@gmail.com>
Date: Sun, 1 Dec 2013 11:40:49 +0000
Subject: [PATCH 2/2] id(1) can handle uid arguments

---
 id.1 |    6 +++---
 id.c |   51 ++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/id.1 b/id.1
index 8252773..6bfa06b 100644
--- a/id.1
+++ b/id.1
@@ -3,10 +3,10 @@
 id \- print real and effective user and group IDs
 .SH SYNOPSIS
 .B id
-.RB [ user ]
+.RB [ user | uid ]
 .SH DESCRIPTION
 Print user and group information of the calling process to standard output.
-If a login name is specified, the user and group information of that user
-is displayed.
+If a login name or uid is specified, the user and group information of that
+user is displayed.
 .SH SEE ALSO
 .IR who(1)
diff --git a/id.c b/id.c
index ce618fd..78fa71d 100644
--- a/id.c
+++ b/id.c
@@ -7,21 +7,22 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
+#include <ctype.h>
 #include "util.h"
 
 static void user(struct passwd *pw);
+static void userid(uid_t id);
+static void usernam(const char *nam);
 
 static void
 usage(void)
 {
-	eprintf("usage: %s [user]\n", argv0);
+	eprintf("usage: %s [user | uid]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
-	struct passwd *pw;
-
 	ARGBEGIN {
 	default:
 		usage();
@@ -30,20 +31,14 @@ main(int argc, char *argv[])
 	errno = 0;
 	switch (argc) {
 	case 0:
-		pw = getpwuid(getuid());
-		if (errno != 0)
-			eprintf("getpwuid %d:", getuid());
-		else if (!pw)
-			eprintf("getpwuid %d: no such user\n", getuid());
-		user(pw);
+		userid(getuid());
 		break;
 	case 1:
-		pw = getpwnam(argv[0]);
-		if (errno != 0)
-			eprintf("getpwnam %s:", argv[0]);
-		else if (!pw)
-			eprintf("getpwnam %s: no such user\n", argv[0]);
-		user(pw);
+		/* user names can't begin [0-9] */
+		if (isdigit(argv[0][0]))
+			userid(estrtol(argv[0], 0));
+		else
+			usernam(argv[0]);
 		break;
 	default:
 		usage();
@@ -52,6 +47,32 @@ main(int argc, char *argv[])
 	return EXIT_SUCCESS;
 }
 
+static void usernam(const char *nam)
+{
+	struct passwd *pw;
+
+	errno = 0;
+	pw = getpwnam(nam);
+	if (errno != 0)
+		eprintf("getpwnam %s:", nam);
+	else if (!pw)
+		eprintf("getpwnam %s: no such user\n", nam);
+	user(pw);
+}
+
+static void userid(uid_t id)
+{
+	struct passwd *pw;
+
+	errno = 0;
+	pw = getpwuid(id);
+	if (errno != 0)
+		eprintf("getpwuid %d:", id);
+	else if (!pw)
+		eprintf("getpwuid %d: no such user\n", id);
+	user(pw);
+}
+
 static void
 user(struct passwd *pw)
 {
-- 
1.7.10.4

From 4c8bad86bc817974603304cb6e7db7db617daec1 Mon Sep 17 00:00:00 2001
From: Rob Pilling <robpill...@gmail.com>
Date: Sun, 1 Dec 2013 11:31:36 +0000
Subject: [PATCH 1/2] curproc() isn't needed in id(1)

---
 id.c |   60 +++++++-----------------------------------------------------
 1 file changed, 7 insertions(+), 53 deletions(-)

diff --git a/id.c b/id.c
index ce72e70..ce618fd 100644
--- a/id.c
+++ b/id.c
@@ -10,7 +10,6 @@
 #include "util.h"
 
 static void user(struct passwd *pw);
-static void curproc(void);
 
 static void
 usage(void)
@@ -28,12 +27,17 @@ main(int argc, char *argv[])
 		usage();
 	} ARGEND;
 
+	errno = 0;
 	switch (argc) {
 	case 0:
-		curproc();
+		pw = getpwuid(getuid());
+		if (errno != 0)
+			eprintf("getpwuid %d:", getuid());
+		else if (!pw)
+			eprintf("getpwuid %d: no such user\n", getuid());
+		user(pw);
 		break;
 	case 1:
-		errno = 0;
 		pw = getpwnam(argv[0]);
 		if (errno != 0)
 			eprintf("getpwnam %s:", argv[0]);
@@ -73,53 +77,3 @@ user(struct passwd *pw)
 	}
 	putchar('\n');
 }
-
-static void
-curproc(void)
-{
-	struct passwd *pw;
-	struct group *gr;
-	uid_t uid, euid;
-	gid_t gid, egid, groups[NGROUPS_MAX];
-	int ngroups;
-	int i;
-
-	/* Print uid/euid info */
-	uid = getuid();
-	printf("uid=%u", uid);
-	if (!(pw = getpwuid(uid)))
-		eprintf("getpwuid:");
-	printf("(%s)", pw->pw_name);
-	if ((euid = geteuid()) != uid) {
-		printf(" euid=%u", euid);
-		if (!(pw = getpwuid(euid)))
-			eprintf("getpwuid:");
-		printf("(%s)", pw->pw_name);
-	}
-
-	/* Print gid/egid info */
-	gid = getgid();
-	printf(" gid=%u", gid);
-	if (!(gr = getgrgid(gid)))
-		eprintf("getgrgid:");
-	printf("(%s)", gr->gr_name);
-	if ((egid = getegid()) != gid) {
-		printf(" egid=%u", egid);
-		if (!(gr = getgrgid(egid)))
-			eprintf("getgrgid:");
-		printf("(%s)", gr->gr_name);
-	}
-
-	/* Print groups */
-	ngroups = getgroups(NGROUPS_MAX, groups);
-	if (ngroups < 0)
-		eprintf("getgroups:");
-	for (i = 0; i < ngroups; i++) {
-		gid = groups[i];
-		printf("%s%u", !i ? " groups=" : ",", gid);
-		if (!(gr = getgrgid(gid)))
-			eprintf("getgrgid:");
-		printf("(%s)", gr->gr_name);
-	}
-	putchar('\n');
-}
-- 
1.7.10.4

Reply via email to