blob: a8fd2d41abb59e7394a38c888e88386b3ffd294a [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Mark Whitleyd3721892000-06-28 22:00:26 +00002 * Mini grep implementation for busybox using libc regex.
Eric Andersenc4996011999-10-20 22:08:37 +00003 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00006 *
Eric Andersen3e0fbae1999-10-19 06:02:44 +00007 * 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.
11 *
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.
16 *
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
20 *
Eric Andersencc8ed391999-10-05 16:24:54 +000021 */
Eric Andersen8876fb22003-06-20 09:01:58 +000022/*
Eric Andersen7f164cd2004-05-26 09:46:41 +000023 * Apr 2004 by Vladimir Oleynik <dzo@simtreas.ru> -
24 * correction "-e pattern1 -e pattern2" logic and more optimizations.
Eric Andersen8876fb22003-06-20 09:01:58 +000025*/
Eric Andersencc8ed391999-10-05 16:24:54 +000026
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <stdio.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000028#include <stdlib.h>
Eric Andersene9b527a2000-07-09 05:56:14 +000029#include <getopt.h>
Mark Whitleyd3721892000-06-28 22:00:26 +000030#include <regex.h>
Eric Andersen8876fb22003-06-20 09:01:58 +000031#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include <errno.h>
Eric Andersen3570a342000-09-25 21:45:58 +000033#include "busybox.h"
Mark Whitleyd3721892000-06-28 22:00:26 +000034
Eric Andersened3ef502001-01-27 08:24:39 +000035
Mark Whitleyd3721892000-06-28 22:00:26 +000036/* options */
Eric Andersendec7f812004-05-26 11:47:55 +000037#define GREP_OPTS "lnqvscFiHhe:f:L"
Eric Andersenabc513a2004-05-26 11:48:29 +000038#define GREP_OPT_l (1<<0)
Eric Andersen8876fb22003-06-20 09:01:58 +000039static char print_files_with_matches;
Eric Andersenabc513a2004-05-26 11:48:29 +000040#define GREP_OPT_n (1<<1)
Eric Andersen8876fb22003-06-20 09:01:58 +000041static char print_line_num;
Eric Andersenabc513a2004-05-26 11:48:29 +000042#define GREP_OPT_q (1<<2)
Eric Andersen8876fb22003-06-20 09:01:58 +000043static char be_quiet;
Eric Andersenabc513a2004-05-26 11:48:29 +000044#define GREP_OPT_v (1<<3)
Eric Andersen8876fb22003-06-20 09:01:58 +000045typedef char invert_search_t;
46static invert_search_t invert_search;
Eric Andersenabc513a2004-05-26 11:48:29 +000047#define GREP_OPT_s (1<<4)
Eric Andersen8876fb22003-06-20 09:01:58 +000048static char suppress_err_msgs;
Eric Andersenabc513a2004-05-26 11:48:29 +000049#define GREP_OPT_c (1<<5)
Eric Andersen8876fb22003-06-20 09:01:58 +000050static char print_match_counts;
Eric Andersenabc513a2004-05-26 11:48:29 +000051#define GREP_OPT_F (1<<6)
Eric Andersen8876fb22003-06-20 09:01:58 +000052static char fgrep_flag;
Eric Andersenabc513a2004-05-26 11:48:29 +000053#define GREP_OPT_i (1<<7)
54#define GREP_OPT_H (1<<8)
55#define GREP_OPT_h (1<<9)
56#define GREP_OPT_e (1<<10)
57#define GREP_OPT_f (1<<11)
58#define GREP_OPT_L (1<<12)
Eric Andersendec7f812004-05-26 11:47:55 +000059static char print_files_without_matches;
Eric Andersen8876fb22003-06-20 09:01:58 +000060#ifdef CONFIG_FEATURE_GREP_CONTEXT
61#define GREP_OPT_CONTEXT "A:B:C"
Eric Andersenabc513a2004-05-26 11:48:29 +000062#define GREP_OPT_A (1<<13)
63#define GREP_OPT_B (1<<14)
64#define GREP_OPT_C (1<<15)
65#define GREP_OPT_E (1<<16)
Eric Andersen8876fb22003-06-20 09:01:58 +000066#else
67#define GREP_OPT_CONTEXT ""
Eric Andersenabc513a2004-05-26 11:48:29 +000068#define GREP_OPT_E (1<<13)
Eric Andersen8876fb22003-06-20 09:01:58 +000069#endif
70#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
71# define OPT_EGREP "E"
72#else
73# define OPT_EGREP ""
74#endif
75
76static int reflags;
77static int print_filename;
Mark Whitleyd3721892000-06-28 22:00:26 +000078
Eric Andersenbdfd0d72001-10-24 05:00:29 +000079#ifdef CONFIG_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +000080static int lines_before;
81static int lines_after;
82static char **before_buf;
83static int last_line_printed;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000084#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitley2fd52982001-02-09 00:41:10 +000085
86/* globals used internally */
Eric Andersen8876fb22003-06-20 09:01:58 +000087static llist_t *pattern_head; /* growable list of patterns to match */
88static char *cur_file; /* the current file we are reading */
Mark Whitleyd3721892000-06-28 22:00:26 +000089
Mark Whitleyfa43e542001-05-24 18:36:18 +000090
Mark Whitley2fd52982001-02-09 00:41:10 +000091static void print_line(const char *line, int linenum, char decoration)
92{
Eric Andersenbdfd0d72001-10-24 05:00:29 +000093#ifdef CONFIG_FEATURE_GREP_CONTEXT
Eric Andersenaff114c2004-04-14 17:51:38 +000094 /* possibly print the little '--' separator */
Matt Kraaiedc80652001-05-22 14:29:27 +000095 if ((lines_before || lines_after) && last_line_printed &&
96 last_line_printed < linenum - 1) {
Mark Whitley2fd52982001-02-09 00:41:10 +000097 puts("--");
98 }
99 last_line_printed = linenum;
100#endif
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000101 if (print_filename > 0)
Mark Whitley2fd52982001-02-09 00:41:10 +0000102 printf("%s%c", cur_file, decoration);
103 if (print_line_num)
104 printf("%i%c", linenum, decoration);
105 puts(line);
106}
Mark Whitleyd3721892000-06-28 22:00:26 +0000107
Eric Andersen8876fb22003-06-20 09:01:58 +0000108extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000109
Eric Andersen8876fb22003-06-20 09:01:58 +0000110
111static int grep_file(FILE *file)
Mark Whitleyd3721892000-06-28 22:00:26 +0000112{
Eric Andersen8876fb22003-06-20 09:01:58 +0000113 char *line;
114 invert_search_t ret;
Mark Whitleyd3721892000-06-28 22:00:26 +0000115 int linenum = 0;
Matt Kraaideb95f62000-08-06 15:25:53 +0000116 int nmatches = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000117#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitley2fd52982001-02-09 00:41:10 +0000118 int print_n_lines_after = 0;
119 int curpos = 0; /* track where we are in the circular 'before' buffer */
120 int idx = 0; /* used for iteration through the circular buffer */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000121#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000122
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000124 llist_t *pattern_ptr = pattern_head;
125
Mark Whitleyd3721892000-06-28 22:00:26 +0000126 linenum++;
Eric Andersen8876fb22003-06-20 09:01:58 +0000127 ret = 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000128 while (pattern_ptr) {
129 if (fgrep_flag) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000130 ret = strstr(line, pattern_ptr->data) != NULL;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000131 } else {
132 /*
133 * test for a postitive-assertion match (regexec returns success (0)
134 * and the user did not specify invert search), or a negative-assertion
135 * match (regexec returns failure (REG_NOMATCH) and the user specified
136 * invert search)
137 */
138 regex_t regex;
139 xregcomp(&regex, pattern_ptr->data, reflags);
Eric Andersen7f164cd2004-05-26 09:46:41 +0000140 ret |= regexec(&regex, line, 0, NULL, 0) == 0;
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000141 regfree(&regex);
142 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000143 pattern_ptr = pattern_ptr->link;
144 } /* while (pattern_ptr) */
Mark Whitleyd3721892000-06-28 22:00:26 +0000145
Eric Andersen8876fb22003-06-20 09:01:58 +0000146 if ((ret ^ invert_search)) {
147
148 if (print_files_with_matches || be_quiet)
149 free(line);
150
151 /* if we found a match but were told to be quiet, stop here */
Eric Andersendec7f812004-05-26 11:47:55 +0000152 if (be_quiet || print_files_without_matches)
Eric Andersen8876fb22003-06-20 09:01:58 +0000153 return -1;
Mark Whitleyd3721892000-06-28 22:00:26 +0000154
Mark Whitleyfa43e542001-05-24 18:36:18 +0000155 /* keep track of matches */
156 nmatches++;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000157
Mark Whitleyfa43e542001-05-24 18:36:18 +0000158 /* if we're just printing filenames, we stop after the first match */
159 if (print_files_with_matches)
160 break;
Mark Whitley1d9d4112001-05-21 21:13:00 +0000161
Mark Whitleyfa43e542001-05-24 18:36:18 +0000162 /* print the matched line */
163 if (print_match_counts == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000164#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000165 int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
Mark Whitley2fd52982001-02-09 00:41:10 +0000166
Mark Whitleyfa43e542001-05-24 18:36:18 +0000167 /* if we were told to print 'before' lines and there is at least
168 * one line in the circular buffer, print them */
169 if (lines_before && before_buf[prevpos] != NULL) {
170 int first_buf_entry_line_num = linenum - lines_before;
Mark Whitley2fd52982001-02-09 00:41:10 +0000171
Mark Whitleyfa43e542001-05-24 18:36:18 +0000172 /* advance to the first entry in the circular buffer, and
173 * figure out the line number is of the first line in the
174 * buffer */
175 idx = curpos;
176 while (before_buf[idx] == NULL) {
177 idx = (idx + 1) % lines_before;
178 first_buf_entry_line_num++;
179 }
180
181 /* now print each line in the buffer, clearing them as we go */
182 while (before_buf[idx] != NULL) {
183 print_line(before_buf[idx], first_buf_entry_line_num, '-');
184 free(before_buf[idx]);
185 before_buf[idx] = NULL;
186 idx = (idx + 1) % lines_before;
187 first_buf_entry_line_num++;
188 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000189 }
190
Mark Whitleyfa43e542001-05-24 18:36:18 +0000191 /* make a note that we need to print 'after' lines */
192 print_n_lines_after = lines_after;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000193#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyfa43e542001-05-24 18:36:18 +0000194 print_line(line, linenum, ':');
Mark Whitley2fd52982001-02-09 00:41:10 +0000195 }
Matt Kraai0810f722001-01-04 15:11:52 +0000196 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_GREP_CONTEXT
Mark Whitleyfa43e542001-05-24 18:36:18 +0000198 else { /* no match */
199 /* Add the line to the circular 'before' buffer */
200 if(lines_before) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000201 free(before_buf[curpos]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 before_buf[curpos] = bb_xstrdup(line);
Mark Whitleyfa43e542001-05-24 18:36:18 +0000203 curpos = (curpos + 1) % lines_before;
204 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000205 }
Mark Whitley2fd52982001-02-09 00:41:10 +0000206
Mark Whitleyfa43e542001-05-24 18:36:18 +0000207 /* if we need to print some context lines after the last match, do so */
208 if (print_n_lines_after && (last_line_printed != linenum)) {
209 print_line(line, linenum, '-');
210 print_n_lines_after--;
211 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212#endif /* CONFIG_FEATURE_GREP_CONTEXT */
Mark Whitleyd3721892000-06-28 22:00:26 +0000213 free(line);
214 }
Mark Whitley8f122432000-07-18 18:37:01 +0000215
Mark Whitley35e59be2001-05-14 19:40:32 +0000216
217 /* special-case file post-processing for options where we don't print line
Mark Whitley1d9d4112001-05-21 21:13:00 +0000218 * matches, just filenames and possibly match counts */
Mark Whitley35e59be2001-05-14 19:40:32 +0000219
Mark Whitley1d9d4112001-05-21 21:13:00 +0000220 /* grep -c: print [filename:]count, even if count is zero */
Mark Whitley35e59be2001-05-14 19:40:32 +0000221 if (print_match_counts) {
Mike Frysinger5ba5f4d2005-04-16 04:56:11 +0000222 if (print_filename > 0)
Mark Whitley1d9d4112001-05-21 21:13:00 +0000223 printf("%s:", cur_file);
Eric Andersen00344432001-07-31 23:18:49 +0000224 printf("%d\n", nmatches);
Mark Whitley35e59be2001-05-14 19:40:32 +0000225 }
Mark Whitley1d9d4112001-05-21 21:13:00 +0000226
227 /* grep -l: print just the filename, but only if we grepped the line in the file */
228 if (print_files_with_matches && nmatches > 0) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000229 puts(cur_file);
Mark Whitley35e59be2001-05-14 19:40:32 +0000230 }
231
Eric Andersendec7f812004-05-26 11:47:55 +0000232 /* grep -L: print just the filename, but only if we didn't grep the line in the file */
233 if (print_files_without_matches && nmatches == 0) {
234 puts(cur_file);
235 }
236
Eric Andersen8876fb22003-06-20 09:01:58 +0000237 return nmatches;
Mark Whitleyd3721892000-06-28 22:00:26 +0000238}
Eric Andersencc8ed391999-10-05 16:24:54 +0000239
Eric Andersen8876fb22003-06-20 09:01:58 +0000240static void load_regexes_from_file(llist_t *fopt)
Mark Whitleyfa43e542001-05-24 18:36:18 +0000241{
242 char *line;
Eric Andersen8876fb22003-06-20 09:01:58 +0000243 FILE *f;
244
245 while(fopt) {
246 llist_t *cur = fopt;
247 char *ffile = cur->data;
248
249 fopt = cur->link;
250 free(cur);
251 f = bb_xfopen(ffile, "r");
Eric Andersen31c27a92004-10-08 08:10:57 +0000252 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
253 pattern_head = llist_add_to(pattern_head, line);
254 }
Mark Whitleyfa43e542001-05-24 18:36:18 +0000255 }
256}
Mark Whitleyfa43e542001-05-24 18:36:18 +0000257
258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259extern int grep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000260{
Eric Andersen8876fb22003-06-20 09:01:58 +0000261 FILE *file;
262 int matched;
263 unsigned long opt;
Eric Andersen31c27a92004-10-08 08:10:57 +0000264 llist_t *fopt = NULL;
Matt Kraai999623e2001-10-29 15:49:03 +0000265
Mark Whitleyd3721892000-06-28 22:00:26 +0000266 /* do normal option parsing */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000267#ifdef CONFIG_FEATURE_GREP_CONTEXT
Eric Andersen8876fb22003-06-20 09:01:58 +0000268 {
269 char *junk;
270 char *slines_after;
271 char *slines_before;
272 char *Copt;
273
274 bb_opt_complementaly = "H-h:e*:f*:C-AB";
275 opt = bb_getopt_ulflags(argc, argv,
276 GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP,
277 &pattern_head, &fopt,
278 &slines_after, &slines_before, &Copt);
279
280 if(opt & GREP_OPT_C) {
281 /* C option unseted A and B options, but next -A or -B
282 may be ovewrite own option */
283 if(!(opt & GREP_OPT_A)) /* not overwtited */
284 slines_after = Copt;
285 if(!(opt & GREP_OPT_B)) /* not overwtited */
286 slines_before = Copt;
287 opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */
Eric Andersen053b1462000-06-13 06:24:53 +0000288 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000289 if(opt & GREP_OPT_A) {
290 lines_after = strtoul(slines_after, &junk, 10);
291 if(*junk != '\0')
292 bb_error_msg_and_die("invalid context length argument");
293 }
294 if(opt & GREP_OPT_B) {
295 lines_before = strtoul(slines_before, &junk, 10);
296 if(*junk != '\0')
297 bb_error_msg_and_die("invalid context length argument");
298 }
299 /* sanity checks after parse may be invalid numbers ;-) */
Eric Andersendec7f812004-05-26 11:47:55 +0000300 if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000301 opt &= ~GREP_OPT_n;
302 lines_before = 0;
303 lines_after = 0;
304 } else if(lines_before > 0)
305 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
306 }
307#else
308 /* with auto sanity checks */
309 bb_opt_complementaly = "H-h:e*:f*:c-n:q-n:l-n";
310 opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP,
311 &pattern_head, &fopt);
312
313#endif
314 print_files_with_matches = opt & GREP_OPT_l;
Eric Andersendec7f812004-05-26 11:47:55 +0000315 print_files_without_matches = (opt & GREP_OPT_L) != 0;
Eric Andersen8876fb22003-06-20 09:01:58 +0000316 print_line_num = opt & GREP_OPT_n;
317 be_quiet = opt & GREP_OPT_q;
318 invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */
319 suppress_err_msgs = opt & GREP_OPT_s;
320 print_match_counts = opt & GREP_OPT_c;
321 fgrep_flag = opt & GREP_OPT_F;
322 if(opt & GREP_OPT_H)
323 print_filename++;
324 if(opt & GREP_OPT_h)
325 print_filename--;
326 if(opt & GREP_OPT_f)
327 load_regexes_from_file(fopt);
328
Mike Frysinger15ca5862005-07-31 22:41:05 +0000329#ifdef CONFIG_FEATURE_GREP_FGREP_ALIAS
330 if(bb_applet_name[0] == 'f')
331 fgrep_flag = 1;
332#endif
333
Eric Andersen8876fb22003-06-20 09:01:58 +0000334#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
335 if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E))
336 reflags = REG_EXTENDED | REG_NOSUB;
337 else
338#endif
339 reflags = REG_NOSUB;
340
341 if(opt & GREP_OPT_i)
342 reflags |= REG_ICASE;
343
344 argv += optind;
345 argc -= optind;
Eric Andersen053b1462000-06-13 06:24:53 +0000346
Mark Whitleyfa43e542001-05-24 18:36:18 +0000347 /* if we didn't get a pattern from a -e and no command file was specified,
348 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrath26df70a2003-04-27 01:50:57 +0000349 if (pattern_head == NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000350 if (*argv == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000351 bb_show_usage();
Mark Whitleyfa43e542001-05-24 18:36:18 +0000352 else {
Eric Andersen8876fb22003-06-20 09:01:58 +0000353 pattern_head = llist_add_to(pattern_head, *argv++);
354 argc--;
Mark Whitleyfa43e542001-05-24 18:36:18 +0000355 }
356 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000357
Mark Whitleyfa43e542001-05-24 18:36:18 +0000358 /* argv[(optind)..(argc-1)] should be names of file to grep through. If
Mark Whitleyd3721892000-06-28 22:00:26 +0000359 * there is more than one file to grep, we will print the filenames */
Eric Andersen8876fb22003-06-20 09:01:58 +0000360 if (argc > 1) {
Mark Whitleyd3721892000-06-28 22:00:26 +0000361 print_filename++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362
Mark Whitley2e1148b2000-06-28 22:59:30 +0000363 /* If no files were specified, or '-' was specified, take input from
364 * stdin. Otherwise, we grep through all the files specified. */
Eric Andersen8876fb22003-06-20 09:01:58 +0000365 } else if (argc == 0) {
366 argc++;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000367 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000368 matched = 0;
369 while (argc--) {
370 cur_file = *argv++;
371 if(!cur_file || (*cur_file == '-' && !cur_file[1])) {
372 cur_file = "-";
373 file = stdin;
374 } else {
Mark Whitleyd3721892000-06-28 22:00:26 +0000375 file = fopen(cur_file, "r");
Eric Andersen8876fb22003-06-20 09:01:58 +0000376 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000377 if (file == NULL) {
378 if (!suppress_err_msgs)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000379 bb_perror_msg("%s", cur_file);
Eric Andersen8876fb22003-06-20 09:01:58 +0000380 } else {
381 matched += grep_file(file);
382 if(matched < 0) {
383 /* we found a match but were told to be quiet, stop here and
384 * return success */
385 break;
Mark Whitley2ef880b2000-07-18 21:02:06 +0000386 }
Mark Whitleyd3721892000-06-28 22:00:26 +0000387 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000390
391#ifdef CONFIG_FEATURE_CLEAN_UP
392 /* destroy all the elments in the pattern list */
393 while (pattern_head) {
394 llist_t *pattern_head_ptr = pattern_head;
395
396 pattern_head = pattern_head->link;
397 free(pattern_head_ptr);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000399#endif
Mark Whitleyd3721892000-06-28 22:00:26 +0000400
Mark Whitleyb5c29852001-02-01 21:02:41 +0000401 return !matched; /* invert return value 0 = success, 1 = failed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000402}