tags 168606 + patch
thanks

Here's a patch that allows the "-f -" option to work with zgrep in the
same way that it works with grep. For example:

        $ echo "Example file matching pattern" > file
        $ gzip -c file > file.gz
        $ echo pattern | grep -f - file
        Example file matching pattern
        $ echo pattern | /bin/zgrep -f - file.gz
        $ echo $?
        1
        $ echo pattern | ./zgrep -f - file.gz
        Example file matching pattern

Please let me know if you have any questions.

-Carl

--- /bin/zgrep	2008-04-16 06:08:36.000000000 +1000
+++ zgrep	2009-01-24 01:53:59.000000000 +1100
@@ -47,6 +47,7 @@
 '
 operands=
 have_pat=0
+pat_on_stdin=0
 files_with_matches=0
 files_without_matches=0
 no_filename=0
@@ -92,6 +93,23 @@
     printf >&2 '%s: %s: option not supported\n' "$0" "$option"
     exit 2;;
   (-[ef]* | --file | --file=* | --reg*)
+    # The pattern is coming from a file rather than the command-line.
+    # If the file is actually stdin then we need to do a little
+    # magic, (since we use stdin to pass the gzip output to grep).
+    # So find a free fd and change the argument to then use this
+    # file descriptor for the pattern.
+    case $optarg in
+    (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
+      pat_on_stdin=1
+      # 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
+      done
+      optarg=/dev/fd/$pat_fd;
+    esac
     have_pat=1;;
   (--h | --he | --hel | --help)
     echo "$usage" || exit 2
@@ -146,6 +164,9 @@
   # Fail if gzip or grep (or sed) fails.
   gzip_status=$(
     exec 5>&1
+    if test $pat_on_stdin -eq 1; then
+	eval "exec $pat_fd<&0"
+    fi
     (gzip -cdfq -- "$i" 5>&-; echo $? >&5) 3>&- |
     if test $files_with_matches -eq 1; then
       eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to