Deleted Added
full compact
grep.c (210461) grep.c (210578)
1/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210461 2010-07-25 08:42:18Z gabor $");
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210578 2010-07-29 00:11:14Z gabor $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>

--- 40 unchanged lines hidden (view full) ---

80
81/* Searching patterns */
82unsigned int patterns, pattern_sz;
83char **pattern;
84regex_t *r_pattern;
85fastgrep_t *fg_pattern;
86
87/* Filename exclusion/inclusion patterns */
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>

--- 40 unchanged lines hidden (view full) ---

80
81/* Searching patterns */
82unsigned int patterns, pattern_sz;
83char **pattern;
84regex_t *r_pattern;
85fastgrep_t *fg_pattern;
86
87/* Filename exclusion/inclusion patterns */
88unsigned int epatterns, epattern_sz;
89struct epat *epattern;
88unsigned int fpatterns, fpattern_sz;
89unsigned int dpatterns, dpattern_sz;
90struct epat *dpattern, *fpattern;
90
91/* For regex errors */
92char re_error[RE_ERROR_BUF + 1];
93
94/* Command-line flags */
95unsigned long long Aflag; /* -A x: print x lines trailing each match */
96unsigned long long Bflag; /* -B x: print x lines leading each match */
97bool Hflag; /* -H: always print file name */

--- 9 unchanged lines hidden (view full) ---

107bool oflag; /* -o: print only matching part */
108bool qflag; /* -q: quiet mode (don't output anything) */
109bool sflag; /* -s: silent mode (ignore errors) */
110bool vflag; /* -v: only show non-matching lines */
111bool wflag; /* -w: pattern must start and end on word boundaries */
112bool xflag; /* -x: pattern must match entire line */
113bool lbflag; /* --line-buffered */
114bool nullflag; /* --null */
91
92/* For regex errors */
93char re_error[RE_ERROR_BUF + 1];
94
95/* Command-line flags */
96unsigned long long Aflag; /* -A x: print x lines trailing each match */
97unsigned long long Bflag; /* -B x: print x lines leading each match */
98bool Hflag; /* -H: always print file name */

--- 9 unchanged lines hidden (view full) ---

108bool oflag; /* -o: print only matching part */
109bool qflag; /* -q: quiet mode (don't output anything) */
110bool sflag; /* -s: silent mode (ignore errors) */
111bool vflag; /* -v: only show non-matching lines */
112bool wflag; /* -w: pattern must start and end on word boundaries */
113bool xflag; /* -x: pattern must match entire line */
114bool lbflag; /* --line-buffered */
115bool nullflag; /* --null */
115bool exclflag; /* --exclude */
116char *label; /* --label */
117const char *color; /* --color */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
121int devbehave = DEV_READ; /* -D: handling of devices */
122int dirbehave = DIR_READ; /* -dRr: handling of directories */
123int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
124
116char *label; /* --label */
117const char *color; /* --color */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
121int devbehave = DEV_READ; /* -D: handling of devices */
122int dirbehave = DIR_READ; /* -dRr: handling of directories */
123int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
124
125bool dexclude, dinclude; /* --exclude amd --include */
126bool fexclude, finclude; /* --exclude-dir and --include-dir */
127
125enum {
126 BIN_OPT = CHAR_MAX + 1,
127 COLOR_OPT,
128 HELP_OPT,
129 MMAP_OPT,
130 LINEBUF_OPT,
131 LABEL_OPT,
132 NULL_OPT,

--- 96 unchanged lines hidden (view full) ---

229 pattern_sz *= 2;
230 pattern = grep_realloc(pattern, ++pattern_sz *
231 sizeof(*pattern));
232 }
233 if (len > 0 && pat[len - 1] == '\n')
234 --len;
235 /* pat may not be NUL-terminated */
236 pattern[patterns] = grep_malloc(len + 1);
128enum {
129 BIN_OPT = CHAR_MAX + 1,
130 COLOR_OPT,
131 HELP_OPT,
132 MMAP_OPT,
133 LINEBUF_OPT,
134 LABEL_OPT,
135 NULL_OPT,

--- 96 unchanged lines hidden (view full) ---

232 pattern_sz *= 2;
233 pattern = grep_realloc(pattern, ++pattern_sz *
234 sizeof(*pattern));
235 }
236 if (len > 0 && pat[len - 1] == '\n')
237 --len;
238 /* pat may not be NUL-terminated */
239 pattern[patterns] = grep_malloc(len + 1);
237 memcpy(pattern[patterns], pat, len);
238 pattern[patterns][len] = '\0';
240 strlcpy(pattern[patterns], pat, len + 1);
239 ++patterns;
240}
241
242/*
241 ++patterns;
242}
243
244/*
243 * Adds an include/exclude pattern to the internal array.
245 * Adds a file include/exclude pattern to the internal array.
244 */
245static void
246 */
247static void
246add_epattern(char *pat, size_t len, int type, int mode)
248add_fpattern(const char *pat, int mode)
247{
248
249 /* Increase size if necessary */
249{
250
251 /* Increase size if necessary */
250 if (epatterns == epattern_sz) {
251 epattern_sz *= 2;
252 epattern = grep_realloc(epattern, ++epattern_sz *
252 if (fpatterns == fpattern_sz) {
253 fpattern_sz *= 2;
254 fpattern = grep_realloc(fpattern, ++fpattern_sz *
253 sizeof(struct epat));
254 }
255 sizeof(struct epat));
256 }
255 if (len > 0 && pat[len - 1] == '\n')
256 --len;
257 epattern[epatterns].pat = grep_malloc(len + 1);
258 memcpy(epattern[epatterns].pat, pat, len);
259 epattern[epatterns].pat[len] = '\0';
260 epattern[epatterns].type = type;
261 epattern[epatterns].mode = mode;
262 ++epatterns;
257 fpattern[fpatterns].pat = grep_strdup(pat);
258 fpattern[fpatterns].mode = mode;
259 ++fpatterns;
263}
264
265/*
260}
261
262/*
263 * Adds a directory include/exclude pattern to the internal array.
264 */
265static void
266add_dpattern(const char *pat, int mode)
267{
268
269 /* Increase size if necessary */
270 if (dpatterns == dpattern_sz) {
271 dpattern_sz *= 2;
272 dpattern = grep_realloc(dpattern, ++dpattern_sz *
273 sizeof(struct epat));
274 }
275 dpattern[dpatterns].pat = grep_strdup(pat);
276 dpattern[dpatterns].mode = mode;
277 ++dpatterns;
278}
279
280/*
266 * Reads searching patterns from a file and adds them with add_pattern().
267 */
268static void
269read_patterns(const char *fn)
270{
271 FILE *f;
272 char *line;
273 size_t len;

--- 312 unchanged lines hidden (view full) ---

586 break;
587 case LINEBUF_OPT:
588 lbflag = true;
589 break;
590 case NULL_OPT:
591 nullflag = true;
592 break;
593 case R_INCLUDE_OPT:
281 * Reads searching patterns from a file and adds them with add_pattern().
282 */
283static void
284read_patterns(const char *fn)
285{
286 FILE *f;
287 char *line;
288 size_t len;

--- 312 unchanged lines hidden (view full) ---

601 break;
602 case LINEBUF_OPT:
603 lbflag = true;
604 break;
605 case NULL_OPT:
606 nullflag = true;
607 break;
608 case R_INCLUDE_OPT:
594 exclflag = true;
595 add_epattern(basename(optarg), strlen(basename(optarg)),
596 FILE_PAT, INCL_PAT);
609 finclude = true;
610 add_fpattern(optarg, INCL_PAT);
597 break;
598 case R_EXCLUDE_OPT:
611 break;
612 case R_EXCLUDE_OPT:
599 exclflag = true;
600 add_epattern(basename(optarg), strlen(basename(optarg)),
601 FILE_PAT, EXCL_PAT);
613 fexclude = true;
614 add_fpattern(optarg, EXCL_PAT);
602 break;
603 case R_DINCLUDE_OPT:
615 break;
616 case R_DINCLUDE_OPT:
604 exclflag = true;
605 add_epattern(basename(optarg), strlen(basename(optarg)),
606 DIR_PAT, INCL_PAT);
617 dexclude = true;
618 add_dpattern(optarg, INCL_PAT);
607 break;
608 case R_DEXCLUDE_OPT:
619 break;
620 case R_DEXCLUDE_OPT:
609 exclflag = true;
610 add_epattern(basename(optarg), strlen(basename(optarg)),
611 DIR_PAT, EXCL_PAT);
621 dinclude = true;
622 add_dpattern(optarg, EXCL_PAT);
612 break;
613 case HELP_OPT:
614 default:
615 usage();
616 }
617 lastc = c;
618 newarg = optind != prevoptind;
619 prevoptind = optind;

--- 55 unchanged lines hidden (view full) ---

675 hflag = true;
676
677 if (aargc == 0)
678 exit(!procfile("-"));
679
680 if (dirbehave == DIR_RECURSE)
681 c = grep_tree(aargv);
682 else
623 break;
624 case HELP_OPT:
625 default:
626 usage();
627 }
628 lastc = c;
629 newarg = optind != prevoptind;
630 prevoptind = optind;

--- 55 unchanged lines hidden (view full) ---

686 hflag = true;
687
688 if (aargc == 0)
689 exit(!procfile("-"));
690
691 if (dirbehave == DIR_RECURSE)
692 c = grep_tree(aargv);
693 else
683 for (c = 0; aargc--; ++aargv)
694 for (c = 0; aargc--; ++aargv) {
695 if ((finclude || fexclude) && !file_matching(*aargv))
696 continue;
684 c+= procfile(*aargv);
697 c+= procfile(*aargv);
698 }
685
686#ifndef WITHOUT_NLS
687 catclose(catalog);
688#endif
689
690 /* Find out the correct return value according to the
691 results and the command line option. */
692 exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
693}
699
700#ifndef WITHOUT_NLS
701 catclose(catalog);
702#endif
703
704 /* Find out the correct return value according to the
705 results and the command line option. */
706 exit(c ? (notfound ? (qflag ? 0 : 2) : 0) : (notfound ? 2 : 1));
707}