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>
24 const char grep_usage[] =
25 "Search the input file(s) for lines matching the given pattern.\n"
26 "\tI search stdin if no files are given.\n"
27 "\tI can't grok full regular expressions.\n"
28 "usage: grep [in] PATTERN [FILES]...\n"
29 "\ti=ignore case, n=list line numbers\n";
34 * See if the specified word is found in the specified string.
36 static int search (const char *string, const char *word, int ignoreCase)
49 string = strchr (string, word[0]);
54 if (memcmp (string, word, len) == 0)
62 * Here if we need to check case independence.
63 * Do the search by lower casing both strings.
67 if (isupper (lowFirst))
68 lowFirst = tolower (lowFirst);
71 while (*string && (*string != lowFirst) &&
72 (!isupper (*string) || (tolower (*string) != lowFirst))) {
105 extern int grep_main (int argc, char **argv)
123 fprintf (stderr, "%s", grep_usage);
142 fprintf (stderr, "Unknown option\n");
150 tellName = (argc > 1);
155 fp = fopen (name, "r");
165 while (fgets (buf, sizeof (buf), fp)) {
168 cp = &buf[strlen (buf) - 1];
171 fprintf (stderr, "%s: Line too long\n", name);
173 if (search (buf, word, ignoreCase)) {
175 printf ("%s: ", name);
178 printf ("%ld: ", line);