Subject: bug#72472: gzip: simplify printing of offsets using -l and -v

From: Collin Funk <[email-masked]>
Date: Sun, 04 Aug 2024 17:10:01 -0700

Since gzip uses 'printf-posix' can't offsets be printed using that
instead of fprint_off? Patch attached + 1 more removing #ifndef CHAR_BIT
cruft.

Also, now that POSIX-1.2024 requires strcpy to be async-signal-safe can
'volatile_strcpy' be removed? Or is there something more to
that. Signals hurt my brain a bit, sorry...

Collin

>From 411035e0ed3dc024f26dd18b842f4bf10e2fa7bb Mon Sep 17 00:00:00 2001
From: Collin Funk <[email-masked]>
Date: Sun, 4 Aug 2024 16:44:43 -0700
Subject: [PATCH 1/2] gzip: simplify printing of offsets using -l and -v

* bootstrap.conf (gnulib_modules): Add inttypes.
* gzip.c: Include inttypes.h.
(do_list): Use printf instead of fprint_off.
* gzip.h (fprint_off): Remove deceleration.
* util.c (fprint_off): Remove function.
---
 bootstrap.conf |  1 +
 gzip.c         | 16 ++++++----------
 gzip.h         |  1 -
 util.c         | 31 -------------------------------
 4 files changed, 7 insertions(+), 42 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index f9d29fc..c373ee6 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -40,6 +40,7 @@ gnumakefile
 gnupload
 ignore-value
 intprops
+inttypes
 largefile
 lib-ignore
 lstat
diff --git a/gzip.c b/gzip.c
index 866028a..3e0a642 100644
--- a/gzip.c
+++ b/gzip.c
@@ -82,6 +82,7 @@ static char const *const license_msg[] = {
                 /* configuration */
 
 #include <limits.h>
+#include <inttypes.h>
 #include <unistd.h>
 #include <stdlib.h>
 
@@ -1747,12 +1748,9 @@ do_list (int method)
         if (verbose) {
             printf("                            ");
         }
-        if (verbose || !quiet) {
-            fprint_off(stdout, total_in, positive_off_t_width);
-            printf(" ");
-            fprint_off(stdout, total_out, positive_off_t_width);
-            printf(" ");
-        }
+        if (verbose || !quiet)
+          printf ("%*jd %*jd ", positive_off_t_width, (intmax_t) total_in,
+                  positive_off_t_width, (intmax_t) total_out);
         display_ratio(total_out-(total_in-header_bytes), total_out, stdout);
         /* header_bytes is not meaningful but used to ensure the same
          * ratio if there is a single file.
@@ -1779,10 +1777,8 @@ do_list (int method)
         else
           printf ("??? ?? ??:?? ");
       }
-    fprint_off(stdout, bytes_in, positive_off_t_width);
-    printf(" ");
-    fprint_off(stdout, bytes_out, positive_off_t_width);
-    printf(" ");
+    printf ("%*jd %*jd ", positive_off_t_width, (intmax_t) bytes_in,
+            positive_off_t_width, (intmax_t) bytes_out);
     if (bytes_in  == -1L) {
         total_in = -1L;
         bytes_in = bytes_out = header_bytes = 0;
diff --git a/gzip.h b/gzip.h
index c19045a..b6a4e7c 100644
--- a/gzip.h
+++ b/gzip.h
@@ -322,7 +322,6 @@ extern void warning       (char const *m);
 _Noreturn extern void read_error (void);
 _Noreturn extern void write_error (void);
 extern void display_ratio (off_t num, off_t den, FILE *file);
-extern void fprint_off    (FILE *, off_t, int);
 
         /* in inflate.c */
 extern int inflate (void);
diff --git a/util.c b/util.c
index cc0b124..8835d2f 100644
--- a/util.c
+++ b/util.c
@@ -470,34 +470,3 @@ display_ratio (off_t num, off_t den, FILE *file)
 {
     fprintf(file, "%5.1f%%", den == 0 ? 0 : 100.0 * num / den);
 }
-
-/* ========================================================================
- * Print an off_t.  There's no completely portable way to use printf,
- * so we do it ourselves.
- */
-void
-fprint_off (FILE *file, off_t offset, int width)
-{
-    char buf[CHAR_BIT * sizeof (off_t)];
-    char *p = buf + sizeof buf;
-
-    /* Don't negate offset here; it might overflow.  */
-    if (offset < 0) {
-        do
-          *--p = '0' - offset % 10;
-        while ((offset /= 10) != 0);
-
-        *--p = '-';
-    } else {
-        do
-          *--p = '0' + offset % 10;
-        while ((offset /= 10) != 0);
-    }
-
-    width -= buf + sizeof buf - p;
-    while (0 < width--) {
-        putc (' ', file);
-    }
-    for (;  p < buf + sizeof buf;  p++)
-        putc (*p, file);
-}
-- 
2.45.2

>From 5c929794ae9eabe1beafbd4bdea303eeef6aebe0 Mon Sep 17 00:00:00 2001
From: Collin Funk <[email-masked]>
Date: Sun, 4 Aug 2024 16:52:29 -0700
Subject: [PATCH 2/2] maint: depend on Gnulib's limits-h

* bootstrap.conf (gnulib_modules): Add limits-h.
* gzip.c (CHAR_BIT): Remove definition.
* unlzh.c: Include limits.h.
(CHAR_BIT, UCHAR_MAX): Remove definitions.
* util.c (CHAR_BIT): Remove definition.
---
 bootstrap.conf | 1 +
 gzip.c         | 4 ----
 unlzh.c        | 9 +--------
 util.c         | 4 ----
 4 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index c373ee6..2654b04 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -43,6 +43,7 @@ intprops
 inttypes
 largefile
 lib-ignore
+limits-h
 lstat
 maintainer-makefile
 malloc-gnu
diff --git a/gzip.c b/gzip.c
index 3e0a642..5cd1586 100644
--- a/gzip.c
+++ b/gzip.c
@@ -106,10 +106,6 @@ static char const *const license_msg[] = {
 #  define SEEK_END 2
 #endif
 
-#ifndef CHAR_BIT
-#  define CHAR_BIT 8
-#endif
-
 #ifdef off_t
   off_t lseek (int fd, off_t offset, int whence);
 #endif
diff --git a/unlzh.c b/unlzh.c
index 25c05e3..3320196 100644
--- a/unlzh.c
+++ b/unlzh.c
@@ -4,6 +4,7 @@
  */
 
 #include <config.h>
+#include <limits.h>
 #include <stdio.h>
 
 #include "tailor.h"
@@ -35,14 +36,6 @@ static void make_table (int nchar, uch bitlen[], int tablebits, ush table[]);
 #define DICBIT    13    /* 12(-lh4-) or 13(-lh5-) */
 #define DICSIZ ((unsigned) 1 << DICBIT)
 
-#ifndef CHAR_BIT
-#  define CHAR_BIT 8
-#endif
-
-#ifndef UCHAR_MAX
-#  define UCHAR_MAX 255
-#endif
-
 #define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
 /* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
  * for which short is not on 16 bits (Cray).
diff --git a/util.c b/util.c
index 8835d2f..ee94337 100644
--- a/util.c
+++ b/util.c
@@ -32,10 +32,6 @@
 #include <dirname.h>
 #include <xalloc.h>
 
-#ifndef CHAR_BIT
-#  define CHAR_BIT 8
-#endif
-
 #ifndef EPIPE
 # define EPIPE 0
 #endif
-- 
2.45.2