diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
commit | 50d6360771be509737bb55b2cc5bc5e25f2a4fea (patch) | |
tree | 81d4cfe9ec9b5281924f678c28f61542616a3db7 /findutils | |
parent | fbb39c83b69d6c4de943c0b7374000339635d13d (diff) |
Stuff
Diffstat (limited to 'findutils')
-rw-r--r-- | findutils/grep.c | 79 |
1 files changed, 38 insertions, 41 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 50a296178..8dcff0586 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -40,45 +40,46 @@ static const char grep_usage[] = #if defined BB_REGEXP "This version of grep matches full regexps.\n"; #else -"This version of grep matches strings (not full regexps).\n"; +"This version of grep matches strings (not regexps).\n"; #endif -int tellName=TRUE; -int ignoreCase=FALSE; -int tellLine=FALSE; -static do_grep(char* needle, char* haystack ) +static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine) { - line = 0; + char *cp; + long line = 0; + char haystack[BUF_SIZE]; - while (fgets (haystack, sizeof (haystack), fp)) { - line++; - cp = &haystack[strlen (haystack) - 1]; + while (fgets (haystack, sizeof (haystack), fp)) { + line++; + cp = &haystack[strlen (haystack) - 1]; - if (*cp != '\n') - fprintf (stderr, "%s: Line too long\n", name); + if (*cp != '\n') + fprintf (stderr, "%s: Line too long\n", fileName); - if (find_match(haystack, needle, ignoreCase) == TRUE) { - if (tellName==TRUE) - printf ("%s: ", name); + if (find_match(haystack, needle, ignoreCase) == TRUE) { + if (tellName==TRUE) + printf ("%s:", fileName); - if (tellLine==TRUE) - printf ("%ld: ", line); + if (tellLine==TRUE) + printf ("%ld:", line); - fputs (haystack, stdout); - } + fputs (haystack, stdout); } + } } extern int grep_main (int argc, char **argv) { FILE *fp; - char *needle; - char *name; char *cp; - long line; - char haystack[BUF_SIZE]; + char *needle; + char *fileName; + int tellName=FALSE; + int ignoreCase=FALSE; + int tellLine=FALSE; + ignoreCase = FALSE; tellLine = FALSE; @@ -100,7 +101,7 @@ extern int grep_main (int argc, char **argv) break; case 'h': - tellName = FALSE; + tellName = TRUE; break; case 'n': @@ -115,28 +116,24 @@ extern int grep_main (int argc, char **argv) needle = *argv++; argc--; + if (argc==0) { + do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine); + } else { + while (argc-- > 0) { + fileName = *argv++; - while (argc-- > 0) { - - if (argc==0) { - file = stdin; - } - else - file = fopen(*argv, "r"); - + fp = fopen (fileName, "r"); + if (fp == NULL) { + perror (fileName); + continue; + } - name = *argv++; + do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine); - fp = fopen (name, "r"); - if (fp == NULL) { - perror (name); - continue; + if (ferror (fp)) + perror (fileName); + fclose (fp); } - - if (ferror (fp)) - perror (name); - - fclose (fp); } exit( TRUE); } |