blob: a495c62ae9dfdb42cff7d65a6675e9872c91ee00 [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 Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00007 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00008 * 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 *
Eric Andersencc8ed391999-10-05 16:24:54 +000022 */
23
24#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000025#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
Eric Andersene77ae3a1999-10-19 20:03:34 +000034static const char grep_usage[] =
Eric Andersen3e0fbae1999-10-19 06:02:44 +000035"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";
Eric Andersencc8ed391999-10-05 16:24:54 +000041
42
Eric Andersen3cf52d11999-10-12 22:26:06 +000043/*
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044 * See if the specified needle is found in the specified haystack.
Eric Andersen3cf52d11999-10-12 22:26:06 +000045 */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000046static int search (const char *haystack, const char *needle, int ignoreCase)
Eric Andersen3cf52d11999-10-12 22:26:06 +000047{
Eric Andersen3cf52d11999-10-12 22:26:06 +000048
Eric Andersen3e0fbae1999-10-19 06:02:44 +000049 if (ignoreCase == FALSE) {
50 haystack = strstr (haystack, needle);
51 if (haystack == NULL)
Eric Andersen3cf52d11999-10-12 22:26:06 +000052 return FALSE;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000053 return TRUE;
54 } else {
55 int i;
56 char needle1[BUF_SIZE];
57 char haystack1[BUF_SIZE];
Eric Andersen3cf52d11999-10-12 22:26:06 +000058
Eric Andersen3e0fbae1999-10-19 06:02:44 +000059 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;
Eric Andersen3cf52d11999-10-12 22:26:06 +000069 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000070}
Eric Andersencc8ed391999-10-05 16:24:54 +000071
72
Eric Andersen596e5461999-10-07 08:30:23 +000073extern int grep_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000074{
Eric Andersen596e5461999-10-07 08:30:23 +000075 FILE *fp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000076 const char *needle;
Eric Andersen596e5461999-10-07 08:30:23 +000077 const char *name;
78 const char *cp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000079 int tellName=TRUE;
80 int ignoreCase=FALSE;
81 int tellLine=FALSE;
Eric Andersen596e5461999-10-07 08:30:23 +000082 long line;
83 char buf[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Eric Andersen596e5461999-10-07 08:30:23 +000085 ignoreCase = FALSE;
86 tellLine = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Eric Andersen596e5461999-10-07 08:30:23 +000088 argc--;
89 argv++;
90 if (argc < 1) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +000091 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000092 }
93
94 if (**argv == '-') {
Eric Andersencc8ed391999-10-05 16:24:54 +000095 argc--;
Eric Andersen596e5461999-10-07 08:30:23 +000096 cp = *argv++;
97
98 while (*++cp)
99 switch (*cp) {
100 case 'i':
101 ignoreCase = TRUE;
102 break;
103
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000104 case 'h':
105 tellName = FALSE;
106 break;
107
Eric Andersen596e5461999-10-07 08:30:23 +0000108 case 'n':
109 tellLine = TRUE;
110 break;
111
112 default:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000113 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +0000114 }
115 }
116
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000117 needle = *argv++;
Eric Andersen596e5461999-10-07 08:30:23 +0000118 argc--;
119
Eric Andersen596e5461999-10-07 08:30:23 +0000120 while (argc-- > 0) {
121 name = *argv++;
122
123 fp = fopen (name, "r");
Eric Andersen596e5461999-10-07 08:30:23 +0000124 if (fp == NULL) {
125 perror (name);
Eric Andersen596e5461999-10-07 08:30:23 +0000126 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000127 }
128
Eric Andersen596e5461999-10-07 08:30:23 +0000129 line = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000130
Eric Andersen596e5461999-10-07 08:30:23 +0000131 while (fgets (buf, sizeof (buf), fp)) {
132 line++;
Eric Andersen596e5461999-10-07 08:30:23 +0000133 cp = &buf[strlen (buf) - 1];
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Eric Andersen596e5461999-10-07 08:30:23 +0000135 if (*cp != '\n')
136 fprintf (stderr, "%s: Line too long\n", name);
137
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000138 if (search (buf, needle, ignoreCase)==TRUE) {
139 if (tellName==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000140 printf ("%s: ", name);
141
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000142 if (tellLine==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000143 printf ("%ld: ", line);
144
145 fputs (buf, stdout);
146 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000147 }
148
Eric Andersen596e5461999-10-07 08:30:23 +0000149 if (ferror (fp))
150 perror (name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Eric Andersen596e5461999-10-07 08:30:23 +0000152 fclose (fp);
153 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000154 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000155}
156
157
Eric Andersencc8ed391999-10-05 16:24:54 +0000158/* END CODE */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000159
160