10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*486Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */
310Sstevel@tonic-gate /* All Rights Reserved */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
340Sstevel@tonic-gate
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate * grep -- print lines matching (or not matching) a pattern
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * status returns:
390Sstevel@tonic-gate * 0 - ok, and some matches
400Sstevel@tonic-gate * 1 - ok, but no matches
410Sstevel@tonic-gate * 2 - some error
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include <memory.h>
500Sstevel@tonic-gate #include <regexpr.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <string.h>
540Sstevel@tonic-gate #include <unistd.h>
550Sstevel@tonic-gate
56*486Sceastha static const char *errstr[] = {
570Sstevel@tonic-gate "Range endpoint too large.",
580Sstevel@tonic-gate "Bad number.",
590Sstevel@tonic-gate "``\\digit'' out of range.",
600Sstevel@tonic-gate "No remembered search string.",
610Sstevel@tonic-gate "\\( \\) imbalance.",
620Sstevel@tonic-gate "Too many \\(.",
630Sstevel@tonic-gate "More than 2 numbers given in \\{ \\}.",
640Sstevel@tonic-gate "} expected after \\.",
650Sstevel@tonic-gate "First number exceeds second in \\{ \\}.",
660Sstevel@tonic-gate "[ ] imbalance.",
670Sstevel@tonic-gate "Regular expression overflow.",
680Sstevel@tonic-gate "Illegal byte sequence.",
690Sstevel@tonic-gate "Unknown regexp error code!!",
700Sstevel@tonic-gate NULL
710Sstevel@tonic-gate };
720Sstevel@tonic-gate
730Sstevel@tonic-gate #define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg)
740Sstevel@tonic-gate #define BLKSIZE 512
750Sstevel@tonic-gate #define GBUFSIZ 8192
760Sstevel@tonic-gate
770Sstevel@tonic-gate static int temp;
780Sstevel@tonic-gate static long long lnum;
790Sstevel@tonic-gate static char *linebuf;
800Sstevel@tonic-gate static char *prntbuf = NULL;
810Sstevel@tonic-gate static long fw_lPrntBufLen = 0;
820Sstevel@tonic-gate static int nflag;
830Sstevel@tonic-gate static int bflag;
840Sstevel@tonic-gate static int lflag;
850Sstevel@tonic-gate static int cflag;
860Sstevel@tonic-gate static int vflag;
870Sstevel@tonic-gate static int sflag;
880Sstevel@tonic-gate static int iflag;
890Sstevel@tonic-gate static int wflag;
900Sstevel@tonic-gate static int hflag;
910Sstevel@tonic-gate static int errflg;
920Sstevel@tonic-gate static int nfile;
930Sstevel@tonic-gate static long long tln;
940Sstevel@tonic-gate static int nsucc;
950Sstevel@tonic-gate static int nlflag;
960Sstevel@tonic-gate static char *ptr, *ptrend;
970Sstevel@tonic-gate static char *expbuf;
980Sstevel@tonic-gate
990Sstevel@tonic-gate static void execute(char *);
1000Sstevel@tonic-gate static void regerr(int);
1010Sstevel@tonic-gate static int succeed(char *);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate int
main(int argc,char ** argv)104*486Sceastha main(int argc, char **argv)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate int c;
1070Sstevel@tonic-gate char *arg;
1080Sstevel@tonic-gate extern int optind;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1110Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1120Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate while ((c = getopt(argc, argv, "hblcnsviyw")) != -1)
1170Sstevel@tonic-gate switch (c) {
1180Sstevel@tonic-gate case 'h':
1190Sstevel@tonic-gate hflag++;
1200Sstevel@tonic-gate break;
1210Sstevel@tonic-gate case 'v':
1220Sstevel@tonic-gate vflag++;
1230Sstevel@tonic-gate break;
1240Sstevel@tonic-gate case 'c':
1250Sstevel@tonic-gate cflag++;
1260Sstevel@tonic-gate break;
1270Sstevel@tonic-gate case 'n':
1280Sstevel@tonic-gate nflag++;
1290Sstevel@tonic-gate break;
1300Sstevel@tonic-gate case 'b':
1310Sstevel@tonic-gate bflag++;
1320Sstevel@tonic-gate break;
1330Sstevel@tonic-gate case 's':
1340Sstevel@tonic-gate sflag++;
1350Sstevel@tonic-gate break;
1360Sstevel@tonic-gate case 'l':
1370Sstevel@tonic-gate lflag++;
1380Sstevel@tonic-gate break;
1390Sstevel@tonic-gate case 'y':
1400Sstevel@tonic-gate case 'i':
1410Sstevel@tonic-gate iflag++;
1420Sstevel@tonic-gate break;
1430Sstevel@tonic-gate case 'w':
1440Sstevel@tonic-gate wflag++;
1450Sstevel@tonic-gate break;
1460Sstevel@tonic-gate case '?':
1470Sstevel@tonic-gate errflg++;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (errflg || (optind >= argc)) {
1510Sstevel@tonic-gate errmsg("Usage: grep -hblcnsviw pattern file . . .\n",
152*486Sceastha (char *)NULL);
1530Sstevel@tonic-gate exit(2);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate argv = &argv[optind];
1570Sstevel@tonic-gate argc -= optind;
1580Sstevel@tonic-gate nfile = argc - 1;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (strrchr(*argv, '\n') != NULL)
1610Sstevel@tonic-gate regerr(41);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (iflag) {
1640Sstevel@tonic-gate for (arg = *argv; *arg != NULL; ++arg)
1650Sstevel@tonic-gate *arg = (char)tolower((int)((unsigned char)*arg));
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (wflag) {
1690Sstevel@tonic-gate unsigned int wordlen;
1700Sstevel@tonic-gate char *wordbuf;
1710Sstevel@tonic-gate
172*486Sceastha wordlen = strlen(*argv) + 5; /* '\\' '<' *argv '\\' '>' '\0' */
1730Sstevel@tonic-gate if ((wordbuf = malloc(wordlen)) == NULL) {
1740Sstevel@tonic-gate errmsg("grep: Out of memory for word\n", (char *)NULL);
1750Sstevel@tonic-gate exit(2);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate (void) strcpy(wordbuf, "\\<");
1790Sstevel@tonic-gate (void) strcat(wordbuf, *argv);
1800Sstevel@tonic-gate (void) strcat(wordbuf, "\\>");
1810Sstevel@tonic-gate *argv = wordbuf;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate expbuf = compile(*argv, (char *)0, (char *)0);
1850Sstevel@tonic-gate if (regerrno)
1860Sstevel@tonic-gate regerr(regerrno);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate if (--argc == 0)
1890Sstevel@tonic-gate execute(NULL);
1900Sstevel@tonic-gate else
1910Sstevel@tonic-gate while (argc-- > 0)
1920Sstevel@tonic-gate execute(*++argv);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate return (nsucc == 2 ? 2 : (nsucc == 0 ? 1 : 0));
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate static void
execute(char * file)198*486Sceastha execute(char *file)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate char *lbuf, *p;
2010Sstevel@tonic-gate long count;
2020Sstevel@tonic-gate long offset = 0;
2030Sstevel@tonic-gate char *next_ptr = NULL;
2040Sstevel@tonic-gate long next_count = 0;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate tln = 0;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (prntbuf == NULL) {
2090Sstevel@tonic-gate fw_lPrntBufLen = GBUFSIZ + 1;
2100Sstevel@tonic-gate if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) {
2110Sstevel@tonic-gate exit(2); /* out of memory - BAIL */
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) {
2140Sstevel@tonic-gate exit(2); /* out of memory - BAIL */
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (file == NULL)
2190Sstevel@tonic-gate temp = 0;
2200Sstevel@tonic-gate else if ((temp = open(file, O_RDONLY)) == -1) {
2210Sstevel@tonic-gate if (!sflag)
2220Sstevel@tonic-gate errmsg("grep: can't open %s\n", file);
2230Sstevel@tonic-gate nsucc = 2;
2240Sstevel@tonic-gate return;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* read in first block of bytes */
2280Sstevel@tonic-gate if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) {
2290Sstevel@tonic-gate (void) close(temp);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate if (cflag) {
2320Sstevel@tonic-gate if (nfile > 1 && !hflag && file)
2330Sstevel@tonic-gate (void) fprintf(stdout, "%s:", file);
2340Sstevel@tonic-gate (void) fprintf(stdout, "%lld\n", tln);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate return;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate lnum = 0;
2400Sstevel@tonic-gate ptr = prntbuf;
2410Sstevel@tonic-gate for (;;) {
2420Sstevel@tonic-gate /* look for next newline */
2430Sstevel@tonic-gate if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) {
2440Sstevel@tonic-gate offset += count;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * shift unused data to the beginning of the buffer
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate if (ptr > prntbuf) {
2500Sstevel@tonic-gate (void) memmove(prntbuf, ptr, offset);
2510Sstevel@tonic-gate ptr = prntbuf;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * re-allocate a larger buffer if this one is full
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate if (offset + GBUFSIZ > fw_lPrntBufLen) {
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * allocate a new buffer and preserve the
2600Sstevel@tonic-gate * contents...
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate fw_lPrntBufLen += GBUFSIZ;
2630Sstevel@tonic-gate if ((prntbuf = realloc(prntbuf,
2640Sstevel@tonic-gate fw_lPrntBufLen)) == NULL)
2650Sstevel@tonic-gate exit(2);
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * set up a bigger linebuffer (this is only used
2690Sstevel@tonic-gate * for case insensitive operations). Contents do
2700Sstevel@tonic-gate * not have to be preserved.
2710Sstevel@tonic-gate */
2720Sstevel@tonic-gate free(linebuf);
2730Sstevel@tonic-gate if ((linebuf = malloc(fw_lPrntBufLen)) == NULL)
2740Sstevel@tonic-gate exit(2);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate ptr = prntbuf;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate p = prntbuf + offset;
2800Sstevel@tonic-gate if ((count = read(temp, p, GBUFSIZ)) > 0)
2810Sstevel@tonic-gate continue;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate if (offset == 0)
2840Sstevel@tonic-gate /* end of file already reached */
2850Sstevel@tonic-gate break;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /* last line of file has no newline */
2880Sstevel@tonic-gate ptrend = ptr + offset;
2890Sstevel@tonic-gate nlflag = 0;
2900Sstevel@tonic-gate } else {
2910Sstevel@tonic-gate next_ptr = ptrend + 1;
2920Sstevel@tonic-gate next_count = offset + count - (next_ptr - ptr);
2930Sstevel@tonic-gate nlflag = 1;
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate lnum++;
2960Sstevel@tonic-gate *ptrend = '\0';
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if (iflag) {
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * Make a lower case copy of the record
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate p = ptr;
3030Sstevel@tonic-gate for (lbuf = linebuf; p < ptrend; )
3040Sstevel@tonic-gate *lbuf++ = (char)tolower((int)
3050Sstevel@tonic-gate (unsigned char)*p++);
3060Sstevel@tonic-gate *lbuf = '\0';
3070Sstevel@tonic-gate lbuf = linebuf;
3080Sstevel@tonic-gate } else
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Use record as is
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate lbuf = ptr;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /* lflag only once */
3150Sstevel@tonic-gate if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate if (!nlflag)
3190Sstevel@tonic-gate break;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate ptr = next_ptr;
3220Sstevel@tonic-gate count = next_count;
3230Sstevel@tonic-gate offset = 0;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate (void) close(temp);
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate if (cflag) {
3280Sstevel@tonic-gate if (nfile > 1 && !hflag && file)
3290Sstevel@tonic-gate (void) fprintf(stdout, "%s:", file);
3300Sstevel@tonic-gate (void) fprintf(stdout, "%lld\n", tln);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate static int
succeed(char * f)335*486Sceastha succeed(char *f)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate int nchars;
3380Sstevel@tonic-gate nsucc = (nsucc == 2) ? 2 : 1;
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate if (f == NULL)
3410Sstevel@tonic-gate f = "<stdin>";
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (cflag) {
3440Sstevel@tonic-gate tln++;
3450Sstevel@tonic-gate return (0);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if (lflag) {
3490Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", f);
3500Sstevel@tonic-gate return (1);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate if (nfile > 1 && !hflag)
3540Sstevel@tonic-gate /* print filename */
3550Sstevel@tonic-gate (void) fprintf(stdout, "%s:", f);
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate if (bflag)
3580Sstevel@tonic-gate /* print block number */
3590Sstevel@tonic-gate (void) fprintf(stdout, "%lld:", (offset_t)
360*486Sceastha ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE));
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (nflag)
3630Sstevel@tonic-gate /* print line number */
3640Sstevel@tonic-gate (void) fprintf(stdout, "%lld:", lnum);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate if (nlflag) {
3670Sstevel@tonic-gate /* newline at end of line */
3680Sstevel@tonic-gate *ptrend = '\n';
3690Sstevel@tonic-gate nchars = ptrend - ptr + 1;
3700Sstevel@tonic-gate } else {
3710Sstevel@tonic-gate /* don't write sentinel \0 */
3720Sstevel@tonic-gate nchars = ptrend - ptr;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate (void) fwrite(ptr, 1, nchars, stdout);
3760Sstevel@tonic-gate return (0);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate static void
regerr(int err)380*486Sceastha regerr(int err)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate errmsg("grep: RE error %d: ", err);
3830Sstevel@tonic-gate switch (err) {
3840Sstevel@tonic-gate case 11:
3850Sstevel@tonic-gate err = 0;
3860Sstevel@tonic-gate break;
3870Sstevel@tonic-gate case 16:
3880Sstevel@tonic-gate err = 1;
3890Sstevel@tonic-gate break;
3900Sstevel@tonic-gate case 25:
3910Sstevel@tonic-gate err = 2;
3920Sstevel@tonic-gate break;
3930Sstevel@tonic-gate case 41:
3940Sstevel@tonic-gate err = 3;
3950Sstevel@tonic-gate break;
3960Sstevel@tonic-gate case 42:
3970Sstevel@tonic-gate err = 4;
3980Sstevel@tonic-gate break;
3990Sstevel@tonic-gate case 43:
4000Sstevel@tonic-gate err = 5;
4010Sstevel@tonic-gate break;
4020Sstevel@tonic-gate case 44:
4030Sstevel@tonic-gate err = 6;
4040Sstevel@tonic-gate break;
4050Sstevel@tonic-gate case 45:
4060Sstevel@tonic-gate err = 7;
4070Sstevel@tonic-gate break;
4080Sstevel@tonic-gate case 46:
4090Sstevel@tonic-gate err = 8;
4100Sstevel@tonic-gate break;
4110Sstevel@tonic-gate case 49:
4120Sstevel@tonic-gate err = 9;
4130Sstevel@tonic-gate break;
4140Sstevel@tonic-gate case 50:
4150Sstevel@tonic-gate err = 10;
4160Sstevel@tonic-gate break;
4170Sstevel@tonic-gate case 67:
4180Sstevel@tonic-gate err = 11;
4190Sstevel@tonic-gate break;
4200Sstevel@tonic-gate default:
4210Sstevel@tonic-gate err = 12;
4220Sstevel@tonic-gate break;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate errmsg("%s\n", gettext(errstr[err]));
4260Sstevel@tonic-gate exit(2);
4270Sstevel@tonic-gate }
428