blob: 44ca028345380fb79776de87a9eddbecf7fb7819 [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 Andersen3e0fbae1999-10-19 06:02:44 +000043"This version of grep matches strings (not full regexps).\n";
Eric Andersenaa0765e1999-10-22 04:30:20 +000044#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000045
46
Eric Andersen596e5461999-10-07 08:30:23 +000047extern int grep_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Eric Andersen596e5461999-10-07 08:30:23 +000049 FILE *fp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000050 const char *needle;
Eric Andersen596e5461999-10-07 08:30:23 +000051 const char *name;
52 const char *cp;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000053 int tellName=TRUE;
54 int ignoreCase=FALSE;
55 int tellLine=FALSE;
Eric Andersen596e5461999-10-07 08:30:23 +000056 long line;
Eric Andersenaa0765e1999-10-22 04:30:20 +000057 char haystack[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Eric Andersen596e5461999-10-07 08:30:23 +000059 ignoreCase = FALSE;
60 tellLine = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Eric Andersen596e5461999-10-07 08:30:23 +000062 argc--;
63 argv++;
64 if (argc < 1) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +000065 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000066 }
67
68 if (**argv == '-') {
Eric Andersencc8ed391999-10-05 16:24:54 +000069 argc--;
Eric Andersen596e5461999-10-07 08:30:23 +000070 cp = *argv++;
71
72 while (*++cp)
73 switch (*cp) {
74 case 'i':
75 ignoreCase = TRUE;
76 break;
77
Eric Andersen3e0fbae1999-10-19 06:02:44 +000078 case 'h':
79 tellName = FALSE;
80 break;
81
Eric Andersen596e5461999-10-07 08:30:23 +000082 case 'n':
83 tellLine = TRUE;
84 break;
85
86 default:
Eric Andersen3e0fbae1999-10-19 06:02:44 +000087 usage(grep_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000088 }
89 }
90
Eric Andersen3e0fbae1999-10-19 06:02:44 +000091 needle = *argv++;
Eric Andersen596e5461999-10-07 08:30:23 +000092 argc--;
93
Eric Andersen596e5461999-10-07 08:30:23 +000094 while (argc-- > 0) {
95 name = *argv++;
96
97 fp = fopen (name, "r");
Eric Andersen596e5461999-10-07 08:30:23 +000098 if (fp == NULL) {
99 perror (name);
Eric Andersen596e5461999-10-07 08:30:23 +0000100 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000101 }
102
Eric Andersen596e5461999-10-07 08:30:23 +0000103 line = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000104
Eric Andersenaa0765e1999-10-22 04:30:20 +0000105 while (fgets (haystack, sizeof (haystack), fp)) {
Eric Andersen596e5461999-10-07 08:30:23 +0000106 line++;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000107 cp = &haystack[strlen (haystack) - 1];
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
Eric Andersen596e5461999-10-07 08:30:23 +0000109 if (*cp != '\n')
110 fprintf (stderr, "%s: Line too long\n", name);
111
Eric Andersenaa0765e1999-10-22 04:30:20 +0000112 if (find_match(haystack, needle, ignoreCase) == TRUE) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000113 if (tellName==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000114 printf ("%s: ", name);
115
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000116 if (tellLine==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +0000117 printf ("%ld: ", line);
118
Eric Andersenaa0765e1999-10-22 04:30:20 +0000119 fputs (haystack, stdout);
Eric Andersen596e5461999-10-07 08:30:23 +0000120 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000121 }
122
Eric Andersen596e5461999-10-07 08:30:23 +0000123 if (ferror (fp))
124 perror (name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Eric Andersen596e5461999-10-07 08:30:23 +0000126 fclose (fp);
127 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000128 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129}
130
131
Eric Andersencc8ed391999-10-05 16:24:54 +0000132/* END CODE */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000133
134