a495c62ae9dfdb42cff7d65a6675e9872c91ee00
[oweals/busybox.git] / findutils / grep.c
1 /*
2  * Mini grep implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <time.h>
31 #include <ctype.h>
32
33
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";
41
42
43 /*
44  * See if the specified needle is found in the specified haystack.
45  */
46 static int search (const char *haystack, const char *needle, int ignoreCase)
47 {
48
49     if (ignoreCase == FALSE) {
50         haystack = strstr (haystack, needle);
51         if (haystack == NULL)
52             return FALSE;
53         return TRUE;
54     } else {
55         int i;
56         char needle1[BUF_SIZE];
57         char haystack1[BUF_SIZE];
58
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);
66         if (haystack == NULL)
67             return FALSE;
68         return TRUE;
69     }
70 }
71
72
73 extern int grep_main (int argc, char **argv)
74 {
75     FILE *fp;
76     const char *needle;
77     const char *name;
78     const char *cp;
79     int tellName=TRUE;
80     int ignoreCase=FALSE;
81     int tellLine=FALSE;
82     long line;
83     char buf[BUF_SIZE];
84
85     ignoreCase = FALSE;
86     tellLine = FALSE;
87
88     argc--;
89     argv++;
90     if (argc < 1) {
91         usage(grep_usage);
92     }
93
94     if (**argv == '-') {
95         argc--;
96         cp = *argv++;
97
98         while (*++cp)
99             switch (*cp) {
100             case 'i':
101                 ignoreCase = TRUE;
102                 break;
103
104             case 'h':
105                 tellName = FALSE;
106                 break;
107
108             case 'n':
109                 tellLine = TRUE;
110                 break;
111
112             default:
113                 usage(grep_usage);
114             }
115     }
116
117     needle = *argv++;
118     argc--;
119
120     while (argc-- > 0) {
121         name = *argv++;
122
123         fp = fopen (name, "r");
124         if (fp == NULL) {
125             perror (name);
126             continue;
127         }
128
129         line = 0;
130
131         while (fgets (buf, sizeof (buf), fp)) {
132             line++;
133             cp = &buf[strlen (buf) - 1];
134
135             if (*cp != '\n')
136                 fprintf (stderr, "%s: Line too long\n", name);
137
138             if (search (buf, needle, ignoreCase)==TRUE) {
139                 if (tellName==TRUE)
140                     printf ("%s: ", name);
141
142                 if (tellLine==TRUE)
143                     printf ("%ld: ", line);
144
145                 fputs (buf, stdout);
146             }
147         }
148
149         if (ferror (fp))
150             perror (name);
151
152         fclose (fp);
153     }
154     exit( TRUE);
155 }
156
157
158 /* END CODE */
159
160