xref: /illumos-gate/usr/src/cmd/grep/grep.c (revision dbe930bf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23d2d52addSAlexander Pyhalov  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
2741599e9fSDamian Bogel /*
28d2d52addSAlexander Pyhalov  * grep - pattern matching program - combined grep, egrep, and fgrep.
29d2d52addSAlexander Pyhalov  *	Based on MKS grep command, with XCU & Solaris mods.
3041599e9fSDamian Bogel  */
3141599e9fSDamian Bogel 
327c478bd9Sstevel@tonic-gate /*
33d2d52addSAlexander Pyhalov  * Copyright 1985, 1992 by Mortice Kern Systems Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
37d2d52addSAlexander Pyhalov /*
384adc6f15SPeter Tribble  * Copyright 2020 Peter Tribble.
39d9241f99SAndrew Stormont  * Copyright 2018 RackTop Systems.
40925beec7SYuri Pankov  * Copyright 2018 Nexenta Systems, Inc.
41d2d52addSAlexander Pyhalov  * Copyright 2013 Damian Bogel. All rights reserved.
422e5ac464SRobert Mustacchi  * Copyright 2020 Oxide Computer Company
43d2d52addSAlexander Pyhalov  */
44d2d52addSAlexander Pyhalov 
45d2d52addSAlexander Pyhalov #include <string.h>
46d2d52addSAlexander Pyhalov #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
48d2d52addSAlexander Pyhalov #include <stdarg.h>
49d2d52addSAlexander Pyhalov #include <regex.h>
50d2d52addSAlexander Pyhalov #include <limits.h>
51d2d52addSAlexander Pyhalov #include <sys/types.h>
52d2d52addSAlexander Pyhalov #include <sys/stat.h>
537c478bd9Sstevel@tonic-gate #include <fcntl.h>
547c478bd9Sstevel@tonic-gate #include <stdio.h>
55d2d52addSAlexander Pyhalov #include <locale.h>
56d2d52addSAlexander Pyhalov #include <wchar.h>
57d2d52addSAlexander Pyhalov #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <unistd.h>
59d2d52addSAlexander Pyhalov #include <wctype.h>
60e52fb54bSAlexander Eremin #include <ftw.h>
61e52fb54bSAlexander Eremin #include <sys/param.h>
628ccd0217SRobert Mustacchi #include <getopt.h>
64d2d52addSAlexander Pyhalov #define	STDIN_FILENAME gettext("(standard input)")
65d2d52addSAlexander Pyhalov 
66d2d52addSAlexander Pyhalov #define	BSIZE		512		/* Size of block for -b */
67d2d52addSAlexander Pyhalov #define	BUFSIZE		8192		/* Input buffer size */
68d2d52addSAlexander Pyhalov #define	MAX_DEPTH	1000		/* how deep to recurse */
69d2d52addSAlexander Pyhalov 
704adc6f15SPeter Tribble #define	AFTER	1			/* 'After' Context */
714adc6f15SPeter Tribble #define	BEFORE	2			/* 'Before' Context */
72d2d52addSAlexander Pyhalov #define	CONTEXT	(AFTER|BEFORE)		/* Full Context */
73d2d52addSAlexander Pyhalov 
74d2d52addSAlexander Pyhalov #define	M_CSETSIZE	256		/* singlebyte chars */
75d2d52addSAlexander Pyhalov static int	bmglen;			/* length of BMG pattern */
76d2d52addSAlexander Pyhalov static char	*bmgpat;		/* BMG pattern */
77d2d52addSAlexander Pyhalov static int	bmgtab[M_CSETSIZE];	/* BMG delta1 table */
78d2d52addSAlexander Pyhalov 
79d2d52addSAlexander Pyhalov typedef	struct	_PATTERN	{
80d2d52addSAlexander Pyhalov 	char	*pattern;		/* original pattern */
81d2d52addSAlexander Pyhalov 	struct	_PATTERN	*next;
82d2d52addSAlexander Pyhalov 	regex_t	re;			/* compiled pattern */
83d2d52addSAlexander Pyhalov } PATTERN;
84d2d52addSAlexander Pyhalov 
85d2d52addSAlexander Pyhalov static PATTERN	*patterns;
86d2d52addSAlexander Pyhalov static char	errstr[128];		/* regerror string buffer */
87d2d52addSAlexander Pyhalov static int	regflags = 0;		/* regcomp options */
88d2d52addSAlexander Pyhalov static int	matched = 0;		/* return of the grep() */
89d2d52addSAlexander Pyhalov static int	errors = 0;		/* count of errors */
90d2d52addSAlexander Pyhalov static uchar_t	fgrep = 0;		/* Invoked as fgrep */
91d2d52addSAlexander Pyhalov static uchar_t	egrep = 0;		/* Invoked as egrep */
92d2d52addSAlexander Pyhalov static boolean_t	nvflag = B_TRUE;	/* Print matching lines */
93d2d52addSAlexander Pyhalov static uchar_t	cflag;			/* Count of matches */
94d2d52addSAlexander Pyhalov static uchar_t	iflag;			/* Case insensitve matching */
95d2d52addSAlexander Pyhalov static uchar_t	Hflag;			/* Precede lines by file name */
964adc6f15SPeter Tribble static uchar_t	hflag;			/* Suppress printing of filename */
97d2d52addSAlexander Pyhalov static uchar_t	lflag;			/* Print file names of matches */
982e5ac464SRobert Mustacchi static uchar_t	Lflag;			/* Print file names of non-matches */
99d2d52addSAlexander Pyhalov static uchar_t	nflag;			/* Precede lines by line number */
100d2d52addSAlexander Pyhalov static uchar_t	rflag;			/* Search directories recursively */
1014adc6f15SPeter Tribble static uchar_t	bflag;			/* Precede matches by block number */
102d2d52addSAlexander Pyhalov static uchar_t	sflag;			/* Suppress file error messages */
103d2d52addSAlexander Pyhalov static uchar_t	qflag;			/* Suppress standard output */
104d2d52addSAlexander Pyhalov static uchar_t	wflag;			/* Search for expression as a word */
105d2d52addSAlexander Pyhalov static uchar_t	xflag;			/* Anchoring */
106d2d52addSAlexander Pyhalov static uchar_t	Eflag;			/* Egrep or -E flag */
107d2d52addSAlexander Pyhalov static uchar_t	Fflag;			/* Fgrep or -F flag */
108d2d52addSAlexander Pyhalov static uchar_t	Rflag;			/* Like rflag, but follow symlinks */
109d2d52addSAlexander Pyhalov static uchar_t	outfn;			/* Put out file name */
110d2d52addSAlexander Pyhalov static uchar_t	conflag;		/* show context of matches */
11181dd18d8SRobert Mustacchi static uchar_t	oflag;			/* Print only matching output */
112d2d52addSAlexander Pyhalov static char	*cmdname;
1138ccd0217SRobert Mustacchi static char	*stdin_label;		/* Optional lable for stdin */
114d2d52addSAlexander Pyhalov 
11581dd18d8SRobert Mustacchi static int	use_bmg, mblocale;
116d2d52addSAlexander Pyhalov 
11781dd18d8SRobert Mustacchi static size_t	prntbuflen, conbuflen;
1184adc6f15SPeter Tribble static unsigned long	conalen, conblen, conmatches;
119d2d52addSAlexander Pyhalov static char	*prntbuf, *conbuf;
120d2d52addSAlexander Pyhalov 
121d2d52addSAlexander Pyhalov static void	addfile(const char *fn);
122d2d52addSAlexander Pyhalov static void	addpattern(char *s);
123d2d52addSAlexander Pyhalov static void	fixpatterns(void);
124d2d52addSAlexander Pyhalov static void	usage(void);
125d2d52addSAlexander Pyhalov static int	grep(int, const char *);
126d2d52addSAlexander Pyhalov static void	bmgcomp(char *, int);
127d2d52addSAlexander Pyhalov static char	*bmgexec(char *, char *);
128e52fb54bSAlexander Eremin static int	recursive(const char *, const struct stat *, int, struct FTW *);
129d2d52addSAlexander Pyhalov static void	process_path(const char *);
130d2d52addSAlexander Pyhalov static void	process_file(const char *, int);
1328ccd0217SRobert Mustacchi /*
1338ccd0217SRobert Mustacchi  * These are values that we use to return from getopt_long. They start at
1348ccd0217SRobert Mustacchi  * SHRT_MAX to avoid any possible conflict with the normal options. These are
1358ccd0217SRobert Mustacchi  * used for long options that have no short option equivalent.
1368ccd0217SRobert Mustacchi  */
1378ccd0217SRobert Mustacchi enum grep_opts {
1388ccd0217SRobert Mustacchi 	OPT_LABEL = SHRT_MAX + 1
1398ccd0217SRobert Mustacchi };
1408ccd0217SRobert Mustacchi 
1418ccd0217SRobert Mustacchi static struct option grep_options[] = {
1428ccd0217SRobert Mustacchi 	{ "label", required_argument, NULL, OPT_LABEL },
1438ccd0217SRobert Mustacchi 	{ NULL }
1448ccd0217SRobert Mustacchi };
1458ccd0217SRobert Mustacchi 
146d2d52addSAlexander Pyhalov /*
147d2d52addSAlexander Pyhalov  * mainline for grep
148d2d52addSAlexander Pyhalov  */
1497c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)15055f91622Sceastha main(int argc, char **argv)
1517c478bd9Sstevel@tonic-gate {
152d2d52addSAlexander Pyhalov 	char	*ap, *test;
1537c478bd9Sstevel@tonic-gate 	int	c;
154d2d52addSAlexander Pyhalov 	int	fflag = 0;
155d2d52addSAlexander Pyhalov 	int	i, n_pattern = 0, n_file = 0;
156d2d52addSAlexander Pyhalov 	char	**pattern_list = NULL;
157d2d52addSAlexander Pyhalov 	char	**file_list = NULL;
1597c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1607c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
161d2d52addSAlexander Pyhalov #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
165d2d52addSAlexander Pyhalov 	/*
166d2d52addSAlexander Pyhalov 	 * true if this is running on the multibyte locale
167d2d52addSAlexander Pyhalov 	 */
168d2d52addSAlexander Pyhalov 	mblocale = (MB_CUR_MAX > 1);
169d2d52addSAlexander Pyhalov 	/*
170d2d52addSAlexander Pyhalov 	 * Skip leading slashes
171d2d52addSAlexander Pyhalov 	 */
172d2d52addSAlexander Pyhalov 	cmdname = argv[0];
173d2d52addSAlexander Pyhalov 	if (ap = strrchr(cmdname, '/'))
174d2d52addSAlexander Pyhalov 		cmdname = ap + 1;
175d2d52addSAlexander Pyhalov 
176d2d52addSAlexander Pyhalov 	ap = cmdname;
177d2d52addSAlexander Pyhalov 	/*
178d2d52addSAlexander Pyhalov 	 * Detect egrep/fgrep via command name, map to -E and -F options.
179d2d52addSAlexander Pyhalov 	 */
180d2d52addSAlexander Pyhalov 	if (*ap == 'e' || *ap == 'E') {
181d2d52addSAlexander Pyhalov 		regflags |= REG_EXTENDED;
182d2d52addSAlexander Pyhalov 		egrep++;
183d2d52addSAlexander Pyhalov 	} else {
184d2d52addSAlexander Pyhalov 		if (*ap == 'f' || *ap == 'F') {
185d2d52addSAlexander Pyhalov 			fgrep++;
18681dd18d8SRobert Mustacchi 			regflags |= REG_NOSPEC;
187d2d52addSAlexander Pyhalov 		}
188d2d52addSAlexander Pyhalov 	}
189d2d52addSAlexander Pyhalov 
190d2d52addSAlexander Pyhalov 	/* check for non-standard "-line-count" option */
191d2d52addSAlexander Pyhalov 	for (i = 1; i < argc; i++) {
192d2d52addSAlexander Pyhalov 		if (strcmp(argv[i], "--") == 0)
19341599e9fSDamian Bogel 			break;
194d2d52addSAlexander Pyhalov 
195d2d52addSAlexander Pyhalov 		/* isdigit() check prevents negative arguments */
196d2d52addSAlexander Pyhalov 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
197d2d52addSAlexander Pyhalov 			if (strlen(&argv[i][1]) !=
198d2d52addSAlexander Pyhalov 			    strspn(&argv[i][1], "0123456789")) {
199d2d52addSAlexander Pyhalov 				(void) fprintf(stderr, gettext(
200d2d52addSAlexander Pyhalov 				    "%s: Bad number flag\n"), argv[0]);
201d2d52addSAlexander Pyhalov 				usage();
202d2d52addSAlexander Pyhalov 			}
203d2d52addSAlexander Pyhalov 
204d2d52addSAlexander Pyhalov 			errno = 0;
205d2d52addSAlexander Pyhalov 			conalen = conblen = strtoul(&argv[i][1], (char **)NULL,
206d2d52addSAlexander Pyhalov 			    10);
207d2d52addSAlexander Pyhalov 
208d2d52addSAlexander Pyhalov 			if (errno != 0 || conalen >= ULONG_MAX) {
209d2d52addSAlexander Pyhalov 				(void) fprintf(stderr, gettext(
210d2d52addSAlexander Pyhalov 				    "%s: Bad context argument\n"), argv[0]);
211d2d52addSAlexander Pyhalov 			} else if (conalen)
212d2d52addSAlexander Pyhalov 				conflag = CONTEXT;
213d2d52addSAlexander Pyhalov 
214d2d52addSAlexander Pyhalov 			while (i < argc) {
215d2d52addSAlexander Pyhalov 				argv[i] = argv[i + 1];
216d2d52addSAlexander Pyhalov 				i++;
217d2d52addSAlexander Pyhalov 			}
218d2d52addSAlexander Pyhalov 			argc--;
219d2d52addSAlexander Pyhalov 		}
220d2d52addSAlexander Pyhalov 	}
221d2d52addSAlexander Pyhalov 
22281dd18d8SRobert Mustacchi 	while ((c = getopt_long(argc, argv, "+vwchHilLnrbse:f:qxEFIRA:B:C:o",
2238ccd0217SRobert Mustacchi 	    grep_options, NULL)) != EOF) {
224d2d52addSAlexander Pyhalov 		unsigned long tval;
225d2d52addSAlexander Pyhalov 		switch (c) {
226d2d52addSAlexander Pyhalov 		case 'v':	/* POSIX: negate matches */
227d2d52addSAlexander Pyhalov 			nvflag = B_FALSE;
2287c478bd9Sstevel@tonic-gate 			break;
229d2d52addSAlexander Pyhalov 
230d2d52addSAlexander Pyhalov 		case 'c':	/* POSIX: write count */
231d2d52addSAlexander Pyhalov 			cflag++;
2323ed621bcSAlexander Eremin 			break;
233d2d52addSAlexander Pyhalov 
234d2d52addSAlexander Pyhalov 		case 'i':	/* POSIX: ignore case */
235d2d52addSAlexander Pyhalov 			iflag++;
236d2d52addSAlexander Pyhalov 			regflags |= REG_ICASE;
2377c478bd9Sstevel@tonic-gate 			break;
238d2d52addSAlexander Pyhalov 
2392e5ac464SRobert Mustacchi 		/*
2402e5ac464SRobert Mustacchi 		 * The last of -l and -L are honored.
2412e5ac464SRobert Mustacchi 		 */
242d2d52addSAlexander Pyhalov 		case 'l':	/* POSIX: Write filenames only */
243d2d52addSAlexander Pyhalov 			lflag++;
2442e5ac464SRobert Mustacchi 			Lflag = 0;
2452e5ac464SRobert Mustacchi 			break;
2462e5ac464SRobert Mustacchi 
2472e5ac464SRobert Mustacchi 		case 'L':	/* Write non-matching filenames */
2482e5ac464SRobert Mustacchi 			Lflag++;
2492e5ac464SRobert Mustacchi 			lflag = 0;
2507c478bd9Sstevel@tonic-gate 			break;
251d2d52addSAlexander Pyhalov 
252d2d52addSAlexander Pyhalov 		case 'n':	/* POSIX: Write line numbers */
2537c478bd9Sstevel@tonic-gate 			nflag++;
2547c478bd9Sstevel@tonic-gate 			break;
255d2d52addSAlexander Pyhalov 
256d2d52addSAlexander Pyhalov 		case 'r':	/* Solaris: search recursively */
257e52fb54bSAlexander Eremin 			rflag++;
258e52fb54bSAlexander Eremin 			break;
259d2d52addSAlexander Pyhalov 
260d2d52addSAlexander Pyhalov 		case 'b':	/* Solaris: Write file block numbers */
2617c478bd9Sstevel@tonic-gate 			bflag++;
2627c478bd9Sstevel@tonic-gate 			break;
263d2d52addSAlexander Pyhalov 
264d2d52addSAlexander Pyhalov 		case 's':	/* POSIX: No error msgs for files */
2657c478bd9Sstevel@tonic-gate 			sflag++;
2667c478bd9Sstevel@tonic-gate 			break;
267d2d52addSAlexander Pyhalov 
268d2d52addSAlexander Pyhalov 		case 'e':	/* POSIX: pattern list */
269d2d52addSAlexander Pyhalov 			n_pattern++;
270d2d52addSAlexander Pyhalov 			pattern_list = realloc(pattern_list,
271d2d52addSAlexander Pyhalov 			    sizeof (char *) * n_pattern);
272d2d52addSAlexander Pyhalov 			if (pattern_list == NULL) {
273d2d52addSAlexander Pyhalov 				(void) fprintf(stderr,
274d2d52addSAlexander Pyhalov 				    gettext("%s: out of memory\n"),
275d2d52addSAlexander Pyhalov 				    cmdname);
276d2d52addSAlexander Pyhalov 				exit(2);
277d2d52addSAlexander Pyhalov 			}
278d2d52addSAlexander Pyhalov 			*(pattern_list + n_pattern - 1) = optarg;
2797c478bd9Sstevel@tonic-gate 			break;
280d2d52addSAlexander Pyhalov 
281d2d52addSAlexander Pyhalov 		case 'f':	/* POSIX: pattern file */
282d2d52addSAlexander Pyhalov 			fflag = 1;
283d2d52addSAlexander Pyhalov 			n_file++;
284d2d52addSAlexander Pyhalov 			file_list = realloc(file_list,
285d2d52addSAlexander Pyhalov 			    sizeof (char *) * n_file);
286d2d52addSAlexander Pyhalov 			if (file_list == NULL) {
287d2d52addSAlexander Pyhalov 				(void) fprintf(stderr,
288d2d52addSAlexander Pyhalov 				    gettext("%s: out of memory\n"),
289d2d52addSAlexander Pyhalov 				    cmdname);
290d2d52addSAlexander Pyhalov 				exit(2);
291d2d52addSAlexander Pyhalov 			}
292d2d52addSAlexander Pyhalov 			*(file_list + n_file - 1) = optarg;
2937c478bd9Sstevel@tonic-gate 			break;
294d2d52addSAlexander Pyhalov 
295d2d52addSAlexander Pyhalov 		/* based on options order h or H is set as in GNU grep */
2964adc6f15SPeter Tribble 		case 'h':	/* Solaris: suppress printing of file name */
297d2d52addSAlexander Pyhalov 			hflag = 1;
298d2d52addSAlexander Pyhalov 			Hflag = 0;
299d2d52addSAlexander Pyhalov 			break;
3004adc6f15SPeter Tribble 		/* Solaris: precede every match with file name */
301d2d52addSAlexander Pyhalov 		case 'H':
302d2d52addSAlexander Pyhalov 			Hflag = 1;
303d2d52addSAlexander Pyhalov 			hflag = 0;
304d2d52addSAlexander Pyhalov 			break;
305d2d52addSAlexander Pyhalov 
306d2d52addSAlexander Pyhalov 		case 'q':	/* POSIX: quiet: status only */
307d2d52addSAlexander Pyhalov 			qflag++;
308d2d52addSAlexander Pyhalov 			break;
309d2d52addSAlexander Pyhalov 
310d2d52addSAlexander Pyhalov 		case 'w':	/* Solaris: treat pattern as word */
3117c478bd9Sstevel@tonic-gate 			wflag++;
3127c478bd9Sstevel@tonic-gate 			break;
313d2d52addSAlexander Pyhalov 
314d2d52addSAlexander Pyhalov 		case 'x':	/* POSIX: full line matches */
315d2d52addSAlexander Pyhalov 			xflag++;
316d2d52addSAlexander Pyhalov 			break;
317d2d52addSAlexander Pyhalov 
318d2d52addSAlexander Pyhalov 		case 'E':	/* POSIX: Extended RE's */
319d2d52addSAlexander Pyhalov 			regflags |= REG_EXTENDED;
320d2d52addSAlexander Pyhalov 			Eflag++;
321d2d52addSAlexander Pyhalov 			break;
322d2d52addSAlexander Pyhalov 
323d2d52addSAlexander Pyhalov 		case 'F':	/* POSIX: strings, not RE's */
324d2d52addSAlexander Pyhalov 			Fflag++;
32581dd18d8SRobert Mustacchi 			regflags |= REG_NOSPEC;
326d2d52addSAlexander Pyhalov 			break;
327d2d52addSAlexander Pyhalov 
328d2d52addSAlexander Pyhalov 		case 'R':	/* Solaris: like rflag, but follow symlinks */
329d2d52addSAlexander Pyhalov 			Rflag++;
330d2d52addSAlexander Pyhalov 			rflag++;
331d2d52addSAlexander Pyhalov 			break;
332d2d52addSAlexander Pyhalov 
333d2d52addSAlexander Pyhalov 		case 'A':	/* print N lines after each match */
334d2d52addSAlexander Pyhalov 			errno = 0;
335d2d52addSAlexander Pyhalov 			conalen = strtoul(optarg, &test, 10);
336d2d52addSAlexander Pyhalov 			/* *test will be non-null if optarg is negative */
337d2d52addSAlexander Pyhalov 			if (errno != 0 || *test != '\0' ||
338d2d52addSAlexander Pyhalov 			    conalen >= ULONG_MAX) {
339d2d52addSAlexander Pyhalov 				(void) fprintf(stderr, gettext(
340d2d52addSAlexander Pyhalov 				    "%s: Bad context argument: %s\n"),
341d2d52addSAlexander Pyhalov 				    argv[0], optarg);
342d2d52addSAlexander Pyhalov 				exit(2);
343d2d52addSAlexander Pyhalov 			}
344d2d52addSAlexander Pyhalov 			if (conalen)
345d2d52addSAlexander Pyhalov 				conflag |= AFTER;
346d2d52addSAlexander Pyhalov 			else
347d2d52addSAlexander Pyhalov 				conflag &= ~AFTER;
348d2d52addSAlexander Pyhalov 			break;
349d2d52addSAlexander Pyhalov 		case 'B':	/* print N lines before each match */
350d2d52addSAlexander Pyhalov 			errno = 0;
351d2d52addSAlexander Pyhalov 			conblen = strtoul(optarg, &test, 10);
352d2d52addSAlexander Pyhalov 			/* *test will be non-null if optarg is negative */
353d2d52addSAlexander Pyhalov 			if (errno != 0 || *test != '\0' ||
354d2d52addSAlexander Pyhalov 			    conblen >= ULONG_MAX) {
355d2d52addSAlexander Pyhalov 				(void) fprintf(stderr, gettext(
356d2d52addSAlexander Pyhalov 				    "%s: Bad context argument: %s\n"),
357d2d52addSAlexander Pyhalov 				    argv[0], optarg);
358d2d52addSAlexander Pyhalov 				exit(2);
359d2d52addSAlexander Pyhalov 			}
360d2d52addSAlexander Pyhalov 			if (conblen)
361d2d52addSAlexander Pyhalov 				conflag |= BEFORE;
362d2d52addSAlexander Pyhalov 			else
363d2d52addSAlexander Pyhalov 				conflag &= ~BEFORE;
364d2d52addSAlexander Pyhalov 			break;
365d2d52addSAlexander Pyhalov 		case 'C':	/* print N lines around each match */
366d2d52addSAlexander Pyhalov 			errno = 0;
367d2d52addSAlexander Pyhalov 			tval = strtoul(optarg, &test, 10);
368d2d52addSAlexander Pyhalov 			/* *test will be non-null if optarg is negative */
369d2d52addSAlexander Pyhalov 			if (errno != 0 || *test != '\0' || tval >= ULONG_MAX) {
370d2d52addSAlexander Pyhalov 				(void) fprintf(stderr, gettext(
371d2d52addSAlexander Pyhalov 				    "%s: Bad context argument: %s\n"),
372d2d52addSAlexander Pyhalov 				    argv[0], optarg);
373d2d52addSAlexander Pyhalov 				exit(2);
374d2d52addSAlexander Pyhalov 			}
375d2d52addSAlexander Pyhalov 			if (tval) {
376d2d52addSAlexander Pyhalov 				if ((conflag & BEFORE) == 0)
377d2d52addSAlexander Pyhalov 					conblen = tval;
378d2d52addSAlexander Pyhalov 				if ((conflag & AFTER) == 0)
379d2d52addSAlexander Pyhalov 					conalen = tval;
380d2d52addSAlexander Pyhalov 				conflag = CONTEXT;
381d2d52addSAlexander Pyhalov 			}
382d2d52addSAlexander Pyhalov 			break;
383d2d52addSAlexander Pyhalov 
3848ccd0217SRobert Mustacchi 		case OPT_LABEL:
3858ccd0217SRobert Mustacchi 			stdin_label = optarg;
3868ccd0217SRobert Mustacchi 			break;
3878ccd0217SRobert Mustacchi 
38881dd18d8SRobert Mustacchi 		case 'o':
38981dd18d8SRobert Mustacchi 			oflag++;
39081dd18d8SRobert Mustacchi 			break;
39181dd18d8SRobert Mustacchi 
392d2d52addSAlexander Pyhalov 		default:
393d2d52addSAlexander Pyhalov 			usage();
3947c478bd9Sstevel@tonic-gate 		}
395d2d52addSAlexander Pyhalov 	}
396d2d52addSAlexander Pyhalov 	/*
397d2d52addSAlexander Pyhalov 	 * If we're invoked as egrep or fgrep we need to do some checks
398d2d52addSAlexander Pyhalov 	 */
400d2d52addSAlexander Pyhalov 	if (egrep || fgrep) {
401d2d52addSAlexander Pyhalov 		/*
402d2d52addSAlexander Pyhalov 		 * Use of -E or -F with egrep or fgrep is illegal
403d2d52addSAlexander Pyhalov 		 */
404d2d52addSAlexander Pyhalov 		if (Eflag || Fflag)
405d2d52addSAlexander Pyhalov 			usage();
406d2d52addSAlexander Pyhalov 		/*
407d2d52addSAlexander Pyhalov 		 * Don't allow use of wflag with egrep / fgrep
408d2d52addSAlexander Pyhalov 		 */
409d2d52addSAlexander Pyhalov 		if (wflag)
410d2d52addSAlexander Pyhalov 			usage();
411d2d52addSAlexander Pyhalov 		/*
412d2d52addSAlexander Pyhalov 		 * For Solaris the -s flag is equivalent to XCU -q
413d2d52addSAlexander Pyhalov 		 */
414d2d52addSAlexander Pyhalov 		if (sflag)
415d2d52addSAlexander Pyhalov 			qflag++;
416d2d52addSAlexander Pyhalov 		/*
417d2d52addSAlexander Pyhalov 		 * done with above checks - set the appropriate flags
418d2d52addSAlexander Pyhalov 		 */
419d2d52addSAlexander Pyhalov 		if (egrep)
420d2d52addSAlexander Pyhalov 			Eflag++;
421d2d52addSAlexander Pyhalov 		else			/* Else fgrep */
422d2d52addSAlexander Pyhalov 			Fflag++;
4237c478bd9Sstevel@tonic-gate 	}
425d2d52addSAlexander Pyhalov 	if (wflag && (Eflag || Fflag)) {
426d2d52addSAlexander Pyhalov 		/*
427d2d52addSAlexander Pyhalov 		 * -w cannot be specified with grep -F
428d2d52addSAlexander Pyhalov 		 */
429d2d52addSAlexander Pyhalov 		usage();
430d2d52addSAlexander Pyhalov 	}
432d2d52addSAlexander Pyhalov 	/*
433d2d52addSAlexander Pyhalov 	 * -E and -F flags are mutually exclusive - check for this
434d2d52addSAlexander Pyhalov 	 */
435d2d52addSAlexander Pyhalov 	if (Eflag && Fflag)
436d2d52addSAlexander Pyhalov 		usage();
438d2d52addSAlexander Pyhalov 	/*
43981dd18d8SRobert Mustacchi 	 * -l or -L overrides -H like in GNU grep. It also overrides -o.
440d2d52addSAlexander Pyhalov 	 */
44181dd18d8SRobert Mustacchi 	if (lflag || Lflag) {
442d2d52addSAlexander Pyhalov 		Hflag = 0;
44381dd18d8SRobert Mustacchi 		oflag = 0;
44481dd18d8SRobert Mustacchi 	}
445d2d52addSAlexander Pyhalov 
446d2d52addSAlexander Pyhalov 	/*
447d2d52addSAlexander Pyhalov 	 * -c, -l and -q flags are mutually exclusive
448d2d52addSAlexander Pyhalov 	 * We have -c override -l like in Solaris.
449d2d52addSAlexander Pyhalov 	 * -q overrides -l & -c programmatically in grep() function.
45081dd18d8SRobert Mustacchi 	 * -c overrides -o in GNU grep, we honor that.
451d2d52addSAlexander Pyhalov 	 */
45281dd18d8SRobert Mustacchi 	if (cflag) {
453d2d52addSAlexander Pyhalov 		lflag = 0;
4542e5ac464SRobert Mustacchi 		Lflag = 0;
45581dd18d8SRobert Mustacchi 		oflag = 0;
45681dd18d8SRobert Mustacchi 	}
45781dd18d8SRobert Mustacchi 
45881dd18d8SRobert Mustacchi 	/*
45981dd18d8SRobert Mustacchi 	 * If -o is set then we ignore all context related options, like other
46081dd18d8SRobert Mustacchi 	 * greps.
46181dd18d8SRobert Mustacchi 	 */
46281dd18d8SRobert Mustacchi 	if (oflag) {
46381dd18d8SRobert Mustacchi 		conflag = 0;
46481dd18d8SRobert Mustacchi 	}
46581dd18d8SRobert Mustacchi 
46681dd18d8SRobert Mustacchi 	/*
46781dd18d8SRobert Mustacchi 	 * These flags are a semantic mess with no clear answers as to their
46881dd18d8SRobert Mustacchi 	 * behvaior. Based on some experimentation GNU grep will exit zero if a
46981dd18d8SRobert Mustacchi 	 * non-match is present, but never print anything. BSD grep seems to
47081dd18d8SRobert Mustacchi 	 * exit 1 and not print anything, even if there would have been a match.
47181dd18d8SRobert Mustacchi 	 * Also, you probably don't want to ask about what happens with grep -x
47281dd18d8SRobert Mustacchi 	 * -o -v, some implementations seem to just ignore -v.
47381dd18d8SRobert Mustacchi 	 */
47481dd18d8SRobert Mustacchi 	if (oflag && !nvflag) {
47581dd18d8SRobert Mustacchi 		(void) fprintf(stderr, gettext("%s: the combination of -v and "
47681dd18d8SRobert Mustacchi 		    "-o is not supported currently\n"), argv[0]);
47781dd18d8SRobert Mustacchi 		exit(2);
4782e5ac464SRobert Mustacchi 	}
480d2d52addSAlexander Pyhalov 	argv += optind - 1;
481d2d52addSAlexander Pyhalov 	argc -= optind - 1;
483d2d52addSAlexander Pyhalov 	/*
484d2d52addSAlexander Pyhalov 	 * Now handling -e and -f option
485d2d52addSAlexander Pyhalov 	 */
486d2d52addSAlexander Pyhalov 	if (pattern_list) {
487d2d52addSAlexander Pyhalov 		for (i = 0; i < n_pattern; i++) {
488d2d52addSAlexander Pyhalov 			addpattern(pattern_list[i]);
489d2d52addSAlexander Pyhalov 		}
490d2d52addSAlexander Pyhalov 		free(pattern_list);
491d2d52addSAlexander Pyhalov 	}
492d2d52addSAlexander Pyhalov 	if (file_list) {
493d2d52addSAlexander Pyhalov 		for (i = 0; i < n_file; i++) {
494d2d52addSAlexander Pyhalov 			addfile(file_list[i]);
4957c478bd9Sstevel@tonic-gate 		}
496d2d52addSAlexander Pyhalov 		free(file_list);
497d2d52addSAlexander Pyhalov 	}
499d2d52addSAlexander Pyhalov 	/*
500d2d52addSAlexander Pyhalov 	 * No -e or -f?  Make sure there is one more arg, use it as the pattern.
501d2d52addSAlexander Pyhalov 	 */
502d2d52addSAlexander Pyhalov 	if (patterns == NULL && !fflag) {
503d2d52addSAlexander Pyhalov 		if (argc < 2)
504d2d52addSAlexander Pyhalov 			usage();
505d2d52addSAlexander Pyhalov 		addpattern(argv[1]);
506d2d52addSAlexander Pyhalov 		argc--;
507d2d52addSAlexander Pyhalov 		argv++;
5087c478bd9Sstevel@tonic-gate 	}
510d2d52addSAlexander Pyhalov 	/*
511d2d52addSAlexander Pyhalov 	 * Compile Patterns and also decide if BMG can be used
512d2d52addSAlexander Pyhalov 	 */
513d2d52addSAlexander Pyhalov 	fixpatterns();
514d2d52addSAlexander Pyhalov 
5158ccd0217SRobert Mustacchi 	if (stdin_label == NULL) {
5168ccd0217SRobert Mustacchi 		stdin_label = STDIN_FILENAME;
5178ccd0217SRobert Mustacchi 	}
5188ccd0217SRobert Mustacchi 
519d2d52addSAlexander Pyhalov 	/* Process all files: stdin, or rest of arg list */
520d2d52addSAlexander Pyhalov 	if (argc < 2) {
5218ccd0217SRobert Mustacchi 		matched = grep(0, stdin_label);
522d2d52addSAlexander Pyhalov 	} else {
523d2d52addSAlexander Pyhalov 		if (Hflag || (argc > 2 && hflag == 0))
524d2d52addSAlexander Pyhalov 			outfn = 1;	/* Print filename on match line */
525d2d52addSAlexander Pyhalov 		for (argv++; *argv != NULL; argv++) {
526d2d52addSAlexander Pyhalov 			process_path(*argv);
527d2d52addSAlexander Pyhalov 		}
528d2d52addSAlexander Pyhalov 	}
529d2d52addSAlexander Pyhalov 	/*
530d2d52addSAlexander Pyhalov 	 * Return() here is used instead of exit
531d2d52addSAlexander Pyhalov 	 */
533d2d52addSAlexander Pyhalov 	(void) fflush(stdout);
535d2d52addSAlexander Pyhalov 	if (errors)
536d2d52addSAlexander Pyhalov 		return (2);
537d2d52addSAlexander Pyhalov 	return (matched ? 0 : 1);
5387c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate static void
process_path(const char * path)541d2d52addSAlexander Pyhalov process_path(const char *path)
542e52fb54bSAlexander Eremin {
543e52fb54bSAlexander Eremin 	struct	stat st;
544e52fb54bSAlexander Eremin 	int	walkflags = FTW_CHDIR;
545e52fb54bSAlexander Eremin 	char	*buf = NULL;
546e52fb54bSAlexander Eremin 
547e52fb54bSAlexander Eremin 	if (rflag) {
548e52fb54bSAlexander Eremin 		if (stat(path, &st) != -1 &&
549e52fb54bSAlexander Eremin 		    (st.st_mode & S_IFMT) == S_IFDIR) {
5504adc6f15SPeter Tribble 			if (!hflag)
5514adc6f15SPeter Tribble 				outfn = 1; /* Print filename unless -h */
552e52fb54bSAlexander Eremin 
553e52fb54bSAlexander Eremin 			/*
554e52fb54bSAlexander Eremin 			 * Add trailing slash if arg
555e52fb54bSAlexander Eremin 			 * is directory, to resolve symlinks.
556e52fb54bSAlexander Eremin 			 */
557e52fb54bSAlexander Eremin 			if (path[strlen(path) - 1] != '/') {
558e52fb54bSAlexander Eremin 				(void) asprintf(&buf, "%s/", path);
559e52fb54bSAlexander Eremin 				if (buf != NULL)
560e52fb54bSAlexander Eremin 					path = buf;
561e52fb54bSAlexander Eremin 			}
562e52fb54bSAlexander Eremin 
563e52fb54bSAlexander Eremin 			/*
564e52fb54bSAlexander Eremin 			 * Search through subdirs if path is directory.
565e52fb54bSAlexander Eremin 			 * Don't follow symlinks if Rflag is not set.
566e52fb54bSAlexander Eremin 			 */
567e52fb54bSAlexander Eremin 			if (!Rflag)
568e52fb54bSAlexander Eremin 				walkflags |= FTW_PHYS;
569e52fb54bSAlexander Eremin 
570e52fb54bSAlexander Eremin 			if (nftw(path, recursive, MAX_DEPTH, walkflags) != 0) {
571e52fb54bSAlexander Eremin 				if (!sflag)
572d2d52addSAlexander Pyhalov 					(void) fprintf(stderr,
573d2d52addSAlexander Pyhalov 					    gettext("%s: can't open \"%s\"\n"),
574d2d52addSAlexander Pyhalov 					    cmdname, path);
575d2d52addSAlexander Pyhalov 				errors = 1;
576e52fb54bSAlexander Eremin 			}
577e52fb54bSAlexander Eremin 			return;
578e52fb54bSAlexander Eremin 		}
579e52fb54bSAlexander Eremin 	}
580d2d52addSAlexander Pyhalov 	process_file(path, 0);
581e52fb54bSAlexander Eremin }
582e52fb54bSAlexander Eremin 
583d2d52addSAlexander Pyhalov /*
584d2d52addSAlexander Pyhalov  * Read and process all files in directory recursively.
585d2d52addSAlexander Pyhalov  */
586e52fb54bSAlexander Eremin static int
recursive(const char * name,const struct stat * statp,int info,struct FTW * ftw)587e52fb54bSAlexander Eremin recursive(const char *name, const struct stat *statp, int info, struct FTW *ftw)
588e52fb54bSAlexander Eremin {
589e52fb54bSAlexander Eremin 	/*
590d2d52addSAlexander Pyhalov 	 * Process files and follow symlinks if Rflag set.
591e52fb54bSAlexander Eremin 	 */
592e52fb54bSAlexander Eremin 	if (info != FTW_F) {
593d2d52addSAlexander Pyhalov 		/* Report broken symlinks and unreadable files */
594e52fb54bSAlexander Eremin 		if (!sflag &&
595e52fb54bSAlexander Eremin 		    (info == FTW_SLN || info == FTW_DNR || info == FTW_NS)) {
596d2d52addSAlexander Pyhalov 			(void) fprintf(stderr,
597d2d52addSAlexander Pyhalov 			    gettext("%s: can't open \"%s\"\n"), cmdname, name);
598e52fb54bSAlexander Eremin 		}
599e52fb54bSAlexander Eremin 		return (0);
600e52fb54bSAlexander Eremin 	}
601e52fb54bSAlexander Eremin 
602d2d52addSAlexander Pyhalov 
603d2d52addSAlexander Pyhalov 	/* Skip devices and pipes if Rflag is not set */
604e52fb54bSAlexander Eremin 	if (!Rflag && !S_ISREG(statp->st_mode))
605e52fb54bSAlexander Eremin 		return (0);
606d2d52addSAlexander Pyhalov 	/* Pass offset to relative name from FTW_CHDIR */
607d2d52addSAlexander Pyhalov 	process_file(name, ftw->base);
608e52fb54bSAlexander Eremin 	return (0);
609e52fb54bSAlexander Eremin }
610e52fb54bSAlexander Eremin 
611d2d52addSAlexander Pyhalov /*
612d2d52addSAlexander Pyhalov  * Opens file and call grep function.
613d2d52addSAlexander Pyhalov  */
614e52fb54bSAlexander Eremin static void
process_file(const char * name,int base)615d2d52addSAlexander Pyhalov process_file(const char *name, int base)
6167c478bd9Sstevel@tonic-gate {
617d2d52addSAlexander Pyhalov 	int fd;
619d2d52addSAlexander Pyhalov 	if ((fd = open(name + base, O_RDONLY)) == -1) {
620d2d52addSAlexander Pyhalov 		errors = 1;
621d2d52addSAlexander Pyhalov 		if (!sflag) /* Silent mode */
622d2d52addSAlexander Pyhalov 			(void) fprintf(stderr, gettext(
623d2d52addSAlexander Pyhalov 			    "%s: can't open \"%s\"\n"),
624d2d52addSAlexander Pyhalov 			    cmdname, name);
625d2d52addSAlexander Pyhalov 		return;
626d2d52addSAlexander Pyhalov 	}
627d2d52addSAlexander Pyhalov 	matched |= grep(fd, name);
628d2d52addSAlexander Pyhalov 	(void) close(fd);
630d2d52addSAlexander Pyhalov 	if (ferror(stdout)) {
631d2d52addSAlexander Pyhalov 		(void) fprintf(stderr, gettext(
632d2d52addSAlexander Pyhalov 		    "%s: error writing to stdout\n"),
633d2d52addSAlexander Pyhalov 		    cmdname);
634d2d52addSAlexander Pyhalov 		(void) fflush(stdout);
635d2d52addSAlexander Pyhalov 		exit(2);
636d2d52addSAlexander Pyhalov 	}
637d2d52addSAlexander Pyhalov 
638d2d52addSAlexander Pyhalov }
639d2d52addSAlexander Pyhalov 
640d2d52addSAlexander Pyhalov /*
641d2d52addSAlexander Pyhalov  * Add a file of strings to the pattern list.
642d2d52addSAlexander Pyhalov  */
643d2d52addSAlexander Pyhalov static void
addfile(const char * fn)644d2d52addSAlexander Pyhalov addfile(const char *fn)
645d2d52addSAlexander Pyhalov {
646d2d52addSAlexander Pyhalov 	FILE	*fp;
647d2d52addSAlexander Pyhalov 	char	*inbuf;
648d2d52addSAlexander Pyhalov 	char	*bufp;
649d2d52addSAlexander Pyhalov 	size_t	bufsiz, buflen, bufused;
650d2d52addSAlexander Pyhalov 
651d2d52addSAlexander Pyhalov 	/*
652d2d52addSAlexander Pyhalov 	 * Open the pattern file
653d2d52addSAlexander Pyhalov 	 */
654d2d52addSAlexander Pyhalov 	if ((fp = fopen(fn, "r")) == NULL) {
655d2d52addSAlexander Pyhalov 		(void) fprintf(stderr, gettext("%s: can't open \"%s\"\n"),
656d2d52addSAlexander Pyhalov 		    cmdname, fn);
657d2d52addSAlexander Pyhalov 		exit(2);
658d2d52addSAlexander Pyhalov 	}
659d2d52addSAlexander Pyhalov 	bufsiz = BUFSIZE;
660d2d52addSAlexander Pyhalov 	if ((inbuf = malloc(bufsiz)) == NULL) {
661d2d52addSAlexander Pyhalov 		(void) fprintf(stderr,
662d2d52addSAlexander Pyhalov 		    gettext("%s: out of memory\n"), cmdname);
663d2d52addSAlexander Pyhalov 		exit(2);
664d2d52addSAlexander Pyhalov 	}
665d2d52addSAlexander Pyhalov 	bufp = inbuf;
666d2d52addSAlexander Pyhalov 	bufused = 0;
667d2d52addSAlexander Pyhalov 	/*
668d2d52addSAlexander Pyhalov 	 * Read in the file, reallocing as we need more memory
669d2d52addSAlexander Pyhalov 	 */
670d2d52addSAlexander Pyhalov 	while (fgets(bufp, bufsiz - bufused, fp) != NULL) {
671d2d52addSAlexander Pyhalov 		buflen = strlen(bufp);
672d2d52addSAlexander Pyhalov 		bufused += buflen;
673d2d52addSAlexander Pyhalov 		if (bufused + 1 == bufsiz && bufp[buflen - 1] != '\n') {
674d2d52addSAlexander Pyhalov 			/*
675d2d52addSAlexander Pyhalov 			 * if this line does not fit to the buffer,
676d2d52addSAlexander Pyhalov 			 * realloc larger buffer
677d2d52addSAlexander Pyhalov 			 */
678d2d52addSAlexander Pyhalov 			bufsiz += BUFSIZE;
679d2d52addSAlexander Pyhalov 			if ((inbuf = realloc(inbuf, bufsiz)) == NULL) {
680d2d52addSAlexander Pyhalov 				(void) fprintf(stderr,
681d2d52addSAlexander Pyhalov 				    gettext("%s: out of memory\n"),
682d2d52addSAlexander Pyhalov 				    cmdname);
683d2d52addSAlexander Pyhalov 				exit(2);
684d2d52addSAlexander Pyhalov 			}
685d2d52addSAlexander Pyhalov 			bufp = inbuf + bufused;
686d2d52addSAlexander Pyhalov 			continue;
6877c478bd9Sstevel@tonic-gate 		}
688d2d52addSAlexander Pyhalov 		if (bufp[buflen - 1] == '\n') {
689d2d52addSAlexander Pyhalov 			bufp[--buflen] = '\0';
6907c478bd9Sstevel@tonic-gate 		}
691d2d52addSAlexander Pyhalov 		addpattern(inbuf);
692d2d52addSAlexander Pyhalov 
693d2d52addSAlexander Pyhalov 		bufp = inbuf;
694d2d52addSAlexander Pyhalov 		bufused = 0;
6957c478bd9Sstevel@tonic-gate 	}
696d2d52addSAlexander Pyhalov 	free(inbuf);
697d2d52addSAlexander Pyhalov 	free(prntbuf);
698d2d52addSAlexander Pyhalov 	free(conbuf);
699d2d52addSAlexander Pyhalov 	(void) fclose(fp);
700d2d52addSAlexander Pyhalov }
702d2d52addSAlexander Pyhalov /*
703d2d52addSAlexander Pyhalov  * Add a string to the pattern list.
704d2d52addSAlexander Pyhalov  */
705d2d52addSAlexander Pyhalov static void
addpattern(char * s)706d2d52addSAlexander Pyhalov addpattern(char *s)
707d2d52addSAlexander Pyhalov {
708d2d52addSAlexander Pyhalov 	PATTERN	*pp;
709d2d52addSAlexander Pyhalov 	char	*wordbuf;
710d2d52addSAlexander Pyhalov 	char	*np;