diff options
author | Eric Andersen | 1999-10-07 08:30:23 +0000 |
---|---|---|
committer | Eric Andersen | 1999-10-07 08:30:23 +0000 |
commit | 596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe (patch) | |
tree | 4d48d109e66c5197bb27c8215062aab28d385509 /findutils | |
parent | 5c3199e0a519695c367b773e179b5458670f452b (diff) | |
download | busybox-596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe.zip busybox-596e5469d00fa4a74d8a3b1ebfaae20ce8dc3afe.tar.gz |
more stuff
Diffstat (limited to 'findutils')
-rw-r--r-- | findutils/grep.c | 251 |
1 files changed, 119 insertions, 132 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index a07feda..11198b8 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -23,187 +23,174 @@ const char grep_usage[] = -"Search the input file(s) for lines matching the given pattern.\n" -"\tI search stdin if no files are given.\n" -"\tI can't grok full regular expressions.\n" -"usage: grep [in] PATTERN [FILES]...\n" -"\ti=ignore case, n=list line numbers\n"; + "Search the input file(s) for lines matching the given pattern.\n" + "\tI search stdin if no files are given.\n" + "\tI can't grok full regular expressions.\n" + "usage: grep [in] PATTERN [FILES]...\n" + "\ti=ignore case, n=list line numbers\n"; -static BOOL search - (const char * string, const char * word, BOOL ignoreCase); +static BOOL search (const char *string, const char *word, BOOL ignoreCase); -extern int -grep_main(int argc, char ** argv) +extern int grep_main (int argc, char **argv) { - FILE * fp; - const char * word; - const char * name; - const char * cp; - BOOL tellName; - BOOL ignoreCase; - BOOL tellLine; - long line; - char buf[BUF_SIZE]; - - ignoreCase = FALSE; - tellLine = FALSE; - + FILE *fp; + const char *word; + const char *name; + const char *cp; + BOOL tellName; + BOOL ignoreCase; + BOOL tellLine; + long line; + char buf[BUF_SIZE]; + + ignoreCase = FALSE; + tellLine = FALSE; + + argc--; + argv++; + if (argc < 1) { + fprintf (stderr, "%s", grep_usage); + return 1; + } + + if (**argv == '-') { argc--; - argv++; - if (argc < 1) - { - fprintf(stderr, "%s", grep_usage); - return 1; - } + cp = *argv++; - if (**argv == '-') - { - argc--; - cp = *argv++; - - while (*++cp) switch (*cp) - { - case 'i': - ignoreCase = TRUE; - break; - - case 'n': - tellLine = TRUE; - break; - - default: - fprintf(stderr, "Unknown option\n"); - return 1; - } - } + while (*++cp) + switch (*cp) { + case 'i': + ignoreCase = TRUE; + break; - word = *argv++; - argc--; + case 'n': + tellLine = TRUE; + break; - tellName = (argc > 1); + default: + fprintf (stderr, "Unknown option\n"); + return 1; + } + } - while (argc-- > 0) - { - name = *argv++; + word = *argv++; + argc--; - fp = fopen(name, "r"); + tellName = (argc > 1); - if (fp == NULL) - { - perror(name); + while (argc-- > 0) { + name = *argv++; - continue; - } + fp = fopen (name, "r"); - line = 0; + if (fp == NULL) { + perror (name); - while (fgets(buf, sizeof(buf), fp)) - { - line++; + continue; + } - cp = &buf[strlen(buf) - 1]; + line = 0; - if (*cp != '\n') - fprintf(stderr, "%s: Line too long\n", name); + while (fgets (buf, sizeof (buf), fp)) { + line++; - if (search(buf, word, ignoreCase)) - { - if (tellName) - printf("%s: ", name); + cp = &buf[strlen (buf) - 1]; - if (tellLine) - printf("%ld: ", line); + if (*cp != '\n') + fprintf (stderr, "%s: Line too long\n", name); - fputs(buf, stdout); - } - } + if (search (buf, word, ignoreCase)) { + if (tellName) + printf ("%s: ", name); - if (ferror(fp)) - perror(name); + if (tellLine) + printf ("%ld: ", line); - fclose(fp); + fputs (buf, stdout); + } } - return 0; + + if (ferror (fp)) + perror (name); + + fclose (fp); + } + return 0; } /* * See if the specified word is found in the specified string. */ -static BOOL -search(const char * string, const char * word, BOOL ignoreCase) +static BOOL search (const char *string, const char *word, BOOL ignoreCase) { - const char * cp1; - const char * cp2; - int len; - int lowFirst; - int ch1; - int ch2; + const char *cp1; + const char *cp2; + int len; + int lowFirst; + int ch1; + int ch2; - len = strlen(word); + len = strlen (word); - if (!ignoreCase) - { - while (TRUE) - { - string = strchr(string, word[0]); + if (!ignoreCase) { + while (TRUE) { + string = strchr (string, word[0]); - if (string == NULL) - return FALSE; + if (string == NULL) + return FALSE; - if (memcmp(string, word, len) == 0) - return TRUE; + if (memcmp (string, word, len) == 0) + return TRUE; - string++; - } + string++; } + } - /* - * Here if we need to check case independence. - * Do the search by lower casing both strings. - */ - lowFirst = *word; + /* + * Here if we need to check case independence. + * Do the search by lower casing both strings. + */ + lowFirst = *word; - if (isupper(lowFirst)) - lowFirst = tolower(lowFirst); + if (isupper (lowFirst)) + lowFirst = tolower (lowFirst); - while (TRUE) - { - while (*string && (*string != lowFirst) && - (!isupper(*string) || (tolower(*string) != lowFirst))) - { - string++; - } - - if (*string == '\0') - return FALSE; + while (TRUE) { + while (*string && (*string != lowFirst) && + (!isupper (*string) || (tolower (*string) != lowFirst))) { + string++; + } - cp1 = string; - cp2 = word; + if (*string == '\0') + return FALSE; - do - { - if (*cp2 == '\0') - return TRUE; + cp1 = string; + cp2 = word; - ch1 = *cp1++; + do { + if (*cp2 == '\0') + return TRUE; - if (isupper(ch1)) - ch1 = tolower(ch1); + ch1 = *cp1++; - ch2 = *cp2++; + if (isupper (ch1)) + ch1 = tolower (ch1); - if (isupper(ch2)) - ch2 = tolower(ch2); + ch2 = *cp2++; - } - while (ch1 == ch2); + if (isupper (ch2)) + ch2 = tolower (ch2); - string++; } + while (ch1 == ch2); + + string++; + } + return (TRUE); } #endif |