Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 Sep 2011 21:17:55 +0000 (UTC)
From:      Gabor Kovesdan <gabor@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r225493 - user/gabor/grep/trunk
Message-ID:  <201109112117.p8BLHtOt096309@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: gabor
Date: Sun Sep 11 21:17:55 2011
New Revision: 225493
URL: http://svn.freebsd.org/changeset/base/225493

Log:
  - Partly fix context after maximum count of matches. GNU grep does not
    include further matching lines in the context so the behavior is still
    not totally the same.
  
  Reported by:	aakuusta@gmail.com

Modified:
  user/gabor/grep/trunk/grep.c
  user/gabor/grep/trunk/grep.h
  user/gabor/grep/trunk/util.c

Modified: user/gabor/grep/trunk/grep.c
==============================================================================
--- user/gabor/grep/trunk/grep.c	Sun Sep 11 20:38:33 2011	(r225492)
+++ user/gabor/grep/trunk/grep.c	Sun Sep 11 21:17:55 2011	(r225493)
@@ -106,7 +106,7 @@ bool	 hflag;		/* -h: don't print filenam
 bool	 iflag;		/* -i: ignore case */
 bool	 lflag;		/* -l: only show names of files with matches */
 bool	 mflag;		/* -m x: stop reading the files after x matches */
-unsigned long long mcount;	/* count for -m */
+long long mcount;	/* count for -m */
 bool	 nflag;		/* -n: show line numbers in front of matching lines */
 bool	 oflag;		/* -o: print only matching part */
 bool	 qflag;		/* -q: quiet mode (don't output anything) */
@@ -521,8 +521,8 @@ main(int argc, char *argv[])
 		case 'm':
 			mflag = true;
 			errno = 0;
-			mcount = strtoull(optarg, &ep, 10);
-			if (((errno == ERANGE) && (mcount == ULLONG_MAX)) ||
+			mcount = strtoll(optarg, &ep, 10);
+			if (((errno == ERANGE) && (mcount == LLONG_MAX)) ||
 			    ((errno == EINVAL) && (mcount == 0)))
 				err(2, NULL);
 			else if (ep[0] != '\0') {

Modified: user/gabor/grep/trunk/grep.h
==============================================================================
--- user/gabor/grep/trunk/grep.h	Sun Sep 11 20:38:33 2011	(r225492)
+++ user/gabor/grep/trunk/grep.h	Sun Sep 11 21:17:55 2011	(r225493)
@@ -113,7 +113,8 @@ extern bool	 Eflag, Fflag, Gflag, Hflag,
 		 bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
 		 qflag, sflag, vflag, wflag, xflag;
 extern bool	 dexclude, dinclude, fexclude, finclude, lbflag, nullflag;
-extern unsigned long long Aflag, Bflag, mcount;
+extern unsigned long long Aflag, Bflag;
+extern long long mcount;
 extern char	*label;
 extern const char *color;
 extern int	 binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave;

Modified: user/gabor/grep/trunk/util.c
==============================================================================
--- user/gabor/grep/trunk/util.c	Sun Sep 11 20:38:33 2011	(r225492)
+++ user/gabor/grep/trunk/util.c	Sun Sep 11 21:17:55 2011	(r225493)
@@ -233,13 +233,8 @@ procfile(const char *fn)
 			linesqueued++;
 		}
 		c += t;
-
-		/* Count the matches if we have a match limit */
-		if (mflag) {
-			mcount -= t;
-			if (mcount <= 0)
-				break;
-		}
+		if (mflag && mcount < 0)
+			break;
 	}
 	if (Bflag > 0)
 		clearqueue();
@@ -348,6 +343,11 @@ procline(struct str *l, int nottext)
 			break; 	/* No matches */
 	}
 
+
+	/* Count the matches if we have a match limit */
+	if (mflag)
+		mcount -= c;
+
 	if (c && binbehave == BINFILE_BIN && nottext)
 		return (c); /* Binary file */
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201109112117.p8BLHtOt096309>