function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
Command=${0integer Errors=0
function grep
{
vflag= xflag= cflag= lflag= nflag=
set -f
while ((1)) do case "$1" in
-v*) vflag=1;;
-x*) xflag=1;;
-c*) cflag=1;;
-l*) lflag=1;;
-n*) nflag=1;;
-b*) print 'b option not supported';;
-e*) shift;expr="$1";;
-f*) shift;expr=$(< $1);;
-*) print $0: 'unknown flag';return 2;;
*)
if test "$expr" = ''
then expr="$1";shift
fi
test "$xflag" || expr="*${expr}*"
break;;
esac
shift done
noprint=$vflag$cflag$lflag integer n=0 c=0 tc=0 nargs=$ for i in "$@" do if ((nargs<=1))
then fname=''
else fname="$i":
fi
test "$i" && exec 0< $i while read -r line do let n=n+1
case "$line" in
$expr) test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
let c=c+1 ;;
*) if test "$vflag"
then print -r -- "$fname${nflag:+$n:}$line"
fi;;
esac
done
if test "$lflag" && ((c))
then print -r -- "$i"
fi
let tc=tc+c n=0 c=0
done
test "$cflag" && print $tc let tc }
trap 'rm -f /tmp/grep$$' EXIT
cat > /tmp/grep$$ <<\!
this is a food bar test
to see how many lines find both foo and bar.
Some line contain foo only,
and some lines contain bar only.
However, many lines contain both foo and also bar.
A line containing foobar should also be counted.
There should be six lines with foo and bar.
There are only two line with out foo but with bar.
!
if (( $(grep -c 'foo*bar' /tmp/grep$$ ) != 6))
then err_exit
fi
exit $((Errors))