84bb99667373720d1fcbb0108a89988d691e2ca7
[oweals/busybox.git] / 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 "regexp.h"
26 #include <stdio.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <time.h>
32 #include <ctype.h>
33
34 static const char grep_usage[] =
35 "grep [OPTIONS]... PATTERN [FILE]...\n\n"
36 "Search for PATTERN in each FILE or standard input.\n\n"
37 "OPTIONS:\n"
38 "\t-h\tsuppress the prefixing filename on output\n"
39 "\t-i\tignore case distinctions\n"
40 "\t-n\tprint line number with output lines\n\n"
41 #if defined BB_REGEXP
42 "This version of grep matches full regular expresions.\n";
43 #else
44 "This version of grep matches strings (not regular expresions).\n";
45 #endif
46
47
48 static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine)
49 {
50     char *cp;
51     long line = 0;
52     char haystack[BUF_SIZE];
53
54     while (fgets (haystack, sizeof (haystack), fp)) {
55         line++;
56         cp = &haystack[strlen (haystack) - 1];
57
58         if (*cp != '\n')
59             fprintf (stderr, "%s: Line too long\n", fileName);
60
61         if (find_match(haystack, needle, ignoreCase) == TRUE) {
62             if (tellName==TRUE)
63                 printf ("%s:", fileName);
64
65             if (tellLine==TRUE)
66                 printf ("%ld:", line);
67
68             fputs (haystack, stdout);
69         }
70     }
71 }
72
73
74 extern int grep_main (int argc, char **argv)
75 {
76     FILE *fp;
77     char *cp;
78     char *needle;
79     char *fileName;
80     int tellName=FALSE;
81     int ignoreCase=FALSE;
82     int tellLine=FALSE;
83
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 = TRUE;
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     if (argc==0) {
121         do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
122     } else {
123         while (argc-- > 0) {
124             fileName = *argv++;
125
126             fp = fopen (fileName, "r");
127             if (fp == NULL) {
128                 perror (fileName);
129                 continue;
130             }
131
132             do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine);
133
134             if (ferror (fp))
135                 perror (fileName);
136             fclose (fp);
137         }
138     }
139     exit( TRUE);
140 }
141
142
143 /* END CODE */
144
145