+ grep -v # yay!

This commit is contained in:
John Beppu 2000-04-24 18:07:30 +00:00
parent e90f4045af
commit f93a95de69
4 changed files with 36 additions and 25 deletions

View File

@ -1,3 +1,7 @@
0.44
* added the -v option (inverted search) to grep,
updated docs/busybox.pod accordingly. -beppu
0.43 0.43
* Major update to the provided documentation. * Major update to the provided documentation.
* Busybox now includes a shell! It currently costs 7.5 k (plus an * Busybox now includes a shell! It currently costs 7.5 k (plus an

View File

@ -542,6 +542,7 @@ OPTIONS:
-i ignore case distinctions -i ignore case distinctions
-n print line number with output lines -n print line number with output lines
-q be quiet. Returns 0 if result was found, 1 otherwise -q be quiet. Returns 0 if result was found, 1 otherwise
-v select non-matching lines
This version of grep matches full regular expresions. This version of grep matches full regular expresions.
@ -1816,4 +1817,4 @@ Enrique Zanardi <ezanardi@ull.es>
=cut =cut
# $Id: busybox.pod,v 1.21 2000/04/21 21:53:58 erik Exp $ # $Id: busybox.pod,v 1.22 2000/04/24 18:07:30 beppu Exp $

View File

@ -47,7 +47,8 @@ static const char grep_usage[] =
"\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-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\n" "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n"
"\t-v\tselect non-matching lines\n\n"
#if defined BB_REGEXP #if defined BB_REGEXP
"This version of grep matches full regular expresions.\n"; "This version of grep matches full regular expresions.\n";
#else #else
@ -57,11 +58,12 @@ static const char grep_usage[] =
static int match = FALSE, beQuiet = FALSE; static int match = FALSE, beQuiet = FALSE;
static void do_grep(FILE * fp, char *needle, char *fileName, int tellName, static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
int ignoreCase, int tellLine) int ignoreCase, int tellLine, int invertSearch)
{ {
char *cp; char *cp;
long line = 0; long line = 0;
char haystack[BUF_SIZE]; char haystack[BUF_SIZE];
int truth = !invertSearch;
while (fgets(haystack, sizeof(haystack), fp)) { while (fgets(haystack, sizeof(haystack), fp)) {
line++; line++;
@ -70,7 +72,7 @@ static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
if (*cp != '\n') if (*cp != '\n')
fprintf(stderr, "%s: Line too long\n", fileName); fprintf(stderr, "%s: Line too long\n", fileName);
if (find_match(haystack, needle, ignoreCase) == TRUE) { if (find_match(haystack, needle, ignoreCase) == truth) {
if (tellName == TRUE) if (tellName == TRUE)
printf("%s:", fileName); printf("%s:", fileName);
@ -92,13 +94,10 @@ extern int grep_main(int argc, char **argv)
char *cp; char *cp;
char *needle; char *needle;
char *fileName; char *fileName;
int tellName = TRUE; int tellName = TRUE;
int ignoreCase = TRUE; int ignoreCase = FALSE;
int tellLine = FALSE; int tellLine = FALSE;
int invertSearch = FALSE;
ignoreCase = FALSE;
tellLine = FALSE;
argc--; argc--;
argv++; argv++;
@ -128,6 +127,10 @@ extern int grep_main(int argc, char **argv)
beQuiet = TRUE; beQuiet = TRUE;
break; break;
case 'v':
invertSearch = TRUE;
break;
default: default:
usage(grep_usage); usage(grep_usage);
} }
@ -137,7 +140,7 @@ extern int grep_main(int argc, char **argv)
argc--; argc--;
if (argc == 0) { if (argc == 0) {
do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine); do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine, invertSearch);
} else { } else {
/* Never print the filename for just one file */ /* Never print the filename for just one file */
if (argc == 1) if (argc == 1)
@ -151,7 +154,7 @@ extern int grep_main(int argc, char **argv)
continue; continue;
} }
do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine); do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine, invertSearch);
if (ferror(fp)) if (ferror(fp))
perror(fileName); perror(fileName);

27
grep.c
View File

@ -47,7 +47,8 @@ static const char grep_usage[] =
"\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-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\n" "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n"
"\t-v\tselect non-matching lines\n\n"
#if defined BB_REGEXP #if defined BB_REGEXP
"This version of grep matches full regular expresions.\n"; "This version of grep matches full regular expresions.\n";
#else #else
@ -57,11 +58,12 @@ static const char grep_usage[] =
static int match = FALSE, beQuiet = FALSE; static int match = FALSE, beQuiet = FALSE;
static void do_grep(FILE * fp, char *needle, char *fileName, int tellName, static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
int ignoreCase, int tellLine) int ignoreCase, int tellLine, int invertSearch)
{ {
char *cp; char *cp;
long line = 0; long line = 0;
char haystack[BUF_SIZE]; char haystack[BUF_SIZE];
int truth = !invertSearch;
while (fgets(haystack, sizeof(haystack), fp)) { while (fgets(haystack, sizeof(haystack), fp)) {
line++; line++;
@ -70,7 +72,7 @@ static void do_grep(FILE * fp, char *needle, char *fileName, int tellName,
if (*cp != '\n') if (*cp != '\n')
fprintf(stderr, "%s: Line too long\n", fileName); fprintf(stderr, "%s: Line too long\n", fileName);
if (find_match(haystack, needle, ignoreCase) == TRUE) { if (find_match(haystack, needle, ignoreCase) == truth) {
if (tellName == TRUE) if (tellName == TRUE)
printf("%s:", fileName); printf("%s:", fileName);
@ -92,13 +94,10 @@ extern int grep_main(int argc, char **argv)
char *cp; char *cp;
char *needle; char *needle;
char *fileName; char *fileName;
int tellName = TRUE; int tellName = TRUE;
int ignoreCase = TRUE; int ignoreCase = FALSE;
int tellLine = FALSE; int tellLine = FALSE;
int invertSearch = FALSE;
ignoreCase = FALSE;
tellLine = FALSE;
argc--; argc--;
argv++; argv++;
@ -128,6 +127,10 @@ extern int grep_main(int argc, char **argv)
beQuiet = TRUE; beQuiet = TRUE;
break; break;
case 'v':
invertSearch = TRUE;
break;
default: default:
usage(grep_usage); usage(grep_usage);
} }
@ -137,7 +140,7 @@ extern int grep_main(int argc, char **argv)
argc--; argc--;
if (argc == 0) { if (argc == 0) {
do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine); do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine, invertSearch);
} else { } else {
/* Never print the filename for just one file */ /* Never print the filename for just one file */
if (argc == 1) if (argc == 1)
@ -151,7 +154,7 @@ extern int grep_main(int argc, char **argv)
continue; continue;
} }
do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine); do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine, invertSearch);
if (ferror(fp)) if (ferror(fp))
perror(fileName); perror(fileName);