Subject: bug#34672: [RFC] id --numeric

From: Giuseppe Scrivano <[email-masked]>
Date: Tue, 26 Feb 2019 21:36:12 +0100

Hi,

there are cases where I'd like to see only the numeric values in the
`id` output.

I could get this information separately, but having a new option
and just one command simplifies the task.

Are you fine with a change like the following?

If you have nothing against it, I will clean it up and send it again.

Thanks,
Giuseppe

diff --git a/src/id.c b/src/id.c
index d710caaee..e16c9c625 100644
--- a/src/id.c
+++ b/src/id.c
@@ -59,6 +59,8 @@ static bool ok = true;
 static bool multiple_users = false;
 /* If true, output user/group name instead of ID number. -n */
 static bool use_name = false;
+/* If true, prints only numbers for the IDs. */
+static bool just_numeric = false;
 
 /* The real and effective IDs of the user to print. */
 static uid_t ruid, euid;
@@ -78,6 +80,7 @@ static struct option const longopts[] =
   {"group", no_argument, NULL, 'g'},
   {"groups", no_argument, NULL, 'G'},
   {"name", no_argument, NULL, 'n'},
+  {"numeric", no_argument, NULL, 'N'},
   {"real", no_argument, NULL, 'r'},
   {"user", no_argument, NULL, 'u'},
   {"zero", no_argument, NULL, 'z'},
@@ -105,6 +108,7 @@ or (when USER omitted) for the current user.\n\
   -g, --group    print only the effective group ID\n\
   -G, --groups   print all group IDs\n\
   -n, --name     print a name instead of a number, for -ugG\n\
+  -N, --numeric  print only numeric values for IDs\n\
   -r, --real     print the real ID instead of the effective ID, with -ugG\n\
   -u, --user     print only the effective user ID\n\
   -z, --zero     delimit entries with NUL characters, not whitespace;\n\
@@ -137,7 +141,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  while ((optc = getopt_long (argc, argv, "agnruzGZ", longopts, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "agnruzGZN", longopts, NULL)) != -1)
     {
       switch (optc)
         {
@@ -178,6 +182,9 @@ main (int argc, char **argv)
         case 'G':
           just_group_list = true;
           break;
+        case 'N':
+          just_numeric = true;
+          break;
         case_GETOPT_HELP_CHAR;
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
         default:
@@ -361,19 +368,19 @@ print_full_info (const char *username)
 
   printf (_("uid=%s"), uidtostr (ruid));
   pwd = getpwuid (ruid);
-  if (pwd)
+  if (!just_numeric && pwd)
     printf ("(%s)", pwd->pw_name);
 
   printf (_(" gid=%s"), gidtostr (rgid));
   grp = getgrgid (rgid);
-  if (grp)
+  if (!just_numeric && grp)
     printf ("(%s)", grp->gr_name);
 
   if (euid != ruid)
     {
       printf (_(" euid=%s"), uidtostr (euid));
       pwd = getpwuid (euid);
-      if (pwd)
+      if (!just_numeric && pwd)
         printf ("(%s)", pwd->pw_name);
     }
 
@@ -381,7 +388,7 @@ print_full_info (const char *username)
     {
       printf (_(" egid=%s"), gidtostr (egid));
       grp = getgrgid (egid);
-      if (grp)
+      if (!just_numeric && grp)
         printf ("(%s)", grp->gr_name);
     }
 
@@ -413,9 +420,12 @@ print_full_info (const char *username)
         if (i > 0)
           putchar (',');
         fputs (gidtostr (groups[i]), stdout);
-        grp = getgrgid (groups[i]);
-        if (grp)
-          printf ("(%s)", grp->gr_name);
+        if (! just_numeric)
+          {
+            grp = getgrgid (groups[i]);
+            if (grp)
+              printf ("(%s)", grp->gr_name);
+          }
       }
     free (groups);
   }

Regards,
Giuseppe