Here is a patch simplifying some printf invocations like:

    printf ("%s", intmaxtostr (value, buf));

to just use C99 conversion specifiers:

    printf ("%jd", value);

I've left ones that modify the return value (e.g. with quote) and ones
that use it for formatting like in 'ls'.

Collin

>From 0c2bd6a6840d6f46e01e2caeaad9d42d10c34367 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Mon, 29 Jul 2024 21:16:36 -0700
Subject: [PATCH] maint: remove unnecessary inttostr usage in printf

* src/cksum.c (output_crc): Use '%ju' instead of umaxtostr.
* src/shred.c (dopass): Likewise.
* src/csplit.c (handle_line_error, regexp_error, close_output_file)
(parse_patterns): Use '%jd' instead of offtostr.
* src/tail.c (xlseek): Likewise.
* src/head.c (elseek): Likewise.
* src/group-list.c (gidtostr_ptr): Remove function.
(gidtostr): Remove macro.
(print_group): Use '%ju' instead of umaxtostr.
* src/id.c (gidtostr_ptr, uidtostr_ptr): Remove functions.
(gidtostr, uidtostr): Remove macros.
(print_user, print_full_info): Use '%ju' instead of umaxtostr.
* src/sort.c (specify_nmerge): Use '%u' instead of uinttostr.
---
 src/cksum.c      |  3 +--
 src/csplit.c     | 22 ++++++----------------
 src/group-list.c | 17 ++++-------------
 src/head.c       |  7 +++----
 src/id.c         | 41 ++++++++++-------------------------------
 src/shred.c      |  6 +++---
 src/sort.c       |  6 ++----
 src/tail.c       | 15 ++++++---------
 8 files changed, 35 insertions(+), 82 deletions(-)

diff --git a/src/cksum.c b/src/cksum.c
index 9909b14f5..a97ffc26f 100644
--- a/src/cksum.c
+++ b/src/cksum.c
@@ -257,8 +257,7 @@ output_crc (char const *file, int binary_file, void const *digest, bool raw,
       return;
     }
 
-  char length_buf[INT_BUFSIZE_BOUND (uintmax_t)];
-  printf ("%u %s", *(unsigned int *)digest, umaxtostr (length, length_buf));
+  printf ("%u %ju", *(unsigned int *)digest, length);
   if (args)
     printf (" %s", file);
   putchar (delim);
diff --git a/src/csplit.c b/src/csplit.c
index 4767d8aa0..acde48bbd 100644
--- a/src/csplit.c
+++ b/src/csplit.c
@@ -673,7 +673,7 @@ handle_line_error (const struct control *p, intmax_t repetition)
   fprintf (stderr, _("%s: %s: line number out of range"),
            program_name, quote (imaxtostr (p->lines_required, buf)));
   if (repetition)
-    fprintf (stderr, _(" on repetition %s\n"), imaxtostr (repetition, buf));
+    fprintf (stderr, _(" on repetition %jd\n"), repetition);
   else
     fprintf (stderr, "\n");
 
@@ -726,10 +726,7 @@ regexp_error (struct control *p, intmax_t repetition, bool ignore)
            program_name, quote (global_argv[p->argnum]));
 
   if (repetition)
-    {
-      char buf[INT_BUFSIZE_BOUND (intmax_t)];
-      fprintf (stderr, _(" on repetition %s\n"), imaxtostr (repetition, buf));
-    }
+    fprintf (stderr, _(" on repetition %jd\n"), repetition);
   else
     fprintf (stderr, "\n");
 
@@ -988,10 +985,7 @@ close_output_file (void)
       else
         {
           if (!suppress_count)
-            {
-              char buf[INT_BUFSIZE_BOUND (intmax_t)];
-              fprintf (stdout, "%s\n", imaxtostr (bytes_written, buf));
-            }
+            fprintf (stdout, "%jd\n", bytes_written);
         }
       output_stream = nullptr;
     }
@@ -1152,13 +1146,9 @@ parse_patterns (int argc, int start, char **argv)
             error (EXIT_FAILURE, 0,
                    _("%s: line number must be greater than zero"), argv[i]);
           if (val < last_val)
-            {
-              char buf[INT_BUFSIZE_BOUND (intmax_t)];
-              error (EXIT_FAILURE, 0,
-                     _("line number %s is smaller than preceding line number,"
-                       " %s"),
-                     quote (argv[i]), imaxtostr (last_val, buf));
-            }
+            error (EXIT_FAILURE, 0,
+                   _("line number %s is smaller than preceding line number,"
+                     " %jd"), quote (argv[i]), last_val);
 
           if (val == last_val)
             error (0, 0,
diff --git a/src/group-list.c b/src/group-list.c
index d6af680ef..e8c9a002c 100644
--- a/src/group-list.c
+++ b/src/group-list.c
@@ -86,17 +86,6 @@ print_group_list (char const *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
 print_group (gid_t gid, bool use_name)
@@ -123,7 +112,9 @@ print_group (gid_t gid, bool use_name)
         }
     }
 
-  char *s = grp ? grp->gr_name : gidtostr (gid);
-  fputs (s, stdout);
+  if (grp)
+    printf ("%s", grp->gr_name);
+  else
+    printf ("%ju", (uintmax_t) gid);
   return ok;
 }
diff --git a/src/head.c b/src/head.c
index 4612a8e10..9feda5653 100644
--- a/src/head.c
+++ b/src/head.c
@@ -224,15 +224,14 @@ static off_t
 elseek (int fd, off_t offset, int whence, char const *filename)
 {
   off_t new_offset = lseek (fd, offset, whence);
-  char buf[INT_BUFSIZE_BOUND (offset)];
 
   if (new_offset < 0)
     error (0, errno,
            _(whence == SEEK_SET
-             ? N_("%s: cannot seek to offset %s")
-             : N_("%s: cannot seek to relative offset %s")),
+             ? N_("%s: cannot seek to offset %jd")
+             : N_("%s: cannot seek to relative offset %jd")),
            quotef (filename),
-           offtostr (offset, buf));
+           (intmax_t) offset);
 
   return new_offset;
 }
diff --git a/src/id.c b/src/id.c
index 38d5517bd..80a116e7a 100644
--- a/src/id.c
+++ b/src/id.c
@@ -303,28 +303,6 @@ main (int argc, char **argv)
   return 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 (uintmax_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 (uintmax_t)];
-  return umaxtostr (*uid, buf);
-}
-#define uidtostr(u) uidtostr_ptr (&(u))
-
 /* Print the name or value of user ID UID. */
 
 static void
@@ -337,14 +315,15 @@ print_user (uid_t uid)
       pwd = getpwuid (uid);
       if (pwd == nullptr)
         {
-          error (0, 0, _("cannot find name for user ID %s"),
-                 uidtostr (uid));
+          error (0, 0, _("cannot find name for user ID %ju"), (uintmax_t) uid);
           ok &= false;
         }
     }
 
-  char *s = pwd ? pwd->pw_name : uidtostr (uid);
-  fputs (s, stdout);
+  if (pwd)
+    printf ("%s", pwd->pw_name);
+  else
+    printf ("%ju", (uintmax_t) uid);
 }
 
 /* Print all of the info about the user's user and group IDs. */
@@ -355,19 +334,19 @@ print_full_info (char const *username)
   struct passwd *pwd;
   struct group *grp;
 
-  printf (_("uid=%s"), uidtostr (ruid));
+  printf (_("uid=%ju"), (uintmax_t) ruid);
   pwd = getpwuid (ruid);
   if (pwd)
     printf ("(%s)", pwd->pw_name);
 
-  printf (_(" gid=%s"), gidtostr (rgid));
+  printf (_(" gid=%ju"), (uintmax_t) rgid);
   grp = getgrgid (rgid);
   if (grp)
     printf ("(%s)", grp->gr_name);
 
   if (euid != ruid)
     {
-      printf (_(" euid=%s"), uidtostr (euid));
+      printf (_(" euid=%ju"), (uintmax_t) euid);
       pwd = getpwuid (euid);
       if (pwd)
         printf ("(%s)", pwd->pw_name);
@@ -375,7 +354,7 @@ print_full_info (char const *username)
 
   if (egid != rgid)
     {
-      printf (_(" egid=%s"), gidtostr (egid));
+      printf (_(" egid=%ju"), (uintmax_t) egid);
       grp = getgrgid (egid);
       if (grp)
         printf ("(%s)", grp->gr_name);
@@ -408,7 +387,7 @@ print_full_info (char const *username)
       {
         if (i > 0)
           putchar (',');
-        fputs (gidtostr (groups[i]), stdout);
+        printf ("%ju", (uintmax_t) groups[i]);
         grp = getgrgid (groups[i]);
         if (grp)
           printf ("(%s)", grp->gr_name);
diff --git a/src/shred.c b/src/shred.c
index 564d3cc62..ecb207a8b 100644
--- a/src/shred.c
+++ b/src/shred.c
@@ -485,7 +485,6 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
               else
                 {
                   int errnum = errno;
-                  char buf[INT_BUFSIZE_BOUND (uintmax_t)];
 
                   /* Retry without direct I/O since this may not be supported
                      at all on some (file) systems, or with the current size.
@@ -498,8 +497,9 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
                       try_without_directio = true;
                       continue;
                     }
-                  error (0, errnum, _("%s: error writing at offset %s"),
-                         qname, umaxtostr (offset + soff, buf));
+                  uintmax_t error_offset = offset + soff;
+                  error (0, errnum, _("%s: error writing at offset %ju"),
+                         qname, error_offset);
 
                   /* 'shred' is often used on bad media, before throwing it
                      out.  Thus, it shouldn't give up on bad blocks.  This
diff --git a/src/sort.c b/src/sort.c
index 6c273fab9..3357733c1 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1368,13 +1368,11 @@ specify_nmerge (int oi, char c, char const *s)
 
   if (e == LONGINT_OVERFLOW)
     {
-      char max_nmerge_buf[INT_BUFSIZE_BOUND (max_nmerge)];
       error (0, 0, _("--%s argument %s too large"),
              long_options[oi].name, quote (s));
       error (SORT_FAILURE, 0,
-             _("maximum --%s argument with current rlimit is %s"),
-             long_options[oi].name,
-             uinttostr (max_nmerge, max_nmerge_buf));
+             _("maximum --%s argument with current rlimit is %u"),
+             long_options[oi].name, max_nmerge);
     }
   else
     xstrtol_fatal (e, oi, c, long_options, s);
diff --git a/src/tail.c b/src/tail.c
index a3b46ca2d..f373f768e 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -481,27 +481,24 @@ static off_t
 xlseek (int fd, off_t offset, int whence, char const *filename)
 {
   off_t new_offset = lseek (fd, offset, whence);
-  char buf[INT_BUFSIZE_BOUND (offset)];
-  char *s;
 
   if (0 <= new_offset)
     return new_offset;
 
-  s = offtostr (offset, buf);
   switch (whence)
     {
     case SEEK_SET:
-      error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %s"),
-             quotef (filename), s);
+      error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %jd"),
+             quotef (filename), (intmax_t) offset);
       break;
     case SEEK_CUR:
-      error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %s"),
-             quotef (filename), s);
+      error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %jd"),
+             quotef (filename), (intmax_t) offset);
       break;
     case SEEK_END:
       error (EXIT_FAILURE, errno,
-             _("%s: cannot seek to end-relative offset %s"),
-             quotef (filename), s);
+             _("%s: cannot seek to end-relative offset %jd"),
+             quotef (filename), (intmax_t) offset);
       break;
     default:
       unreachable ();
-- 
2.45.2

Reply via email to