xref: /illumos-gate/usr/src/cmd/grep/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 /*
2355f91622Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
317c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
333ed621bcSAlexander Eremin /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * grep -- print lines matching (or not matching) a pattern
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *	status returns:
397c478bd9Sstevel@tonic-gate  *		0 - ok, and some matches
407c478bd9Sstevel@tonic-gate  *		1 - ok, but no matches
417c478bd9Sstevel@tonic-gate  *		2 - some error
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate #include <memory.h>
507c478bd9Sstevel@tonic-gate #include <regexpr.h>
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
55*e52fb54bSAlexander Eremin #include <ftw.h>
56*e52fb54bSAlexander Eremin #include <limits.h>
57*e52fb54bSAlexander Eremin #include <sys/param.h>
587c478bd9Sstevel@tonic-gate 
5955f91622Sceastha static const char *errstr[] = {
607c478bd9Sstevel@tonic-gate 	"Range endpoint too large.",
617c478bd9Sstevel@tonic-gate 	"Bad number.",
627c478bd9Sstevel@tonic-gate 	"``\\digit'' out of range.",
637c478bd9Sstevel@tonic-gate 	"No remembered search string.",
647c478bd9Sstevel@tonic-gate 	"\\( \\) imbalance.",
657c478bd9Sstevel@tonic-gate 	"Too many \\(.",
667c478bd9Sstevel@tonic-gate 	"More than 2 numbers given in \\{ \\}.",
677c478bd9Sstevel@tonic-gate 	"} expected after \\.",
687c478bd9Sstevel@tonic-gate 	"First number exceeds second in \\{ \\}.",
697c478bd9Sstevel@tonic-gate 	"[ ] imbalance.",
707c478bd9Sstevel@tonic-gate 	"Regular expression overflow.",
717c478bd9Sstevel@tonic-gate 	"Illegal byte sequence.",
727c478bd9Sstevel@tonic-gate 	"Unknown regexp error code!!",
737c478bd9Sstevel@tonic-gate 	NULL
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	errmsg(msg, arg)	(void) fprintf(stderr, gettext(msg), arg)
777c478bd9Sstevel@tonic-gate #define	BLKSIZE	512
787c478bd9Sstevel@tonic-gate #define	GBUFSIZ	8192
79*e52fb54bSAlexander Eremin #define	MAX_DEPTH	1000
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int	temp;
827c478bd9Sstevel@tonic-gate static long long	lnum;
837c478bd9Sstevel@tonic-gate static char	*linebuf;
847c478bd9Sstevel@tonic-gate static char	*prntbuf = NULL;
857c478bd9Sstevel@tonic-gate static long	fw_lPrntBufLen = 0;
867c478bd9Sstevel@tonic-gate static int	nflag;
877c478bd9Sstevel@tonic-gate static int	bflag;
887c478bd9Sstevel@tonic-gate static int	lflag;
897c478bd9Sstevel@tonic-gate static int	cflag;
90*e52fb54bSAlexander Eremin static int	rflag;
91*e52fb54bSAlexander Eremin static int	Rflag;
927c478bd9Sstevel@tonic-gate static int	vflag;
937c478bd9Sstevel@tonic-gate static int	sflag;
947c478bd9Sstevel@tonic-gate static int	iflag;
957c478bd9Sstevel@tonic-gate static int	wflag;
967c478bd9Sstevel@tonic-gate static int	hflag;
973ed621bcSAlexander Eremin static int	qflag;
987c478bd9Sstevel@tonic-gate static int	errflg;
997c478bd9Sstevel@tonic-gate static int	nfile;
1007c478bd9Sstevel@tonic-gate static long long	tln;
1017c478bd9Sstevel@tonic-gate static int	nsucc;
102*e52fb54bSAlexander Eremin static int	outfn = 0;
1037c478bd9Sstevel@tonic-gate static int	nlflag;
1047c478bd9Sstevel@tonic-gate static char	*ptr, *ptrend;
1057c478bd9Sstevel@tonic-gate static char	*expbuf;
1067c478bd9Sstevel@tonic-gate 
107*e52fb54bSAlexander Eremin static void	execute(const char *, int);
1087c478bd9Sstevel@tonic-gate static void	regerr(int);
109*e52fb54bSAlexander Eremin static void	prepare(const char *);
110*e52fb54bSAlexander Eremin static int	recursive(const char *, const struct stat *, int, struct FTW *);
111*e52fb54bSAlexander Eremin static int	succeed(const char *);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
11455f91622Sceastha main(int argc, char **argv)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int	c;
1177c478bd9Sstevel@tonic-gate 	char	*arg;
1187c478bd9Sstevel@tonic-gate 	extern int	optind;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1217c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1227c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1257c478bd9Sstevel@tonic-gate 
126*e52fb54bSAlexander Eremin 	while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
1277c478bd9Sstevel@tonic-gate 		switch (c) {
1287c478bd9Sstevel@tonic-gate 		case 'h':
1297c478bd9Sstevel@tonic-gate 			hflag++;
1307c478bd9Sstevel@tonic-gate 			break;
1313ed621bcSAlexander Eremin 		case 'q':	/* POSIX: quiet: status only */
1323ed621bcSAlexander Eremin 			qflag++;
1333ed621bcSAlexander Eremin 			break;
1347c478bd9Sstevel@tonic-gate 		case 'v':
1357c478bd9Sstevel@tonic-gate 			vflag++;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case 'c':
1387c478bd9Sstevel@tonic-gate 			cflag++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case 'n':
1417c478bd9Sstevel@tonic-gate 			nflag++;
1427c478bd9Sstevel@tonic-gate 			break;
143*e52fb54bSAlexander Eremin 		case 'R':
144*e52fb54bSAlexander Eremin 			Rflag++;
145*e52fb54bSAlexander Eremin 			/* FALLTHROUGH */
146*e52fb54bSAlexander Eremin 		case 'r':
147*e52fb54bSAlexander Eremin 			rflag++;
148*e52fb54bSAlexander Eremin 			break;
1497c478bd9Sstevel@tonic-gate 		case 'b':
1507c478bd9Sstevel@tonic-gate 			bflag++;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case 's':
1537c478bd9Sstevel@tonic-gate 			sflag++;
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		case 'l':
1567c478bd9Sstevel@tonic-gate 			lflag++;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case 'y':
1597c478bd9Sstevel@tonic-gate 		case 'i':
1607c478bd9Sstevel@tonic-gate 			iflag++;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case 'w':
1637c478bd9Sstevel@tonic-gate 			wflag++;
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		case '?':
1667c478bd9Sstevel@tonic-gate 			errflg++;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (errflg || (optind >= argc)) {
170*e52fb54bSAlexander Eremin 		errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
171*e52fb54bSAlexander Eremin 		    "pattern file . . .\n",
17255f91622Sceastha 		    (char *)NULL);
1737c478bd9Sstevel@tonic-gate 		exit(2);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1777c478bd9Sstevel@tonic-gate 	argc -= optind;
1787c478bd9Sstevel@tonic-gate 	nfile = argc - 1;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (strrchr(*argv, '\n') != NULL)
1817c478bd9Sstevel@tonic-gate 		regerr(41);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (iflag) {
1847c478bd9Sstevel@tonic-gate 		for (arg = *argv; *arg != NULL; ++arg)
1857c478bd9Sstevel@tonic-gate 			*arg = (char)tolower((int)((unsigned char)*arg));
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (wflag) {
1897c478bd9Sstevel@tonic-gate 		unsigned int	wordlen;
1907c478bd9Sstevel@tonic-gate 		char		*wordbuf;
1917c478bd9Sstevel@tonic-gate 
19255f91622Sceastha 		wordlen = strlen(*argv) + 5; /* '\\' '<' *argv '\\' '>' '\0' */
1937c478bd9Sstevel@tonic-gate 		if ((wordbuf = malloc(wordlen)) == NULL) {
1947c478bd9Sstevel@tonic-gate 			errmsg("grep: Out of memory for word\n", (char *)NULL);
1957c478bd9Sstevel@tonic-gate 			exit(2);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		(void) strcpy(wordbuf, "\\<");
1997c478bd9Sstevel@tonic-gate 		(void) strcat(wordbuf, *argv);
2007c478bd9Sstevel@tonic-gate 		(void) strcat(wordbuf, "\\>");
2017c478bd9Sstevel@tonic-gate 		*argv = wordbuf;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	expbuf = compile(*argv, (char *)0, (char *)0);
2057c478bd9Sstevel@tonic-gate 	if (regerrno)
2067c478bd9Sstevel@tonic-gate 		regerr(regerrno);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (--argc == 0)
209*e52fb54bSAlexander Eremin 		execute(NULL, 0);
2107c478bd9Sstevel@tonic-gate 	else
2117c478bd9Sstevel@tonic-gate 		while (argc-- > 0)
212*e52fb54bSAlexander Eremin 			prepare(*++argv);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (nsucc == 2 ? 2 : (nsucc == 0 ? 1 : 0));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static void
218*e52fb54bSAlexander Eremin prepare(const char *path)
219*e52fb54bSAlexander Eremin {
220*e52fb54bSAlexander Eremin 	struct	stat st;
221*e52fb54bSAlexander Eremin 	int	walkflags = FTW_CHDIR;
222*e52fb54bSAlexander Eremin 	char	*buf = NULL;
223*e52fb54bSAlexander Eremin 
224*e52fb54bSAlexander Eremin 	if (rflag) {
225*e52fb54bSAlexander Eremin 		if (stat(path, &st) != -1 &&
226*e52fb54bSAlexander Eremin 		    (st.st_mode & S_IFMT) == S_IFDIR) {
227*e52fb54bSAlexander Eremin 			outfn = 1;
228*e52fb54bSAlexander Eremin 
229*e52fb54bSAlexander Eremin 			/*
230*e52fb54bSAlexander Eremin 			 * Add trailing slash if arg
231*e52fb54bSAlexander Eremin 			 * is directory, to resolve symlinks.
232*e52fb54bSAlexander Eremin 			 */
233*e52fb54bSAlexander Eremin 			if (path[strlen(path) - 1] != '/') {
234*e52fb54bSAlexander Eremin 				(void) asprintf(&buf, "%s/", path);
235*e52fb54bSAlexander Eremin 				if (buf != NULL)
236*e52fb54bSAlexander Eremin 					path = buf;
237*e52fb54bSAlexander Eremin 			}
238*e52fb54bSAlexander Eremin 
239*e52fb54bSAlexander Eremin 			/*
240*e52fb54bSAlexander Eremin 			 * Search through subdirs if path is directory.
241*e52fb54bSAlexander Eremin 			 * Don't follow symlinks if Rflag is not set.
242*e52fb54bSAlexander Eremin 			 */
243*e52fb54bSAlexander Eremin 			if (!Rflag)
244*e52fb54bSAlexander Eremin 				walkflags |= FTW_PHYS;
245*e52fb54bSAlexander Eremin 
246*e52fb54bSAlexander Eremin 			if (nftw(path, recursive, MAX_DEPTH, walkflags) != 0) {
247*e52fb54bSAlexander Eremin 				if (!sflag)
248*e52fb54bSAlexander Eremin 					errmsg("grep: can't open %s\n", path);
249*e52fb54bSAlexander Eremin 				nsucc = 2;
250*e52fb54bSAlexander Eremin 			}
251*e52fb54bSAlexander Eremin 			return;
252*e52fb54bSAlexander Eremin 		}
253*e52fb54bSAlexander Eremin 	}
254*e52fb54bSAlexander Eremin 	execute(path, 0);
255*e52fb54bSAlexander Eremin }
256*e52fb54bSAlexander Eremin 
257*e52fb54bSAlexander Eremin static int
258*e52fb54bSAlexander Eremin recursive(const char *name, const struct stat *statp, int info, struct FTW *ftw)
259*e52fb54bSAlexander Eremin {
260*e52fb54bSAlexander Eremin 	/*
261*e52fb54bSAlexander Eremin 	 * process files and follow symlinks if Rflag set.
262*e52fb54bSAlexander Eremin 	 */
263*e52fb54bSAlexander Eremin 	if (info != FTW_F) {
264*e52fb54bSAlexander Eremin 		if (!sflag &&
265*e52fb54bSAlexander Eremin 		    (info == FTW_SLN || info == FTW_DNR || info == FTW_NS)) {
266*e52fb54bSAlexander Eremin 			/* report broken symlinks and unreadable files */
267*e52fb54bSAlexander Eremin 			errmsg("grep: can't open %s\n", name);
268*e52fb54bSAlexander Eremin 		}
269*e52fb54bSAlexander Eremin 		return (0);
270*e52fb54bSAlexander Eremin 	}
271*e52fb54bSAlexander Eremin 
272*e52fb54bSAlexander Eremin 	/* skip devices and pipes if Rflag is not set */
273*e52fb54bSAlexander Eremin 	if (!Rflag && !S_ISREG(statp->st_mode))
274*e52fb54bSAlexander Eremin 		return (0);
275*e52fb54bSAlexander Eremin 
276*e52fb54bSAlexander Eremin 	/* pass offset to relative name from FTW_CHDIR */
277*e52fb54bSAlexander Eremin 	execute(name, ftw->base);
278*e52fb54bSAlexander Eremin 	return (0);
279*e52fb54bSAlexander Eremin }
280*e52fb54bSAlexander Eremin 
281*e52fb54bSAlexander Eremin static void
282*e52fb54bSAlexander Eremin execute(const char *file, int base)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	char	*lbuf, *p;
2857c478bd9Sstevel@tonic-gate 	long	count;
2867c478bd9Sstevel@tonic-gate 	long	offset = 0;
2877c478bd9Sstevel@tonic-gate 	char	*next_ptr = NULL;
2887c478bd9Sstevel@tonic-gate 	long	next_count = 0;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	tln = 0;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (prntbuf == NULL) {
2937c478bd9Sstevel@tonic-gate 		fw_lPrntBufLen = GBUFSIZ + 1;
2947c478bd9Sstevel@tonic-gate 		if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
2957c478bd9Sstevel@tonic-gate 			exit(2); /* out of memory - BAIL */
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
2987c478bd9Sstevel@tonic-gate 			exit(2); /* out of memory - BAIL */
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (file == NULL)
3037c478bd9Sstevel@tonic-gate 		temp = 0;
304*e52fb54bSAlexander Eremin 	else if ((temp = open(file + base, O_RDONLY)) == -1) {
3057c478bd9Sstevel@tonic-gate 		if (!sflag)
3067c478bd9Sstevel@tonic-gate 			errmsg("grep: can't open %s\n", file);
3077c478bd9Sstevel@tonic-gate 		nsucc = 2;
3087c478bd9Sstevel@tonic-gate 		return;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* read in first block of bytes */
3127c478bd9Sstevel@tonic-gate 	if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
3137c478bd9Sstevel@tonic-gate 		(void) close(temp);
3147c478bd9Sstevel@tonic-gate 
3153ed621bcSAlexander Eremin 		if (cflag && !qflag) {
3167c478bd9Sstevel@tonic-gate 			if (nfile > 1 && !hflag && file)
3177c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s:", file);
318*e52fb54bSAlexander Eremin 			if (!rflag)
3197c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%lld\n", tln);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		return;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	lnum = 0;
3257c478bd9Sstevel@tonic-gate 	ptr = prntbuf;
3267c478bd9Sstevel@tonic-gate 	for (;;) {
3277c478bd9Sstevel@tonic-gate 		/* look for next newline */
3287c478bd9Sstevel@tonic-gate 		if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
3297c478bd9Sstevel@tonic-gate 			offset += count;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			/*
3327c478bd9Sstevel@tonic-gate 			 * shift unused data to the beginning of the buffer
3337c478bd9Sstevel@tonic-gate 			 */
3347c478bd9Sstevel@tonic-gate 			if (ptr > prntbuf) {
3357c478bd9Sstevel@tonic-gate 				(void) memmove(prntbuf, ptr, offset);
3367c478bd9Sstevel@tonic-gate 				ptr = prntbuf;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			/*
3407c478bd9Sstevel@tonic-gate 			 * re-allocate a larger buffer if this one is full
3417c478bd9Sstevel@tonic-gate 			 */
3427c478bd9Sstevel@tonic-gate 			if (offset + GBUFSIZ > fw_lPrntBufLen) {
3437c478bd9Sstevel@tonic-gate 				/*
3447c478bd9Sstevel@tonic-gate 				 * allocate a new buffer and preserve the
3457c478bd9Sstevel@tonic-gate 				 * contents...
3467c478bd9Sstevel@tonic-gate 				 */
3477c478bd9Sstevel@tonic-gate 				fw_lPrntBufLen += GBUFSIZ;
3487c478bd9Sstevel@tonic-gate 				if ((prntbuf = realloc(prntbuf,
3497c478bd9Sstevel@tonic-gate 				    fw_lPrntBufLen)) == NULL)
3507c478bd9Sstevel@tonic-gate 					exit(2);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 				/*
3537c478bd9Sstevel@tonic-gate 				 * set up a bigger linebuffer (this is only used
3547c478bd9Sstevel@tonic-gate 				 * for case insensitive operations). Contents do
3557c478bd9Sstevel@tonic-gate 				 * not have to be preserved.
3567c478bd9Sstevel@tonic-gate 				 */
3577c478bd9Sstevel@tonic-gate 				free(linebuf);
3587c478bd9Sstevel@tonic-gate 				if ((linebuf = malloc(fw_lPrntBufLen)) == NULL)
3597c478bd9Sstevel@tonic-gate 					exit(2);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 				ptr = prntbuf;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			p = prntbuf + offset;
3657c478bd9Sstevel@tonic-gate 			if ((count = read(temp, p, GBUFSIZ)) > 0)
3667c478bd9Sstevel@tonic-gate 				continue;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			if (offset == 0)
3697c478bd9Sstevel@tonic-gate 				/* end of file already reached */
3707c478bd9Sstevel@tonic-gate 				break;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			/* last line of file has no newline */
3737c478bd9Sstevel@tonic-gate 			ptrend = ptr + offset;
3747c478bd9Sstevel@tonic-gate 			nlflag = 0;
3757c478bd9Sstevel@tonic-gate 		} else {
3767c478bd9Sstevel@tonic-gate 			next_ptr = ptrend + 1;
3777c478bd9Sstevel@tonic-gate 			next_count = offset + count - (next_ptr - ptr);
3787c478bd9Sstevel@tonic-gate 			nlflag = 1;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		lnum++;
3817c478bd9Sstevel@tonic-gate 		*ptrend = '\0';
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		if (iflag) {
3847c478bd9Sstevel@tonic-gate 			/*
3857c478bd9Sstevel@tonic-gate 			 * Make a lower case copy of the record
3867c478bd9Sstevel@tonic-gate 			 */
3877c478bd9Sstevel@tonic-gate 			p = ptr;
3887c478bd9Sstevel@tonic-gate 			for (lbuf = linebuf; p < ptrend; )
3897c478bd9Sstevel@tonic-gate 				*lbuf++ = (char)tolower((int)
3907c478bd9Sstevel@tonic-gate 				    (unsigned char)*p++);
3917c478bd9Sstevel@tonic-gate 			*lbuf = '\0';
3927c478bd9Sstevel@tonic-gate 			lbuf = linebuf;
3937c478bd9Sstevel@tonic-gate 		} else
3947c478bd9Sstevel@tonic-gate 			/*
3957c478bd9Sstevel@tonic-gate 			 * Use record as is
3967c478bd9Sstevel@tonic-gate 			 */
3977c478bd9Sstevel@tonic-gate 			lbuf = ptr;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		/* lflag only once */
4007c478bd9Sstevel@tonic-gate 		if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
4017c478bd9Sstevel@tonic-gate 			break;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if (!nlflag)
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		ptr = next_ptr;
4077c478bd9Sstevel@tonic-gate 		count = next_count;
4087c478bd9Sstevel@tonic-gate 		offset = 0;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	(void) close(temp);
4117c478bd9Sstevel@tonic-gate 
4123ed621bcSAlexander Eremin 	if (cflag && !qflag) {
413*e52fb54bSAlexander Eremin 		if (!hflag && file && (nfile > 1 ||
414*e52fb54bSAlexander Eremin 		    (rflag && outfn)))
4157c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", file);
4167c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld\n", tln);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static int
421*e52fb54bSAlexander Eremin succeed(const char *f)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int nchars;
4247c478bd9Sstevel@tonic-gate 	nsucc = (nsucc == 2) ? 2 : 1;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (f == NULL)
4277c478bd9Sstevel@tonic-gate 		f = "<stdin>";
4287c478bd9Sstevel@tonic-gate 
4293ed621bcSAlexander Eremin 	if (qflag) {
4303ed621bcSAlexander Eremin 		/* no need to continue */
4313ed621bcSAlexander Eremin 		return (1);
4323ed621bcSAlexander Eremin 	}
4333ed621bcSAlexander Eremin 
4347c478bd9Sstevel@tonic-gate 	if (cflag) {
4357c478bd9Sstevel@tonic-gate 		tln++;
4367c478bd9Sstevel@tonic-gate 		return (0);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (lflag) {
4407c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", f);
4417c478bd9Sstevel@tonic-gate 		return (1);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
444*e52fb54bSAlexander Eremin 	if (!hflag && (nfile > 1 || (rflag && outfn))) {
4457c478bd9Sstevel@tonic-gate 		/* print filename */
4467c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", f);
447*e52fb54bSAlexander Eremin 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (bflag)
4507c478bd9Sstevel@tonic-gate 		/* print block number */
4517c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld:", (offset_t)
45255f91622Sceastha 		    ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (nflag)
4557c478bd9Sstevel@tonic-gate 		/* print line number */
4567c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld:", lnum);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (nlflag) {
4597c478bd9Sstevel@tonic-gate 		/* newline at end of line */
4607c478bd9Sstevel@tonic-gate 		*ptrend = '\n';
4617c478bd9Sstevel@tonic-gate 		nchars = ptrend - ptr + 1;
4627c478bd9Sstevel@tonic-gate 	} else {
4637c478bd9Sstevel@tonic-gate 		/* don't write sentinel \0 */
4647c478bd9Sstevel@tonic-gate 		nchars = ptrend - ptr;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	(void) fwrite(ptr, 1, nchars, stdout);
4687c478bd9Sstevel@tonic-gate 	return (0);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate static void
47255f91622Sceastha regerr(int err)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	errmsg("grep: RE error %d: ", err);
4757c478bd9Sstevel@tonic-gate 	switch (err) {
4767c478bd9Sstevel@tonic-gate 		case 11:
4777c478bd9Sstevel@tonic-gate 			err = 0;
4787c478bd9Sstevel@tonic-gate 			break;
4797c478bd9Sstevel@tonic-gate 		case 16:
4807c478bd9Sstevel@tonic-gate 			err = 1;
4817c478bd9Sstevel@tonic-gate 			break;
4827c478bd9Sstevel@tonic-gate 		case 25:
4837c478bd9Sstevel@tonic-gate 			err = 2;
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		case 41:
4867c478bd9Sstevel@tonic-gate 			err = 3;
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 		case 42:
4897c478bd9Sstevel@tonic-gate 			err = 4;
4907c478bd9Sstevel@tonic-gate 			break;
4917c478bd9Sstevel@tonic-gate 		case 43:
4927c478bd9Sstevel@tonic-gate 			err = 5;
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 		case 44:
4957c478bd9Sstevel@tonic-gate 			err = 6;
4967c478bd9Sstevel@tonic-gate 			break;
4977c478bd9Sstevel@tonic-gate 		case 45:
4987c478bd9Sstevel@tonic-gate 			err = 7;
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 		case 46:
5017c478bd9Sstevel@tonic-gate 			err = 8;
5027c478bd9Sstevel@tonic-gate 			break;
5037c478bd9Sstevel@tonic-gate 		case 49:
5047c478bd9Sstevel@tonic-gate 			err = 9;
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 		case 50:
5077c478bd9Sstevel@tonic-gate 			err = 10;
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 		case 67:
5107c478bd9Sstevel@tonic-gate 			err = 11;
5117c478bd9Sstevel@tonic-gate 			break;
5127c478bd9Sstevel@tonic-gate 		default:
5137c478bd9Sstevel@tonic-gate 			err = 12;
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	errmsg("%s\n", gettext(errstr[err]));
5187c478bd9Sstevel@tonic-gate 	exit(2);
5197c478bd9Sstevel@tonic-gate }
520