xref: /illumos-gate/usr/src/cmd/grep/grep.c (revision 3ed621bc8eb70ee11c69cd408fb8450eac33c281)
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 
33*3ed621bcSAlexander 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>
557c478bd9Sstevel@tonic-gate 
5655f91622Sceastha static const char *errstr[] = {
577c478bd9Sstevel@tonic-gate 	"Range endpoint too large.",
587c478bd9Sstevel@tonic-gate 	"Bad number.",
597c478bd9Sstevel@tonic-gate 	"``\\digit'' out of range.",
607c478bd9Sstevel@tonic-gate 	"No remembered search string.",
617c478bd9Sstevel@tonic-gate 	"\\( \\) imbalance.",
627c478bd9Sstevel@tonic-gate 	"Too many \\(.",
637c478bd9Sstevel@tonic-gate 	"More than 2 numbers given in \\{ \\}.",
647c478bd9Sstevel@tonic-gate 	"} expected after \\.",
657c478bd9Sstevel@tonic-gate 	"First number exceeds second in \\{ \\}.",
667c478bd9Sstevel@tonic-gate 	"[ ] imbalance.",
677c478bd9Sstevel@tonic-gate 	"Regular expression overflow.",
687c478bd9Sstevel@tonic-gate 	"Illegal byte sequence.",
697c478bd9Sstevel@tonic-gate 	"Unknown regexp error code!!",
707c478bd9Sstevel@tonic-gate 	NULL
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	errmsg(msg, arg)	(void) fprintf(stderr, gettext(msg), arg)
747c478bd9Sstevel@tonic-gate #define	BLKSIZE	512
757c478bd9Sstevel@tonic-gate #define	GBUFSIZ	8192
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int	temp;
787c478bd9Sstevel@tonic-gate static long long	lnum;
797c478bd9Sstevel@tonic-gate static char	*linebuf;
807c478bd9Sstevel@tonic-gate static char	*prntbuf = NULL;
817c478bd9Sstevel@tonic-gate static long	fw_lPrntBufLen = 0;
827c478bd9Sstevel@tonic-gate static int	nflag;
837c478bd9Sstevel@tonic-gate static int	bflag;
847c478bd9Sstevel@tonic-gate static int	lflag;
857c478bd9Sstevel@tonic-gate static int	cflag;
867c478bd9Sstevel@tonic-gate static int	vflag;
877c478bd9Sstevel@tonic-gate static int	sflag;
887c478bd9Sstevel@tonic-gate static int	iflag;
897c478bd9Sstevel@tonic-gate static int	wflag;
907c478bd9Sstevel@tonic-gate static int	hflag;
91*3ed621bcSAlexander Eremin static int	qflag;
927c478bd9Sstevel@tonic-gate static int	errflg;
937c478bd9Sstevel@tonic-gate static int	nfile;
947c478bd9Sstevel@tonic-gate static long long	tln;
957c478bd9Sstevel@tonic-gate static int	nsucc;
967c478bd9Sstevel@tonic-gate static int	nlflag;
977c478bd9Sstevel@tonic-gate static char	*ptr, *ptrend;
987c478bd9Sstevel@tonic-gate static char	*expbuf;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void	execute(char *);
1017c478bd9Sstevel@tonic-gate static void	regerr(int);
1027c478bd9Sstevel@tonic-gate static int	succeed(char *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate int
10555f91622Sceastha main(int argc, char **argv)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	int	c;
1087c478bd9Sstevel@tonic-gate 	char	*arg;
1097c478bd9Sstevel@tonic-gate 	extern int	optind;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1127c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1137c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1167c478bd9Sstevel@tonic-gate 
117*3ed621bcSAlexander Eremin 	while ((c = getopt(argc, argv, "hqblcnsviyw")) != -1)
1187c478bd9Sstevel@tonic-gate 		switch (c) {
1197c478bd9Sstevel@tonic-gate 		case 'h':
1207c478bd9Sstevel@tonic-gate 			hflag++;
1217c478bd9Sstevel@tonic-gate 			break;
122*3ed621bcSAlexander Eremin 		case 'q':	/* POSIX: quiet: status only */
123*3ed621bcSAlexander Eremin 			qflag++;
124*3ed621bcSAlexander Eremin 			break;
1257c478bd9Sstevel@tonic-gate 		case 'v':
1267c478bd9Sstevel@tonic-gate 			vflag++;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case 'c':
1297c478bd9Sstevel@tonic-gate 			cflag++;
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case 'n':
1327c478bd9Sstevel@tonic-gate 			nflag++;
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case 'b':
1357c478bd9Sstevel@tonic-gate 			bflag++;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case 's':
1387c478bd9Sstevel@tonic-gate 			sflag++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case 'l':
1417c478bd9Sstevel@tonic-gate 			lflag++;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		case 'y':
1447c478bd9Sstevel@tonic-gate 		case 'i':
1457c478bd9Sstevel@tonic-gate 			iflag++;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 		case 'w':
1487c478bd9Sstevel@tonic-gate 			wflag++;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		case '?':
1517c478bd9Sstevel@tonic-gate 			errflg++;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (errflg || (optind >= argc)) {
155*3ed621bcSAlexander Eremin 		errmsg("Usage: grep [-c|-l|-q] -hbnsviw pattern file . . .\n",
15655f91622Sceastha 		    (char *)NULL);
1577c478bd9Sstevel@tonic-gate 		exit(2);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1617c478bd9Sstevel@tonic-gate 	argc -= optind;
1627c478bd9Sstevel@tonic-gate 	nfile = argc - 1;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (strrchr(*argv, '\n') != NULL)
1657c478bd9Sstevel@tonic-gate 		regerr(41);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (iflag) {
1687c478bd9Sstevel@tonic-gate 		for (arg = *argv; *arg != NULL; ++arg)
1697c478bd9Sstevel@tonic-gate 			*arg = (char)tolower((int)((unsigned char)*arg));
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (wflag) {
1737c478bd9Sstevel@tonic-gate 		unsigned int	wordlen;
1747c478bd9Sstevel@tonic-gate 		char		*wordbuf;
1757c478bd9Sstevel@tonic-gate 
17655f91622Sceastha 		wordlen = strlen(*argv) + 5; /* '\\' '<' *argv '\\' '>' '\0' */
1777c478bd9Sstevel@tonic-gate 		if ((wordbuf = malloc(wordlen)) == NULL) {
1787c478bd9Sstevel@tonic-gate 			errmsg("grep: Out of memory for word\n", (char *)NULL);
1797c478bd9Sstevel@tonic-gate 			exit(2);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		(void) strcpy(wordbuf, "\\<");
1837c478bd9Sstevel@tonic-gate 		(void) strcat(wordbuf, *argv);
1847c478bd9Sstevel@tonic-gate 		(void) strcat(wordbuf, "\\>");
1857c478bd9Sstevel@tonic-gate 		*argv = wordbuf;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	expbuf = compile(*argv, (char *)0, (char *)0);
1897c478bd9Sstevel@tonic-gate 	if (regerrno)
1907c478bd9Sstevel@tonic-gate 		regerr(regerrno);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (--argc == 0)
1937c478bd9Sstevel@tonic-gate 		execute(NULL);
1947c478bd9Sstevel@tonic-gate 	else
1957c478bd9Sstevel@tonic-gate 		while (argc-- > 0)
1967c478bd9Sstevel@tonic-gate 			execute(*++argv);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (nsucc == 2 ? 2 : (nsucc == 0 ? 1 : 0));
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static void
20255f91622Sceastha execute(char *file)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	char	*lbuf, *p;
2057c478bd9Sstevel@tonic-gate 	long	count;
2067c478bd9Sstevel@tonic-gate 	long	offset = 0;
2077c478bd9Sstevel@tonic-gate 	char	*next_ptr = NULL;
2087c478bd9Sstevel@tonic-gate 	long	next_count = 0;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	tln = 0;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (prntbuf == NULL) {
2137c478bd9Sstevel@tonic-gate 		fw_lPrntBufLen = GBUFSIZ + 1;
2147c478bd9Sstevel@tonic-gate 		if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
2157c478bd9Sstevel@tonic-gate 			exit(2); /* out of memory - BAIL */
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
2187c478bd9Sstevel@tonic-gate 			exit(2); /* out of memory - BAIL */
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (file == NULL)
2237c478bd9Sstevel@tonic-gate 		temp = 0;
2247c478bd9Sstevel@tonic-gate 	else if ((temp = open(file, O_RDONLY)) == -1) {
2257c478bd9Sstevel@tonic-gate 		if (!sflag)
2267c478bd9Sstevel@tonic-gate 			errmsg("grep: can't open %s\n", file);
2277c478bd9Sstevel@tonic-gate 		nsucc = 2;
2287c478bd9Sstevel@tonic-gate 		return;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* read in first block of bytes */
2327c478bd9Sstevel@tonic-gate 	if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
2337c478bd9Sstevel@tonic-gate 		(void) close(temp);
2347c478bd9Sstevel@tonic-gate 
235*3ed621bcSAlexander Eremin 		if (cflag && !qflag) {
2367c478bd9Sstevel@tonic-gate 			if (nfile > 1 && !hflag && file)
2377c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s:", file);
2387c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%lld\n", tln);
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	lnum = 0;
2447c478bd9Sstevel@tonic-gate 	ptr = prntbuf;
2457c478bd9Sstevel@tonic-gate 	for (;;) {
2467c478bd9Sstevel@tonic-gate 		/* look for next newline */
2477c478bd9Sstevel@tonic-gate 		if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
2487c478bd9Sstevel@tonic-gate 			offset += count;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 			/*
2517c478bd9Sstevel@tonic-gate 			 * shift unused data to the beginning of the buffer
2527c478bd9Sstevel@tonic-gate 			 */
2537c478bd9Sstevel@tonic-gate 			if (ptr > prntbuf) {
2547c478bd9Sstevel@tonic-gate 				(void) memmove(prntbuf, ptr, offset);
2557c478bd9Sstevel@tonic-gate 				ptr = prntbuf;
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			/*
2597c478bd9Sstevel@tonic-gate 			 * re-allocate a larger buffer if this one is full
2607c478bd9Sstevel@tonic-gate 			 */
2617c478bd9Sstevel@tonic-gate 			if (offset + GBUFSIZ > fw_lPrntBufLen) {
2627c478bd9Sstevel@tonic-gate 				/*
2637c478bd9Sstevel@tonic-gate 				 * allocate a new buffer and preserve the
2647c478bd9Sstevel@tonic-gate 				 * contents...
2657c478bd9Sstevel@tonic-gate 				 */
2667c478bd9Sstevel@tonic-gate 				fw_lPrntBufLen += GBUFSIZ;
2677c478bd9Sstevel@tonic-gate 				if ((prntbuf = realloc(prntbuf,
2687c478bd9Sstevel@tonic-gate 				    fw_lPrntBufLen)) == NULL)
2697c478bd9Sstevel@tonic-gate 					exit(2);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 				/*
2727c478bd9Sstevel@tonic-gate 				 * set up a bigger linebuffer (this is only used
2737c478bd9Sstevel@tonic-gate 				 * for case insensitive operations). Contents do
2747c478bd9Sstevel@tonic-gate 				 * not have to be preserved.
2757c478bd9Sstevel@tonic-gate 				 */
2767c478bd9Sstevel@tonic-gate 				free(linebuf);
2777c478bd9Sstevel@tonic-gate 				if ((linebuf = malloc(fw_lPrntBufLen)) == NULL)
2787c478bd9Sstevel@tonic-gate 					exit(2);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 				ptr = prntbuf;
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			p = prntbuf + offset;
2847c478bd9Sstevel@tonic-gate 			if ((count = read(temp, p, GBUFSIZ)) > 0)
2857c478bd9Sstevel@tonic-gate 				continue;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			if (offset == 0)
2887c478bd9Sstevel@tonic-gate 				/* end of file already reached */
2897c478bd9Sstevel@tonic-gate 				break;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			/* last line of file has no newline */
2927c478bd9Sstevel@tonic-gate 			ptrend = ptr + offset;
2937c478bd9Sstevel@tonic-gate 			nlflag = 0;
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			next_ptr = ptrend + 1;
2967c478bd9Sstevel@tonic-gate 			next_count = offset + count - (next_ptr - ptr);
2977c478bd9Sstevel@tonic-gate 			nlflag = 1;
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 		lnum++;
3007c478bd9Sstevel@tonic-gate 		*ptrend = '\0';
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		if (iflag) {
3037c478bd9Sstevel@tonic-gate 			/*
3047c478bd9Sstevel@tonic-gate 			 * Make a lower case copy of the record
3057c478bd9Sstevel@tonic-gate 			 */
3067c478bd9Sstevel@tonic-gate 			p = ptr;
3077c478bd9Sstevel@tonic-gate 			for (lbuf = linebuf; p < ptrend; )
3087c478bd9Sstevel@tonic-gate 				*lbuf++ = (char)tolower((int)
3097c478bd9Sstevel@tonic-gate 				    (unsigned char)*p++);
3107c478bd9Sstevel@tonic-gate 			*lbuf = '\0';
3117c478bd9Sstevel@tonic-gate 			lbuf = linebuf;
3127c478bd9Sstevel@tonic-gate 		} else
3137c478bd9Sstevel@tonic-gate 			/*
3147c478bd9Sstevel@tonic-gate 			 * Use record as is
3157c478bd9Sstevel@tonic-gate 			 */
3167c478bd9Sstevel@tonic-gate 			lbuf = ptr;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		/* lflag only once */
3197c478bd9Sstevel@tonic-gate 		if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		if (!nlflag)
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		ptr = next_ptr;
3267c478bd9Sstevel@tonic-gate 		count = next_count;
3277c478bd9Sstevel@tonic-gate 		offset = 0;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	(void) close(temp);
3307c478bd9Sstevel@tonic-gate 
331*3ed621bcSAlexander Eremin 	if (cflag && !qflag) {
3327c478bd9Sstevel@tonic-gate 		if (nfile > 1 && !hflag && file)
3337c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", file);
3347c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld\n", tln);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate static int
33955f91622Sceastha succeed(char *f)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	int nchars;
3427c478bd9Sstevel@tonic-gate 	nsucc = (nsucc == 2) ? 2 : 1;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (f == NULL)
3457c478bd9Sstevel@tonic-gate 		f = "<stdin>";
3467c478bd9Sstevel@tonic-gate 
347*3ed621bcSAlexander Eremin 	if (qflag) {
348*3ed621bcSAlexander Eremin 		/* no need to continue */
349*3ed621bcSAlexander Eremin 		return (1);
350*3ed621bcSAlexander Eremin 	}
351*3ed621bcSAlexander Eremin 
3527c478bd9Sstevel@tonic-gate 	if (cflag) {
3537c478bd9Sstevel@tonic-gate 		tln++;
3547c478bd9Sstevel@tonic-gate 		return (0);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (lflag) {
3587c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", f);
3597c478bd9Sstevel@tonic-gate 		return (1);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (nfile > 1 && !hflag)
3637c478bd9Sstevel@tonic-gate 		/* print filename */
3647c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", f);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (bflag)
3677c478bd9Sstevel@tonic-gate 		/* print block number */
3687c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld:", (offset_t)
36955f91622Sceastha 		    ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (nflag)
3727c478bd9Sstevel@tonic-gate 		/* print line number */
3737c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld:", lnum);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (nlflag) {
3767c478bd9Sstevel@tonic-gate 		/* newline at end of line */
3777c478bd9Sstevel@tonic-gate 		*ptrend = '\n';
3787c478bd9Sstevel@tonic-gate 		nchars = ptrend - ptr + 1;
3797c478bd9Sstevel@tonic-gate 	} else {
3807c478bd9Sstevel@tonic-gate 		/* don't write sentinel \0 */
3817c478bd9Sstevel@tonic-gate 		nchars = ptrend - ptr;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	(void) fwrite(ptr, 1, nchars, stdout);
3857c478bd9Sstevel@tonic-gate 	return (0);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static void
38955f91622Sceastha regerr(int err)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	errmsg("grep: RE error %d: ", err);
3927c478bd9Sstevel@tonic-gate 	switch (err) {
3937c478bd9Sstevel@tonic-gate 		case 11:
3947c478bd9Sstevel@tonic-gate 			err = 0;
3957c478bd9Sstevel@tonic-gate 			break;
3967c478bd9Sstevel@tonic-gate 		case 16:
3977c478bd9Sstevel@tonic-gate 			err = 1;
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		case 25:
4007c478bd9Sstevel@tonic-gate 			err = 2;
4017c478bd9Sstevel@tonic-gate 			break;
4027c478bd9Sstevel@tonic-gate 		case 41:
4037c478bd9Sstevel@tonic-gate 			err = 3;
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 		case 42:
4067c478bd9Sstevel@tonic-gate 			err = 4;
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 		case 43:
4097c478bd9Sstevel@tonic-gate 			err = 5;
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 		case 44:
4127c478bd9Sstevel@tonic-gate 			err = 6;
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate 		case 45:
4157c478bd9Sstevel@tonic-gate 			err = 7;
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 		case 46:
4187c478bd9Sstevel@tonic-gate 			err = 8;
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 		case 49:
4217c478bd9Sstevel@tonic-gate 			err = 9;
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 		case 50:
4247c478bd9Sstevel@tonic-gate 			err = 10;
4257c478bd9Sstevel@tonic-gate 			break;
4267c478bd9Sstevel@tonic-gate 		case 67:
4277c478bd9Sstevel@tonic-gate 			err = 11;
4287c478bd9Sstevel@tonic-gate 			break;
4297c478bd9Sstevel@tonic-gate 		default:
4307c478bd9Sstevel@tonic-gate 			err = 12;
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	errmsg("%s\n", gettext(errstr[err]));
4357c478bd9Sstevel@tonic-gate 	exit(2);
4367c478bd9Sstevel@tonic-gate }
437