2 * Copyright (c) 1999 by David I. Bell
3 * Permission is granted to use, distribute, or modify this source,
4 * provided that this copyright notice remains intact.
6 * The "grep" command, taken from sash.
7 * This provides basic file searching.
9 * Permission to distribute this code under the GPL has been granted.
10 * Modified for busybox by Erik Andersen <andersee@debian.org> <andersen@lineo.com>
25 const char grep_usage[] =
26 "Search the input file(s) for lines matching the given pattern.\n"
27 "\tI search stdin if no files are given.\n"
28 "\tI can't grok full regular expressions.\n"
29 "usage: grep [in] PATTERN [FILES]...\n"
30 "\ti=ignore case, n=list line numbers\n";
35 (const char * string, const char * word, BOOL ignoreCase);
39 grep_main(int argc, char ** argv)
58 fprintf(stderr, "%s", grep_usage);
67 while (*++cp) switch (*cp)
78 fprintf(stderr, "Unknown option\n");
86 tellName = (argc > 1);
92 fp = fopen(name, "r");
103 while (fgets(buf, sizeof(buf), fp))
107 cp = &buf[strlen(buf) - 1];
110 fprintf(stderr, "%s: Line too long\n", name);
112 if (search(buf, word, ignoreCase))
115 printf("%s: ", name);
118 printf("%ld: ", line);
134 * See if the specified word is found in the specified string.
137 search(const char * string, const char * word, BOOL ignoreCase)
152 string = strchr(string, word[0]);
157 if (memcmp(string, word, len) == 0)
165 * Here if we need to check case independence.
166 * Do the search by lower casing both strings.
170 if (isupper(lowFirst))
171 lowFirst = tolower(lowFirst);
175 while (*string && (*string != lowFirst) &&
176 (!isupper(*string) || (tolower(*string) != lowFirst)))