2 * Mini grep implementation for busybox using libc regex.
4 * Copyright (C) 1999,2000,2001 by Lineo, inc.
5 * Written by Mark Whitley <markw@lineo.com>, <markw@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <string.h> /* for strerror() */
31 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
33 extern int optind; /* in unistd.h */
34 extern int errno; /* for use with strerror() */
37 static int ignore_case = 0;
38 static int print_filename = 0;
39 static int print_line_num = 0;
40 static int print_count_only = 0;
41 static int be_quiet = 0;
42 static int invert_search = 0;
43 static int suppress_err_msgs = 0;
46 static regex_t regex; /* storage space for compiled regular expression */
47 static int matched; /* keeps track of whether we ever matched */
48 static char *cur_file = NULL; /* the current file we are reading */
51 static void grep_file(FILE *file)
58 while ((line = get_line_from_file(file)) != NULL) {
61 ret = regexec(®ex, line, 0, NULL, 0);
63 /* test for a postitive-assertion match (regexec returned success (0)
64 * and the user did not specify invert search), or a negative-assertion
65 * match (vice versa) */
66 if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
68 /* if we found a match but were told to be quiet, stop here and
75 /* otherwise, keep track of matches, print the matched line, and
76 * whatever else the user wanted */
78 if (!print_count_only) {
80 printf("%s:", cur_file);
82 printf("%i:", linenum);
89 /* special-case post processing */
90 if (print_count_only) {
92 printf("%s:", cur_file);
93 printf("%i\n", nmatches);
96 /* record if we matched */
101 extern int grep_main(int argc, char **argv)
106 /* do normal option parsing */
107 while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
136 /* argv[optind] should be the regex pattern; no pattern, no worky */
137 if (argv[optind] == NULL)
140 /* compile the regular expression
141 * we're not going to mess with sub-expressions, and we need to
142 * treat newlines right. */
145 reflags |= REG_ICASE;
146 xregcomp(®ex, argv[optind], reflags);
148 /* argv[(optind+1)..(argc-1)] should be names of file to grep through. If
149 * there is more than one file to grep, we will print the filenames */
150 if ((argc-1) - (optind+1) > 0)
153 /* If no files were specified, or '-' was specified, take input from
154 * stdin. Otherwise, we grep through all the files specified. */
155 if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
161 for (i = optind + 1; i < argc; i++) {
163 file = fopen(cur_file, "r");
165 if (!suppress_err_msgs)
166 perror_msg("%s", cur_file);
177 return !matched; /* invert return value 0 = success, 1 = failed */