mirror of
https://github.com/sheumann/hush.git
synced 2024-12-22 14:30:31 +00:00
Make 'grep -l' work
This commit is contained in:
parent
4391a16c92
commit
303dd28bb0
@ -522,6 +522,7 @@
|
|||||||
"\t-H\tprefix output lines with filename where match was found\n" \
|
"\t-H\tprefix output lines with filename where match was found\n" \
|
||||||
"\t-h\tsuppress the prefixing filename on output\n" \
|
"\t-h\tsuppress the prefixing filename on output\n" \
|
||||||
"\t-i\tignore case distinctions\n" \
|
"\t-i\tignore case distinctions\n" \
|
||||||
|
"\t-l\tlist names of files that match\n" \
|
||||||
"\t-n\tprint line number with output lines\n" \
|
"\t-n\tprint line number with output lines\n" \
|
||||||
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
||||||
"\t-v\tselect non-matching lines\n" \
|
"\t-v\tselect non-matching lines\n" \
|
||||||
|
@ -41,6 +41,7 @@ static int print_count_only = 0;
|
|||||||
static int be_quiet = 0;
|
static int be_quiet = 0;
|
||||||
static int invert_search = 0;
|
static int invert_search = 0;
|
||||||
static int suppress_err_msgs = 0;
|
static int suppress_err_msgs = 0;
|
||||||
|
static int files_that_match = 0;
|
||||||
|
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
extern char *optarg; /* in getopt.h */
|
extern char *optarg; /* in getopt.h */
|
||||||
@ -105,7 +106,7 @@ static void grep_file(FILE *file)
|
|||||||
|
|
||||||
/* otherwise, keep track of matches and print the matched line */
|
/* otherwise, keep track of matches and print the matched line */
|
||||||
nmatches++;
|
nmatches++;
|
||||||
if (!print_count_only) {
|
if (print_count_only==0 && files_that_match==0) {
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
|
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
|
||||||
|
|
||||||
@ -163,7 +164,14 @@ static void grep_file(FILE *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* special-case post processing */
|
/* special-case post processing */
|
||||||
if (print_count_only) {
|
if (files_that_match) {
|
||||||
|
if (nmatches > 0) {
|
||||||
|
printf("%s", cur_file);
|
||||||
|
if (nmatches)
|
||||||
|
printf(":%d", nmatches);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
} else if (print_count_only) {
|
||||||
if (print_filename)
|
if (print_filename)
|
||||||
printf("%s:", cur_file);
|
printf("%s:", cur_file);
|
||||||
printf("%i\n", nmatches);
|
printf("%i\n", nmatches);
|
||||||
@ -183,7 +191,7 @@ extern int grep_main(int argc, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* do normal option parsing */
|
/* do normal option parsing */
|
||||||
while ((opt = getopt(argc, argv, "iHhnqvsc"
|
while ((opt = getopt(argc, argv, "iHhlnqvsc"
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
"A:B:C:"
|
"A:B:C:"
|
||||||
#endif
|
#endif
|
||||||
@ -192,6 +200,9 @@ extern int grep_main(int argc, char **argv)
|
|||||||
case 'i':
|
case 'i':
|
||||||
ignore_case++;
|
ignore_case++;
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
files_that_match++;
|
||||||
|
break;
|
||||||
case 'H':
|
case 'H':
|
||||||
print_filename++;
|
print_filename++;
|
||||||
break;
|
break;
|
||||||
@ -242,7 +253,7 @@ extern int grep_main(int argc, char **argv)
|
|||||||
show_usage();
|
show_usage();
|
||||||
|
|
||||||
/* sanity check */
|
/* sanity check */
|
||||||
if (print_count_only || be_quiet) {
|
if (print_count_only || be_quiet || files_that_match) {
|
||||||
print_line_num = 0;
|
print_line_num = 0;
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
lines_before = 0;
|
lines_before = 0;
|
||||||
|
19
grep.c
19
grep.c
@ -41,6 +41,7 @@ static int print_count_only = 0;
|
|||||||
static int be_quiet = 0;
|
static int be_quiet = 0;
|
||||||
static int invert_search = 0;
|
static int invert_search = 0;
|
||||||
static int suppress_err_msgs = 0;
|
static int suppress_err_msgs = 0;
|
||||||
|
static int files_that_match = 0;
|
||||||
|
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
extern char *optarg; /* in getopt.h */
|
extern char *optarg; /* in getopt.h */
|
||||||
@ -105,7 +106,7 @@ static void grep_file(FILE *file)
|
|||||||
|
|
||||||
/* otherwise, keep track of matches and print the matched line */
|
/* otherwise, keep track of matches and print the matched line */
|
||||||
nmatches++;
|
nmatches++;
|
||||||
if (!print_count_only) {
|
if (print_count_only==0 && files_that_match==0) {
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
|
int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
|
||||||
|
|
||||||
@ -163,7 +164,14 @@ static void grep_file(FILE *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* special-case post processing */
|
/* special-case post processing */
|
||||||
if (print_count_only) {
|
if (files_that_match) {
|
||||||
|
if (nmatches > 0) {
|
||||||
|
printf("%s", cur_file);
|
||||||
|
if (nmatches)
|
||||||
|
printf(":%d", nmatches);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
} else if (print_count_only) {
|
||||||
if (print_filename)
|
if (print_filename)
|
||||||
printf("%s:", cur_file);
|
printf("%s:", cur_file);
|
||||||
printf("%i\n", nmatches);
|
printf("%i\n", nmatches);
|
||||||
@ -183,7 +191,7 @@ extern int grep_main(int argc, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* do normal option parsing */
|
/* do normal option parsing */
|
||||||
while ((opt = getopt(argc, argv, "iHhnqvsc"
|
while ((opt = getopt(argc, argv, "iHhlnqvsc"
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
"A:B:C:"
|
"A:B:C:"
|
||||||
#endif
|
#endif
|
||||||
@ -192,6 +200,9 @@ extern int grep_main(int argc, char **argv)
|
|||||||
case 'i':
|
case 'i':
|
||||||
ignore_case++;
|
ignore_case++;
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
files_that_match++;
|
||||||
|
break;
|
||||||
case 'H':
|
case 'H':
|
||||||
print_filename++;
|
print_filename++;
|
||||||
break;
|
break;
|
||||||
@ -242,7 +253,7 @@ extern int grep_main(int argc, char **argv)
|
|||||||
show_usage();
|
show_usage();
|
||||||
|
|
||||||
/* sanity check */
|
/* sanity check */
|
||||||
if (print_count_only || be_quiet) {
|
if (print_count_only || be_quiet || files_that_match) {
|
||||||
print_line_num = 0;
|
print_line_num = 0;
|
||||||
#ifdef BB_FEATURE_GREP_CONTEXT
|
#ifdef BB_FEATURE_GREP_CONTEXT
|
||||||
lines_before = 0;
|
lines_before = 0;
|
||||||
|
@ -522,6 +522,7 @@
|
|||||||
"\t-H\tprefix output lines with filename where match was found\n" \
|
"\t-H\tprefix output lines with filename where match was found\n" \
|
||||||
"\t-h\tsuppress the prefixing filename on output\n" \
|
"\t-h\tsuppress the prefixing filename on output\n" \
|
||||||
"\t-i\tignore case distinctions\n" \
|
"\t-i\tignore case distinctions\n" \
|
||||||
|
"\t-l\tlist names of files that match\n" \
|
||||||
"\t-n\tprint line number with output lines\n" \
|
"\t-n\tprint line number with output lines\n" \
|
||||||
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
||||||
"\t-v\tselect non-matching lines\n" \
|
"\t-v\tselect non-matching lines\n" \
|
||||||
|
1
usage.h
1
usage.h
@ -522,6 +522,7 @@
|
|||||||
"\t-H\tprefix output lines with filename where match was found\n" \
|
"\t-H\tprefix output lines with filename where match was found\n" \
|
||||||
"\t-h\tsuppress the prefixing filename on output\n" \
|
"\t-h\tsuppress the prefixing filename on output\n" \
|
||||||
"\t-i\tignore case distinctions\n" \
|
"\t-i\tignore case distinctions\n" \
|
||||||
|
"\t-l\tlist names of files that match\n" \
|
||||||
"\t-n\tprint line number with output lines\n" \
|
"\t-n\tprint line number with output lines\n" \
|
||||||
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n" \
|
||||||
"\t-v\tselect non-matching lines\n" \
|
"\t-v\tselect non-matching lines\n" \
|
||||||
|
Loading…
Reference in New Issue
Block a user