bug-gzip
[Top][All Lists]
Advanced

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

bug#60326: [PATCH 2/2] gzip: use strerror, not perror


From: Paul Eggert
Subject: bug#60326: [PATCH 2/2] gzip: use strerror, not perror
Date: Tue, 27 Dec 2022 22:33:30 -0800

* bootstrap.conf (gnulib_modules): Use strerror, not perror.
This removes dependencies on Gnulib’s ‘threadlib’ and ‘lock’ modules,
and simplifies the mainline code.  Apparently the old code was written
before strerror was universally supported; nowadays we can use Gnulib
strerror instead.  All uses of perror changed to use strerror+fprintf.

* gzip.h (WARN): Since this expands to a statement, wrap within
‘do ... while (false)’ for the usual reasons.  Helpful now that
calls to WARN are now simplified.
---
 bootstrap.conf |  2 +-
 gzip.c         | 35 ++++++-----------------------------
 gzip.h         | 11 +++++++++--
 lib/.gitignore | 11 -----------
 m4/.gitignore  |  6 ------
 util.c         | 16 ++++------------
 6 files changed, 20 insertions(+), 61 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 9a67bc2..324cb48 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -46,7 +46,6 @@ maintainer-makefile
 malloc-gnu
 manywarnings
 openat-safer
-perror
 printf-posix
 readme-release
 realloc-gnu
@@ -54,6 +53,7 @@ savedir
 sigaction
 stat-time
 stdalign
+strerror
 sys_stat
 time
 unistd-safer
diff --git a/gzip.c b/gzip.c
index b17f264..d7a088b 100644
--- a/gzip.c
+++ b/gzip.c
@@ -414,10 +414,7 @@ version ()
 static void
 progerror (char const *string)
 {
-    int e = errno;
-    fprintf (stderr, "%s: ", program_name);
-    errno = e;
-    perror(string);
+    fprintf (stderr, "%s: %s: %s\n", program_name, string, strerror (errno));
     exit_code = ERROR;
 }
 
@@ -1045,14 +1042,8 @@ treat_file (char *iname)
             sigprocmask (SIG_SETMASK, &oldset, NULL);
 
             if (unlink_errno)
-              {
-                WARN ((stderr, "%s: ", program_name));
-                if (!quiet)
-                  {
-                    errno = unlink_errno;
-                    perror (ifname);
-                  }
-              }
+              WARN ((stderr, "%s: %s: %s\n", program_name, ifname,
+                     strerror (unlink_errno)));
           }
       }
 
@@ -1948,15 +1939,7 @@ copy_stat (struct stat *ifstat)
         }
       }
     else
-      {
-        int e = errno;
-        WARN ((stderr, "%s: ", program_name));
-        if (!quiet)
-          {
-            errno = e;
-            perror (ofname);
-          }
-      }
+      WARN ((stderr, "%s: %s: %s\n", program_name, ofname, strerror (errno)));
 #endif
 
     /* Change the group first, then the permissions, then the owner.
@@ -1972,14 +1955,8 @@ copy_stat (struct stat *ifstat)
 #else
     r = chmod (ofname, mode);
 #endif
-    if (r != 0) {
-        int e = errno;
-        WARN ((stderr, "%s: ", program_name));
-        if (!quiet) {
-            errno = e;
-            perror(ofname);
-        }
-    }
+    if (r != 0)
+      WARN ((stderr, "%s: %s: %s\n", program_name, ofname, strerror (errno)));
 
     do_chown (ofd, ofname, ifstat->st_uid, -1);
 }
diff --git a/gzip.h b/gzip.h
index 3334cd4..b5eb020 100644
--- a/gzip.h
+++ b/gzip.h
@@ -251,8 +251,15 @@ extern int verbose;        /* be verbose (-v) */
 #  define Tracecv(c,x)
 #endif
 
-#define WARN(msg) {if (!quiet) fprintf msg ; \
-                   if (exit_code == OK) exit_code = WARNING;}
+#define WARN(msg) \
+  do \
+    { \
+      if (!quiet) \
+        fprintf msg; \
+      if (exit_code == OK) \
+        exit_code = WARNING; \
+    } \
+  while (false)
 
         /* in zip.c: */
 extern int zip        (int in, int out);
diff --git a/lib/.gitignore b/lib/.gitignore
index c769bf4..53a05cf 100644
--- a/lib/.gitignore
+++ b/lib/.gitignore
@@ -146,7 +146,6 @@
 /opendir-safer.c
 /opendir.c
 /pathmax.h
-/perror.c
 /pipe-safer.c
 /pipe.c
 /printf-args.c
@@ -203,7 +202,6 @@
 /strerror-override.c
 /strerror-override.h
 /strerror.c
-/strerror_r.c
 /string.h
 /string.in.h
 /stripslash.c
@@ -234,15 +232,6 @@
 /warn-on-use.h
 /wchar.h
 /wchar.in.h
-/windows-initguard.h
-/windows-mutex.c
-/windows-mutex.h
-/windows-once.c
-/windows-once.h
-/windows-recmutex.c
-/windows-recmutex.h
-/windows-rwlock.c
-/windows-rwlock.h
 /xalloc-die.c
 /xalloc-oversized.h
 /xalloc.h
diff --git a/m4/.gitignore b/m4/.gitignore
index 48f5605..33621cc 100644
--- a/m4/.gitignore
+++ b/m4/.gitignore
@@ -70,7 +70,6 @@
 /ldexpl.m4
 /lib-ignore.m4
 /limits-h.m4
-/lock.m4
 /lseek.m4
 /lstat.m4
 /malloc.m4
@@ -95,14 +94,12 @@
 /openat.m4
 /opendir.m4
 /pathmax.m4
-/perror.m4
 /pid_t.m4
 /pipe.m4
 /printf-frexp.m4
 /printf-frexpl.m4
 /printf-posix-rpl.m4
 /printf.m4
-/pthread_rwlock_rdlock.m4
 /raise.m4
 /readdir.m4
 /realloc.m4
@@ -128,13 +125,11 @@
 /stpcpy.m4
 /strdup.m4
 /strerror.m4
-/strerror_r.m4
 /string_h.m4
 /sys_socket_h.m4
 /sys_stat_h.m4
 /sys_time_h.m4
 /sys_types_h.m4
-/threadlib.m4
 /time_h.m4
 /timespec.m4
 /ungetc.m4
@@ -156,6 +151,5 @@
 /wint_t.m4
 /xalloc.m4
 /xsize.m4
-/year2038.m4
 /yesno.m4
 /zzgnulib.m4
diff --git a/util.c b/util.c
index 193cb8f..6f5e11f 100644
--- a/util.c
+++ b/util.c
@@ -444,23 +444,15 @@ void warning (char const *m)
 
 void read_error()
 {
-    int e = errno;
-    fprintf (stderr, "\n%s: ", program_name);
-    if (e != 0) {
-        errno = e;
-        perror(ifname);
-    } else {
-        fprintf(stderr, "%s: unexpected end of file\n", ifname);
-    }
+    fprintf (stderr, "\n%s: %s: %s\n",
+             program_name, ifname,
+             errno ? strerror (errno) : "unexpected end of file");
     abort_gzip();
 }
 
 void write_error()
 {
-    int e = errno;
-    fprintf (stderr, "\n%s: ", program_name);
-    errno = e;
-    perror(ofname);
+    fprintf (stderr, "\n%s: %s: %s\n", program_name, ofname, strerror (errno));
     abort_gzip();
 }
 
-- 
2.38.1






reply via email to

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