gzip: fix --suffix=z bug (Bug#18239)
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 Aug 2014 23:29:06 +0000 (16:29 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 Aug 2014 23:29:34 +0000 (16:29 -0700)
* gzip.c (get_suffix): Put --suffix string at the end
of the list of suffixes if it is a suffix of one one them.
* tests/z-suffix: New file.
* tests/Makefile.am (TESTS): Add it.

gzip.c
tests/Makefile.am
tests/z-suffix [new file with mode: 0755]

diff --git a/gzip.c b/gzip.c
index 9c6849e8add8e14834c888d00f97f291bfccb944..713a1c75a4b8fb78ba027fa71338533878a0cf5c 100644 (file)
--- a/gzip.c
+++ b/gzip.c
@@ -989,11 +989,25 @@ local char *get_suffix(name)
 #ifdef MAX_EXT_CHARS
           "z",
 #endif
-          NULL};
-    char const **suf = known_suffixes;
+        NULL, NULL};
+    char const **suf;
+    bool suffix_of_builtin = false;
 
-    *suf = z_suffix;
-    if (strequ(z_suffix, "z")) suf++; /* check long suffixes first */
+    /* Normally put Z_SUFFIX at the start of KNOWN_SUFFIXES, but if it
+       is a suffix of one of them, put it at the end.  */
+    for (suf = known_suffixes + 1; *suf; suf++)
+      {
+        size_t suflen = strlen (*suf);
+        if (z_len < suflen && strequ (z_suffix, *suf + suflen - z_len))
+          {
+            suffix_of_builtin = true;
+            break;
+          }
+      }
+    known_suffixes[suffix_of_builtin
+                   ? sizeof known_suffixes / sizeof *known_suffixes - 2
+                   : 0] = z_suffix;
+    suf = known_suffixes + suffix_of_builtin;
 
 #ifdef SUFFIX_SEP
     /* strip a version number from the file name */
index b0a6d6908cb6204a6f65525626734bfd693357a0..d1e934e6eb343898e24184640ff3372b7217cf30 100644 (file)
@@ -25,6 +25,7 @@ TESTS =                                       \
   stdin                                        \
   trailing-nul                         \
   unpack-invalid                       \
+  z-suffix                             \
   zdiff                                        \
   zgrep-f                              \
   zgrep-context                                \
diff --git a/tests/z-suffix b/tests/z-suffix
new file mode 100755 (executable)
index 0000000..de3a5fd
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/sh
+# Check that -Sz works.
+
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# limit so don't run it by default.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ..
+
+printf anything > F && cp F G || framework_failure_
+gzip -Sz F || fail=1
+test ! -f F || fail=1
+test -f Fz || fail=1
+gzip -dSz F || fail=1
+test ! -f Fz || fail=1
+compare F G || fail\1
+
+Exit $fail