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";
34 static BOOL search (const char *string, const char *word, BOOL ignoreCase);
37 extern int grep_main (int argc, char **argv)
55 fprintf (stderr, "%s", grep_usage);
74 fprintf (stderr, "Unknown option\n");
82 tellName = (argc > 1);
87 fp = fopen (name, "r");
97 while (fgets (buf, sizeof (buf), fp)) {
100 cp = &buf[strlen (buf) - 1];
103 fprintf (stderr, "%s: Line too long\n", name);
105 if (search (buf, word, ignoreCase)) {
107 printf ("%s: ", name);
110 printf ("%ld: ", line);
126 * See if the specified word is found in the specified string.
128 static BOOL search (const char *string, const char *word, BOOL ignoreCase)
141 string = strchr (string, word[0]);
146 if (memcmp (string, word, len) == 0)
154 * Here if we need to check case independence.
155 * Do the search by lower casing both strings.
159 if (isupper (lowFirst))
160 lowFirst = tolower (lowFirst);
163 while (*string && (*string != lowFirst) &&
164 (!isupper (*string) || (tolower (*string) != lowFirst))) {