summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai2000-08-06 15:25:53 +0000
committerMatt Kraai2000-08-06 15:25:53 +0000
commitdeb95f69b6afab4b9e81cb8b2d63ba98469d1622 (patch)
tree742ec804070ae3e9c88f7a6878bf88ba196b328c
parent46ade979839e6ec400d4b6f2c5bbd209b8940738 (diff)
downloadbusybox-deb95f69b6afab4b9e81cb8b2d63ba98469d1622.zip
busybox-deb95f69b6afab4b9e81cb8b2d63ba98469d1622.tar.gz
Exit with the appropriate value when grepping multiple files.
-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 6161ba7..3ee409c 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -42,7 +42,7 @@ static int suppress_err_msgs = 0;
/* globals */
static regex_t regex; /* storage space for compiled regular expression */
-static int nmatches = 0; /* keeps track of the number of matches */
+static int matched; /* keeps track of whether we ever matched */
static char *cur_file = NULL; /* the current file we are reading */
@@ -64,6 +64,7 @@ static void grep_file(FILE *file)
char *line = NULL;
int ret;
int linenum = 0;
+ int nmatches = 0;
while ((line = get_line_from_file(file)) != NULL) {
linenum++;
@@ -96,8 +97,9 @@ static void grep_file(FILE *file)
printf("%i\n", nmatches);
}
- /* reset number of matches found to zero */
- nmatches = 0;
+ /* record if we matched */
+ if (nmatches != 0)
+ matched = 1;
}
extern int grep_main(int argc, char **argv)
@@ -176,7 +178,7 @@ extern int grep_main(int argc, char **argv)
regfree(&regex);
- if (nmatches == 0)
+ if (!matched)
return 1;
return 0;
diff --git a/grep.c b/grep.c
index 6161ba7..3ee409c 100644
--- a/grep.c
+++ b/grep.c
@@ -42,7 +42,7 @@ static int suppress_err_msgs = 0;
/* globals */
static regex_t regex; /* storage space for compiled regular expression */
-static int nmatches = 0; /* keeps track of the number of matches */
+static int matched; /* keeps track of whether we ever matched */
static char *cur_file = NULL; /* the current file we are reading */
@@ -64,6 +64,7 @@ static void grep_file(FILE *file)
char *line = NULL;
int ret;
int linenum = 0;
+ int nmatches = 0;
while ((line = get_line_from_file(file)) != NULL) {
linenum++;
@@ -96,8 +97,9 @@ static void grep_file(FILE *file)
printf("%i\n", nmatches);
}
- /* reset number of matches found to zero */
- nmatches = 0;
+ /* record if we matched */
+ if (nmatches != 0)
+ matched = 1;
}
extern int grep_main(int argc, char **argv)
@@ -176,7 +178,7 @@ extern int grep_main(int argc, char **argv)
regfree(&regex);
- if (nmatches == 0)
+ if (!matched)
return 1;
return 0;