diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-04-09 23:26:31 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-04-09 23:26:31 +0000 |
commit | 303dd28bb0120c8b12c886cadb2d25d1b8fea08b (patch) | |
tree | 277feaf473a69bc582eec25e094a50fd8bf1a903 /findutils/grep.c | |
parent | 4391a16c92cef744fc715cb7347b7b71fc8f312e (diff) | |
download | busybox-w32-303dd28bb0120c8b12c886cadb2d25d1b8fea08b.tar.gz busybox-w32-303dd28bb0120c8b12c886cadb2d25d1b8fea08b.tar.bz2 busybox-w32-303dd28bb0120c8b12c886cadb2d25d1b8fea08b.zip |
Make 'grep -l' work
Diffstat (limited to 'findutils/grep.c')
-rw-r--r-- | findutils/grep.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 8c6797647..b519adf18 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -41,6 +41,7 @@ static int print_count_only = 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 | 45 | ||
45 | #ifdef BB_FEATURE_GREP_CONTEXT | 46 | #ifdef BB_FEATURE_GREP_CONTEXT |
46 | extern char *optarg; /* in getopt.h */ | 47 | extern char *optarg; /* in getopt.h */ |
@@ -105,7 +106,7 @@ static void grep_file(FILE *file) | |||
105 | 106 | ||
106 | /* otherwise, keep track of matches and print the matched line */ | 107 | /* otherwise, keep track of matches and print the matched line */ |
107 | nmatches++; | 108 | nmatches++; |
108 | if (!print_count_only) { | 109 | if (print_count_only==0 && files_that_match==0) { |
109 | #ifdef BB_FEATURE_GREP_CONTEXT | 110 | #ifdef BB_FEATURE_GREP_CONTEXT |
110 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | 111 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
111 | 112 | ||
@@ -163,11 +164,18 @@ static void grep_file(FILE *file) | |||
163 | } | 164 | } |
164 | 165 | ||
165 | /* special-case post processing */ | 166 | /* special-case post processing */ |
166 | if (print_count_only) { | 167 | if (files_that_match) { |
168 | if (nmatches > 0) { | ||
169 | printf("%s", cur_file); | ||
170 | if (nmatches) | ||
171 | printf(":%d", nmatches); | ||
172 | printf("\n"); | ||
173 | } | ||
174 | } else if (print_count_only) { | ||
167 | if (print_filename) | 175 | if (print_filename) |
168 | printf("%s:", cur_file); | 176 | printf("%s:", cur_file); |
169 | printf("%i\n", nmatches); | 177 | printf("%i\n", nmatches); |
170 | } | 178 | } |
171 | 179 | ||
172 | /* remember if we matched */ | 180 | /* remember if we matched */ |
173 | if (nmatches != 0) | 181 | if (nmatches != 0) |
@@ -183,7 +191,7 @@ extern int grep_main(int argc, char **argv) | |||
183 | #endif | 191 | #endif |
184 | 192 | ||
185 | /* do normal option parsing */ | 193 | /* do normal option parsing */ |
186 | while ((opt = getopt(argc, argv, "iHhnqvsc" | 194 | while ((opt = getopt(argc, argv, "iHhlnqvsc" |
187 | #ifdef BB_FEATURE_GREP_CONTEXT | 195 | #ifdef BB_FEATURE_GREP_CONTEXT |
188 | "A:B:C:" | 196 | "A:B:C:" |
189 | #endif | 197 | #endif |
@@ -192,6 +200,9 @@ extern int grep_main(int argc, char **argv) | |||
192 | case 'i': | 200 | case 'i': |
193 | ignore_case++; | 201 | ignore_case++; |
194 | break; | 202 | break; |
203 | case 'l': | ||
204 | files_that_match++; | ||
205 | break; | ||
195 | case 'H': | 206 | case 'H': |
196 | print_filename++; | 207 | print_filename++; |
197 | break; | 208 | break; |
@@ -242,7 +253,7 @@ extern int grep_main(int argc, char **argv) | |||
242 | show_usage(); | 253 | show_usage(); |
243 | 254 | ||
244 | /* sanity check */ | 255 | /* sanity check */ |
245 | if (print_count_only || be_quiet) { | 256 | if (print_count_only || be_quiet || files_that_match) { |
246 | print_line_num = 0; | 257 | print_line_num = 0; |
247 | #ifdef BB_FEATURE_GREP_CONTEXT | 258 | #ifdef BB_FEATURE_GREP_CONTEXT |
248 | lines_before = 0; | 259 | lines_before = 0; |