summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley2000-07-18 21:02:06 +0000
committerMark Whitley2000-07-18 21:02:06 +0000
commit2ef880b23697b552303ab3dbbdea84569f809ddc (patch)
tree633d5a24e9f08bc5d5f01457c265ae4234c5d350
parenta9d69fb5680c28a2f3d68b066e42d6d954d790bf (diff)
downloadbusybox-2ef880b23697b552303ab3dbbdea84569f809ddc.zip
busybox-2ef880b23697b552303ab3dbbdea84569f809ddc.tar.gz
Consolodated some common code into print_matched_line to make 'grep_file' a
little more terse & clean. Also made a few minor formatting changes.
-rw-r--r--findutils/grep.c21
-rw-r--r--grep.c21
2 files changed, 22 insertions, 20 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 10ad76c..2f240e4 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -48,6 +48,9 @@ static char *cur_file = NULL; /* the current file we are reading */
static void print_matched_line(char *line, int linenum)
{
+ if (print_count_only)
+ return;
+
if (print_filename)
printf("%s:", cur_file);
if (print_line_num)
@@ -75,16 +78,12 @@ static void grep_file(FILE *file)
}
nmatches++;
+ print_matched_line(line, linenum);
- if (!print_count_only)
- print_matched_line(line, linenum);
-
- } else if (ret == REG_NOMATCH && invert_search) {
-
+ }
+ else if (ret == REG_NOMATCH && invert_search) {
nmatches++;
-
- if (!print_count_only)
- print_matched_line(line, linenum);
+ print_matched_line(line, linenum);
}
free(line);
@@ -161,7 +160,8 @@ extern int grep_main(int argc, char **argv)
* stdin. Otherwise, we grep through all the files specified. */
if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
grep_file(stdin);
- } else {
+ }
+ else {
int i;
FILE *file;
for (i = optind + 1; i < argc; i++) {
@@ -170,7 +170,8 @@ extern int grep_main(int argc, char **argv)
if (file == NULL) {
if (!suppress_err_msgs)
errorMsg("%s: %s\n", cur_file, strerror(errno));
- } else {
+ }
+ else {
grep_file(file);
fclose(file);
}
diff --git a/grep.c b/grep.c
index 10ad76c..2f240e4 100644
--- a/grep.c
+++ b/grep.c
@@ -48,6 +48,9 @@ static char *cur_file = NULL; /* the current file we are reading */
static void print_matched_line(char *line, int linenum)
{
+ if (print_count_only)
+ return;
+
if (print_filename)
printf("%s:", cur_file);
if (print_line_num)
@@ -75,16 +78,12 @@ static void grep_file(FILE *file)
}
nmatches++;
+ print_matched_line(line, linenum);
- if (!print_count_only)
- print_matched_line(line, linenum);
-
- } else if (ret == REG_NOMATCH && invert_search) {
-
+ }
+ else if (ret == REG_NOMATCH && invert_search) {
nmatches++;
-
- if (!print_count_only)
- print_matched_line(line, linenum);
+ print_matched_line(line, linenum);
}
free(line);
@@ -161,7 +160,8 @@ extern int grep_main(int argc, char **argv)
* stdin. Otherwise, we grep through all the files specified. */
if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
grep_file(stdin);
- } else {
+ }
+ else {
int i;
FILE *file;
for (i = optind + 1; i < argc; i++) {
@@ -170,7 +170,8 @@ extern int grep_main(int argc, char **argv)
if (file == NULL) {
if (!suppress_err_msgs)
errorMsg("%s: %s\n", cur_file, strerror(errno));
- } else {
+ }
+ else {
grep_file(file);
fclose(file);
}