blob: 108c879afd07bf5adb7475c13d0d63aecd628152 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Eric Andersen3e0fbae1999-10-19 06:02:44 +00002 * Mini grep implementation for busybox
Eric Andersencc8ed391999-10-05 16:24:54 +00003 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00004 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00005 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00006 * 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 *
Eric Andersencc8ed391999-10-05 16:24:54 +000020 */
21
22#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000023#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
Eric Andersene77ae3a1999-10-19 20:03:34 +000032static const char grep_usage[] =
Eric Andersen3e0fbae1999-10-19 06:02:44 +000033"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";
Eric Andersencc8ed391999-10-05 16:24:54 +000039
40
Eric Andersen3cf52d11999-10-12 22:26:06 +000041/*
Eric Andersen3e0fbae1999-10-19 06:02:44 +000042 * See if the specified needle is found in the specified haystack.
Eric Andersen3cf52d11999-10-12 22:26:06 +000043 */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044static int search (const char *haystack, const char *needle, int ignoreCase)
Eric Andersen3cf52d11999-10-12 22:26:06 +000045{
Eric Andersen3cf52d11999-10-12 22:26:06 +000046
Eric Andersen3e0fbae1999-10-19 06:02:44 +000047 if (ignoreCase == FALSE) {
48 haystack = strstr (haystack, needle);
49 if (haystack == NULL)
Eric Andersen3cf52d11999-10-12 22:26:06 +000050 return FALSE;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000051 return TRUE;
52 } else {
53 int i;
54 char needle1[BUF_SIZE];
55 char haystack1[BUF_SIZE];
Eric Andersen3cf52d11999-10-12 22:26:06 +000056
Eric Andersen3e0fbae1999-10-19 06:02:44 +000057 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;
Eric Andersen3cf52d11999-10-12 22:26:06 +000067 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000068}
Eric Andersencc8ed391999-10-05 16:24:54 +000069
70
Eric Andersen596e5461999-10-07 08:30:23 +000071extern int grep_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000072{
Eric Andersen596e5461999-10-07 08:30:23 +000073 FILE *fp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000074 const char *needle;
Eric Andersen596e5461999-10-07 08:30:23 +000075 const char *name;
76 const char *cp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000077 int tellName=TRUE;
78 int ignoreCase=FALSE;
79 int tellLine=FALSE;
Eric Andersen596e5461999-10-07 08:30:23 +000080 long line;
81 char buf[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +000082
Eric Andersen596e5461999-10-07 08:30:23 +000083 ignoreCase = FALSE;
84 tellLine = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000085
Eric Andersen596e5461999-10-07 08:30:23 +000086 argc--;
87 argv++;
88 if (argc < 1) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +000089 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000090 }
91
92 if (**argv == '-') {
Eric Andersencc8ed391999-10-05 16:24:54 +000093 argc--;
Eric Andersen596e5461999-10-07 08:30:23 +000094 cp = *argv++;
95
96 while (*++cp)
97 switch (*cp) {
98 case 'i':
99 ignoreCase = TRUE;
100 break;
101
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000102 case 'h':
103 tellName = FALSE;
104 break;
105
Eric Andersen596e5461999-10-07 08:30:23 +0000106 case 'n':
107 tellLine = TRUE;
108 break;
109
110 default:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000111 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +0000112 }
113 }
114
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000115 needle = *argv++;
Eric Andersen596e5461999-10-07 08:30:23 +0000116 argc--;
117
Eric Andersen596e5461999-10-07 08:30:23 +0000118 while (argc-- > 0) {
119 name = *argv++;
120
121 fp = fopen (name, "r");
Eric Andersen596e5461999-10-07 08:30:23 +0000122 if (fp == NULL) {
123 perror (name);
Eric Andersen596e5461999-10-07 08:30:23 +0000124 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000125 }
126
Eric Andersen596e5461999-10-07 08:30:23 +0000127 line = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000128
Eric Andersen596e5461999-10-07 08:30:23 +0000129 while (fgets (buf, sizeof (buf), fp)) {
130 line++;
Eric Andersen596e5461999-10-07 08:30:23 +0000131 cp = &buf[strlen (buf) - 1];
Eric Andersencc8ed391999-10-05 16:24:54 +0000132
Eric Andersen596e5461999-10-07 08:30:23 +0000133 if (*cp != '\n')
134 fprintf (stderr, "%s: Line too long\n", name);
135
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000136 if (search (buf, needle, ignoreCase)==TRUE) {
137 if (tellName==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000138 printf ("%s: ", name);
139
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000140 if (tellLine==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000141 printf ("%ld: ", line);
142
143 fputs (buf, stdout);
144 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000145 }
146
Eric Andersen596e5461999-10-07 08:30:23 +0000147 if (ferror (fp))
148 perror (name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000149
Eric Andersen596e5461999-10-07 08:30:23 +0000150 fclose (fp);
151 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000152 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000153}
154
155
Eric Andersencc8ed391999-10-05 16:24:54 +0000156/* END CODE */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000157
158