diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-07 05:19:31 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-07 05:19:31 +0000 |
commit | a05c0716c256a95861c01d06b5319f3149fca33a (patch) | |
tree | 0d877b4070cd8228e19146365a7888b7305bc6ff | |
parent | 401de648a797c5931df1ade02c26270c82c3a345 (diff) | |
download | busybox-w32-a05c0716c256a95861c01d06b5319f3149fca33a.tar.gz busybox-w32-a05c0716c256a95861c01d06b5319f3149fca33a.tar.bz2 busybox-w32-a05c0716c256a95861c01d06b5319f3149fca33a.zip |
grep: make "-f -" work (+ testsuite)
diff: small code shrink
function old new delta
grep_main 722 714 -8
diffreg 1825 1793 -32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-40) Total: -40 bytes
-rw-r--r-- | editors/diff.c | 5 | ||||
-rw-r--r-- | findutils/grep.c | 12 | ||||
-rwxr-xr-x | testsuite/grep.tests | 4 |
3 files changed, 13 insertions, 8 deletions
diff --git a/editors/diff.c b/editors/diff.c index eac4b08c5..4e51f6f76 100644 --- a/editors/diff.c +++ b/editors/diff.c | |||
@@ -1021,11 +1021,12 @@ static unsigned diffreg(char *file1, char *file2, int flags) | |||
1021 | rval = D_SAME; | 1021 | rval = D_SAME; |
1022 | 1022 | ||
1023 | if (flags & D_EMPTY1) | 1023 | if (flags & D_EMPTY1) |
1024 | f1 = xfopen(bb_dev_null, "r"); | 1024 | /* can't be stdin, but xfopen_stdin() is smaller code */ |
1025 | f1 = xfopen_stdin(bb_dev_null); | ||
1025 | else | 1026 | else |
1026 | f1 = xfopen_stdin(file1); | 1027 | f1 = xfopen_stdin(file1); |
1027 | if (flags & D_EMPTY2) | 1028 | if (flags & D_EMPTY2) |
1028 | f2 = xfopen(bb_dev_null, "r"); | 1029 | f2 = xfopen_stdin(bb_dev_null); |
1029 | else | 1030 | else |
1030 | f2 = xfopen_stdin(file2); | 1031 | f2 = xfopen_stdin(file2); |
1031 | 1032 | ||
diff --git a/findutils/grep.c b/findutils/grep.c index 6af1b46e1..fc7893860 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -126,7 +126,7 @@ struct globals { | |||
126 | typedef struct grep_list_data_t { | 126 | typedef struct grep_list_data_t { |
127 | char *pattern; | 127 | char *pattern; |
128 | regex_t preg; | 128 | regex_t preg; |
129 | #define PATTERN_MEM_A 1 | 129 | #define ALLOCATED 1 |
130 | #define COMPILED 2 | 130 | #define COMPILED 2 |
131 | int flg_mem_alocated_compiled; | 131 | int flg_mem_alocated_compiled; |
132 | } grep_list_data_t; | 132 | } grep_list_data_t; |
@@ -363,10 +363,10 @@ static void load_regexes_from_file(llist_t *fopt) | |||
363 | 363 | ||
364 | fopt = cur->link; | 364 | fopt = cur->link; |
365 | free(cur); | 365 | free(cur); |
366 | f = xfopen(ffile, "r"); | 366 | f = xfopen_stdin(ffile); |
367 | while ((line = xmalloc_fgetline(f)) != NULL) { | 367 | while ((line = xmalloc_fgetline(f)) != NULL) { |
368 | llist_add_to(&pattern_head, | 368 | llist_add_to(&pattern_head, |
369 | new_grep_list_data(line, PATTERN_MEM_A)); | 369 | new_grep_list_data(line, ALLOCATED)); |
370 | } | 370 | } |
371 | } | 371 | } |
372 | } | 372 | } |
@@ -486,7 +486,7 @@ int grep_main(int argc, char **argv) | |||
486 | argc--; | 486 | argc--; |
487 | } | 487 | } |
488 | 488 | ||
489 | /* argv[(optind)..(argc-1)] should be names of file to grep through. If | 489 | /* argv[0..(argc-1)] should be names of file to grep through. If |
490 | * there is more than one file to grep, we will print the filenames. */ | 490 | * there is more than one file to grep, we will print the filenames. */ |
491 | if (argc > 1) | 491 | if (argc > 1) |
492 | print_filename = 1; | 492 | print_filename = 1; |
@@ -535,9 +535,9 @@ int grep_main(int argc, char **argv) | |||
535 | grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data; | 535 | grep_list_data_t *gl = (grep_list_data_t *)pattern_head_ptr->data; |
536 | 536 | ||
537 | pattern_head = pattern_head->link; | 537 | pattern_head = pattern_head->link; |
538 | if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) | 538 | if (gl->flg_mem_alocated_compiled & ALLOCATED) |
539 | free(gl->pattern); | 539 | free(gl->pattern); |
540 | if ((gl->flg_mem_alocated_compiled & COMPILED)) | 540 | if (gl->flg_mem_alocated_compiled & COMPILED) |
541 | regfree(&(gl->preg)); | 541 | regfree(&(gl->preg)); |
542 | free(gl); | 542 | free(gl); |
543 | free(pattern_head_ptr); | 543 | free(pattern_head_ptr); |
diff --git a/testsuite/grep.tests b/testsuite/grep.tests index 34c0fe4f7..b2de2af54 100755 --- a/testsuite/grep.tests +++ b/testsuite/grep.tests | |||
@@ -78,6 +78,10 @@ testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \ | |||
78 | testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \ | 78 | testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \ |
79 | "one\ntwo\n0\n" "one\ntwo\n" "" | 79 | "one\ntwo\n0\n" "one\ntwo\n" "" |
80 | 80 | ||
81 | # -f file/- | ||
82 | testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \ | ||
83 | "two\nthree\n0\n" "tw\ntwo\nthree\n" "tw.\nthr\n" | ||
84 | |||
81 | optional FEATURE_GREP_EGREP_ALIAS | 85 | optional FEATURE_GREP_EGREP_ALIAS |
82 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ | 86 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ |
83 | "b\ar\nfoo\nbaz" | 87 | "b\ar\nfoo\nbaz" |