From 75b1ede7c4efa5dab928435c5a17025962763a6d Mon Sep 17 00:00:00 2001 From: wosch Date: Fri, 27 Sep 1996 19:44:46 +0000 Subject: grep -q pattern file search 'pattern' in whole file 'file', from top to bottom. This is not necessary; if grep found 'pattern' it can stop further searching in file 'file'. Example: $ time ./grep-old -q Adam /usr/share/dict/* 1.93 real 1.05 user 0.85 sys $ time ./grep-new -q Adam /usr/share/dict/* 0.14 real 0.06 user 0.06 sys --- gnu/usr.bin/grep/grep.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'gnu/usr.bin') diff --git a/gnu/usr.bin/grep/grep.c b/gnu/usr.bin/grep/grep.c index ae30167..0353e26 100644 --- a/gnu/usr.bin/grep/grep.c +++ b/gnu/usr.bin/grep/grep.c @@ -343,6 +343,7 @@ static int out_line; /* Print line numbers. */ static int out_byte; /* Print byte offsets. */ static int out_before; /* Lines of leading context. */ static int out_after; /* Lines of trailing context. */ +static int count_matches; /* print a count of matching lines */ /* Internal variables to keep track of byte count, context, etc. */ static size_t totalcc; /* Total character count before bufbeg. */ @@ -589,6 +590,9 @@ grep(fd) nlines += grepbuf(beg, lim); if (pending) prpending(lim); + /* optimization */ + if (nlines && out_quiet && !count_matches) + return(nlines); } i = 0; beg = lim; @@ -659,7 +663,7 @@ main(argc, argv) { char *keys; size_t keycc, oldcc, keyalloc; - int keyfound, count_matches, no_filenames, list_files, suppress_errors; + int keyfound, no_filenames, list_files, suppress_errors; int opt, cc, desc, count, status; FILE *fp; extern char *optarg; -- cgit v1.1