static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
+static int files_that_match = 0;
#ifdef BB_FEATURE_GREP_CONTEXT
extern char *optarg; /* in getopt.h */
/* otherwise, keep track of matches and print the matched line */
nmatches++;
- if (!print_count_only) {
+ if (print_count_only==0 && files_that_match==0) {
#ifdef BB_FEATURE_GREP_CONTEXT
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
}
/* special-case post processing */
- if (print_count_only) {
+ if (files_that_match) {
+ if (nmatches > 0) {
+ printf("%s", cur_file);
+ if (nmatches)
+ printf(":%d", nmatches);
+ printf("\n");
+ }
+ } else if (print_count_only) {
if (print_filename)
printf("%s:", cur_file);
printf("%i\n", nmatches);
- }
+ }
/* remember if we matched */
if (nmatches != 0)
#endif
/* do normal option parsing */
- while ((opt = getopt(argc, argv, "iHhnqvsc"
+ while ((opt = getopt(argc, argv, "iHhlnqvsc"
#ifdef BB_FEATURE_GREP_CONTEXT
"A:B:C:"
#endif
case 'i':
ignore_case++;
break;
+ case 'l':
+ files_that_match++;
+ break;
case 'H':
print_filename++;
break;
before_buf = (char **)calloc(lines_before, sizeof(char *));
break;
#endif /* BB_FEATURE_GREP_CONTEXT */
+ default:
+ show_usage();
}
}
show_usage();
/* sanity check */
- if (print_count_only || be_quiet) {
+ if (print_count_only || be_quiet || files_that_match) {
print_line_num = 0;
#ifdef BB_FEATURE_GREP_CONTEXT
lines_before = 0;