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