From: Jim Meyering Date: Fri, 9 Oct 2009 18:03:09 +0000 (+0200) Subject: zgrep: portability improvements; exercise "-f -" X-Git-Tag: v1.3.14~9 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=beb6a3e7a74a0415826752b25d418d2b54f3d49f;p=debian%2Fgzip zgrep: portability improvements; exercise "-f -" * 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. --- diff --git a/Makefile.am b/Makefile.am index af75eba..206d5bf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 20e09d8..5071241 100644 --- 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 index 0000000..9184b9c --- /dev/null +++ b/tests/zgrep-f @@ -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 . +# 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 diff --git a/zgrep.in b/zgrep.in index d30ec25..aced372 100644 --- 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