coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] id,groups: use gidtostr/uidtostr to avoid casts


From: Jim Meyering
Subject: [PATCH] id,groups: use gidtostr/uidtostr to avoid casts
Date: Wed, 04 Apr 2012 17:50:10 +0200

I wrote this months ago, and like the minimalist (avoiding unnecessary
printf) and more type-safe aspects.  The code-duplication is not ideal,
but the duplicated code is small and fragile, and not generally useful
enough to put in system.h -- too easy to use it from a context where
it would fail.  We could always give it a few more buffer entries, like
human.c does, so two invocations in the same statement won't misbehave.

Opinions?


>From 2c27745185c547e6953f0cb5c25903c2c7503678 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 1 Jan 2012 22:31:41 +0100
Subject: [PATCH] id,groups: use gidtostr/uidtostr to avoid casts

* src/id.c (gidtostr, uidtostr): Define macros.
(gidtostr_ptr, uidtostr_ptr): Define safer functions.
Use gidtostr and uidtostr to print GID and UID without
need/risk of casts.
* src/group-list.c: Likewise.
---
 src/group-list.c |   16 ++++++++++++----
 src/id.c         |   38 +++++++++++++++++++++++++++++---------
 2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/src/group-list.c b/src/group-list.c
index cf49911..786fec4 100644
--- a/src/group-list.c
+++ b/src/group-list.c
@@ -86,6 +86,16 @@ print_group_list (const char *username,
   return ok;
 }

+/* Convert a gid_t to string.  Do not use this function directly.
+   Instead, use it via the gidtostr macro.
+   Beware that it returns a pointer to static storage.  */
+static char *
+gidtostr_ptr (gid_t const *gid)
+{
+  static char buf[INT_BUFSIZE_BOUND (gid_t)];
+  return umaxtostr (*gid, buf);
+}
+#define gidtostr(g) gidtostr_ptr (&(g))

 /* Print the name or value of group ID GID. */
 extern bool
@@ -105,9 +115,7 @@ print_group (gid_t gid, bool use_name)
         }
     }

-  if (grp == NULL)
-    printf ("%lu", (unsigned long int) gid);
-  else
-    printf ("%s", grp->gr_name);
+  char *s = grp ? grp->gr_name : gidtostr (gid);
+  fputs (s, stdout);
   return ok;
 }
diff --git a/src/id.c b/src/id.c
index c600e63..a28903a 100644
--- a/src/id.c
+++ b/src/id.c
@@ -265,6 +265,28 @@ main (int argc, char **argv)
   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }

+/* Convert a gid_t to string.  Do not use this function directly.
+   Instead, use it via the gidtostr macro.
+   Beware that it returns a pointer to static storage.  */
+static char *
+gidtostr_ptr (gid_t const *gid)
+{
+  static char buf[INT_BUFSIZE_BOUND (gid_t)];
+  return umaxtostr (*gid, buf);
+}
+#define gidtostr(g) gidtostr_ptr (&(g))
+
+/* Convert a uid_t to string.  Do not use this function directly.
+   Instead, use it via the uidtostr macro.
+   Beware that it returns a pointer to static storage.  */
+static char *
+uidtostr_ptr (uid_t const *uid)
+{
+  static char buf[INT_BUFSIZE_BOUND (uid_t)];
+  return umaxtostr (*uid, buf);
+}
+#define uidtostr(u) uidtostr_ptr (&(u))
+
 /* Print the name or value of user ID UID. */

 static void
@@ -277,16 +299,14 @@ print_user (uid_t uid)
       pwd = getpwuid (uid);
       if (pwd == NULL)
         {
-          error (0, 0, _("cannot find name for user ID %lu"),
-                 (unsigned long int) uid);
+          error (0, 0, _("cannot find name for user ID %s"),
+                 uidtostr (uid));
           ok = false;
         }
     }

-  if (pwd == NULL)
-    printf ("%lu", (unsigned long int) uid);
-  else
-    printf ("%s", pwd->pw_name);
+  char *s = pwd ? pwd->pw_name : uidtostr (uid);
+  fputs (s, stdout);
 }

 /* Print all of the info about the user's user and group IDs. */
@@ -297,19 +317,19 @@ print_full_info (const char *username)
   struct passwd *pwd;
   struct group *grp;

-  printf (_("uid=%lu"), (unsigned long int) ruid);
+  printf (_("uid=%s"), uidtostr (ruid));
   pwd = getpwuid (ruid);
   if (pwd)
     printf ("(%s)", pwd->pw_name);

-  printf (_(" gid=%lu"), (unsigned long int) rgid);
+  printf (_(" gid=%s"), gidtostr (rgid));
   grp = getgrgid (rgid);
   if (grp)
     printf ("(%s)", grp->gr_name);

   if (euid != ruid)
     {
-      printf (_(" euid=%lu"), (unsigned long int) euid);
+      printf (_(" euid=%s"), uidtostr (euid));
       pwd = getpwuid (euid);
       if (pwd)
         printf ("(%s)", pwd->pw_name);
--
1.7.9.3



reply via email to

[Prev in Thread] Current Thread [Next in Thread]