From: Eric Andersen Date: Mon, 9 Apr 2001 23:26:31 +0000 (-0000) Subject: Make 'grep -l' work X-Git-Tag: 0_51~11 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=303dd28bb0120c8b12c886cadb2d25d1b8fea08b;p=oweals%2Fbusybox.git Make 'grep -l' work --- diff --git a/applets/usage.h b/applets/usage.h index bf2f7bd63..5712c4413 100644 --- a/applets/usage.h +++ b/applets/usage.h @@ -522,6 +522,7 @@ "\t-H\tprefix output lines with filename where match was found\n" \ "\t-h\tsuppress the prefixing filename on output\n" \ "\t-i\tignore case distinctions\n" \ + "\t-l\tlist names of files that match\n" \ "\t-n\tprint line number with output lines\n" \ "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \ "\t-v\tselect non-matching lines\n" \ 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; 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 */ @@ -105,7 +106,7 @@ static void grep_file(FILE *file) /* 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; @@ -163,11 +164,18 @@ static void grep_file(FILE *file) } /* 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) @@ -183,7 +191,7 @@ extern int grep_main(int argc, char **argv) #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 @@ -192,6 +200,9 @@ extern int grep_main(int argc, char **argv) case 'i': ignore_case++; break; + case 'l': + files_that_match++; + break; case 'H': print_filename++; break; @@ -242,7 +253,7 @@ extern int grep_main(int argc, char **argv) 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; diff --git a/grep.c b/grep.c index 8c6797647..b519adf18 100644 --- a/grep.c +++ b/grep.c @@ -41,6 +41,7 @@ static int print_count_only = 0; 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 */ @@ -105,7 +106,7 @@ static void grep_file(FILE *file) /* 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; @@ -163,11 +164,18 @@ static void grep_file(FILE *file) } /* 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) @@ -183,7 +191,7 @@ extern int grep_main(int argc, char **argv) #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 @@ -192,6 +200,9 @@ extern int grep_main(int argc, char **argv) case 'i': ignore_case++; break; + case 'l': + files_that_match++; + break; case 'H': print_filename++; break; @@ -242,7 +253,7 @@ extern int grep_main(int argc, char **argv) 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; diff --git a/include/usage.h b/include/usage.h index bf2f7bd63..5712c4413 100644 --- a/include/usage.h +++ b/include/usage.h @@ -522,6 +522,7 @@ "\t-H\tprefix output lines with filename where match was found\n" \ "\t-h\tsuppress the prefixing filename on output\n" \ "\t-i\tignore case distinctions\n" \ + "\t-l\tlist names of files that match\n" \ "\t-n\tprint line number with output lines\n" \ "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \ "\t-v\tselect non-matching lines\n" \ diff --git a/usage.h b/usage.h index bf2f7bd63..5712c4413 100644 --- a/usage.h +++ b/usage.h @@ -522,6 +522,7 @@ "\t-H\tprefix output lines with filename where match was found\n" \ "\t-h\tsuppress the prefixing filename on output\n" \ "\t-i\tignore case distinctions\n" \ + "\t-l\tlist names of files that match\n" \ "\t-n\tprint line number with output lines\n" \ "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \ "\t-v\tselect non-matching lines\n" \