Bug 173673

Summary: BSD fgrep cannot handle multiple patterns specified as one command-line argument
Product: Base System Reporter: Andrey A. Chernov <ache>
Component: binAssignee: Gabor Kovesdan <gabor>
Status: Closed Works As Intended    
Severity: Affects Only Me    
Priority: Normal    
Version: 9.1-PRERELEASE   
Hardware: Any   
OS: Any   

Description Andrey A. Chernov freebsd_committer freebsd_triage 2012-11-17 03:10:00 UTC
	BSD fgrep -v do different things than gnu fgrep for multi-line
	pattern. Run following simple test. For BSD fgrep it prints
1
2
3
	and for gnu fgrep it correctly prints
3

How-To-Repeat: ------------------------ cut here ----------------------
#!/bin/csh

(echo 1; echo 2; echo 3) | fgrep -v "1\
2"

(echo 1; echo 2; echo 3) | gnugrep -F -v "1\
2"
------------------------ cut here ----------------------
Comment 1 Andrey A. Chernov freebsd_committer freebsd_triage 2012-11-21 05:36:30 UTC
Responsible Changed
From-To: freebsd-bugs->gabor

Assign to bsdgrep maintainer
Comment 2 Andrey A. Chernov freebsd_committer freebsd_triage 2012-12-31 18:49:06 UTC
Better test case including GNU grep from ports too (correct result) and
always using absolute paths to avoid possible confusion. Also note that
first line is /bin/csh not /bin/sh (newline escapes are different).

#!/bin/csh

(echo 1; echo 2; echo 3) | /usr/bin/fgrep -v "1\
2"

(echo 1; echo 2; echo 3) | /usr/local/bin/grep -F -v "1\
2"

(echo 1; echo 2; echo 3) | /usr/bin/gnugrep -F -v "1\
2"

Output:
1
2
3
3
3
Comment 3 dfilter service freebsd_committer freebsd_triage 2013-01-05 14:52:44 UTC
Author: gabor
Date: Sat Jan  5 14:52:31 2013
New Revision: 245057
URL: http://svnweb.freebsd.org/changeset/base/245057

Log:
  - Fix handling of the case when multiple patterns are specified in a single
    command line argument, separated by newlines
  
  PR:		bin/173673
  Submitted by:	ache
  MFC after:	1 week

Modified:
  head/usr.bin/grep/grep.c

Modified: head/usr.bin/grep/grep.c
==============================================================================
--- head/usr.bin/grep/grep.c	Sat Jan  5 11:13:48 2013	(r245056)
+++ head/usr.bin/grep/grep.c	Sat Jan  5 14:52:31 2013	(r245057)
@@ -479,7 +479,13 @@ main(int argc, char *argv[])
 			grepbehave = GREP_EXTENDED;
 			break;
 		case 'e':
-			add_pattern(optarg, strlen(optarg));
+			{
+				char *token;
+				char *string = strdup(optarg);
+
+				while ((token = strsep(&string, "\n")) != NULL)
+					add_pattern(token, strlen(token));
+			}
 			needpattern = 0;
 			break;
 		case 'F':
@@ -668,7 +674,11 @@ main(int argc, char *argv[])
 
 	/* Process patterns from command line */
 	if (aargc != 0 && needpattern) {
-		add_pattern(*aargv, strlen(*aargv));
+		char *token;
+		char *string = strdup(*aargv);
+
+		while ((token = strsep(&string, "\n")) != NULL)
+			add_pattern(token, strlen(token));
 		--aargc;
 		++aargv;
 	}
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 4 Gabor Kovesdan freebsd_committer freebsd_triage 2013-01-05 16:37:00 UTC
State Changed
From-To: open->patched

Committed to HEAD, thanks!
Comment 5 Andrey A. Chernov freebsd_committer freebsd_triage 2014-06-02 16:59:56 UTC
Committed to STABLE too