diff options
author | Mark Whitley <markw@lineo.com> | 2000-07-18 18:37:01 +0000 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2000-07-18 18:37:01 +0000 |
commit | 8f122431f5a6c7dc2f88e86989876ba49e8192ba (patch) | |
tree | 8e0b4ebb364b384e4ddc547df9c984c2a39d5793 /findutils/grep.c | |
parent | 6efc48c1aebfc7bebd05c6f9c21cc0f9014abbd0 (diff) | |
download | busybox-w32-8f122431f5a6c7dc2f88e86989876ba49e8192ba.tar.gz busybox-w32-8f122431f5a6c7dc2f88e86989876ba49e8192ba.tar.bz2 busybox-w32-8f122431f5a6c7dc2f88e86989876ba49e8192ba.zip |
Added support for the -c (count matches) option. Made it so it works just like
GNU grep.
Diffstat (limited to 'findutils/grep.c')
-rw-r--r-- | findutils/grep.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index a2c07c523..10ad76c46 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -35,6 +35,7 @@ extern int errno; /* for use with strerror() */ | |||
35 | static int ignore_case = 0; | 35 | static int ignore_case = 0; |
36 | static int print_filename = 0; | 36 | static int print_filename = 0; |
37 | static int print_line_num = 0; | 37 | static int print_line_num = 0; |
38 | static int print_count_only = 0; | ||
38 | static int be_quiet = 0; | 39 | static int be_quiet = 0; |
39 | static int invert_search = 0; | 40 | static int invert_search = 0; |
40 | static int suppress_err_msgs = 0; | 41 | static int suppress_err_msgs = 0; |
@@ -75,14 +76,29 @@ static void grep_file(FILE *file) | |||
75 | 76 | ||
76 | nmatches++; | 77 | nmatches++; |
77 | 78 | ||
78 | print_matched_line(line, linenum); | 79 | if (!print_count_only) |
80 | print_matched_line(line, linenum); | ||
79 | 81 | ||
80 | } else if (ret == REG_NOMATCH && invert_search) { | 82 | } else if (ret == REG_NOMATCH && invert_search) { |
81 | print_matched_line(line, linenum); | 83 | |
84 | nmatches++; | ||
85 | |||
86 | if (!print_count_only) | ||
87 | print_matched_line(line, linenum); | ||
82 | } | 88 | } |
83 | 89 | ||
84 | free(line); | 90 | free(line); |
85 | } | 91 | } |
92 | |||
93 | /* special-case post processing */ | ||
94 | if (print_count_only) { | ||
95 | if (print_filename) | ||
96 | printf("%s:", cur_file); | ||
97 | printf("%i\n", nmatches); | ||
98 | } | ||
99 | |||
100 | /* reset number of matches found to zero */ | ||
101 | nmatches = 0; | ||
86 | } | 102 | } |
87 | 103 | ||
88 | extern int grep_main(int argc, char **argv) | 104 | extern int grep_main(int argc, char **argv) |
@@ -95,7 +111,7 @@ extern int grep_main(int argc, char **argv) | |||
95 | usage(grep_usage); | 111 | usage(grep_usage); |
96 | 112 | ||
97 | /* do normal option parsing */ | 113 | /* do normal option parsing */ |
98 | while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) { | 114 | while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) { |
99 | switch (opt) { | 115 | switch (opt) { |
100 | case 'i': | 116 | case 'i': |
101 | ignore_case++; | 117 | ignore_case++; |
@@ -118,6 +134,9 @@ extern int grep_main(int argc, char **argv) | |||
118 | case 's': | 134 | case 's': |
119 | suppress_err_msgs++; | 135 | suppress_err_msgs++; |
120 | break; | 136 | break; |
137 | case 'c': | ||
138 | print_count_only++; | ||
139 | break; | ||
121 | } | 140 | } |
122 | } | 141 | } |
123 | 142 | ||