diff options
author | Mark Whitley <markw@lineo.com> | 2001-05-14 19:40:32 +0000 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2001-05-14 19:40:32 +0000 |
commit | 35e59bed9750e2672af11bf3f30093d5e5bd1777 (patch) | |
tree | 4f603c07e329c409652052770e56139de2f099d1 /findutils/grep.c | |
parent | 452cc1f2939db40be0ada7212ea6217e1b5effd6 (diff) | |
download | busybox-w32-35e59bed9750e2672af11bf3f30093d5e5bd1777.tar.gz busybox-w32-35e59bed9750e2672af11bf3f30093d5e5bd1777.tar.bz2 busybox-w32-35e59bed9750e2672af11bf3f30093d5e5bd1777.zip |
Fixed a subtle bug in the handling of -l and -c flags.
Diffstat (limited to 'findutils/grep.c')
-rw-r--r-- | findutils/grep.c | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 12bcead7a..6d5bd7fd1 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -37,11 +37,11 @@ extern void xregcomp(regex_t *preg, const char *regex, int cflags); /* in busybo | |||
37 | static int ignore_case = 0; | 37 | static int ignore_case = 0; |
38 | static int print_filename = 0; | 38 | static int print_filename = 0; |
39 | static int print_line_num = 0; | 39 | static int print_line_num = 0; |
40 | static int print_count_only = 0; | 40 | static int print_match_counts = 0; |
41 | static int be_quiet = 0; | 41 | static int be_quiet = 0; |
42 | static int invert_search = 0; | 42 | static int invert_search = 0; |
43 | static int suppress_err_msgs = 0; | 43 | static int suppress_err_msgs = 0; |
44 | static int files_that_match = 0; | 44 | static int print_files_with_matches = 0; |
45 | 45 | ||
46 | #ifdef BB_FEATURE_GREP_CONTEXT | 46 | #ifdef BB_FEATURE_GREP_CONTEXT |
47 | extern char *optarg; /* in getopt.h */ | 47 | extern char *optarg; /* in getopt.h */ |
@@ -106,7 +106,7 @@ static void grep_file(FILE *file) | |||
106 | 106 | ||
107 | /* otherwise, keep track of matches and print the matched line */ | 107 | /* otherwise, keep track of matches and print the matched line */ |
108 | nmatches++; | 108 | nmatches++; |
109 | if (print_count_only==0 && files_that_match==0) { | 109 | if (print_match_counts==0 && print_files_with_matches==0) { |
110 | #ifdef BB_FEATURE_GREP_CONTEXT | 110 | #ifdef BB_FEATURE_GREP_CONTEXT |
111 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | 111 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
112 | 112 | ||
@@ -160,19 +160,19 @@ static void grep_file(FILE *file) | |||
160 | free(line); | 160 | free(line); |
161 | } | 161 | } |
162 | 162 | ||
163 | /* special-case post processing */ | 163 | |
164 | if (files_that_match) { | 164 | /* special-case file post-processing for options where we don't print line |
165 | if (nmatches > 0) { | 165 | * matches, just filenames */ |
166 | printf("%s", cur_file); | 166 | |
167 | if (nmatches) | 167 | /* grep -cl or just grep -c: print filename:count, even if count is zero */ |
168 | printf(":%d", nmatches); | 168 | if (print_match_counts) { |
169 | printf("\n"); | 169 | printf("%s:%d\n", cur_file, nmatches); |
170 | } | 170 | } |
171 | } else if (print_count_only) { | 171 | /* just grep -l: print just the filename, but only if we grepped the line in the file */ |
172 | if (print_filename) | 172 | else if (print_files_with_matches && !print_match_counts && nmatches > 0) { |
173 | printf("%s:", cur_file); | 173 | printf("%s\n", cur_file); |
174 | printf("%i\n", nmatches); | 174 | } |
175 | } | 175 | |
176 | 176 | ||
177 | /* remember if we matched */ | 177 | /* remember if we matched */ |
178 | if (nmatches != 0) | 178 | if (nmatches != 0) |
@@ -198,7 +198,7 @@ extern int grep_main(int argc, char **argv) | |||
198 | ignore_case++; | 198 | ignore_case++; |
199 | break; | 199 | break; |
200 | case 'l': | 200 | case 'l': |
201 | files_that_match++; | 201 | print_files_with_matches++; |
202 | break; | 202 | break; |
203 | case 'H': | 203 | case 'H': |
204 | print_filename++; | 204 | print_filename++; |
@@ -219,7 +219,7 @@ extern int grep_main(int argc, char **argv) | |||
219 | suppress_err_msgs++; | 219 | suppress_err_msgs++; |
220 | break; | 220 | break; |
221 | case 'c': | 221 | case 'c': |
222 | print_count_only++; | 222 | print_match_counts++; |
223 | break; | 223 | break; |
224 | #ifdef BB_FEATURE_GREP_CONTEXT | 224 | #ifdef BB_FEATURE_GREP_CONTEXT |
225 | case 'A': | 225 | case 'A': |
@@ -250,7 +250,7 @@ extern int grep_main(int argc, char **argv) | |||
250 | show_usage(); | 250 | show_usage(); |
251 | 251 | ||
252 | /* sanity check */ | 252 | /* sanity check */ |
253 | if (print_count_only || be_quiet || files_that_match) { | 253 | if (print_match_counts || be_quiet || print_files_with_matches) { |
254 | print_line_num = 0; | 254 | print_line_num = 0; |
255 | #ifdef BB_FEATURE_GREP_CONTEXT | 255 | #ifdef BB_FEATURE_GREP_CONTEXT |
256 | lines_before = 0; | 256 | lines_before = 0; |