[hackers] [sbase] id(1) can handle uid arguments || Rob Pilling

From: <git_AT_suckless.org>
Date: Sun, 01 Dec 2013 15:29:23 +0100

commit d453be2ae1fc8fd84578cf5b9c56f6d1664fb3dd
Author: Rob Pilling <robpilling_AT_gmail.com>
Date: Sun Dec 1 11:40:49 2013 +0000

    id(1) can handle uid arguments

diff --git a/id.1 b/id.1
index 8252773..6bfa06b 100644
--- a/id.1
+++ b/id.1
_AT_@ -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
_AT_@ -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]
", argv0);
+ eprintf("usage: %s [user | uid]
", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
- struct passwd *pw;
-
         ARGBEGIN {
         default:
                 usage();
_AT_@ -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
", 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
", 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();
_AT_@ -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
", 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
", id);
+ user(pw);
+}
+
 static void
 user(struct passwd *pw)
 {
Received on Sun Dec 01 2013 - 15:29:23 CET

This archive was generated by hypermail 2.3.0 : Sun Dec 01 2013 - 15:36:21 CET