From 0252661afad64e2dbc101829171fab4957e52da9 Mon Sep 17 00:00:00 2001 From: wosch Date: Sun, 7 Dec 1997 01:00:56 +0000 Subject: [PATCH] Use getopts instead getopt(1). This should fix the problem with whitespaces in pattern. PR: 5211 --- gnu/usr.bin/gzip/zgrep.getopt | 53 +++++++++++++++++------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/gnu/usr.bin/gzip/zgrep.getopt b/gnu/usr.bin/gzip/zgrep.getopt index 1c138f4ca4f8..58564b3a5040 100644 --- a/gnu/usr.bin/gzip/zgrep.getopt +++ b/gnu/usr.bin/gzip/zgrep.getopt @@ -26,7 +26,7 @@ # # zgrep - search possibly compressed files for a regular expression # -# $Id: zgrep.getopt,v 1.4 1997/02/22 15:46:11 peter Exp $ +# $Id: zgrep.getopt,v 1.5 1997/04/19 20:06:57 wosch Exp $ PATH=/bin:/usr/bin:$PATH; export PATH @@ -39,20 +39,20 @@ case "$0" in esac gzip=gzip gzipopt="-cdfq" pattern= grepopt= files= line= header= -set -- `getopt "0123456789A:B:CEFGVX:bce:f:hiLlnqsvwxy" "$@"` # check options -while : +while getopts "0123456789A:B:CEFGVX:bce:f:hiLlnqsvwxy" option do - case "$1" in - --) shift; break;; # EOF options - -e|-f) pattern="$1 $2"; shift;; # -f file - -l|-L) line=$1; grepopt="$grepopt $1";; - -h|-q) header=$1; grepopt="$grepopt $1";; - *) grepopt="$grepopt $1";; # rest + case "$option" in + e|f) pattern="-$option $OPTARG";; # -f file + l|L) line=-$option; grepopt="$grepopt -$option";; + h|q) header=-h; grepopt="$grepopt -$option";; + # rest + [0-9CEFGVbchiLlnqsvwxy]) grepopt="$grepopt -$option";; + [ABXef]) grepopt="$grepopt -$option $OPTARG";; esac - shift done +shift $(($OPTIND - 1)) # check pattern case X"$pattern" in @@ -67,29 +67,28 @@ files="$@" # no shell loop neccessary for option -q or -h # and a single file or reading from stdin case "$header"X"$#" in - -h*|-q*|X0|X1) $gzip $gzipopt -- $files | $grep $grepopt -- $pattern + -h*|-q*|X0|X1) $gzip $gzipopt -- $files | $grep $grepopt -- "$pattern" exit $?;; esac - -exit=1 for f in $files do case "$line" in - -l|-L) if $gzip $gzipopt -- $f | - $grep $grepopt -- $pattern >/dev/null - then - echo $f - fi - ;; - *) $gzip $gzipopt -- $f | $grep $grepopt -- $pattern | - sed "s%^%${f}:%";; - esac + -l) if $gzip $gzipopt -- $f | + $grep $grepopt -- "$pattern" >/dev/null + then + echo $f + fi + ;; + -L) if $gzip $gzipopt -- $f | + $grep $grepopt -- "$pattern" >/dev/null; then : + else + echo $f + fi + ;; - case $? in - 2) exit=$?;; - 0) case "$exit" in 2);; 1) exit=0;; esac;; + *) $gzip $gzipopt -- $f | $grep $grepopt -- "$pattern" | + sed "s%^%${f}:%" + ;; esac done - -exit $exit