Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 3 | * Mini grep implementation for busybox using libc regex. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - unsupported option -x. */ |
| 11 | /* BB_AUDIT GNU defects - always acts as -a. */ |
| 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 13 | /* |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 14 | * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> - |
Eric Andersen | 7f164cd | 2004-05-26 09:46:41 +0000 | [diff] [blame] | 15 | * correction "-e pattern1 -e pattern2" logic and more optimizations. |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 16 | * precompiled regex |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 17 | */ |
| 18 | /* |
| 19 | * (C) 2006 Jac Goudsmit added -o option |
| 20 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
"Vladimir N. Oleynik" | 23f62fc | 2005-09-14 16:59:11 +0000 | [diff] [blame] | 23 | #include "xregex.h" |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 24 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 25 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 26 | /* options */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 27 | static unsigned opt; |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 28 | #define GREP_OPTS "lnqvscFiHhe:f:Lo" |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 29 | #define GREP_OPT_l (1<<0) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 30 | #define PRINT_FILES_WITH_MATCHES (opt & GREP_OPT_l) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 31 | #define GREP_OPT_n (1<<1) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 32 | #define PRINT_LINE_NUM (opt & GREP_OPT_n) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 33 | #define GREP_OPT_q (1<<2) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 34 | #define BE_QUIET (opt & GREP_OPT_q) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 35 | #define GREP_OPT_v (1<<3) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 36 | typedef char invert_search_t; |
| 37 | static invert_search_t invert_search; |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 38 | #define GREP_OPT_s (1<<4) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 39 | #define SUPPRESS_ERR_MSGS (opt & GREP_OPT_s) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 40 | #define GREP_OPT_c (1<<5) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 41 | #define PRINT_MATCH_COUNTS (opt & GREP_OPT_c) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 42 | #define GREP_OPT_F (1<<6) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 43 | #define FGREP_FLAG (opt & GREP_OPT_F) |
Eric Andersen | abc513a | 2004-05-26 11:48:29 +0000 | [diff] [blame] | 44 | #define GREP_OPT_i (1<<7) |
| 45 | #define GREP_OPT_H (1<<8) |
| 46 | #define GREP_OPT_h (1<<9) |
| 47 | #define GREP_OPT_e (1<<10) |
| 48 | #define GREP_OPT_f (1<<11) |
| 49 | #define GREP_OPT_L (1<<12) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 50 | #define PRINT_FILES_WITHOUT_MATCHES (opt & GREP_OPT_L) |
| 51 | #define GREP_OPT_o (1<<13) |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 52 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 3312c98 | 2006-09-25 22:18:56 +0000 | [diff] [blame] | 53 | #define GREP_OPT_CONTEXT "A:B:C:" |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 54 | #define GREP_OPT_A (1<<14) |
| 55 | #define GREP_OPT_B (1<<15) |
| 56 | #define GREP_OPT_C (1<<16) |
| 57 | #define GREP_OPT_E (1<<17) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 58 | #else |
| 59 | #define GREP_OPT_CONTEXT "" |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 60 | #define GREP_OPT_A 0 |
| 61 | #define GREP_OPT_B 0 |
| 62 | #define GREP_OPT_C 0 |
| 63 | #define GREP_OPT_E (1<<14) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 64 | #endif |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 65 | #if ENABLE_FEATURE_GREP_EGREP_ALIAS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 66 | # define OPT_EGREP "E" |
| 67 | #else |
| 68 | # define OPT_EGREP "" |
| 69 | #endif |
| 70 | |
| 71 | static int reflags; |
| 72 | static int print_filename; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 73 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 74 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 75 | static int lines_before; |
| 76 | static int lines_after; |
| 77 | static char **before_buf; |
| 78 | static int last_line_printed; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 79 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 80 | |
| 81 | /* globals used internally */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 82 | static llist_t *pattern_head; /* growable list of patterns to match */ |
| 83 | static char *cur_file; /* the current file we are reading */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 84 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 85 | typedef struct GREP_LIST_DATA { |
| 86 | char *pattern; |
| 87 | regex_t preg; |
| 88 | #define PATTERN_MEM_A 1 |
| 89 | #define COMPILED 2 |
| 90 | int flg_mem_alocated_compiled; |
| 91 | } grep_list_data_t; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 92 | |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 93 | static void print_line(const char *line, int linenum, char decoration) |
| 94 | { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 95 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 96 | /* possibly print the little '--' separator */ |
Matt Kraai | edc8065 | 2001-05-22 14:29:27 +0000 | [diff] [blame] | 97 | if ((lines_before || lines_after) && last_line_printed && |
| 98 | last_line_printed < linenum - 1) { |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 99 | puts("--"); |
| 100 | } |
| 101 | last_line_printed = linenum; |
| 102 | #endif |
Mike Frysinger | 5ba5f4d | 2005-04-16 04:56:11 +0000 | [diff] [blame] | 103 | if (print_filename > 0) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 104 | printf("%s%c", cur_file, decoration); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 105 | if (PRINT_LINE_NUM) |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 106 | printf("%i%c", linenum, decoration); |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 107 | /* Emulate weird GNU grep behavior with -ov */ |
| 108 | if ((opt & (GREP_OPT_v+GREP_OPT_o)) != (GREP_OPT_v+GREP_OPT_o)) |
| 109 | puts(line); |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 110 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 111 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 112 | |
| 113 | static int grep_file(FILE *file) |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 114 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 115 | char *line; |
| 116 | invert_search_t ret; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 117 | int linenum = 0; |
Matt Kraai | deb95f6 | 2000-08-06 15:25:53 +0000 | [diff] [blame] | 118 | int nmatches = 0; |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 119 | regmatch_t regmatch; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 120 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 121 | int print_n_lines_after = 0; |
| 122 | int curpos = 0; /* track where we are in the circular 'before' buffer */ |
| 123 | int idx = 0; /* used for iteration through the circular buffer */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 124 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 125 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 126 | while ((line = bb_get_chomped_line_from_file(file)) != NULL) { |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 127 | llist_t *pattern_ptr = pattern_head; |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 128 | grep_list_data_t * gl; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 129 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 130 | linenum++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 131 | ret = 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 132 | while (pattern_ptr) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 133 | gl = (grep_list_data_t *)pattern_ptr->data; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 134 | if (FGREP_FLAG) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 135 | ret = strstr(line, gl->pattern) != NULL; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 136 | } else { |
| 137 | /* |
| 138 | * test for a postitive-assertion match (regexec returns success (0) |
| 139 | * and the user did not specify invert search), or a negative-assertion |
| 140 | * match (regexec returns failure (REG_NOMATCH) and the user specified |
| 141 | * invert search) |
| 142 | */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 143 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 144 | gl->flg_mem_alocated_compiled |= COMPILED; |
| 145 | xregcomp(&(gl->preg), gl->pattern, reflags); |
| 146 | } |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 147 | regmatch.rm_so = 0; |
| 148 | regmatch.rm_eo = 0; |
| 149 | ret |= regexec(&(gl->preg), line, 1, ®match, 0) == 0; |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 150 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 151 | pattern_ptr = pattern_ptr->link; |
| 152 | } /* while (pattern_ptr) */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 154 | if (ret ^ invert_search) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 155 | |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 156 | if (PRINT_FILES_WITH_MATCHES || BE_QUIET) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 157 | free(line); |
| 158 | |
| 159 | /* if we found a match but were told to be quiet, stop here */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 160 | if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 161 | return -1; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 162 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 163 | /* keep track of matches */ |
| 164 | nmatches++; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 165 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 166 | /* if we're just printing filenames, we stop after the first match */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 167 | if (PRINT_FILES_WITH_MATCHES) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 168 | break; |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 169 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 170 | /* print the matched line */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 171 | if (PRINT_MATCH_COUNTS == 0) { |
| 172 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 173 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 174 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 175 | /* if we were told to print 'before' lines and there is at least |
| 176 | * one line in the circular buffer, print them */ |
| 177 | if (lines_before && before_buf[prevpos] != NULL) { |
| 178 | int first_buf_entry_line_num = linenum - lines_before; |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 179 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 180 | /* advance to the first entry in the circular buffer, and |
| 181 | * figure out the line number is of the first line in the |
| 182 | * buffer */ |
| 183 | idx = curpos; |
| 184 | while (before_buf[idx] == NULL) { |
| 185 | idx = (idx + 1) % lines_before; |
| 186 | first_buf_entry_line_num++; |
| 187 | } |
| 188 | |
| 189 | /* now print each line in the buffer, clearing them as we go */ |
| 190 | while (before_buf[idx] != NULL) { |
| 191 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); |
| 192 | free(before_buf[idx]); |
| 193 | before_buf[idx] = NULL; |
| 194 | idx = (idx + 1) % lines_before; |
| 195 | first_buf_entry_line_num++; |
| 196 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 199 | /* make a note that we need to print 'after' lines */ |
| 200 | print_n_lines_after = lines_after; |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 201 | #endif |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 202 | if (opt & GREP_OPT_o) { |
| 203 | line[regmatch.rm_eo] = '\0'; |
| 204 | print_line(line + regmatch.rm_so, linenum, ':'); |
| 205 | } else { |
| 206 | print_line(line, linenum, ':'); |
| 207 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 208 | } |
Matt Kraai | 0810f72 | 2001-01-04 15:11:52 +0000 | [diff] [blame] | 209 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 210 | #if ENABLE_FEATURE_GREP_CONTEXT |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 211 | else { /* no match */ |
| 212 | /* Add the line to the circular 'before' buffer */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 213 | if (lines_before) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 214 | free(before_buf[curpos]); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 215 | before_buf[curpos] = xstrdup(line); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 216 | curpos = (curpos + 1) % lines_before; |
| 217 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 218 | } |
Mark Whitley | 2fd5298 | 2001-02-09 00:41:10 +0000 | [diff] [blame] | 219 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 220 | /* if we need to print some context lines after the last match, do so */ |
| 221 | if (print_n_lines_after && (last_line_printed != linenum)) { |
| 222 | print_line(line, linenum, '-'); |
| 223 | print_n_lines_after--; |
| 224 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 225 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 226 | free(line); |
| 227 | } |
Mark Whitley | 8f12243 | 2000-07-18 18:37:01 +0000 | [diff] [blame] | 228 | |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 229 | /* special-case file post-processing for options where we don't print line |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 230 | * matches, just filenames and possibly match counts */ |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 231 | |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 232 | /* grep -c: print [filename:]count, even if count is zero */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 233 | if (PRINT_MATCH_COUNTS) { |
Mike Frysinger | 5ba5f4d | 2005-04-16 04:56:11 +0000 | [diff] [blame] | 234 | if (print_filename > 0) |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 235 | printf("%s:", cur_file); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 236 | printf("%d\n", nmatches); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 237 | } |
Mark Whitley | 1d9d411 | 2001-05-21 21:13:00 +0000 | [diff] [blame] | 238 | |
| 239 | /* grep -l: print just the filename, but only if we grepped the line in the file */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 240 | if (PRINT_FILES_WITH_MATCHES && nmatches > 0) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 241 | puts(cur_file); |
Mark Whitley | 35e59be | 2001-05-14 19:40:32 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 244 | /* grep -L: print just the filename, but only if we didn't grep the line in the file */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 245 | if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) { |
Eric Andersen | dec7f81 | 2004-05-26 11:47:55 +0000 | [diff] [blame] | 246 | puts(cur_file); |
| 247 | } |
| 248 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 249 | return nmatches; |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 250 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 251 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 252 | #if ENABLE_FEATURE_CLEAN_UP |
| 253 | #define new_grep_list_data(p, m) add_grep_list_data(p, m) |
| 254 | static char * add_grep_list_data(char *pattern, int flg_used_mem) |
| 255 | #else |
| 256 | #define new_grep_list_data(p, m) add_grep_list_data(p) |
| 257 | static char * add_grep_list_data(char *pattern) |
| 258 | #endif |
| 259 | { |
| 260 | grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t)); |
| 261 | gl->pattern = pattern; |
| 262 | #if ENABLE_FEATURE_CLEAN_UP |
| 263 | gl->flg_mem_alocated_compiled = flg_used_mem; |
| 264 | #else |
| 265 | gl->flg_mem_alocated_compiled = 0; |
| 266 | #endif |
| 267 | return (char *)gl; |
| 268 | } |
| 269 | |
| 270 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 271 | static void load_regexes_from_file(llist_t *fopt) |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 272 | { |
| 273 | char *line; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 274 | FILE *f; |
| 275 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 276 | while (fopt) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 277 | llist_t *cur = fopt; |
| 278 | char *ffile = cur->data; |
| 279 | |
| 280 | fopt = cur->link; |
| 281 | free(cur); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 282 | f = xfopen(ffile, "r"); |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 283 | while ((line = bb_get_chomped_line_from_file(f)) != NULL) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 284 | llist_add_to(&pattern_head, |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 285 | new_grep_list_data(line, PATTERN_MEM_A)); |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 286 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 289 | |
| 290 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 291 | int grep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 292 | { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 293 | FILE *file; |
| 294 | int matched; |
Eric Andersen | 31c27a9 | 2004-10-08 08:10:57 +0000 | [diff] [blame] | 295 | llist_t *fopt = NULL; |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 296 | int error_open_count = 0; |
Matt Kraai | 999623e | 2001-10-29 15:49:03 +0000 | [diff] [blame] | 297 | |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 298 | /* do normal option parsing */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 299 | #if ENABLE_FEATURE_GREP_CONTEXT |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 300 | char *junk; |
| 301 | char *slines_after; |
| 302 | char *slines_before; |
| 303 | char *Copt; |
| 304 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 305 | opt_complementary = "H-h:e::f::C-AB"; |
| 306 | opt = getopt32(argc, argv, |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 307 | GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, |
| 308 | &pattern_head, &fopt, |
| 309 | &slines_after, &slines_before, &Copt); |
| 310 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 311 | if (opt & GREP_OPT_C) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 312 | /* C option unseted A and B options, but next -A or -B |
| 313 | may be ovewrite own option */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 314 | if (!(opt & GREP_OPT_A)) /* not overwtited */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 315 | slines_after = Copt; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 316 | if (!(opt & GREP_OPT_B)) /* not overwtited */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 317 | slines_before = Copt; |
| 318 | opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */ |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 319 | } |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 320 | if (opt & GREP_OPT_A) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 321 | lines_after = strtoul(slines_after, &junk, 10); |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 322 | if (*junk != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 323 | bb_error_msg_and_die(bb_msg_invalid_arg, slines_after, "-A"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 324 | } |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 325 | if (opt & GREP_OPT_B) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 326 | lines_before = strtoul(slines_before, &junk, 10); |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 327 | if (*junk != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 328 | bb_error_msg_and_die(bb_msg_invalid_arg, slines_before, "-B"); |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 329 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 330 | /* sanity checks after parse may be invalid numbers ;-) */ |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 331 | if (opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 332 | opt &= ~GREP_OPT_n; |
| 333 | lines_before = 0; |
| 334 | lines_after = 0; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 335 | } else if (lines_before > 0) |
Rob Landley | 081e384 | 2006-08-03 20:07:35 +0000 | [diff] [blame] | 336 | before_buf = (char **)xzalloc(lines_before * sizeof(char *)); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 337 | #else |
| 338 | /* with auto sanity checks */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 339 | opt_complementary = "H-h:e::f::c-n:q-n:l-n"; |
| 340 | opt = getopt32(argc, argv, GREP_OPTS OPT_EGREP, |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 341 | &pattern_head, &fopt); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 342 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 343 | invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */ |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 344 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 345 | if (opt & GREP_OPT_H) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 346 | print_filename++; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 347 | if (opt & GREP_OPT_h) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 348 | print_filename--; |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 349 | if (pattern_head != NULL) { |
| 350 | /* convert char *argv[] to grep_list_data_t */ |
| 351 | llist_t *cur; |
| 352 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 353 | for (cur = pattern_head; cur; cur = cur->link) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 354 | cur->data = new_grep_list_data(cur->data, 0); |
| 355 | } |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 356 | if (opt & GREP_OPT_f) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 357 | load_regexes_from_file(fopt); |
| 358 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame^] | 359 | if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 360 | opt |= GREP_OPT_F; |
Mike Frysinger | 15ca586 | 2005-07-31 22:41:05 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 362 | if (!(opt & GREP_OPT_o)) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 363 | reflags = REG_NOSUB; |
| 364 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 365 | if (ENABLE_FEATURE_GREP_EGREP_ALIAS && |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame^] | 366 | (applet_name[0] == 'e' || (opt & GREP_OPT_E))) |
Denis Vlasenko | 5193753 | 2006-09-29 20:58:53 +0000 | [diff] [blame] | 367 | reflags |= REG_EXTENDED; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 368 | |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 369 | if (opt & GREP_OPT_i) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 370 | reflags |= REG_ICASE; |
| 371 | |
| 372 | argv += optind; |
| 373 | argc -= optind; |
Eric Andersen | 053b146 | 2000-06-13 06:24:53 +0000 | [diff] [blame] | 374 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 375 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 376 | * argv[optind] should be the pattern. no pattern, no worky */ |
Glenn L McGrath | 26df70a | 2003-04-27 01:50:57 +0000 | [diff] [blame] | 377 | if (pattern_head == NULL) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 378 | if (*argv == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 379 | bb_show_usage(); |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 380 | else { |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 381 | char *pattern = new_grep_list_data(*argv++, 0); |
| 382 | |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 383 | llist_add_to(&pattern_head, pattern); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 384 | argc--; |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 387 | |
Mark Whitley | fa43e54 | 2001-05-24 18:36:18 +0000 | [diff] [blame] | 388 | /* argv[(optind)..(argc-1)] should be names of file to grep through. If |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 389 | * there is more than one file to grep, we will print the filenames */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 390 | if (argc > 1) { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 391 | print_filename++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 392 | |
Mark Whitley | 2e1148b | 2000-06-28 22:59:30 +0000 | [diff] [blame] | 393 | /* If no files were specified, or '-' was specified, take input from |
| 394 | * stdin. Otherwise, we grep through all the files specified. */ |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 395 | } else if (argc == 0) { |
| 396 | argc++; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 397 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 398 | matched = 0; |
| 399 | while (argc--) { |
| 400 | cur_file = *argv++; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 401 | if (!cur_file || (*cur_file == '-' && !cur_file[1])) { |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 402 | cur_file = "(standard input)"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 403 | file = stdin; |
| 404 | } else { |
Mark Whitley | d372189 | 2000-06-28 22:00:26 +0000 | [diff] [blame] | 405 | file = fopen(cur_file, "r"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 406 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 407 | if (file == NULL) { |
| 408 | if (!SUPPRESS_ERR_MSGS) |
| 409 | bb_perror_msg("%s", cur_file); |
| 410 | error_open_count++; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 411 | } else { |
| 412 | matched += grep_file(file); |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 413 | if (matched < 0) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 414 | /* we found a match but were told to be quiet, stop here and |
| 415 | * return success */ |
| 416 | break; |
Mark Whitley | 2ef880b | 2000-07-18 21:02:06 +0000 | [diff] [blame] | 417 | } |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 418 | fclose(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 419 | } |
"Vladimir N. Oleynik" | cf40d81 | 2005-09-23 13:23:15 +0000 | [diff] [blame] | 420 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 421 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 422 | /* destroy all the elments in the pattern list */ |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 423 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 424 | while (pattern_head) { |
| 425 | llist_t *pattern_head_ptr = pattern_head; |
| 426 | grep_list_data_t *gl = |
| 427 | (grep_list_data_t *)pattern_head_ptr->data; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 428 | |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 429 | pattern_head = pattern_head->link; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 430 | if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 431 | free(gl->pattern); |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 432 | if ((gl->flg_mem_alocated_compiled & COMPILED)) |
"Vladimir N. Oleynik" | 716bbe9 | 2006-02-28 10:10:19 +0000 | [diff] [blame] | 433 | regfree(&(gl->preg)); |
| 434 | free(pattern_head_ptr); |
| 435 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 436 | } |
Bernhard Reutner-Fischer | aaf0e23 | 2005-09-23 15:38:49 +0000 | [diff] [blame] | 437 | /* 0 = success, 1 = failed, 2 = error */ |
| 438 | /* If the -q option is specified, the exit status shall be zero |
| 439 | * if an input line is selected, even if an error was detected. */ |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 440 | if (BE_QUIET && matched) |
"Vladimir N. Oleynik" | bf44974 | 2005-09-23 13:50:24 +0000 | [diff] [blame] | 441 | return 0; |
Denis Vlasenko | 6c30db8 | 2006-09-29 21:04:12 +0000 | [diff] [blame] | 442 | if (error_open_count) |
"Vladimir N. Oleynik" | bf44974 | 2005-09-23 13:50:24 +0000 | [diff] [blame] | 443 | return 2; |
Mark Whitley | b5c2985 | 2001-02-01 21:02:41 +0000 | [diff] [blame] | 444 | return !matched; /* invert return value 0 = success, 1 = failed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 445 | } |