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