xref: /titanic_53/usr/src/cmd/grep_xpg4/grep.c (revision e52fb54bb8f22da555df8e240ebd249941b0ed95)
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 /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * grep - pattern matching program - combined grep, egrep, and fgrep.
297c478bd9Sstevel@tonic-gate  *	Based on MKS grep command, with XCU & Solaris mods.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Copyright 1985, 1992 by Mortice Kern Systems Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
37*e52fb54bSAlexander Eremin /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
38*e52fb54bSAlexander Eremin 
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <regex.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <wchar.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <wctype.h>
54*e52fb54bSAlexander Eremin #include <ftw.h>
55*e52fb54bSAlexander Eremin #include <sys/param.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	BSIZE		512		/* Size of block for -b */
587c478bd9Sstevel@tonic-gate #define	BUFSIZE		8192		/* Input buffer size */
59*e52fb54bSAlexander Eremin #define	MAX_DEPTH	1000		/* how deep to recurse */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	M_CSETSIZE	256		/* singlebyte chars */
627c478bd9Sstevel@tonic-gate static int	bmglen;			/* length of BMG pattern */
637c478bd9Sstevel@tonic-gate static char	*bmgpat;		/* BMG pattern */
647c478bd9Sstevel@tonic-gate static int	bmgtab[M_CSETSIZE];	/* BMG delta1 table */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate typedef	struct	_PATTERN	{
677c478bd9Sstevel@tonic-gate 	char	*pattern;		/* original pattern */
687c478bd9Sstevel@tonic-gate 	wchar_t	*wpattern;		/* wide, lowercased pattern */
697c478bd9Sstevel@tonic-gate 	struct	_PATTERN	*next;
707c478bd9Sstevel@tonic-gate 	regex_t	re;			/* compiled pattern */
717c478bd9Sstevel@tonic-gate } PATTERN;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static PATTERN	*patterns;
747c478bd9Sstevel@tonic-gate static char	errstr[128];		/* regerror string buffer */
757c478bd9Sstevel@tonic-gate static int	regflags = 0;		/* regcomp options */
76*e52fb54bSAlexander Eremin static int	matched = 0;		/* return of the grep() */
77*e52fb54bSAlexander Eremin static int	errors = 0;		/* count of errors */
787c478bd9Sstevel@tonic-gate static uchar_t	fgrep = 0;		/* Invoked as fgrep */
797c478bd9Sstevel@tonic-gate static uchar_t	egrep = 0;		/* Invoked as egrep */
807c478bd9Sstevel@tonic-gate static uchar_t	nvflag = 1;		/* Print matching lines */
817c478bd9Sstevel@tonic-gate static uchar_t	cflag;			/* Count of matches */
827c478bd9Sstevel@tonic-gate static uchar_t	iflag;			/* Case insensitve matching */
837c478bd9Sstevel@tonic-gate static uchar_t	hflag;			/* Supress printing of filename */
847c478bd9Sstevel@tonic-gate static uchar_t	lflag;			/* Print file names of matches */
857c478bd9Sstevel@tonic-gate static uchar_t	nflag;			/* Precede lines by line number */
86*e52fb54bSAlexander Eremin static uchar_t	rflag;			/* Search directories recursively */
877c478bd9Sstevel@tonic-gate static uchar_t	bflag;			/* Preccede matches by block number */
887c478bd9Sstevel@tonic-gate static uchar_t	sflag;			/* Suppress file error messages */
897c478bd9Sstevel@tonic-gate static uchar_t	qflag;			/* Suppress standard output */
907c478bd9Sstevel@tonic-gate static uchar_t	wflag;			/* Search for expression as a word */
917c478bd9Sstevel@tonic-gate static uchar_t	xflag;			/* Anchoring */
927c478bd9Sstevel@tonic-gate static uchar_t	Eflag;			/* Egrep or -E flag */
937c478bd9Sstevel@tonic-gate static uchar_t	Fflag;			/* Fgrep or -F flag */
94*e52fb54bSAlexander Eremin static uchar_t	Rflag;			/* Like rflag, but follow symlinks */
957c478bd9Sstevel@tonic-gate static uchar_t	outfn;			/* Put out file name */
967c478bd9Sstevel@tonic-gate static char	*cmdname;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int	use_wchar, use_bmg, mblocale;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static size_t	outbuflen, prntbuflen;
1017c478bd9Sstevel@tonic-gate static char	*prntbuf;
1027c478bd9Sstevel@tonic-gate static wchar_t	*outline;
1037c478bd9Sstevel@tonic-gate 
104*e52fb54bSAlexander Eremin static void	addfile(const char *fn);
1057c478bd9Sstevel@tonic-gate static void	addpattern(char *s);
1067c478bd9Sstevel@tonic-gate static void	fixpatterns(void);
1077c478bd9Sstevel@tonic-gate static void	usage(void);
108*e52fb54bSAlexander Eremin static int	grep(int, const char *);
1097c478bd9Sstevel@tonic-gate static void	bmgcomp(char *, int);
1107c478bd9Sstevel@tonic-gate static char	*bmgexec(char *, char *);
111*e52fb54bSAlexander Eremin static int	recursive(const char *, const struct stat *, int, struct FTW *);
112*e52fb54bSAlexander Eremin static void	process_path(const char *);
113*e52fb54bSAlexander Eremin static void	process_file(const char *, int);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * mainline for grep
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate int
1197c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	char	*ap;
1227c478bd9Sstevel@tonic-gate 	int	c;
1237c478bd9Sstevel@tonic-gate 	int	fflag = 0;
1247c478bd9Sstevel@tonic-gate 	int	i, n_pattern = 0, n_file = 0;
1257c478bd9Sstevel@tonic-gate 	char	**pattern_list = NULL;
1267c478bd9Sstevel@tonic-gate 	char	**file_list = NULL;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1297c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1307c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * true if this is running on the multibyte locale
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	mblocale = (MB_CUR_MAX > 1);
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * Skip leading slashes
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	cmdname = argv[0];
1427c478bd9Sstevel@tonic-gate 	if (ap = strrchr(cmdname, '/'))
1437c478bd9Sstevel@tonic-gate 		cmdname = ap + 1;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	ap = cmdname;
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Detect egrep/fgrep via command name, map to -E and -F options.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	if (*ap == 'e' || *ap == 'E') {
1507c478bd9Sstevel@tonic-gate 		regflags |= REG_EXTENDED;
1517c478bd9Sstevel@tonic-gate 		egrep++;
1527c478bd9Sstevel@tonic-gate 	} else {
1537c478bd9Sstevel@tonic-gate 		if (*ap == 'f' || *ap == 'F') {
1547c478bd9Sstevel@tonic-gate 			fgrep++;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
158*e52fb54bSAlexander Eremin 	while ((c = getopt(argc, argv, "vwchilnrbse:f:qxEFIR")) != EOF) {
1597c478bd9Sstevel@tonic-gate 		switch (c) {
1607c478bd9Sstevel@tonic-gate 		case 'v':	/* POSIX: negate matches */
1617c478bd9Sstevel@tonic-gate 			nvflag = 0;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'c':	/* POSIX: write count */
1657c478bd9Sstevel@tonic-gate 			cflag++;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		case 'i':	/* POSIX: ignore case */
1697c478bd9Sstevel@tonic-gate 			iflag++;
1707c478bd9Sstevel@tonic-gate 			regflags |= REG_ICASE;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		case 'l':	/* POSIX: Write filenames only */
1747c478bd9Sstevel@tonic-gate 			lflag++;
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		case 'n':	/* POSIX: Write line numbers */
1787c478bd9Sstevel@tonic-gate 			nflag++;
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 
181*e52fb54bSAlexander Eremin 		case 'r':	/* Solaris: search recursively */
182*e52fb54bSAlexander Eremin 			rflag++;
183*e52fb54bSAlexander Eremin 			break;
184*e52fb54bSAlexander Eremin 
1857c478bd9Sstevel@tonic-gate 		case 'b':	/* Solaris: Write file block numbers */
1867c478bd9Sstevel@tonic-gate 			bflag++;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		case 's':	/* POSIX: No error msgs for files */
1907c478bd9Sstevel@tonic-gate 			sflag++;
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case 'e':	/* POSIX: pattern list */
1947c478bd9Sstevel@tonic-gate 			n_pattern++;
1957c478bd9Sstevel@tonic-gate 			pattern_list = realloc(pattern_list,
1967c478bd9Sstevel@tonic-gate 			    sizeof (char *) * n_pattern);
1977c478bd9Sstevel@tonic-gate 			if (pattern_list == NULL) {
1987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1997c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
2007c478bd9Sstevel@tonic-gate 				    cmdname);
2017c478bd9Sstevel@tonic-gate 				exit(2);
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			*(pattern_list + n_pattern - 1) = optarg;
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		case 'f':	/* POSIX: pattern file */
2077c478bd9Sstevel@tonic-gate 			fflag = 1;
2087c478bd9Sstevel@tonic-gate 			n_file++;
2097c478bd9Sstevel@tonic-gate 			file_list = realloc(file_list,
2107c478bd9Sstevel@tonic-gate 			    sizeof (char *) * n_file);
2117c478bd9Sstevel@tonic-gate 			if (file_list == NULL) {
2127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
2147c478bd9Sstevel@tonic-gate 				    cmdname);
2157c478bd9Sstevel@tonic-gate 				exit(2);
2167c478bd9Sstevel@tonic-gate 			}
2177c478bd9Sstevel@tonic-gate 			*(file_list + n_file - 1) = optarg;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case 'h':	/* Solaris: supress printing of file name */
2207c478bd9Sstevel@tonic-gate 			hflag = 1;
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		case 'q':	/* POSIX: quiet: status only */
2247c478bd9Sstevel@tonic-gate 			qflag++;
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		case 'w':	/* Solaris: treat pattern as word */
2287c478bd9Sstevel@tonic-gate 			wflag++;
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		case 'x':	/* POSIX: full line matches */
2327c478bd9Sstevel@tonic-gate 			xflag++;
2337c478bd9Sstevel@tonic-gate 			regflags |= REG_ANCHOR;
2347c478bd9Sstevel@tonic-gate 			break;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		case 'E':	/* POSIX: Extended RE's */
2377c478bd9Sstevel@tonic-gate 			regflags |= REG_EXTENDED;
2387c478bd9Sstevel@tonic-gate 			Eflag++;
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		case 'F':	/* POSIX: strings, not RE's */
2427c478bd9Sstevel@tonic-gate 			Fflag++;
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 
245*e52fb54bSAlexander Eremin 		case 'R':	/* Solaris: like rflag, but follow symlinks */
246*e52fb54bSAlexander Eremin 			Rflag++;
247*e52fb54bSAlexander Eremin 			rflag++;
248*e52fb54bSAlexander Eremin 			break;
249*e52fb54bSAlexander Eremin 
2507c478bd9Sstevel@tonic-gate 		default:
2517c478bd9Sstevel@tonic-gate 			usage();
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * If we're invoked as egrep or fgrep we need to do some checks
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (egrep || fgrep) {
2597c478bd9Sstevel@tonic-gate 		/*
2607c478bd9Sstevel@tonic-gate 		 * Use of -E or -F with egrep or fgrep is illegal
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		if (Eflag || Fflag)
2637c478bd9Sstevel@tonic-gate 			usage();
2647c478bd9Sstevel@tonic-gate 		/*
2657c478bd9Sstevel@tonic-gate 		 * Don't allow use of wflag with egrep / fgrep
2667c478bd9Sstevel@tonic-gate 		 */
2677c478bd9Sstevel@tonic-gate 		if (wflag)
2687c478bd9Sstevel@tonic-gate 			usage();
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * For Solaris the -s flag is equivalent to XCU -q
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		if (sflag)
2737c478bd9Sstevel@tonic-gate 			qflag++;
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * done with above checks - set the appropriate flags
2767c478bd9Sstevel@tonic-gate 		 */
2777c478bd9Sstevel@tonic-gate 		if (egrep)
2787c478bd9Sstevel@tonic-gate 			Eflag++;
2797c478bd9Sstevel@tonic-gate 		else			/* Else fgrep */
2807c478bd9Sstevel@tonic-gate 			Fflag++;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (wflag && (Eflag || Fflag)) {
2847c478bd9Sstevel@tonic-gate 		/*
2857c478bd9Sstevel@tonic-gate 		 * -w cannot be specified with grep -F
2867c478bd9Sstevel@tonic-gate 		 */
2877c478bd9Sstevel@tonic-gate 		usage();
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * -E and -F flags are mutually exclusive - check for this
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	if (Eflag && Fflag)
2947c478bd9Sstevel@tonic-gate 		usage();
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * -c, -l and -q flags are mutually exclusive
2987c478bd9Sstevel@tonic-gate 	 * We have -c override -l like in Solaris.
2997c478bd9Sstevel@tonic-gate 	 * -q overrides -l & -c programmatically in grep() function.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	if (cflag && lflag)
3027c478bd9Sstevel@tonic-gate 		lflag = 0;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	argv += optind - 1;
3057c478bd9Sstevel@tonic-gate 	argc -= optind - 1;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * Now handling -e and -f option
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (pattern_list) {
3117c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_pattern; i++) {
3127c478bd9Sstevel@tonic-gate 			addpattern(pattern_list[i]);
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		free(pattern_list);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	if (file_list) {
3177c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_file; i++) {
3187c478bd9Sstevel@tonic-gate 			addfile(file_list[i]);
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		free(file_list);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * No -e or -f?  Make sure there is one more arg, use it as the pattern.
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if (patterns == NULL && !fflag) {
3277c478bd9Sstevel@tonic-gate 		if (argc < 2)
3287c478bd9Sstevel@tonic-gate 			usage();
3297c478bd9Sstevel@tonic-gate 		addpattern(argv[1]);
3307c478bd9Sstevel@tonic-gate 		argc--;
3317c478bd9Sstevel@tonic-gate 		argv++;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * If -x flag is not specified or -i flag is specified
3367c478bd9Sstevel@tonic-gate 	 * with fgrep in a multibyte locale, need to use
3377c478bd9Sstevel@tonic-gate 	 * the wide character APIs.  Otherwise, byte-oriented
3387c478bd9Sstevel@tonic-gate 	 * process will be done.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	use_wchar = Fflag && mblocale && (!xflag || iflag);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Compile Patterns and also decide if BMG can be used
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	fixpatterns();
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/* Process all files: stdin, or rest of arg list */
3487c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3497c478bd9Sstevel@tonic-gate 		matched = grep(0, gettext("(standard input)"));
3507c478bd9Sstevel@tonic-gate 	} else {
3517c478bd9Sstevel@tonic-gate 		if (argc > 2 && hflag == 0)
3527c478bd9Sstevel@tonic-gate 			outfn = 1;	/* Print filename on match line */
3537c478bd9Sstevel@tonic-gate 		for (argv++; *argv != NULL; argv++) {
354*e52fb54bSAlexander Eremin 			process_path(*argv);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Return() here is used instead of exit
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (errors)
3647c478bd9Sstevel@tonic-gate 		return (2);
3657c478bd9Sstevel@tonic-gate 	return (matched ? 0 : 1);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
368*e52fb54bSAlexander Eremin static void
369*e52fb54bSAlexander Eremin process_path(const char *path)
370*e52fb54bSAlexander Eremin {
371*e52fb54bSAlexander Eremin 	struct	stat st;
372*e52fb54bSAlexander Eremin 	int	walkflags = FTW_CHDIR;
373*e52fb54bSAlexander Eremin 	char	*buf = NULL;
374*e52fb54bSAlexander Eremin 
375*e52fb54bSAlexander Eremin 	if (rflag) {
376*e52fb54bSAlexander Eremin 		if (stat(path, &st) != -1 &&
377*e52fb54bSAlexander Eremin 		    (st.st_mode & S_IFMT) == S_IFDIR) {
378*e52fb54bSAlexander Eremin 			outfn = 1; /* Print filename */
379*e52fb54bSAlexander Eremin 
380*e52fb54bSAlexander Eremin 			/*
381*e52fb54bSAlexander Eremin 			 * Add trailing slash if arg
382*e52fb54bSAlexander Eremin 			 * is directory, to resolve symlinks.
383*e52fb54bSAlexander Eremin 			 */
384*e52fb54bSAlexander Eremin 			if (path[strlen(path) - 1] != '/') {
385*e52fb54bSAlexander Eremin 				(void) asprintf(&buf, "%s/", path);
386*e52fb54bSAlexander Eremin 				if (buf != NULL)
387*e52fb54bSAlexander Eremin 					path = buf;
388*e52fb54bSAlexander Eremin 			}
389*e52fb54bSAlexander Eremin 
390*e52fb54bSAlexander Eremin 			/*
391*e52fb54bSAlexander Eremin 			 * Search through subdirs if path is directory.
392*e52fb54bSAlexander Eremin 			 * Don't follow symlinks if Rflag is not set.
393*e52fb54bSAlexander Eremin 			 */
394*e52fb54bSAlexander Eremin 			if (!Rflag)
395*e52fb54bSAlexander Eremin 				walkflags |= FTW_PHYS;
396*e52fb54bSAlexander Eremin 
397*e52fb54bSAlexander Eremin 			if (nftw(path, recursive, MAX_DEPTH, walkflags) != 0) {
398*e52fb54bSAlexander Eremin 				if (!sflag)
399*e52fb54bSAlexander Eremin 					(void) fprintf(stderr,
400*e52fb54bSAlexander Eremin 					    gettext("%s: can't open \"%s\"\n"),
401*e52fb54bSAlexander Eremin 					    cmdname, path);
402*e52fb54bSAlexander Eremin 				errors = 1;
403*e52fb54bSAlexander Eremin 			}
404*e52fb54bSAlexander Eremin 			return;
405*e52fb54bSAlexander Eremin 		}
406*e52fb54bSAlexander Eremin 	}
407*e52fb54bSAlexander Eremin 	process_file(path, 0);
408*e52fb54bSAlexander Eremin }
409*e52fb54bSAlexander Eremin 
410*e52fb54bSAlexander Eremin /*
411*e52fb54bSAlexander Eremin  * Read and process all files in directory recursively.
412*e52fb54bSAlexander Eremin  */
413*e52fb54bSAlexander Eremin static int
414*e52fb54bSAlexander Eremin recursive(const char *name, const struct stat *statp, int info, struct FTW *ftw)
415*e52fb54bSAlexander Eremin {
416*e52fb54bSAlexander Eremin 	/*
417*e52fb54bSAlexander Eremin 	 * Process files and follow symlinks if Rflag set.
418*e52fb54bSAlexander Eremin 	 */
419*e52fb54bSAlexander Eremin 	if (info != FTW_F) {
420*e52fb54bSAlexander Eremin 		/* Report broken symlinks and unreadable files */
421*e52fb54bSAlexander Eremin 		if (!sflag &&
422*e52fb54bSAlexander Eremin 		    (info == FTW_SLN || info == FTW_DNR || info == FTW_NS)) {
423*e52fb54bSAlexander Eremin 			(void) fprintf(stderr,
424*e52fb54bSAlexander Eremin 			    gettext("%s: can't open \"%s\"\n"), cmdname, name);
425*e52fb54bSAlexander Eremin 		}
426*e52fb54bSAlexander Eremin 		return (0);
427*e52fb54bSAlexander Eremin 	}
428*e52fb54bSAlexander Eremin 
429*e52fb54bSAlexander Eremin 
430*e52fb54bSAlexander Eremin 	/* Skip devices and pipes if Rflag is not set */
431*e52fb54bSAlexander Eremin 	if (!Rflag && !S_ISREG(statp->st_mode))
432*e52fb54bSAlexander Eremin 		return (0);
433*e52fb54bSAlexander Eremin 	/* Pass offset to relative name from FTW_CHDIR */
434*e52fb54bSAlexander Eremin 	process_file(name, ftw->base);
435*e52fb54bSAlexander Eremin 	return (0);
436*e52fb54bSAlexander Eremin }
437*e52fb54bSAlexander Eremin 
438*e52fb54bSAlexander Eremin /*
439*e52fb54bSAlexander Eremin  * Opens file and call grep function.
440*e52fb54bSAlexander Eremin  */
441*e52fb54bSAlexander Eremin static void
442*e52fb54bSAlexander Eremin process_file(const char *name, int base)
443*e52fb54bSAlexander Eremin {
444*e52fb54bSAlexander Eremin 	int fd;
445*e52fb54bSAlexander Eremin 
446*e52fb54bSAlexander Eremin 	if ((fd = open(name + base, O_RDONLY)) == -1) {
447*e52fb54bSAlexander Eremin 		errors = 1;
448*e52fb54bSAlexander Eremin 		if (!sflag) /* Silent mode */
449*e52fb54bSAlexander Eremin 			(void) fprintf(stderr, gettext(
450*e52fb54bSAlexander Eremin 			    "%s: can't open \"%s\"\n"),
451*e52fb54bSAlexander Eremin 			    cmdname, name);
452*e52fb54bSAlexander Eremin 		return;
453*e52fb54bSAlexander Eremin 	}
454*e52fb54bSAlexander Eremin 	matched |= grep(fd, name);
455*e52fb54bSAlexander Eremin 	(void) close(fd);
456*e52fb54bSAlexander Eremin 
457*e52fb54bSAlexander Eremin 	if (ferror(stdout)) {
458*e52fb54bSAlexander Eremin 		(void) fprintf(stderr, gettext(
459*e52fb54bSAlexander Eremin 		    "%s: error writing to stdout\n"),
460*e52fb54bSAlexander Eremin 		    cmdname);
461*e52fb54bSAlexander Eremin 		(void) fflush(stdout);
462*e52fb54bSAlexander Eremin 		exit(2);
463*e52fb54bSAlexander Eremin 	}
464*e52fb54bSAlexander Eremin 
465*e52fb54bSAlexander Eremin }
466*e52fb54bSAlexander Eremin 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Add a file of strings to the pattern list.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate static void
471*e52fb54bSAlexander Eremin addfile(const char *fn)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	FILE	*fp;
4747c478bd9Sstevel@tonic-gate 	char	*inbuf;
4757c478bd9Sstevel@tonic-gate 	char	*bufp;
4767c478bd9Sstevel@tonic-gate 	size_t	bufsiz, buflen, bufused;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Open the pattern file
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	if ((fp = fopen(fn, "r")) == NULL) {
4827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: can't open \"%s\"\n"),
4837c478bd9Sstevel@tonic-gate 		    cmdname, fn);
4847c478bd9Sstevel@tonic-gate 		exit(2);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	bufsiz = BUFSIZE;
4877c478bd9Sstevel@tonic-gate 	if ((inbuf = malloc(bufsiz)) == NULL) {
4887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4897c478bd9Sstevel@tonic-gate 		    gettext("%s: out of memory\n"), cmdname);
4907c478bd9Sstevel@tonic-gate 		exit(2);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	bufp = inbuf;
4937c478bd9Sstevel@tonic-gate 	bufused = 0;
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * Read in the file, reallocing as we need more memory
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	while (fgets(bufp, bufsiz - bufused, fp) != NULL) {
4987c478bd9Sstevel@tonic-gate 		buflen = strlen(bufp);
4997c478bd9Sstevel@tonic-gate 		bufused += buflen;
5007c478bd9Sstevel@tonic-gate 		if (bufused + 1 == bufsiz && bufp[buflen - 1] != '\n') {
5017c478bd9Sstevel@tonic-gate 			/*
5027c478bd9Sstevel@tonic-gate 			 * if this line does not fit to the buffer,
5037c478bd9Sstevel@tonic-gate 			 * realloc larger buffer
5047c478bd9Sstevel@tonic-gate 			 */
5057c478bd9Sstevel@tonic-gate 			bufsiz += BUFSIZE;
5067c478bd9Sstevel@tonic-gate 			if ((inbuf = realloc(inbuf, bufsiz)) == NULL) {
5077c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5087c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
5097c478bd9Sstevel@tonic-gate 				    cmdname);
5107c478bd9Sstevel@tonic-gate 				exit(2);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			bufp = inbuf + bufused;
5137c478bd9Sstevel@tonic-gate 			continue;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		if (bufp[buflen - 1] == '\n') {
5167c478bd9Sstevel@tonic-gate 			bufp[--buflen] = '\0';
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		addpattern(inbuf);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		bufp = inbuf;
5217c478bd9Sstevel@tonic-gate 		bufused = 0;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	free(inbuf);
5247c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * Add a string to the pattern list.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate static void
5317c478bd9Sstevel@tonic-gate addpattern(char *s)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	PATTERN	*pp;
5347c478bd9Sstevel@tonic-gate 	char	*wordbuf;
5357c478bd9Sstevel@tonic-gate 	char	*np;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	for (; ; ) {
5387c478bd9Sstevel@tonic-gate 		np = strchr(s, '\n');
5397c478bd9Sstevel@tonic-gate 		if (np != NULL)
5407c478bd9Sstevel@tonic-gate 			*np = '\0';
5417c478bd9Sstevel@tonic-gate 		if ((pp = malloc(sizeof (PATTERN))) == NULL) {
5427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5437c478bd9Sstevel@tonic-gate 			    "%s: out of memory\n"),
5447c478bd9Sstevel@tonic-gate 			    cmdname);
5457c478bd9Sstevel@tonic-gate 			exit(2);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		if (wflag) {
5487c478bd9Sstevel@tonic-gate 			/*
5497c478bd9Sstevel@tonic-gate 			 * Solaris wflag support: Add '<' '>' to pattern to
5507c478bd9Sstevel@tonic-gate 			 * select it as a word. Doesn't make sense with -F
5517c478bd9Sstevel@tonic-gate 			 * but we're Libertarian.
5527c478bd9Sstevel@tonic-gate 			 */
5537c478bd9Sstevel@tonic-gate 			size_t	slen, wordlen;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 			slen = strlen(s);
5567c478bd9Sstevel@tonic-gate 			wordlen = slen + 5; /* '\\' '<' s '\\' '>' '\0' */
5577c478bd9Sstevel@tonic-gate 			if ((wordbuf = malloc(wordlen)) == NULL) {
5587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5597c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
5607c478bd9Sstevel@tonic-gate 				    cmdname);
5617c478bd9Sstevel@tonic-gate 				exit(2);
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 			(void) strcpy(wordbuf, "\\<");
5647c478bd9Sstevel@tonic-gate 			(void) strcpy(wordbuf + 2, s);
5657c478bd9Sstevel@tonic-gate 			(void) strcpy(wordbuf + 2 + slen, "\\>");
5667c478bd9Sstevel@tonic-gate 		} else {
5677c478bd9Sstevel@tonic-gate 			if ((wordbuf = strdup(s)) == NULL) {
5687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5697c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
5707c478bd9Sstevel@tonic-gate 				    cmdname);
5717c478bd9Sstevel@tonic-gate 				exit(2);
5727c478bd9Sstevel@tonic-gate 			}
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 		pp->pattern = wordbuf;
5757c478bd9Sstevel@tonic-gate 		pp->next = patterns;
5767c478bd9Sstevel@tonic-gate 		patterns = pp;
5777c478bd9Sstevel@tonic-gate 		if (np == NULL)
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		s = np + 1;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Fix patterns.
5857c478bd9Sstevel@tonic-gate  * Must do after all arguments read, in case later -i option.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static void
5887c478bd9Sstevel@tonic-gate fixpatterns(void)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	PATTERN	*pp;
5917c478bd9Sstevel@tonic-gate 	int	rv, fix_pattern, npatterns;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * As REG_ANCHOR flag is not supported in the current Solaris,
5957c478bd9Sstevel@tonic-gate 	 * need to fix the specified pattern if -x is specified with
5967c478bd9Sstevel@tonic-gate 	 * grep or egrep
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 	fix_pattern = !Fflag && xflag;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	for (npatterns = 0, pp = patterns; pp != NULL; pp = pp->next) {
6017c478bd9Sstevel@tonic-gate 		npatterns++;
6027c478bd9Sstevel@tonic-gate 		if (fix_pattern) {
6037c478bd9Sstevel@tonic-gate 			char	*cp, *cq;
6047c478bd9Sstevel@tonic-gate 			size_t	plen, nplen;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			plen = strlen(pp->pattern);
6077c478bd9Sstevel@tonic-gate 			/* '^' pattern '$' */
6087c478bd9Sstevel@tonic-gate 			nplen = 1 + plen + 1 + 1;
6097c478bd9Sstevel@tonic-gate 			if ((cp = malloc(nplen)) == NULL) {
6107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6117c478bd9Sstevel@tonic-gate 				    gettext("%s: out of memory\n"),
6127c478bd9Sstevel@tonic-gate 				    cmdname);
6137c478bd9Sstevel@tonic-gate 				exit(2);
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 			cq = cp;
6167c478bd9Sstevel@tonic-gate 			*cq++ = '^';
6177c478bd9Sstevel@tonic-gate 			cq = strcpy(cq, pp->pattern) + plen;
6187c478bd9Sstevel@tonic-gate 			*cq++ = '$';
6197c478bd9Sstevel@tonic-gate 			*cq = '\0';
6207c478bd9Sstevel@tonic-gate 			free(pp->pattern);
6217c478bd9Sstevel@tonic-gate 			pp->pattern = cp;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (Fflag) {
6257c478bd9Sstevel@tonic-gate 			if (use_wchar) {
6267c478bd9Sstevel@tonic-gate 				/*
6277c478bd9Sstevel@tonic-gate 				 * Fflag && mblocale && iflag
6287c478bd9Sstevel@tonic-gate 				 * Fflag && mblocale && !xflag
6297c478bd9Sstevel@tonic-gate 				 */
6307c478bd9Sstevel@tonic-gate 				size_t	n;
6317c478bd9Sstevel@tonic-gate 				n = strlen(pp->pattern) + 1;
6327c478bd9Sstevel@tonic-gate 				if ((pp->wpattern =
6337c478bd9Sstevel@tonic-gate 				    malloc(sizeof (wchar_t) * n)) == NULL) {
6347c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6357c478bd9Sstevel@tonic-gate 					    gettext("%s: out of memory\n"),
6367c478bd9Sstevel@tonic-gate 					    cmdname);
6377c478bd9Sstevel@tonic-gate 					exit(2);
6387c478bd9Sstevel@tonic-gate 				}
6397c478bd9Sstevel@tonic-gate 				if (mbstowcs(pp->wpattern, pp->pattern, n) ==
6407c478bd9Sstevel@tonic-gate 				    (size_t)-1) {
6417c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6427c478bd9Sstevel@tonic-gate 					    gettext("%s: failed to convert "
6437c478bd9Sstevel@tonic-gate 					    "\"%s\" to wide-characters\n"),
6447c478bd9Sstevel@tonic-gate 					    cmdname, pp->pattern);
6457c478bd9Sstevel@tonic-gate 					exit(2);
6467c478bd9Sstevel@tonic-gate 				}
6477c478bd9Sstevel@tonic-gate 				if (iflag) {
6487c478bd9Sstevel@tonic-gate 					wchar_t	*wp;
6497c478bd9Sstevel@tonic-gate 					for (wp = pp->wpattern; *wp != L'\0';
6507c478bd9Sstevel@tonic-gate 					    wp++) {
6517c478bd9Sstevel@tonic-gate 						*wp = towlower((wint_t)*wp);
6527c478bd9Sstevel@tonic-gate 					}
6537c478bd9Sstevel@tonic-gate 				}
6547c478bd9Sstevel@tonic-gate 				free(pp->pattern);
6557c478bd9Sstevel@tonic-gate 			} else {
6567c478bd9Sstevel@tonic-gate 				/*
6577c478bd9Sstevel@tonic-gate 				 * Fflag && mblocale && !iflag
6587c478bd9Sstevel@tonic-gate 				 * Fflag && !mblocale && iflag
6597c478bd9Sstevel@tonic-gate 				 * Fflag && !mblocale && !iflag
6607c478bd9Sstevel@tonic-gate 				 */
6617c478bd9Sstevel@tonic-gate 				if (iflag) {
6627c478bd9Sstevel@tonic-gate 					unsigned char	*cp;
6637c478bd9Sstevel@tonic-gate 					for (cp = (unsigned char *)pp->pattern;
6647c478bd9Sstevel@tonic-gate 					    *cp != '\0'; cp++) {
6657c478bd9Sstevel@tonic-gate 						*cp = tolower(*cp);
6667c478bd9Sstevel@tonic-gate 					}
6677c478bd9Sstevel@tonic-gate 				}
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			/*
6707c478bd9Sstevel@tonic-gate 			 * fgrep: No regular expressions.
6717c478bd9Sstevel@tonic-gate 			 */
6727c478bd9Sstevel@tonic-gate 			continue;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		/*
6767c478bd9Sstevel@tonic-gate 		 * For non-fgrep, compile the regular expression,
6777c478bd9Sstevel@tonic-gate 		 * give an informative error message, and exit if
6787c478bd9Sstevel@tonic-gate 		 * it didn't compile.
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		if ((rv = regcomp(&pp->re, pp->pattern, regflags)) != 0) {
6817c478bd9Sstevel@tonic-gate 			(void) regerror(rv, &pp->re, errstr, sizeof (errstr));
6827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6837c478bd9Sstevel@tonic-gate 			    gettext("%s: RE error in %s: %s\n"),
6847c478bd9Sstevel@tonic-gate 			    cmdname, pp->pattern, errstr);
6857c478bd9Sstevel@tonic-gate 			exit(2);
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 		free(pp->pattern);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Decide if we are able to run the Boyer-Moore-Gosper algorithm.
6927c478bd9Sstevel@tonic-gate 	 * Use the Boyer-Moore-Gosper algorithm if:
6937c478bd9Sstevel@tonic-gate 	 * - fgrep			(Fflag)
6947c478bd9Sstevel@tonic-gate 	 * - singlebyte locale		(!mblocale)
6957c478bd9Sstevel@tonic-gate 	 * - no ignoring case		(!iflag)
6967c478bd9Sstevel@tonic-gate 	 * - no printing line numbers	(!nflag)
6977c478bd9Sstevel@tonic-gate 	 * - no negating the output	(nvflag)
6987c478bd9Sstevel@tonic-gate 	 * - only one pattern		(npatterns == 1)
6997c478bd9Sstevel@tonic-gate 	 * - non zero length pattern	(strlen(patterns->pattern) != 0)
7007c478bd9Sstevel@tonic-gate 	 *
7017c478bd9Sstevel@tonic-gate 	 * It's guaranteed patterns->pattern is still alive
7027c478bd9Sstevel@tonic-gate 	 * when Fflag && !mblocale.
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	use_bmg = Fflag && !mblocale && !iflag && !nflag && nvflag &&
7057c478bd9Sstevel@tonic-gate 	    (npatterns == 1) && (strlen(patterns->pattern) != 0);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate  * Search a newline from the beginning of the string
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate static char *
7127c478bd9Sstevel@tonic-gate find_nl(const char *ptr, size_t len)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	while (len-- != 0) {
7157c478bd9Sstevel@tonic-gate 		if (*ptr++ == '\n') {
7167c478bd9Sstevel@tonic-gate 			return ((char *)--ptr);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	return (NULL);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * Search a newline from the end of the string
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate static char *
7267c478bd9Sstevel@tonic-gate rfind_nl(const char *ptr, size_t len)
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate 	const char	*uptr = ptr + len;
7297c478bd9Sstevel@tonic-gate 	while (len--) {
7307c478bd9Sstevel@tonic-gate 		if (*--uptr == '\n') {
7317c478bd9Sstevel@tonic-gate 			return ((char *)uptr);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	return (NULL);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate  * Duplicate the specified string converting each character
7397c478bd9Sstevel@tonic-gate  * into a lower case.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate static char *
7427c478bd9Sstevel@tonic-gate istrdup(const char *s1)
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	static size_t	ibuflen = 0;
7457c478bd9Sstevel@tonic-gate 	static char	*ibuf = NULL;
7467c478bd9Sstevel@tonic-gate 	size_t	slen;
7477c478bd9Sstevel@tonic-gate 	char	*p;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	slen = strlen(s1);
7507c478bd9Sstevel@tonic-gate 	if (slen >= ibuflen) {
7517c478bd9Sstevel@tonic-gate 		/* ibuf does not fit to s1 */
7527c478bd9Sstevel@tonic-gate 		ibuflen = slen + 1;
7537c478bd9Sstevel@tonic-gate 		ibuf = realloc(ibuf, ibuflen);
7547c478bd9Sstevel@tonic-gate 		if (ibuf == NULL) {
7557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7567c478bd9Sstevel@tonic-gate 			    gettext("%s: out of memory\n"), cmdname);
7577c478bd9Sstevel@tonic-gate 			exit(2);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	p = ibuf;
7617c478bd9Sstevel@tonic-gate 	do {
7627c478bd9Sstevel@tonic-gate 		*p++ = tolower(*s1);
7637c478bd9Sstevel@tonic-gate 	} while (*s1++ != '\0');
7647c478bd9Sstevel@tonic-gate 	return (ibuf);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate /*
7687c478bd9Sstevel@tonic-gate  * Do grep on a single file.
7697c478bd9Sstevel@tonic-gate  * Return true in any lines matched.
7707c478bd9Sstevel@tonic-gate  *
7717c478bd9Sstevel@tonic-gate  * We have two strategies:
7727c478bd9Sstevel@tonic-gate  * The fast one is used when we have a single pattern with
7737c478bd9Sstevel@tonic-gate  * a string known to occur in the pattern. We can then
7747c478bd9Sstevel@tonic-gate  * do a BMG match on the whole buffer.
7757c478bd9Sstevel@tonic-gate  * This is an order of magnitude faster.
7767c478bd9Sstevel@tonic-gate  * Otherwise we split the buffer into lines,
7777c478bd9Sstevel@tonic-gate  * and check for a match on each line.
7787c478bd9Sstevel@tonic-gate  */
7797c478bd9Sstevel@tonic-gate static int
780*e52fb54bSAlexander Eremin grep(int fd, const char *fn)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	PATTERN *pp;
7837c478bd9Sstevel@tonic-gate 	off_t	data_len;	/* length of the data chunk */
7847c478bd9Sstevel@tonic-gate 	off_t	line_len;	/* length of the current line */
7857c478bd9Sstevel@tonic-gate 	off_t	line_offset;	/* current line's offset from the beginning */
7867c478bd9Sstevel@tonic-gate 	long long	lineno;
7877c478bd9Sstevel@tonic-gate 	long long	matches = 0;	/* Number of matching lines */
7887c478bd9Sstevel@tonic-gate 	int	newlinep;	/* 0 if the last line of file has no newline */
7897c478bd9Sstevel@tonic-gate 	char	*ptr, *ptrend;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (patterns == NULL)
7937c478bd9Sstevel@tonic-gate 		return (0);	/* no patterns to match -- just return */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	pp = patterns;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (use_bmg) {
7987c478bd9Sstevel@tonic-gate 		bmgcomp(pp->pattern, strlen(pp->pattern));
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (use_wchar && outline == NULL) {
8027c478bd9Sstevel@tonic-gate 		outbuflen = BUFSIZE + 1;
8037c478bd9Sstevel@tonic-gate 		outline = malloc(sizeof (wchar_t) * outbuflen);
8047c478bd9Sstevel@tonic-gate 		if (outline == NULL) {
8057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: out of memory\n"),
8067c478bd9Sstevel@tonic-gate 			    cmdname);
8077c478bd9Sstevel@tonic-gate 			exit(2);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (prntbuf == NULL) {
8127c478bd9Sstevel@tonic-gate 		prntbuflen = BUFSIZE;
8137c478bd9Sstevel@tonic-gate 		if ((prntbuf = malloc(prntbuflen + 1)) == NULL) {
8147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: out of memory\n"),
8157c478bd9Sstevel@tonic-gate 			    cmdname);
8167c478bd9Sstevel@tonic-gate 			exit(2);
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	line_offset = 0;
8217c478bd9Sstevel@tonic-gate 	lineno = 0;
8227c478bd9Sstevel@tonic-gate 	newlinep = 1;
8237c478bd9Sstevel@tonic-gate 	data_len = 0;
8247c478bd9Sstevel@tonic-gate 	for (; ; ) {
8257c478bd9Sstevel@tonic-gate 		long	count;
8267c478bd9Sstevel@tonic-gate 		off_t	offset = 0;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		if (data_len == 0) {
8297c478bd9Sstevel@tonic-gate 			/*
8307c478bd9Sstevel@tonic-gate 			 * If no data in the buffer, reset ptr
8317c478bd9Sstevel@tonic-gate 			 */
8327c478bd9Sstevel@tonic-gate 			ptr = prntbuf;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 		if (ptr == prntbuf) {
8357c478bd9Sstevel@tonic-gate 			/*
8367c478bd9Sstevel@tonic-gate 			 * The current data chunk starts from prntbuf.
8377c478bd9Sstevel@tonic-gate 			 * This means either the buffer has no data
8387c478bd9Sstevel@tonic-gate 			 * or the buffer has no newline.
8397c478bd9Sstevel@tonic-gate 			 * So, read more data from input.
8407c478bd9Sstevel@tonic-gate 			 */
8417c478bd9Sstevel@tonic-gate 			count = read(fd, ptr + data_len, prntbuflen - data_len);
8427c478bd9Sstevel@tonic-gate 			if (count < 0) {
8437c478bd9Sstevel@tonic-gate 				/* read error */
8447c478bd9Sstevel@tonic-gate 				if (cflag) {
845*e52fb54bSAlexander Eremin 					if (outfn && !rflag) {
8467c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout,
8477c478bd9Sstevel@tonic-gate 						    "%s:", fn);
8487c478bd9Sstevel@tonic-gate 					}
849*e52fb54bSAlexander Eremin 					if (!qflag && !rflag) {
8507c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout, "%lld\n",
8517c478bd9Sstevel@tonic-gate 						    matches);
8527c478bd9Sstevel@tonic-gate 					}
8537c478bd9Sstevel@tonic-gate 				}
8547c478bd9Sstevel@tonic-gate 				return (0);
8557c478bd9Sstevel@tonic-gate 			} else if (count == 0) {
8567c478bd9Sstevel@tonic-gate 				/* no new data */
8577c478bd9Sstevel@tonic-gate 				if (data_len == 0) {
8587c478bd9Sstevel@tonic-gate 					/* end of file already reached */
8597c478bd9Sstevel@tonic-gate 					break;
8607c478bd9Sstevel@tonic-gate 				}
8617c478bd9Sstevel@tonic-gate 				/* last line of file has no newline */
8627c478bd9Sstevel@tonic-gate 				ptrend = ptr + data_len;
8637c478bd9Sstevel@tonic-gate 				newlinep = 0;
8647c478bd9Sstevel@tonic-gate 				goto L_start_process;
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 			offset = data_len;
8677c478bd9Sstevel@tonic-gate 			data_len += count;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * Look for newline in the chunk
8727c478bd9Sstevel@tonic-gate 		 * between ptr + offset and ptr + data_len - offset.
8737c478bd9Sstevel@tonic-gate 		 */
8747c478bd9Sstevel@tonic-gate 		ptrend = find_nl(ptr + offset, data_len - offset);
8757c478bd9Sstevel@tonic-gate 		if (ptrend == NULL) {
8767c478bd9Sstevel@tonic-gate 			/* no newline found in this chunk */
8777c478bd9Sstevel@tonic-gate 			if (ptr > prntbuf) {
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * Move remaining data to the beginning
8807c478bd9Sstevel@tonic-gate 				 * of the buffer.
8817c478bd9Sstevel@tonic-gate 				 * Remaining data lie from ptr for
8827c478bd9Sstevel@tonic-gate 				 * data_len bytes.
8837c478bd9Sstevel@tonic-gate 				 */
8847c478bd9Sstevel@tonic-gate 				(void) memmove(prntbuf, ptr, data_len);
8857c478bd9Sstevel@tonic-gate 			}
8867c478bd9Sstevel@tonic-gate 			if (data_len == prntbuflen) {
8877c478bd9Sstevel@tonic-gate 				/*
8887c478bd9Sstevel@tonic-gate 				 * No enough room in the buffer
8897c478bd9Sstevel@tonic-gate 				 */
8907c478bd9Sstevel@tonic-gate 				prntbuflen += BUFSIZE;
8917c478bd9Sstevel@tonic-gate 				prntbuf = realloc(prntbuf, prntbuflen + 1);
8927c478bd9Sstevel@tonic-gate 				if (prntbuf == NULL) {
8937c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8947c478bd9Sstevel@tonic-gate 					    gettext("%s: out of memory\n"),
8957c478bd9Sstevel@tonic-gate 					    cmdname);
8967c478bd9Sstevel@tonic-gate 					exit(2);
8977c478bd9Sstevel@tonic-gate 				}
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 			ptr = prntbuf;
9007c478bd9Sstevel@tonic-gate 			/* read the next input */
9017c478bd9Sstevel@tonic-gate 			continue;
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate L_start_process:
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		/*
9067c478bd9Sstevel@tonic-gate 		 * Beginning of the chunk:	ptr
9077c478bd9Sstevel@tonic-gate 		 * End of the chunk:		ptr + data_len
9087c478bd9Sstevel@tonic-gate 		 * Beginning of the line:	ptr
9097c478bd9Sstevel@tonic-gate 		 * End of the line:		ptrend
9107c478bd9Sstevel@tonic-gate 		 */
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		if (use_bmg) {
9137c478bd9Sstevel@tonic-gate 			/*
9147c478bd9Sstevel@tonic-gate 			 * Use Boyer-Moore-Gosper algorithm to find out if
9157c478bd9Sstevel@tonic-gate 			 * this chunk (not this line) contains the specified
9167c478bd9Sstevel@tonic-gate 			 * pattern.  If not, restart from the last line
9177c478bd9Sstevel@tonic-gate 			 * of this chunk.
9187c478bd9Sstevel@tonic-gate 			 */
9197c478bd9Sstevel@tonic-gate 			char	*bline;
9207c478bd9Sstevel@tonic-gate 			bline = bmgexec(ptr, ptr + data_len);
9217c478bd9Sstevel@tonic-gate 			if (bline == NULL) {
9227c478bd9Sstevel@tonic-gate 				/*
9237c478bd9Sstevel@tonic-gate 				 * No pattern found in this chunk.
9247c478bd9Sstevel@tonic-gate 				 * Need to find the last line
9257c478bd9Sstevel@tonic-gate 				 * in this chunk.
9267c478bd9Sstevel@tonic-gate 				 */
9277c478bd9Sstevel@tonic-gate 				ptrend = rfind_nl(ptr, data_len);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 				/*
9307c478bd9Sstevel@tonic-gate 				 * When this chunk does not contain newline,
9317c478bd9Sstevel@tonic-gate 				 * ptrend becomes NULL, which should happen
9327c478bd9Sstevel@tonic-gate 				 * when the last line of file does not end
9337c478bd9Sstevel@tonic-gate 				 * with a newline.  At such a point,
9347c478bd9Sstevel@tonic-gate 				 * newlinep should have been set to 0.
9357c478bd9Sstevel@tonic-gate 				 * Therefore, just after jumping to
9367c478bd9Sstevel@tonic-gate 				 * L_skip_line, the main for-loop quits,
9377c478bd9Sstevel@tonic-gate 				 * and the line_len value won't be
9387c478bd9Sstevel@tonic-gate 				 * used.
9397c478bd9Sstevel@tonic-gate 				 */
9407c478bd9Sstevel@tonic-gate 				line_len = ptrend - ptr;
9417c478bd9Sstevel@tonic-gate 				goto L_skip_line;
9427c478bd9Sstevel@tonic-gate 			}
9437c478bd9Sstevel@tonic-gate 			if (bline > ptrend) {
9447c478bd9Sstevel@tonic-gate 				/*
9457c478bd9Sstevel@tonic-gate 				 * Pattern found not in the first line
9467c478bd9Sstevel@tonic-gate 				 * of this chunk.
9477c478bd9Sstevel@tonic-gate 				 * Discard the first line.
9487c478bd9Sstevel@tonic-gate 				 */
9497c478bd9Sstevel@tonic-gate 				line_len = ptrend - ptr;
9507c478bd9Sstevel@tonic-gate 				goto L_skip_line;
9517c478bd9Sstevel@tonic-gate 			}
9527c478bd9Sstevel@tonic-gate 			/*
9537c478bd9Sstevel@tonic-gate 			 * Pattern found in the first line of this chunk.
9547c478bd9Sstevel@tonic-gate 			 * Using this result.
9557c478bd9Sstevel@tonic-gate 			 */
9567c478bd9Sstevel@tonic-gate 			*ptrend = '\0';
9577c478bd9Sstevel@tonic-gate 			line_len = ptrend - ptr;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 			/*
9607c478bd9Sstevel@tonic-gate 			 * before jumping to L_next_line,
9617c478bd9Sstevel@tonic-gate 			 * need to handle xflag if specified
9627c478bd9Sstevel@tonic-gate 			 */
9637c478bd9Sstevel@tonic-gate 			if (xflag && (line_len != bmglen ||
9647c478bd9Sstevel@tonic-gate 			    strcmp(bmgpat, ptr) != 0)) {
9657c478bd9Sstevel@tonic-gate 				/* didn't match */
9667c478bd9Sstevel@tonic-gate 				pp = NULL;
9677c478bd9Sstevel@tonic-gate 			} else {
9687c478bd9Sstevel@tonic-gate 				pp = patterns; /* to make it happen */
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 			goto L_next_line;
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		lineno++;
9737c478bd9Sstevel@tonic-gate 		/*
9747c478bd9Sstevel@tonic-gate 		 * Line starts from ptr and ends at ptrend.
9757c478bd9Sstevel@tonic-gate 		 * line_len will be the length of the line.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		*ptrend = '\0';
9787c478bd9Sstevel@tonic-gate 		line_len = ptrend - ptr;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		/*
9817c478bd9Sstevel@tonic-gate 		 * From now, the process will be performed based
9827c478bd9Sstevel@tonic-gate 		 * on the line from ptr to ptrend.
9837c478bd9Sstevel@tonic-gate 		 */
9847c478bd9Sstevel@tonic-gate 		if (use_wchar) {
9857c478bd9Sstevel@tonic-gate 			size_t	len;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 			if (line_len >= outbuflen) {
9887c478bd9Sstevel@tonic-gate 				outbuflen = line_len + 1;
9897c478bd9Sstevel@tonic-gate 				outline = realloc(outline,
9907c478bd9Sstevel@tonic-gate 				    sizeof (wchar_t) * outbuflen);
9917c478bd9Sstevel@tonic-gate 				if (outline == NULL) {
9927c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
9937c478bd9Sstevel@tonic-gate 					    gettext("%s: out of memory\n"),
9947c478bd9Sstevel@tonic-gate 					    cmdname);
9957c478bd9Sstevel@tonic-gate 					exit(2);
9967c478bd9Sstevel@tonic-gate 				}
9977c478bd9Sstevel@tonic-gate 			}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 			len = mbstowcs(outline, ptr, line_len);
10007c478bd9Sstevel@tonic-gate 			if (len == (size_t)-1) {
10017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
10027c478bd9Sstevel@tonic-gate 	"%s: input file \"%s\": line %lld: invalid multibyte character\n"),
10037c478bd9Sstevel@tonic-gate 				    cmdname, fn, lineno);
10047c478bd9Sstevel@tonic-gate 				/* never match a line with invalid sequence */
10057c478bd9Sstevel@tonic-gate 				goto L_skip_line;
10067c478bd9Sstevel@tonic-gate 			}
10077c478bd9Sstevel@tonic-gate 			outline[len] = L'\0';
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 			if (iflag) {
10107c478bd9Sstevel@tonic-gate 				wchar_t	*cp;
10117c478bd9Sstevel@tonic-gate 				for (cp = outline; *cp != '\0'; cp++) {
10127c478bd9Sstevel@tonic-gate 					*cp = towlower((wint_t)*cp);
10137c478bd9Sstevel@tonic-gate 				}
10147c478bd9Sstevel@tonic-gate 			}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			if (xflag) {
10177c478bd9Sstevel@tonic-gate 				for (pp = patterns; pp; pp = pp->next) {
10187c478bd9Sstevel@tonic-gate 					if (outline[0] == pp->wpattern[0] &&
10197c478bd9Sstevel@tonic-gate 					    wcscmp(outline,
10207c478bd9Sstevel@tonic-gate 					    pp->wpattern) == 0) {
10217c478bd9Sstevel@tonic-gate 						/* matched */
10227c478bd9Sstevel@tonic-gate 						break;
10237c478bd9Sstevel@tonic-gate 					}
10247c478bd9Sstevel@tonic-gate 				}
10257c478bd9Sstevel@tonic-gate 			} else {
10267c478bd9Sstevel@tonic-gate 				for (pp = patterns; pp; pp = pp->next) {
10277c478bd9Sstevel@tonic-gate 					if (wcswcs(outline, pp->wpattern)
10287c478bd9Sstevel@tonic-gate 					    != NULL) {
10297c478bd9Sstevel@tonic-gate 						/* matched */
10307c478bd9Sstevel@tonic-gate 						break;
10317c478bd9Sstevel@tonic-gate 					}
10327c478bd9Sstevel@tonic-gate 				}
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 		} else if (Fflag) {
10357c478bd9Sstevel@tonic-gate 			/* fgrep in byte-oriented handling */
10367c478bd9Sstevel@tonic-gate 			char	*fptr;
10377c478bd9Sstevel@tonic-gate 			if (iflag) {
10387c478bd9Sstevel@tonic-gate 				fptr = istrdup(ptr);
10397c478bd9Sstevel@tonic-gate 			} else {
10407c478bd9Sstevel@tonic-gate 				fptr = ptr;
10417c478bd9Sstevel@tonic-gate 			}
10427c478bd9Sstevel@tonic-gate 			if (xflag) {
10437c478bd9Sstevel@tonic-gate 				/* fgrep -x */
10447c478bd9Sstevel@tonic-gate 				for (pp = patterns; pp; pp = pp->next) {
10457c478bd9Sstevel@tonic-gate 					if (fptr[0] == pp->pattern[0] &&
10467c478bd9Sstevel@tonic-gate 					    strcmp(fptr, pp->pattern) == 0) {
10477c478bd9Sstevel@tonic-gate 						/* matched */
10487c478bd9Sstevel@tonic-gate 						break;
10497c478bd9Sstevel@tonic-gate 					}
10507c478bd9Sstevel@tonic-gate 				}
10517c478bd9Sstevel@tonic-gate 			} else {
10527c478bd9Sstevel@tonic-gate 				for (pp = patterns; pp; pp = pp->next) {
10537c478bd9Sstevel@tonic-gate 					if (strstr(fptr, pp->pattern) != NULL) {
10547c478bd9Sstevel@tonic-gate 						/* matched */
10557c478bd9Sstevel@tonic-gate 						break;
10567c478bd9Sstevel@tonic-gate 					}
10577c478bd9Sstevel@tonic-gate 				}
10587c478bd9Sstevel@tonic-gate 			}
10597c478bd9Sstevel@tonic-gate 		} else {
10607c478bd9Sstevel@tonic-gate 			/* grep or egrep */
10617c478bd9Sstevel@tonic-gate 			for (pp = patterns; pp; pp = pp->next) {
10627c478bd9Sstevel@tonic-gate 				int	rv;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 				rv = regexec(&pp->re, ptr, 0, NULL, 0);
10657c478bd9Sstevel@tonic-gate 				if (rv == REG_OK) {
10667c478bd9Sstevel@tonic-gate 					/* matched */
10677c478bd9Sstevel@tonic-gate 					break;
10687c478bd9Sstevel@tonic-gate 				}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 				switch (rv) {
10717c478bd9Sstevel@tonic-gate 				case REG_NOMATCH:
10727c478bd9Sstevel@tonic-gate 					break;
10737c478bd9Sstevel@tonic-gate 				case REG_ECHAR:
10747c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
10757c478bd9Sstevel@tonic-gate 	    "%s: input file \"%s\": line %lld: invalid multibyte character\n"),
10767c478bd9Sstevel@tonic-gate 					    cmdname, fn, lineno);
10777c478bd9Sstevel@tonic-gate 					break;
10787c478bd9Sstevel@tonic-gate 				default:
10797c478bd9Sstevel@tonic-gate 					(void) regerror(rv, &pp->re, errstr,
10807c478bd9Sstevel@tonic-gate 					    sizeof (errstr));
10817c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
10827c478bd9Sstevel@tonic-gate 	    "%s: input file \"%s\": line %lld: %s\n"),
10837c478bd9Sstevel@tonic-gate 					    cmdname, fn, lineno, errstr);
10847c478bd9Sstevel@tonic-gate 					exit(2);
10857c478bd9Sstevel@tonic-gate 				}
10867c478bd9Sstevel@tonic-gate 			}
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate L_next_line:
10907c478bd9Sstevel@tonic-gate 		/*
10917c478bd9Sstevel@tonic-gate 		 * Here, if pp points to non-NULL, something has been matched
10927c478bd9Sstevel@tonic-gate 		 * to the pattern.
10937c478bd9Sstevel@tonic-gate 		 */
10947c478bd9Sstevel@tonic-gate 		if (nvflag == (pp != NULL)) {
10957c478bd9Sstevel@tonic-gate 			matches++;
10967c478bd9Sstevel@tonic-gate 			/*
10977c478bd9Sstevel@tonic-gate 			 * Handle q, l, and c flags.
10987c478bd9Sstevel@tonic-gate 			 */
10997c478bd9Sstevel@tonic-gate 			if (qflag) {
11007c478bd9Sstevel@tonic-gate 				/* no need to continue */
11017c478bd9Sstevel@tonic-gate 				/*
11027c478bd9Sstevel@tonic-gate 				 * End of this line is ptrend.
11037c478bd9Sstevel@tonic-gate 				 * We have read up to ptr + data_len.
11047c478bd9Sstevel@tonic-gate 				 */
11057c478bd9Sstevel@tonic-gate 				off_t	pos;
11067c478bd9Sstevel@tonic-gate 				pos = ptr + data_len - (ptrend + 1);
11077c478bd9Sstevel@tonic-gate 				(void) lseek(fd, -pos, SEEK_CUR);
11087c478bd9Sstevel@tonic-gate 				exit(0);
11097c478bd9Sstevel@tonic-gate 			}
11107c478bd9Sstevel@tonic-gate 			if (lflag) {
11117c478bd9Sstevel@tonic-gate 				(void) printf("%s\n", fn);
11127c478bd9Sstevel@tonic-gate 				break;
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 			if (!cflag) {
11157c478bd9Sstevel@tonic-gate 				if (outfn) {
11167c478bd9Sstevel@tonic-gate 					(void) printf("%s:", fn);
11177c478bd9Sstevel@tonic-gate 				}
11187c478bd9Sstevel@tonic-gate 				if (bflag) {
11197c478bd9Sstevel@tonic-gate 					(void) printf("%lld:", (offset_t)
11207c478bd9Sstevel@tonic-gate 					    (line_offset / BSIZE));
11217c478bd9Sstevel@tonic-gate 				}
11227c478bd9Sstevel@tonic-gate 				if (nflag) {
11237c478bd9Sstevel@tonic-gate 					(void) printf("%lld:", lineno);
11247c478bd9Sstevel@tonic-gate 				}
11257c478bd9Sstevel@tonic-gate 				*ptrend = '\n';
11267c478bd9Sstevel@tonic-gate 				(void) fwrite(ptr, 1, line_len + 1, stdout);
11277c478bd9Sstevel@tonic-gate 			}
11287c478bd9Sstevel@tonic-gate 			if (ferror(stdout)) {
11297c478bd9Sstevel@tonic-gate 				return (0);
11307c478bd9Sstevel@tonic-gate 			}
11317c478bd9Sstevel@tonic-gate 		}
11327c478bd9Sstevel@tonic-gate L_skip_line:
11337c478bd9Sstevel@tonic-gate 		if (!newlinep)
11347c478bd9Sstevel@tonic-gate 			break;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		data_len -= line_len + 1;
11377c478bd9Sstevel@tonic-gate 		line_offset += line_len + 1;
11387c478bd9Sstevel@tonic-gate 		ptr = ptrend + 1;
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	if (cflag) {
11427c478bd9Sstevel@tonic-gate 		if (outfn) {
11437c478bd9Sstevel@tonic-gate 			(void) printf("%s:", fn);
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 		if (!qflag) {
11467c478bd9Sstevel@tonic-gate 			(void) printf("%lld\n", matches);
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 	return (matches != 0);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate  * usage message for grep
11547c478bd9Sstevel@tonic-gate  */
11557c478bd9Sstevel@tonic-gate static void
11567c478bd9Sstevel@tonic-gate usage(void)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	if (egrep || fgrep) {
11597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
11607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1161*e52fb54bSAlexander Eremin 		    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
11627c478bd9Sstevel@tonic-gate 		    "pattern_list [file ...]\n"));
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1166*e52fb54bSAlexander Eremin 		    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
1167*e52fb54bSAlexander Eremin 		    "[-e pattern_list]... "
11687c478bd9Sstevel@tonic-gate 		    "[-f pattern_file]... [file...]\n"));
11697c478bd9Sstevel@tonic-gate 	} else {
11707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
11717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1172*e52fb54bSAlexander Eremin 		    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
11737c478bd9Sstevel@tonic-gate 		    "pattern_list [file ...]\n"));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1177*e52fb54bSAlexander Eremin 		    gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
1178*e52fb54bSAlexander Eremin 		    "[-e pattern_list]... "
11797c478bd9Sstevel@tonic-gate 		    "[-f pattern_file]... [file...]\n"));
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1183*e52fb54bSAlexander Eremin 		    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
11847c478bd9Sstevel@tonic-gate 		    "pattern_list [file ...]\n"));
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1188*e52fb54bSAlexander Eremin 		    gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
1189*e52fb54bSAlexander Eremin 		    "[-e pattern_list]... "
11907c478bd9Sstevel@tonic-gate 		    "[-f pattern_file]... [file...]\n"));
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1194*e52fb54bSAlexander Eremin 		    gettext(" -F [-c|-l|-q] [-r|-R] [-bhinsvx] "
11957c478bd9Sstevel@tonic-gate 		    "pattern_list [file ...]\n"));
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s", cmdname);
11987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11997c478bd9Sstevel@tonic-gate 		    gettext(" -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... "
12007c478bd9Sstevel@tonic-gate 		    "[-f pattern_file]... [file...]\n"));
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 	exit(2);
12037c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Compile literal pattern into BMG tables
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate static void
12107c478bd9Sstevel@tonic-gate bmgcomp(char *pat, int len)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	int	i;
12137c478bd9Sstevel@tonic-gate 	int	tlen;
12147c478bd9Sstevel@tonic-gate 	unsigned char	*uc = (unsigned char *)pat;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	bmglen = len;
12177c478bd9Sstevel@tonic-gate 	bmgpat = pat;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	for (i = 0; i < M_CSETSIZE; i++) {
12207c478bd9Sstevel@tonic-gate 		bmgtab[i] = len;
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	len--;
12247c478bd9Sstevel@tonic-gate 	for (tlen = len, i = 0; i <= len; i++, tlen--) {
12257c478bd9Sstevel@tonic-gate 		bmgtab[*uc++] = tlen;
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate  * BMG search.
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static char *
12337c478bd9Sstevel@tonic-gate bmgexec(char *str, char *end)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	int	t;
12367c478bd9Sstevel@tonic-gate 	char	*k, *s, *p;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	k = str + bmglen - 1;
12397c478bd9Sstevel@tonic-gate 	if (bmglen == 1) {
12407c478bd9Sstevel@tonic-gate 		return (memchr(str, bmgpat[0], end - str));
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 	for (; ; ) {
12437c478bd9Sstevel@tonic-gate 		/* inner loop, should be most optimized */
12447c478bd9Sstevel@tonic-gate 		while (k < end && (t = bmgtab[(unsigned char)*k]) != 0) {
12457c478bd9Sstevel@tonic-gate 			k += t;
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 		if (k >= end) {
12487c478bd9Sstevel@tonic-gate 			return (NULL);
12497c478bd9Sstevel@tonic-gate 		}
12507c478bd9Sstevel@tonic-gate 		for (s = k, p = bmgpat + bmglen - 1; *--s == *--p; ) {
12517c478bd9Sstevel@tonic-gate 			if (p == bmgpat) {
12527c478bd9Sstevel@tonic-gate 				return (s);
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 		k++;
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
12587c478bd9Sstevel@tonic-gate }
1259