blob: 8dcff0586f4de0b6e3b3b00ee38022a2d4755aa7 [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 Andersenaa0765e1999-10-22 04:30:20 +000025#include "regexp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026#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
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"
Eric Andersenaa0765e1999-10-22 04:30:20 +000040#if defined BB_REGEXP
41"This version of grep matches full regexps.\n";
42#else
Eric Andersen50d63601999-11-09 01:47:36 +000043"This version of grep matches strings (not regexps).\n";
Eric Andersenaa0765e1999-10-22 04:30:20 +000044#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersenfbb39c81999-11-08 17:00:52 +000046
Eric Andersen50d63601999-11-09 01:47:36 +000047static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine)
Eric Andersenfbb39c81999-11-08 17:00:52 +000048{
Eric Andersen50d63601999-11-09 01:47:36 +000049 char *cp;
50 long line = 0;
51 char haystack[BUF_SIZE];
Eric Andersenfbb39c81999-11-08 17:00:52 +000052
Eric Andersen50d63601999-11-09 01:47:36 +000053 while (fgets (haystack, sizeof (haystack), fp)) {
54 line++;
55 cp = &haystack[strlen (haystack) - 1];
Eric Andersenfbb39c81999-11-08 17:00:52 +000056
Eric Andersen50d63601999-11-09 01:47:36 +000057 if (*cp != '\n')
58 fprintf (stderr, "%s: Line too long\n", fileName);
Eric Andersenfbb39c81999-11-08 17:00:52 +000059
Eric Andersen50d63601999-11-09 01:47:36 +000060 if (find_match(haystack, needle, ignoreCase) == TRUE) {
61 if (tellName==TRUE)
62 printf ("%s:", fileName);
Eric Andersenfbb39c81999-11-08 17:00:52 +000063
Eric Andersen50d63601999-11-09 01:47:36 +000064 if (tellLine==TRUE)
65 printf ("%ld:", line);
Eric Andersenfbb39c81999-11-08 17:00:52 +000066
Eric Andersen50d63601999-11-09 01:47:36 +000067 fputs (haystack, stdout);
Eric Andersenfbb39c81999-11-08 17:00:52 +000068 }
Eric Andersen50d63601999-11-09 01:47:36 +000069 }
Eric Andersenfbb39c81999-11-08 17:00:52 +000070}
71
Eric Andersencc8ed391999-10-05 16:24:54 +000072
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 Andersen6b6b3f61999-10-28 16:06:25 +000076 char *cp;
Eric Andersen50d63601999-11-09 01:47:36 +000077 char *needle;
78 char *fileName;
79 int tellName=FALSE;
80 int ignoreCase=FALSE;
81 int tellLine=FALSE;
82
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Eric Andersen596e5461999-10-07 08:30:23 +000084 ignoreCase = FALSE;
85 tellLine = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersen596e5461999-10-07 08:30:23 +000087 argc--;
88 argv++;
89 if (argc < 1) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +000090 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000091 }
92
93 if (**argv == '-') {
Eric Andersencc8ed391999-10-05 16:24:54 +000094 argc--;
Eric Andersen596e5461999-10-07 08:30:23 +000095 cp = *argv++;
96
97 while (*++cp)
98 switch (*cp) {
99 case 'i':
100 ignoreCase = TRUE;
101 break;
102
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000103 case 'h':
Eric Andersen50d63601999-11-09 01:47:36 +0000104 tellName = TRUE;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000105 break;
106
Eric Andersen596e5461999-10-07 08:30:23 +0000107 case 'n':
108 tellLine = TRUE;
109 break;
110
111 default:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000112 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +0000113 }
114 }
115
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000116 needle = *argv++;
Eric Andersen596e5461999-10-07 08:30:23 +0000117 argc--;
118
Eric Andersen50d63601999-11-09 01:47:36 +0000119 if (argc==0) {
120 do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine);
121 } else {
122 while (argc-- > 0) {
123 fileName = *argv++;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000124
Eric Andersen50d63601999-11-09 01:47:36 +0000125 fp = fopen (fileName, "r");
126 if (fp == NULL) {
127 perror (fileName);
128 continue;
129 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000130
Eric Andersen50d63601999-11-09 01:47:36 +0000131 do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine);
132
133 if (ferror (fp))
134 perror (fileName);
135 fclose (fp);
Eric Andersenfbb39c81999-11-08 17:00:52 +0000136 }
Eric Andersen596e5461999-10-07 08:30:23 +0000137 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000138 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000139}
140
141
Eric Andersencc8ed391999-10-05 16:24:54 +0000142/* END CODE */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000143
144