projects
/
oweals
/
busybox.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
6efc48c
)
Added support for the -c (count matches) option. Made it so it works just like
author
Mark Whitley
<markw@lineo.com>
Tue, 18 Jul 2000 18:37:01 +0000
(18:37 -0000)
committer
Mark Whitley
<markw@lineo.com>
Tue, 18 Jul 2000 18:37:01 +0000
(18:37 -0000)
GNU grep.
findutils/grep.c
patch
|
blob
|
history
grep.c
patch
|
blob
|
history
diff --git
a/findutils/grep.c
b/findutils/grep.c
index a2c07c52371513e07ad41408eb9140f9230edbb3..10ad76c46a17acf49723cd92c49b4b6e4a5c3f7c 100644
(file)
--- a/
findutils/grep.c
+++ b/
findutils/grep.c
@@
-35,6
+35,7
@@
extern int errno; /* for use with strerror() */
static int ignore_case = 0;
static int print_filename = 0;
static int print_line_num = 0;
static int ignore_case = 0;
static int print_filename = 0;
static int print_line_num = 0;
+static int print_count_only = 0;
static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
@@
-75,14
+76,29
@@
static void grep_file(FILE *file)
nmatches++;
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) {
- print_matched_line(line, linenum);
+
+ nmatches++;
+
+ if (!print_count_only)
+ print_matched_line(line, linenum);
}
free(line);
}
}
free(line);
}
+
+ /* special-case post processing */
+ if (print_count_only) {
+ if (print_filename)
+ printf("%s:", cur_file);
+ printf("%i\n", nmatches);
+ }
+
+ /* reset number of matches found to zero */
+ nmatches = 0;
}
extern int grep_main(int argc, char **argv)
}
extern int grep_main(int argc, char **argv)
@@
-95,7
+111,7
@@
extern int grep_main(int argc, char **argv)
usage(grep_usage);
/* do normal option parsing */
usage(grep_usage);
/* do normal option parsing */
- while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) {
+ while ((opt = getopt(argc, argv, "iHhnqvs
c
")) > 0) {
switch (opt) {
case 'i':
ignore_case++;
switch (opt) {
case 'i':
ignore_case++;
@@
-118,6
+134,9
@@
extern int grep_main(int argc, char **argv)
case 's':
suppress_err_msgs++;
break;
case 's':
suppress_err_msgs++;
break;
+ case 'c':
+ print_count_only++;
+ break;
}
}
}
}
diff --git
a/grep.c
b/grep.c
index a2c07c52371513e07ad41408eb9140f9230edbb3..10ad76c46a17acf49723cd92c49b4b6e4a5c3f7c 100644
(file)
--- a/
grep.c
+++ b/
grep.c
@@
-35,6
+35,7
@@
extern int errno; /* for use with strerror() */
static int ignore_case = 0;
static int print_filename = 0;
static int print_line_num = 0;
static int ignore_case = 0;
static int print_filename = 0;
static int print_line_num = 0;
+static int print_count_only = 0;
static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
static int be_quiet = 0;
static int invert_search = 0;
static int suppress_err_msgs = 0;
@@
-75,14
+76,29
@@
static void grep_file(FILE *file)
nmatches++;
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) {
- print_matched_line(line, linenum);
+
+ nmatches++;
+
+ if (!print_count_only)
+ print_matched_line(line, linenum);
}
free(line);
}
}
free(line);
}
+
+ /* special-case post processing */
+ if (print_count_only) {
+ if (print_filename)
+ printf("%s:", cur_file);
+ printf("%i\n", nmatches);
+ }
+
+ /* reset number of matches found to zero */
+ nmatches = 0;
}
extern int grep_main(int argc, char **argv)
}
extern int grep_main(int argc, char **argv)
@@
-95,7
+111,7
@@
extern int grep_main(int argc, char **argv)
usage(grep_usage);
/* do normal option parsing */
usage(grep_usage);
/* do normal option parsing */
- while ((opt = getopt(argc, argv, "iHhnqvs")) > 0) {
+ while ((opt = getopt(argc, argv, "iHhnqvs
c
")) > 0) {
switch (opt) {
case 'i':
ignore_case++;
switch (opt) {
case 'i':
ignore_case++;
@@
-118,6
+134,9
@@
extern int grep_main(int argc, char **argv)
case 's':
suppress_err_msgs++;
break;
case 's':
suppress_err_msgs++;
break;
+ case 'c':
+ print_count_only++;
+ break;
}
}
}
}