zgrep: portability improvements; exercise "-f -"
authorJim Meyering <meyering@redhat.com>
Fri, 9 Oct 2009 18:03:09 +0000 (20:03 +0200)
committerJim Meyering <meyering@redhat.com>
Fri, 9 Oct 2009 18:15:50 +0000 (20:15 +0200)
* zgrep.in: Adjust loop not to use seq; it's not portable enough.
Fail if we don't find a free file descriptor.
(exists): New function; Use it in place of less portable "test -e".
Testing for existence of /dev/fd/$fd doesn't work on Solaris 10,
since all 256 always exist (as char devices), but testing for
/proc/$$/fd/$fd does work, so do that instead.
* Makefile.am (TESTS): Add tests/zgrep-f.
* tests/zgrep-f: New test; exercise this bug.
* NEWS (Bug fixes): Mention it.

Makefile.am
NEWS
tests/zgrep-f [new file with mode: 0644]
zgrep.in

index af75eba8b4cc8476323876eac4cedfcbd21454f3..206d5bf1ddfd7019e0ac67904b587eaae817f70c 100644 (file)
@@ -101,7 +101,8 @@ check-local: $(FILES_TO_CHECK) $(bin_PROGRAMS) gzip.doc.gz
        @echo 'Test succeeded.'
 
 TESTS =                \
-  tests/zdiff
+  tests/zdiff  \
+  tests/zgrep-f
 EXTRA_DIST += $(TESTS)
 
 install-exec-hook: remove-installed-links
diff --git a/NEWS b/NEWS
index 20e09d845995ff29d1383c759a3e9ec2166d5fab..5071241e60886159008d54daf5f8b5e86ee6d0a5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ GNU gzip NEWS                                    -*- outline -*-
 
   zdiff would fail to print differences in two compressed inputs
 
+  zgrep -f - didn't work
+
 
 * Noteworthy changes in release 1.3.13 (2009-09-30) [stable]
 
diff --git a/tests/zgrep-f b/tests/zgrep-f
new file mode 100644 (file)
index 0000000..9184b9c
--- /dev/null
@@ -0,0 +1,38 @@
+#!/bin/sh
+# Ensure that zgrep -f - works like grep -f -
+# Before gzip-1.4, it would fail.
+
+# Copyright (C) 2009 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.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  zgrep --version
+fi
+
+. $srcdir/tests/test-lib.sh
+
+echo needle > n || framework_failure
+echo needle > haystack || framework_failure
+gzip haystack || framework_failure
+
+
+fail=0
+zgrep -f - haystack.gz < n > out 2>&1 || fail=1
+
+compare out n || fail=1
+
+Exit $fail
index d30ec2550fbd969fe4092cd3cffd3ad34d750c84..aced3728d2e0052814ba3f256abcb6023c87a9ca 100644 (file)
--- a/zgrep.in
+++ b/zgrep.in
@@ -106,12 +106,22 @@ while test $# -ne 0; do
     case $optarg in
     (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
       pat_on_stdin=1
+      eval 'test -e .' 2>/dev/null \
+        && eval 'exists(){ test -e "$@"; }' \
+        || eval 'exists(){ test -r "$@" || test -w "$@"; }'
       # Start search from 6 since the script already uses 3 and 5
-      for fd in $(seq 6 254); do
-       if test ! -e /dev/fd/$fd; then
-         pat_fd=$fd
-         break;
-       fi
+      fd=6
+      pat_fd=
+      while : ; do
+        if ! exists /proc/$$/fd/$fd; then
+          pat_fd=$fd
+          break;
+        fi
+        fd=$(expr $fd + 1)
+        if test $fd = 255; then
+          printf >&2 '%s: no free file descriptor\n' "$0"
+          exit 2
+        fi
       done
       optarg=/dev/fd/$pat_fd;
     esac