aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--findutils/grep.c10
-rw-r--r--grep.c10
2 files changed, 12 insertions, 8 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 6161ba766..3ee409c64 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -42,7 +42,7 @@ static int suppress_err_msgs = 0;
42 42
43/* globals */ 43/* globals */
44static regex_t regex; /* storage space for compiled regular expression */ 44static regex_t regex; /* storage space for compiled regular expression */
45static int nmatches = 0; /* keeps track of the number of matches */ 45static int matched; /* keeps track of whether we ever matched */
46static char *cur_file = NULL; /* the current file we are reading */ 46static char *cur_file = NULL; /* the current file we are reading */
47 47
48 48
@@ -64,6 +64,7 @@ static void grep_file(FILE *file)
64 char *line = NULL; 64 char *line = NULL;
65 int ret; 65 int ret;
66 int linenum = 0; 66 int linenum = 0;
67 int nmatches = 0;
67 68
68 while ((line = get_line_from_file(file)) != NULL) { 69 while ((line = get_line_from_file(file)) != NULL) {
69 linenum++; 70 linenum++;
@@ -96,8 +97,9 @@ static void grep_file(FILE *file)
96 printf("%i\n", nmatches); 97 printf("%i\n", nmatches);
97 } 98 }
98 99
99 /* reset number of matches found to zero */ 100 /* record if we matched */
100 nmatches = 0; 101 if (nmatches != 0)
102 matched = 1;
101} 103}
102 104
103extern int grep_main(int argc, char **argv) 105extern int grep_main(int argc, char **argv)
@@ -176,7 +178,7 @@ extern int grep_main(int argc, char **argv)
176 178
177 regfree(&regex); 179 regfree(&regex);
178 180
179 if (nmatches == 0) 181 if (!matched)
180 return 1; 182 return 1;
181 183
182 return 0; 184 return 0;
diff --git a/grep.c b/grep.c
index 6161ba766..3ee409c64 100644
--- a/grep.c
+++ b/grep.c
@@ -42,7 +42,7 @@ static int suppress_err_msgs = 0;
42 42
43/* globals */ 43/* globals */
44static regex_t regex; /* storage space for compiled regular expression */ 44static regex_t regex; /* storage space for compiled regular expression */
45static int nmatches = 0; /* keeps track of the number of matches */ 45static int matched; /* keeps track of whether we ever matched */
46static char *cur_file = NULL; /* the current file we are reading */ 46static char *cur_file = NULL; /* the current file we are reading */
47 47
48 48
@@ -64,6 +64,7 @@ static void grep_file(FILE *file)
64 char *line = NULL; 64 char *line = NULL;
65 int ret; 65 int ret;
66 int linenum = 0; 66 int linenum = 0;
67 int nmatches = 0;
67 68
68 while ((line = get_line_from_file(file)) != NULL) { 69 while ((line = get_line_from_file(file)) != NULL) {
69 linenum++; 70 linenum++;
@@ -96,8 +97,9 @@ static void grep_file(FILE *file)
96 printf("%i\n", nmatches); 97 printf("%i\n", nmatches);
97 } 98 }
98 99
99 /* reset number of matches found to zero */ 100 /* record if we matched */
100 nmatches = 0; 101 if (nmatches != 0)
102 matched = 1;
101} 103}
102 104
103extern int grep_main(int argc, char **argv) 105extern int grep_main(int argc, char **argv)
@@ -176,7 +178,7 @@ extern int grep_main(int argc, char **argv)
176 178
177 regfree(&regex); 179 regfree(&regex);
178 180
179 if (nmatches == 0) 181 if (!matched)
180 return 1; 182 return 1;
181 183
182 return 0; 184 return 0;