diff options
-rw-r--r-- | findutils/grep.c | 23 | ||||
-rw-r--r-- | grep.c | 23 |
2 files changed, 32 insertions, 14 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index a2fff440c..061462ebc 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -104,9 +104,15 @@ static void grep_file(FILE *file) | |||
104 | exit(0); | 104 | exit(0); |
105 | } | 105 | } |
106 | 106 | ||
107 | /* otherwise, keep track of matches and print the matched line */ | 107 | /* keep track of matches */ |
108 | nmatches++; | 108 | nmatches++; |
109 | if (print_match_counts==0 && print_files_with_matches==0) { | 109 | |
110 | /* if we're just printing filenames, we stop after the first match */ | ||
111 | if (print_files_with_matches) | ||
112 | break; | ||
113 | |||
114 | /* print the matched line */ | ||
115 | if (print_match_counts == 0) { | ||
110 | #ifdef BB_FEATURE_GREP_CONTEXT | 116 | #ifdef BB_FEATURE_GREP_CONTEXT |
111 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | 117 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
112 | 118 | ||
@@ -162,14 +168,17 @@ static void grep_file(FILE *file) | |||
162 | 168 | ||
163 | 169 | ||
164 | /* special-case file post-processing for options where we don't print line | 170 | /* special-case file post-processing for options where we don't print line |
165 | * matches, just filenames */ | 171 | * matches, just filenames and possibly match counts */ |
166 | 172 | ||
167 | /* grep -cl or just grep -c: print filename:count, even if count is zero */ | 173 | /* grep -c: print [filename:]count, even if count is zero */ |
168 | if (print_match_counts) { | 174 | if (print_match_counts) { |
169 | printf("%s:%d\n", cur_file, nmatches); | 175 | if (print_filename) |
176 | printf("%s:", cur_file); | ||
177 | printf("%d\n", nmatches); | ||
170 | } | 178 | } |
171 | /* just grep -l: print just the filename, but only if we grepped the line in the file */ | 179 | |
172 | else if (print_files_with_matches && !print_match_counts && nmatches > 0) { | 180 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
181 | if (print_files_with_matches && nmatches > 0) { | ||
173 | puts(cur_file); | 182 | puts(cur_file); |
174 | } | 183 | } |
175 | 184 | ||
@@ -104,9 +104,15 @@ static void grep_file(FILE *file) | |||
104 | exit(0); | 104 | exit(0); |
105 | } | 105 | } |
106 | 106 | ||
107 | /* otherwise, keep track of matches and print the matched line */ | 107 | /* keep track of matches */ |
108 | nmatches++; | 108 | nmatches++; |
109 | if (print_match_counts==0 && print_files_with_matches==0) { | 109 | |
110 | /* if we're just printing filenames, we stop after the first match */ | ||
111 | if (print_files_with_matches) | ||
112 | break; | ||
113 | |||
114 | /* print the matched line */ | ||
115 | if (print_match_counts == 0) { | ||
110 | #ifdef BB_FEATURE_GREP_CONTEXT | 116 | #ifdef BB_FEATURE_GREP_CONTEXT |
111 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | 117 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
112 | 118 | ||
@@ -162,14 +168,17 @@ static void grep_file(FILE *file) | |||
162 | 168 | ||
163 | 169 | ||
164 | /* special-case file post-processing for options where we don't print line | 170 | /* special-case file post-processing for options where we don't print line |
165 | * matches, just filenames */ | 171 | * matches, just filenames and possibly match counts */ |
166 | 172 | ||
167 | /* grep -cl or just grep -c: print filename:count, even if count is zero */ | 173 | /* grep -c: print [filename:]count, even if count is zero */ |
168 | if (print_match_counts) { | 174 | if (print_match_counts) { |
169 | printf("%s:%d\n", cur_file, nmatches); | 175 | if (print_filename) |
176 | printf("%s:", cur_file); | ||
177 | printf("%d\n", nmatches); | ||
170 | } | 178 | } |
171 | /* just grep -l: print just the filename, but only if we grepped the line in the file */ | 179 | |
172 | else if (print_files_with_matches && !print_match_counts && nmatches > 0) { | 180 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
181 | if (print_files_with_matches && nmatches > 0) { | ||
173 | puts(cur_file); | 182 | puts(cur_file); |
174 | } | 183 | } |
175 | 184 | ||