aboutsummaryrefslogtreecommitdiff
path: root/findutils/grep.c
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>2000-04-24 18:07:30 +0000
committerJohn Beppu <beppu@lbox.org>2000-04-24 18:07:30 +0000
commitf93a95de6960b1d1977a934aafd3e9a8b8ea1765 (patch)
treed4e4a0cf3135af1926677c8f866b2bbab5a867f3 /findutils/grep.c
parente90f4045afbcdcae81c417fffa635b3a5ab9166b (diff)
downloadbusybox-w32-f93a95de6960b1d1977a934aafd3e9a8b8ea1765.tar.gz
busybox-w32-f93a95de6960b1d1977a934aafd3e9a8b8ea1765.tar.bz2
busybox-w32-f93a95de6960b1d1977a934aafd3e9a8b8ea1765.zip
+ grep -v # yay!
Diffstat (limited to 'findutils/grep.c')
-rw-r--r--findutils/grep.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index abdd44236..06b6980bc 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -47,7 +47,8 @@ static const char grep_usage[] =
47 "\t-h\tsuppress the prefixing filename on output\n" 47 "\t-h\tsuppress the prefixing filename on output\n"
48 "\t-i\tignore case distinctions\n" 48 "\t-i\tignore case distinctions\n"
49 "\t-n\tprint line number with output lines\n" 49 "\t-n\tprint line number with output lines\n"
50 "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" 50 "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n"
51 "\t-v\tselect non-matching lines\n\n"
51#if defined BB_REGEXP 52#if defined BB_REGEXP
52 "This version of grep matches full regular expresions.\n"; 53 "This version of grep matches full regular expresions.\n";
53#else 54#else
@@ -57,11 +58,12 @@ static const char grep_usage[] =
57static int match = FALSE, beQuiet = FALSE; 58static int match = FALSE, beQuiet = FALSE;
58 59
59static void do_grep(FILE * fp, char *needle, char *fileName, int tellName, 60static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
60 int ignoreCase, int tellLine) 61 int ignoreCase, int tellLine, int invertSearch)
61{ 62{
62 char *cp; 63 char *cp;
63 long line = 0; 64 long line = 0;
64 char haystack[BUF_SIZE]; 65 char haystack[BUF_SIZE];
66 int truth = !invertSearch;
65 67
66 while (fgets(haystack, sizeof(haystack), fp)) { 68 while (fgets(haystack, sizeof(haystack), fp)) {
67 line++; 69 line++;
@@ -70,7 +72,7 @@ static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
70 if (*cp != '\n') 72 if (*cp != '\n')
71 fprintf(stderr, "%s: Line too long\n", fileName); 73 fprintf(stderr, "%s: Line too long\n", fileName);
72 74
73 if (find_match(haystack, needle, ignoreCase) == TRUE) { 75 if (find_match(haystack, needle, ignoreCase) == truth) {
74 if (tellName == TRUE) 76 if (tellName == TRUE)
75 printf("%s:", fileName); 77 printf("%s:", fileName);
76 78
@@ -92,13 +94,10 @@ extern int grep_main(int argc, char **argv)
92 char *cp; 94 char *cp;
93 char *needle; 95 char *needle;
94 char *fileName; 96 char *fileName;
95 int tellName = TRUE; 97 int tellName = TRUE;
96 int ignoreCase = TRUE; 98 int ignoreCase = FALSE;
97 int tellLine = FALSE; 99 int tellLine = FALSE;
98 100 int invertSearch = FALSE;
99
100 ignoreCase = FALSE;
101 tellLine = FALSE;
102 101
103 argc--; 102 argc--;
104 argv++; 103 argv++;
@@ -128,6 +127,10 @@ extern int grep_main(int argc, char **argv)
128 beQuiet = TRUE; 127 beQuiet = TRUE;
129 break; 128 break;
130 129
130 case 'v':
131 invertSearch = TRUE;
132 break;
133
131 default: 134 default:
132 usage(grep_usage); 135 usage(grep_usage);
133 } 136 }
@@ -137,7 +140,7 @@ extern int grep_main(int argc, char **argv)
137 argc--; 140 argc--;
138 141
139 if (argc == 0) { 142 if (argc == 0) {
140 do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine); 143 do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine, invertSearch);
141 } else { 144 } else {
142 /* Never print the filename for just one file */ 145 /* Never print the filename for just one file */
143 if (argc == 1) 146 if (argc == 1)
@@ -151,7 +154,7 @@ extern int grep_main(int argc, char **argv)
151 continue; 154 continue;
152 } 155 }
153 156
154 do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine); 157 do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine, invertSearch);
155 158
156 if (ferror(fp)) 159 if (ferror(fp))
157 perror(fileName); 160 perror(fileName);