summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Tribble <peter.tribble@gmail.com>2020-05-23 10:02:56 +0100
committerPeter Tribble <peter.tribble@gmail.com>2020-05-28 09:21:44 +0100
commit4adc6f153a42519cb03b97bf215e1d4823300ebe (patch)
tree1d441a88d4abfbdbe24fe79a4ebb356eb3eb8d80
parentc9ad389f76487048d6fde034960b0f5edfb778d7 (diff)
downloadillumos-joyent-4adc6f153a42519cb03b97bf215e1d4823300ebe.tar.gz
12785 grep -h ignored with -r
Reviewed by: Yuri Pankov <yuripv@yuripv.net> Reviewed by: Andy Fiddaman <omnios@citrus-it.co.uk> Approved by: Joshua M. Clulow <josh@sysmgr.org>
-rw-r--r--usr/src/cmd/grep/grep.c20
-rw-r--r--usr/src/pkg/manifests/system-test-utiltest.mf4
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/Makefile5
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout671
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout681
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout691
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/files/gout701
-rw-r--r--usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh44
8 files changed, 68 insertions, 9 deletions
diff --git a/usr/src/cmd/grep/grep.c b/usr/src/cmd/grep/grep.c
index 7054d1f602..ac51023678 100644
--- a/usr/src/cmd/grep/grep.c
+++ b/usr/src/cmd/grep/grep.c
@@ -35,6 +35,7 @@
*/
/*
+ * Copyright 2020 Peter Tribble.
* Copyright 2018 RackTop Systems.
* Copyright 2018 Nexenta Systems, Inc.
* Copyright 2013 Damian Bogel. All rights reserved.
@@ -64,8 +65,8 @@
#define BUFSIZE 8192 /* Input buffer size */
#define MAX_DEPTH 1000 /* how deep to recurse */
-#define AFTER 1 /* 'After' Context */
-#define BEFORE 2 /* 'Before' Context */
+#define AFTER 1 /* 'After' Context */
+#define BEFORE 2 /* 'Before' Context */
#define CONTEXT (AFTER|BEFORE) /* Full Context */
#define M_CSETSIZE 256 /* singlebyte chars */
@@ -91,11 +92,11 @@ static boolean_t nvflag = B_TRUE; /* Print matching lines */
static uchar_t cflag; /* Count of matches */
static uchar_t iflag; /* Case insensitve matching */
static uchar_t Hflag; /* Precede lines by file name */
-static uchar_t hflag; /* Supress printing of filename */
+static uchar_t hflag; /* Suppress printing of filename */
static uchar_t lflag; /* Print file names of matches */
static uchar_t nflag; /* Precede lines by line number */
static uchar_t rflag; /* Search directories recursively */
-static uchar_t bflag; /* Preccede matches by block number */
+static uchar_t bflag; /* Precede matches by block number */
static uchar_t sflag; /* Suppress file error messages */
static uchar_t qflag; /* Suppress standard output */
static uchar_t wflag; /* Search for expression as a word */
@@ -110,7 +111,7 @@ static char *cmdname;
static int use_wchar, use_bmg, mblocale;
static size_t outbuflen, prntbuflen, conbuflen;
-static unsigned long conalen, conblen, conmatches;
+static unsigned long conalen, conblen, conmatches;
static char *prntbuf, *conbuf;
static wchar_t *outline;
@@ -264,11 +265,11 @@ main(int argc, char **argv)
break;
/* based on options order h or H is set as in GNU grep */
- case 'h': /* Solaris: supress printing of file name */
+ case 'h': /* Solaris: suppress printing of file name */
hflag = 1;
Hflag = 0;
break;
- /* Solaris: precede every matching with file name */
+ /* Solaris: precede every match with file name */
case 'H':
Hflag = 1;
hflag = 0;
@@ -485,7 +486,8 @@ process_path(const char *path)
if (rflag) {
if (stat(path, &st) != -1 &&
(st.st_mode & S_IFMT) == S_IFDIR) {
- outfn = 1; /* Print filename */
+ if (!hflag)
+ outfn = 1; /* Print filename unless -h */
/*
* Add trailing slash if arg
@@ -898,7 +900,7 @@ grep(int fd, const char *fn)
off_t blkoffset; /* line_offset but context-compatible */
long long lineno, linenum;
long long matches = 0; /* Number of matching lines */
- long long conacnt = 0, conbcnt = 0; /* context line count */
+ long long conacnt = 0, conbcnt = 0; /* context line count */
int newlinep; /* 0 if the last line of file has no newline */
char *ptr, *ptrend, *prntptr, *prntptrend;
char *nextptr = NULL, *nextend = NULL;
diff --git a/usr/src/pkg/manifests/system-test-utiltest.mf b/usr/src/pkg/manifests/system-test-utiltest.mf
index 2a23e780a5..578b825ecb 100644
--- a/usr/src/pkg/manifests/system-test-utiltest.mf
+++ b/usr/src/pkg/manifests/system-test-utiltest.mf
@@ -1421,7 +1421,11 @@ file path=opt/util-tests/tests/files/gout63 mode=0444
file path=opt/util-tests/tests/files/gout64 mode=0444
file path=opt/util-tests/tests/files/gout65 mode=0444
file path=opt/util-tests/tests/files/gout66 mode=0444
+file path=opt/util-tests/tests/files/gout67 mode=0444
+file path=opt/util-tests/tests/files/gout68 mode=0444
+file path=opt/util-tests/tests/files/gout69 mode=0444
file path=opt/util-tests/tests/files/gout7 mode=0444
+file path=opt/util-tests/tests/files/gout70 mode=0444
file path=opt/util-tests/tests/files/gout8 mode=0444
file path=opt/util-tests/tests/files/gout9 mode=0444
file path=opt/util-tests/tests/files/make_a/Makefile mode=0444
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile b/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
index cec093b8a1..1d332a352f 100644
--- a/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/Makefile
@@ -12,6 +12,7 @@
#
# Copyright (c) 2013 by Delphix. All rights reserved.
# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
+# Copyright 2020 Peter Tribble.
#
include $(SRC)/cmd/Makefile.cmd
@@ -95,6 +96,10 @@ PROGS = test0 \
gout64 \
gout65 \
gout66 \
+ gout67 \
+ gout68 \
+ gout69 \
+ gout70 \
testnl
CMDS = $(PROGS:%=$(TESTDIR)/%)
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout67 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout67
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout67
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout68 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout68
new file mode 100644
index 0000000000..f69ef4b88d
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout68
@@ -0,0 +1 @@
+test0:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout69 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout69
new file mode 100644
index 0000000000..88ccf63397
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout69
@@ -0,0 +1 @@
+/tmp/test0/test0:a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/files/gout70 b/usr/src/test/util-tests/tests/grep_xpg4/files/gout70
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/usr/src/test/util-tests/tests/grep_xpg4/files/gout70
@@ -0,0 +1 @@
+a
diff --git a/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh b/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
index 0df113d490..911e0b9b79 100644
--- a/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
+++ b/usr/src/test/util-tests/tests/grep_xpg4/grep_test.ksh
@@ -13,6 +13,7 @@
#
# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
+# Copyright 2020 Peter Tribble.
#
XGREP=${XGREP:=/usr/bin/grep}
@@ -145,3 +146,46 @@ while read flags; do
echo "passed"
((i++))
done < /tmp/flags
+
+FLAGS4="-h
+-H"
+
+echo "$FLAGS4" > /tmp/flags
+
+while read flags; do
+ print -n "test $i: grep $flags: "
+ $XGREP $flags a test0 > out
+ err="$?"
+ if [[ $err -ne 0 ]]; then
+ fail "failed on exit: $err"
+ elif [ -n "$(diff out gout$i)" ]; then
+ print "$(diff out gout$i)"
+ fail "output is different"
+ fi
+ echo "passed"
+ ((i++))
+done < /tmp/flags
+
+FLAGS5="-r
+-hr"
+
+# need a directory with predictable contents
+rm -fr /tmp/test0
+mkdir /tmp/test0
+cp test0 /tmp/test0
+
+echo "$FLAGS5" > /tmp/flags
+
+while read flags; do
+ print -n "test $i: grep $flags: "
+ $XGREP $flags a /tmp/test0 > out
+ err="$?"
+ if [[ $err -ne 0 ]]; then
+ fail "failed on exit: $err"
+ elif [ -n "$(diff out gout$i)" ]; then
+ print "$(diff out gout$i)"
+ fail "output is different"
+ fi
+ echo "passed"
+ ((i++))
+done < /tmp/flags