summaryrefslogtreecommitdiff
path: root/findutils/grep.c
diff options
context:
space:
mode:
authorMark Whitley2001-05-14 19:40:32 +0000
committerMark Whitley2001-05-14 19:40:32 +0000
commit35e59bed9750e2672af11bf3f30093d5e5bd1777 (patch)
tree4f603c07e329c409652052770e56139de2f099d1 /findutils/grep.c
parent452cc1f2939db40be0ada7212ea6217e1b5effd6 (diff)
downloadbusybox-35e59bed9750e2672af11bf3f30093d5e5bd1777.zip
busybox-35e59bed9750e2672af11bf3f30093d5e5bd1777.tar.gz
Fixed a subtle bug in the handling of -l and -c flags.
Diffstat (limited to 'findutils/grep.c')
-rw-r--r--findutils/grep.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 12bcead..6d5bd7f 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
static int ignore_case = 0;
static int print_filename = 0;
static int print_line_num = 0;
-static int print_count_only = 0;
+static int print_match_counts = 0;
static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
-static int files_that_match = 0;
+static int print_files_with_matches = 0;
#ifdef BB_FEATURE_GREP_CONTEXT
extern char *optarg; /* in getopt.h */
@@ -106,7 +106,7 @@ static void grep_file(FILE *file)
/* otherwise, keep track of matches and print the matched line */
nmatches++;
- if (print_count_only==0 && files_that_match==0) {
+ if (print_match_counts==0 && print_files_with_matches==0) {
#ifdef BB_FEATURE_GREP_CONTEXT
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
@@ -160,19 +160,19 @@ static void grep_file(FILE *file)
free(line);
}
- /* special-case post processing */
- 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);
- }
+
+ /* special-case file post-processing for options where we don't print line
+ * matches, just filenames */
+
+ /* grep -cl or just grep -c: print filename:count, even if count is zero */
+ if (print_match_counts) {
+ printf("%s:%d\n", cur_file, nmatches);
+ }
+ /* just grep -l: print just the filename, but only if we grepped the line in the file */
+ else if (print_files_with_matches && !print_match_counts && nmatches > 0) {
+ printf("%s\n", cur_file);
+ }
+
/* remember if we matched */
if (nmatches != 0)
@@ -198,7 +198,7 @@ extern int grep_main(int argc, char **argv)
ignore_case++;
break;
case 'l':
- files_that_match++;
+ print_files_with_matches++;
break;
case 'H':
print_filename++;
@@ -219,7 +219,7 @@ extern int grep_main(int argc, char **argv)
suppress_err_msgs++;
break;
case 'c':
- print_count_only++;
+ print_match_counts++;
break;
#ifdef BB_FEATURE_GREP_CONTEXT
case 'A':
@@ -250,7 +250,7 @@ extern int grep_main(int argc, char **argv)
show_usage();
/* sanity check */
- if (print_count_only || be_quiet || files_that_match) {
+ if (print_match_counts || be_quiet || print_files_with_matches) {
print_line_num = 0;
#ifdef BB_FEATURE_GREP_CONTEXT
lines_before = 0;