blob: 287d9f80dff05721d8599062f58b09c840721e59 [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
Erik Andersen3fe2ecf1999-12-21 08:52:04 +000024/*
25 18-Dec-1999 Konstantin Boldyshev <konst@voshod.com>
26
27 + -q option (be quiet)
28 + exit code depending on grep result (TRUE or FALSE)
29 (useful for scripts)
30*/
31
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include "internal.h"
Eric Andersenaa0765e1999-10-22 04:30:20 +000033#include "regexp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000034#include <stdio.h>
35#include <dirent.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <time.h>
40#include <ctype.h>
41
Eric Andersene77ae3a1999-10-19 20:03:34 +000042static const char grep_usage[] =
Eric Andersend73dc5b1999-11-10 23:13:02 +000043"grep [OPTIONS]... PATTERN [FILE]...\n\n"
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044"Search for PATTERN in each FILE or standard input.\n\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000045"OPTIONS:\n"
Eric Andersen3e0fbae1999-10-19 06:02:44 +000046"\t-h\tsuppress the prefixing filename on output\n"
47"\t-i\tignore case distinctions\n"
Erik Andersen3fe2ecf1999-12-21 08:52:04 +000048"\t-n\tprint line number with output lines\n"
Erik Andersen2fe08c71999-12-29 02:10:35 +000049"\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n"
Eric Andersenaa0765e1999-10-22 04:30:20 +000050#if defined BB_REGEXP
Eric Andersend73dc5b1999-11-10 23:13:02 +000051"This version of grep matches full regular expresions.\n";
Eric Andersenaa0765e1999-10-22 04:30:20 +000052#else
Eric Andersend73dc5b1999-11-10 23:13:02 +000053"This version of grep matches strings (not regular expresions).\n";
Eric Andersenaa0765e1999-10-22 04:30:20 +000054#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersen3fe2ecf1999-12-21 08:52:04 +000056static int match = FALSE, beQuiet = FALSE;
Eric Andersenfbb39c81999-11-08 17:00:52 +000057
Eric Andersen50d63601999-11-09 01:47:36 +000058static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine)
Eric Andersenfbb39c81999-11-08 17:00:52 +000059{
Eric Andersen50d63601999-11-09 01:47:36 +000060 char *cp;
61 long line = 0;
62 char haystack[BUF_SIZE];
Eric Andersenfbb39c81999-11-08 17:00:52 +000063
Eric Andersen50d63601999-11-09 01:47:36 +000064 while (fgets (haystack, sizeof (haystack), fp)) {
65 line++;
66 cp = &haystack[strlen (haystack) - 1];
Eric Andersenfbb39c81999-11-08 17:00:52 +000067
Eric Andersen50d63601999-11-09 01:47:36 +000068 if (*cp != '\n')
69 fprintf (stderr, "%s: Line too long\n", fileName);
Eric Andersenfbb39c81999-11-08 17:00:52 +000070
Eric Andersen50d63601999-11-09 01:47:36 +000071 if (find_match(haystack, needle, ignoreCase) == TRUE) {
72 if (tellName==TRUE)
73 printf ("%s:", fileName);
Eric Andersenfbb39c81999-11-08 17:00:52 +000074
Eric Andersen50d63601999-11-09 01:47:36 +000075 if (tellLine==TRUE)
76 printf ("%ld:", line);
Eric Andersenfbb39c81999-11-08 17:00:52 +000077
Erik Andersen3fe2ecf1999-12-21 08:52:04 +000078 if (beQuiet==FALSE)
79 fputs (haystack, stdout);
80
81 match = TRUE;
Eric Andersenfbb39c81999-11-08 17:00:52 +000082 }
Eric Andersen50d63601999-11-09 01:47:36 +000083 }
Eric Andersenfbb39c81999-11-08 17:00:52 +000084}
85
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersen596e5461999-10-07 08:30:23 +000087extern int grep_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000088{
Eric Andersen596e5461999-10-07 08:30:23 +000089 FILE *fp;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000090 char *cp;
Eric Andersen50d63601999-11-09 01:47:36 +000091 char *needle;
92 char *fileName;
Erik Andersena2f69e52000-01-19 04:30:37 +000093 int tellName=TRUE;
94 int ignoreCase=TRUE;
Eric Andersen50d63601999-11-09 01:47:36 +000095 int tellLine=FALSE;
96
Eric Andersencc8ed391999-10-05 16:24:54 +000097
Eric Andersen596e5461999-10-07 08:30:23 +000098 ignoreCase = FALSE;
99 tellLine = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000100
Eric Andersen596e5461999-10-07 08:30:23 +0000101 argc--;
102 argv++;
103 if (argc < 1) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000104 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +0000105 }
106
107 if (**argv == '-') {
Eric Andersencc8ed391999-10-05 16:24:54 +0000108 argc--;
Eric Andersen596e5461999-10-07 08:30:23 +0000109 cp = *argv++;
110
111 while (*++cp)
112 switch (*cp) {
113 case 'i':
114 ignoreCase = TRUE;
115 break;
116
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000117 case 'h':
Erik Andersena2f69e52000-01-19 04:30:37 +0000118 tellName = FALSE;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000119 break;
120
Eric Andersen596e5461999-10-07 08:30:23 +0000121 case 'n':
122 tellLine = TRUE;
123 break;
124
Erik Andersen3fe2ecf1999-12-21 08:52:04 +0000125 case 'q':
126 beQuiet = TRUE;
127 break;
128
Eric Andersen596e5461999-10-07 08:30:23 +0000129 default:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000130 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +0000131 }
132 }
133
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000134 needle = *argv++;
Eric Andersen596e5461999-10-07 08:30:23 +0000135 argc--;
136
Eric Andersen50d63601999-11-09 01:47:36 +0000137 if (argc==0) {
138 do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
139 } else {
Erik Andersena2f69e52000-01-19 04:30:37 +0000140 /* Never print the filename for just one file */
141 if (argc==1)
142 tellName=FALSE;
Eric Andersen50d63601999-11-09 01:47:36 +0000143 while (argc-- > 0) {
144 fileName = *argv++;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000145
Eric Andersen50d63601999-11-09 01:47:36 +0000146 fp = fopen (fileName, "r");
147 if (fp == NULL) {
148 perror (fileName);
149 continue;
150 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000151
Eric Andersen50d63601999-11-09 01:47:36 +0000152 do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine);
153
154 if (ferror (fp))
155 perror (fileName);
156 fclose (fp);
Eric Andersenfbb39c81999-11-08 17:00:52 +0000157 }
Eric Andersen596e5461999-10-07 08:30:23 +0000158 }
Erik Andersen3fe2ecf1999-12-21 08:52:04 +0000159 exit(match);
Eric Andersencc8ed391999-10-05 16:24:54 +0000160}
161
162
Eric Andersencc8ed391999-10-05 16:24:54 +0000163/* END CODE */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000164
165