2 * Mini grep implementation for busybox
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 static const char grep_usage[] =
33 "grep [-ihn]... PATTERN [FILE]...\n"
34 "Search for PATTERN in each FILE or standard input.\n\n"
35 "\t-h\tsuppress the prefixing filename on output\n"
36 "\t-i\tignore case distinctions\n"
37 "\t-n\tprint line number with output lines\n\n"
38 "This version of grep matches strings (not full regexps).\n";
42 * See if the specified needle is found in the specified haystack.
44 static int search (const char *haystack, const char *needle, int ignoreCase)
47 if (ignoreCase == FALSE) {
48 haystack = strstr (haystack, needle);
54 char needle1[BUF_SIZE];
55 char haystack1[BUF_SIZE];
57 strncpy( haystack1, haystack, sizeof(haystack1));
58 strncpy( needle1, needle, sizeof(needle1));
59 for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
60 haystack1[i]=tolower( haystack1[i]);
61 for( i=0; i<sizeof(needle1) && needle1[i]; i++)
62 needle1[i]=tolower( needle1[i]);
63 haystack = strstr (haystack1, needle1);
71 extern int grep_main (int argc, char **argv)
121 fp = fopen (name, "r");
129 while (fgets (buf, sizeof (buf), fp)) {
131 cp = &buf[strlen (buf) - 1];
134 fprintf (stderr, "%s: Line too long\n", name);
136 if (search (buf, needle, ignoreCase)==TRUE) {
138 printf ("%s: ", name);
141 printf ("%ld: ", line);