summaryrefslogtreecommitdiff
path: root/src/group-list.c
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2012-01-01 22:31:41 +0100
committerJim Meyering <meyering@redhat.com>2012-05-20 17:29:32 +0200
commitb663bc8eb0b87c21584b931872db5fd82cf72f49 (patch)
tree793fd18deced33ef019c01607af84eb881211985 /src/group-list.c
parent2e9f5ca4ebbbdb6a9fa2dd3d5add3f7720a172d7 (diff)
downloadcoreutils-b663bc8eb0b87c21584b931872db5fd82cf72f49.tar.xz
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.
Diffstat (limited to 'src/group-list.c')
-rw-r--r--src/group-list.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/group-list.c b/src/group-list.c
index edbb34213..a25601ecf 100644
--- a/src/group-list.c
+++ b/src/group-list.c
@@ -88,6 +88,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 (uintmax_t)];
+ return umaxtostr (*gid, buf);
+}
+#define gidtostr(g) gidtostr_ptr (&(g))
/* Print the name or value of group ID GID. */
extern bool
@@ -107,9 +117,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;
}