1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 31*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate /* 36*7c478bd9Sstevel@tonic-gate * grep -- print lines matching (or not matching) a pattern 37*7c478bd9Sstevel@tonic-gate * 38*7c478bd9Sstevel@tonic-gate * status returns: 39*7c478bd9Sstevel@tonic-gate * 0 - ok, and some matches 40*7c478bd9Sstevel@tonic-gate * 1 - ok, but no matches 41*7c478bd9Sstevel@tonic-gate * 2 - some error 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #include <ctype.h> 47*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 48*7c478bd9Sstevel@tonic-gate #include <locale.h> 49*7c478bd9Sstevel@tonic-gate #include <memory.h> 50*7c478bd9Sstevel@tonic-gate #include <regexpr.h> 51*7c478bd9Sstevel@tonic-gate #include <stdio.h> 52*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 53*7c478bd9Sstevel@tonic-gate #include <string.h> 54*7c478bd9Sstevel@tonic-gate #include <unistd.h> 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate static const char * const errstr[] = { 57*7c478bd9Sstevel@tonic-gate "Range endpoint too large.", 58*7c478bd9Sstevel@tonic-gate "Bad number.", 59*7c478bd9Sstevel@tonic-gate "``\\digit'' out of range.", 60*7c478bd9Sstevel@tonic-gate "No remembered search string.", 61*7c478bd9Sstevel@tonic-gate "\\( \\) imbalance.", 62*7c478bd9Sstevel@tonic-gate "Too many \\(.", 63*7c478bd9Sstevel@tonic-gate "More than 2 numbers given in \\{ \\}.", 64*7c478bd9Sstevel@tonic-gate "} expected after \\.", 65*7c478bd9Sstevel@tonic-gate "First number exceeds second in \\{ \\}.", 66*7c478bd9Sstevel@tonic-gate "[ ] imbalance.", 67*7c478bd9Sstevel@tonic-gate "Regular expression overflow.", 68*7c478bd9Sstevel@tonic-gate "Illegal byte sequence.", 69*7c478bd9Sstevel@tonic-gate "Unknown regexp error code!!", 70*7c478bd9Sstevel@tonic-gate NULL 71*7c478bd9Sstevel@tonic-gate }; 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate #define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg) 74*7c478bd9Sstevel@tonic-gate #define BLKSIZE 512 75*7c478bd9Sstevel@tonic-gate #define GBUFSIZ 8192 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate static int temp; 78*7c478bd9Sstevel@tonic-gate static long long lnum; 79*7c478bd9Sstevel@tonic-gate static char *linebuf; 80*7c478bd9Sstevel@tonic-gate static char *prntbuf = NULL; 81*7c478bd9Sstevel@tonic-gate static long fw_lPrntBufLen = 0; 82*7c478bd9Sstevel@tonic-gate static int nflag; 83*7c478bd9Sstevel@tonic-gate static int bflag; 84*7c478bd9Sstevel@tonic-gate static int lflag; 85*7c478bd9Sstevel@tonic-gate static int cflag; 86*7c478bd9Sstevel@tonic-gate static int vflag; 87*7c478bd9Sstevel@tonic-gate static int sflag; 88*7c478bd9Sstevel@tonic-gate static int iflag; 89*7c478bd9Sstevel@tonic-gate static int wflag; 90*7c478bd9Sstevel@tonic-gate static int hflag; 91*7c478bd9Sstevel@tonic-gate static int errflg; 92*7c478bd9Sstevel@tonic-gate static int nfile; 93*7c478bd9Sstevel@tonic-gate static long long tln; 94*7c478bd9Sstevel@tonic-gate static int nsucc; 95*7c478bd9Sstevel@tonic-gate static int nlflag; 96*7c478bd9Sstevel@tonic-gate static char *ptr, *ptrend; 97*7c478bd9Sstevel@tonic-gate static char *expbuf; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate static void execute(char *); 100*7c478bd9Sstevel@tonic-gate static void regerr(int); 101*7c478bd9Sstevel@tonic-gate static int succeed(char *); 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate int 104*7c478bd9Sstevel@tonic-gate main( 105*7c478bd9Sstevel@tonic-gate int argc, 106*7c478bd9Sstevel@tonic-gate char **argv) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate int c; 109*7c478bd9Sstevel@tonic-gate char *arg; 110*7c478bd9Sstevel@tonic-gate extern int optind; 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 113*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 114*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 115*7c478bd9Sstevel@tonic-gate #endif 116*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "hblcnsviyw")) != -1) 119*7c478bd9Sstevel@tonic-gate switch (c) { 120*7c478bd9Sstevel@tonic-gate case 'h': 121*7c478bd9Sstevel@tonic-gate hflag++; 122*7c478bd9Sstevel@tonic-gate break; 123*7c478bd9Sstevel@tonic-gate case 'v': 124*7c478bd9Sstevel@tonic-gate vflag++; 125*7c478bd9Sstevel@tonic-gate break; 126*7c478bd9Sstevel@tonic-gate case 'c': 127*7c478bd9Sstevel@tonic-gate cflag++; 128*7c478bd9Sstevel@tonic-gate break; 129*7c478bd9Sstevel@tonic-gate case 'n': 130*7c478bd9Sstevel@tonic-gate nflag++; 131*7c478bd9Sstevel@tonic-gate break; 132*7c478bd9Sstevel@tonic-gate case 'b': 133*7c478bd9Sstevel@tonic-gate bflag++; 134*7c478bd9Sstevel@tonic-gate break; 135*7c478bd9Sstevel@tonic-gate case 's': 136*7c478bd9Sstevel@tonic-gate sflag++; 137*7c478bd9Sstevel@tonic-gate break; 138*7c478bd9Sstevel@tonic-gate case 'l': 139*7c478bd9Sstevel@tonic-gate lflag++; 140*7c478bd9Sstevel@tonic-gate break; 141*7c478bd9Sstevel@tonic-gate case 'y': 142*7c478bd9Sstevel@tonic-gate case 'i': 143*7c478bd9Sstevel@tonic-gate iflag++; 144*7c478bd9Sstevel@tonic-gate break; 145*7c478bd9Sstevel@tonic-gate case 'w': 146*7c478bd9Sstevel@tonic-gate wflag++; 147*7c478bd9Sstevel@tonic-gate break; 148*7c478bd9Sstevel@tonic-gate case '?': 149*7c478bd9Sstevel@tonic-gate errflg++; 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate if (errflg || (optind >= argc)) { 153*7c478bd9Sstevel@tonic-gate errmsg("Usage: grep -hblcnsviw pattern file . . .\n", 154*7c478bd9Sstevel@tonic-gate (char *)NULL); 155*7c478bd9Sstevel@tonic-gate exit(2); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate argv = &argv[optind]; 159*7c478bd9Sstevel@tonic-gate argc -= optind; 160*7c478bd9Sstevel@tonic-gate nfile = argc - 1; 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate if (strrchr(*argv, '\n') != NULL) 163*7c478bd9Sstevel@tonic-gate regerr(41); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (iflag) { 166*7c478bd9Sstevel@tonic-gate for (arg = *argv; *arg != NULL; ++arg) 167*7c478bd9Sstevel@tonic-gate *arg = (char)tolower((int)((unsigned char)*arg)); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate if (wflag) { 171*7c478bd9Sstevel@tonic-gate unsigned int wordlen; 172*7c478bd9Sstevel@tonic-gate char *wordbuf; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate wordlen = strlen(*argv) + 4; 175*7c478bd9Sstevel@tonic-gate if ((wordbuf = malloc(wordlen)) == NULL) { 176*7c478bd9Sstevel@tonic-gate errmsg("grep: Out of memory for word\n", (char *)NULL); 177*7c478bd9Sstevel@tonic-gate exit(2); 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate (void) strcpy(wordbuf, "\\<"); 181*7c478bd9Sstevel@tonic-gate (void) strcat(wordbuf, *argv); 182*7c478bd9Sstevel@tonic-gate (void) strcat(wordbuf, "\\>"); 183*7c478bd9Sstevel@tonic-gate *argv = wordbuf; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate expbuf = compile(*argv, (char *)0, (char *)0); 187*7c478bd9Sstevel@tonic-gate if (regerrno) 188*7c478bd9Sstevel@tonic-gate regerr(regerrno); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (--argc == 0) 191*7c478bd9Sstevel@tonic-gate execute(NULL); 192*7c478bd9Sstevel@tonic-gate else 193*7c478bd9Sstevel@tonic-gate while (argc-- > 0) 194*7c478bd9Sstevel@tonic-gate execute(*++argv); 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate return (nsucc == 2 ? 2 : (nsucc == 0 ? 1 : 0)); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate static void 200*7c478bd9Sstevel@tonic-gate execute( 201*7c478bd9Sstevel@tonic-gate char *file) 202*7c478bd9Sstevel@tonic-gate { 203*7c478bd9Sstevel@tonic-gate char *lbuf, *p; 204*7c478bd9Sstevel@tonic-gate long count; 205*7c478bd9Sstevel@tonic-gate long offset = 0; 206*7c478bd9Sstevel@tonic-gate char *next_ptr = NULL; 207*7c478bd9Sstevel@tonic-gate long next_count = 0; 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate tln = 0; 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate if (prntbuf == NULL) { 212*7c478bd9Sstevel@tonic-gate fw_lPrntBufLen = GBUFSIZ + 1; 213*7c478bd9Sstevel@tonic-gate if ((prntbuf = malloc(fw_lPrntBufLen)) == NULL) { 214*7c478bd9Sstevel@tonic-gate exit(2); /* out of memory - BAIL */ 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) { 217*7c478bd9Sstevel@tonic-gate exit(2); /* out of memory - BAIL */ 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate if (file == NULL) 222*7c478bd9Sstevel@tonic-gate temp = 0; 223*7c478bd9Sstevel@tonic-gate else if ((temp = open(file, O_RDONLY)) == -1) { 224*7c478bd9Sstevel@tonic-gate if (!sflag) 225*7c478bd9Sstevel@tonic-gate errmsg("grep: can't open %s\n", file); 226*7c478bd9Sstevel@tonic-gate nsucc = 2; 227*7c478bd9Sstevel@tonic-gate return; 228*7c478bd9Sstevel@tonic-gate } 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate /* read in first block of bytes */ 231*7c478bd9Sstevel@tonic-gate if ((count = read(temp, prntbuf, GBUFSIZ)) <= 0) { 232*7c478bd9Sstevel@tonic-gate (void) close(temp); 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate if (cflag) { 235*7c478bd9Sstevel@tonic-gate if (nfile > 1 && !hflag && file) 236*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s:", file); 237*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%lld\n", tln); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate return; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate lnum = 0; 243*7c478bd9Sstevel@tonic-gate ptr = prntbuf; 244*7c478bd9Sstevel@tonic-gate for (;;) { 245*7c478bd9Sstevel@tonic-gate /* look for next newline */ 246*7c478bd9Sstevel@tonic-gate if ((ptrend = memchr(ptr + offset, '\n', count)) == NULL) { 247*7c478bd9Sstevel@tonic-gate offset += count; 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate /* 250*7c478bd9Sstevel@tonic-gate * shift unused data to the beginning of the buffer 251*7c478bd9Sstevel@tonic-gate */ 252*7c478bd9Sstevel@tonic-gate if (ptr > prntbuf) { 253*7c478bd9Sstevel@tonic-gate (void) memmove(prntbuf, ptr, offset); 254*7c478bd9Sstevel@tonic-gate ptr = prntbuf; 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate /* 258*7c478bd9Sstevel@tonic-gate * re-allocate a larger buffer if this one is full 259*7c478bd9Sstevel@tonic-gate */ 260*7c478bd9Sstevel@tonic-gate if (offset + GBUFSIZ > fw_lPrntBufLen) { 261*7c478bd9Sstevel@tonic-gate /* 262*7c478bd9Sstevel@tonic-gate * allocate a new buffer and preserve the 263*7c478bd9Sstevel@tonic-gate * contents... 264*7c478bd9Sstevel@tonic-gate */ 265*7c478bd9Sstevel@tonic-gate fw_lPrntBufLen += GBUFSIZ; 266*7c478bd9Sstevel@tonic-gate if ((prntbuf = realloc(prntbuf, 267*7c478bd9Sstevel@tonic-gate fw_lPrntBufLen)) == NULL) 268*7c478bd9Sstevel@tonic-gate exit(2); 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate /* 271*7c478bd9Sstevel@tonic-gate * set up a bigger linebuffer (this is only used 272*7c478bd9Sstevel@tonic-gate * for case insensitive operations). Contents do 273*7c478bd9Sstevel@tonic-gate * not have to be preserved. 274*7c478bd9Sstevel@tonic-gate */ 275*7c478bd9Sstevel@tonic-gate free(linebuf); 276*7c478bd9Sstevel@tonic-gate if ((linebuf = malloc(fw_lPrntBufLen)) == NULL) 277*7c478bd9Sstevel@tonic-gate exit(2); 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate ptr = prntbuf; 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate p = prntbuf + offset; 283*7c478bd9Sstevel@tonic-gate if ((count = read(temp, p, GBUFSIZ)) > 0) 284*7c478bd9Sstevel@tonic-gate continue; 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate if (offset == 0) 287*7c478bd9Sstevel@tonic-gate /* end of file already reached */ 288*7c478bd9Sstevel@tonic-gate break; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate /* last line of file has no newline */ 291*7c478bd9Sstevel@tonic-gate ptrend = ptr + offset; 292*7c478bd9Sstevel@tonic-gate nlflag = 0; 293*7c478bd9Sstevel@tonic-gate } else { 294*7c478bd9Sstevel@tonic-gate next_ptr = ptrend + 1; 295*7c478bd9Sstevel@tonic-gate next_count = offset + count - (next_ptr - ptr); 296*7c478bd9Sstevel@tonic-gate nlflag = 1; 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate lnum++; 299*7c478bd9Sstevel@tonic-gate *ptrend = '\0'; 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate if (iflag) { 302*7c478bd9Sstevel@tonic-gate /* 303*7c478bd9Sstevel@tonic-gate * Make a lower case copy of the record 304*7c478bd9Sstevel@tonic-gate */ 305*7c478bd9Sstevel@tonic-gate p = ptr; 306*7c478bd9Sstevel@tonic-gate for (lbuf = linebuf; p < ptrend; ) 307*7c478bd9Sstevel@tonic-gate *lbuf++ = (char)tolower((int) 308*7c478bd9Sstevel@tonic-gate (unsigned char)*p++); 309*7c478bd9Sstevel@tonic-gate *lbuf = '\0'; 310*7c478bd9Sstevel@tonic-gate lbuf = linebuf; 311*7c478bd9Sstevel@tonic-gate } else 312*7c478bd9Sstevel@tonic-gate /* 313*7c478bd9Sstevel@tonic-gate * Use record as is 314*7c478bd9Sstevel@tonic-gate */ 315*7c478bd9Sstevel@tonic-gate lbuf = ptr; 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate /* lflag only once */ 318*7c478bd9Sstevel@tonic-gate if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1) 319*7c478bd9Sstevel@tonic-gate break; 320*7c478bd9Sstevel@tonic-gate 321*7c478bd9Sstevel@tonic-gate if (!nlflag) 322*7c478bd9Sstevel@tonic-gate break; 323*7c478bd9Sstevel@tonic-gate 324*7c478bd9Sstevel@tonic-gate ptr = next_ptr; 325*7c478bd9Sstevel@tonic-gate count = next_count; 326*7c478bd9Sstevel@tonic-gate offset = 0; 327*7c478bd9Sstevel@tonic-gate } 328*7c478bd9Sstevel@tonic-gate (void) close(temp); 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate if (cflag) { 331*7c478bd9Sstevel@tonic-gate if (nfile > 1 && !hflag && file) 332*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s:", file); 333*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%lld\n", tln); 334*7c478bd9Sstevel@tonic-gate } 335*7c478bd9Sstevel@tonic-gate } 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate static int 338*7c478bd9Sstevel@tonic-gate succeed( 339*7c478bd9Sstevel@tonic-gate char *f) 340*7c478bd9Sstevel@tonic-gate { 341*7c478bd9Sstevel@tonic-gate int nchars; 342*7c478bd9Sstevel@tonic-gate nsucc = (nsucc == 2) ? 2 : 1; 343*7c478bd9Sstevel@tonic-gate 344*7c478bd9Sstevel@tonic-gate if (f == NULL) 345*7c478bd9Sstevel@tonic-gate f = "<stdin>"; 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate if (cflag) { 348*7c478bd9Sstevel@tonic-gate tln++; 349*7c478bd9Sstevel@tonic-gate return (0); 350*7c478bd9Sstevel@tonic-gate } 351*7c478bd9Sstevel@tonic-gate 352*7c478bd9Sstevel@tonic-gate if (lflag) { 353*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", f); 354*7c478bd9Sstevel@tonic-gate return (1); 355*7c478bd9Sstevel@tonic-gate } 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate if (nfile > 1 && !hflag) 358*7c478bd9Sstevel@tonic-gate /* print filename */ 359*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s:", f); 360*7c478bd9Sstevel@tonic-gate 361*7c478bd9Sstevel@tonic-gate if (bflag) 362*7c478bd9Sstevel@tonic-gate /* print block number */ 363*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%lld:", (offset_t) 364*7c478bd9Sstevel@tonic-gate ((lseek(temp, (off_t)0, SEEK_CUR) - 1) / BLKSIZE)); 365*7c478bd9Sstevel@tonic-gate 366*7c478bd9Sstevel@tonic-gate if (nflag) 367*7c478bd9Sstevel@tonic-gate /* print line number */ 368*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%lld:", lnum); 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate if (nlflag) { 371*7c478bd9Sstevel@tonic-gate /* newline at end of line */ 372*7c478bd9Sstevel@tonic-gate *ptrend = '\n'; 373*7c478bd9Sstevel@tonic-gate nchars = ptrend - ptr + 1; 374*7c478bd9Sstevel@tonic-gate } else { 375*7c478bd9Sstevel@tonic-gate /* don't write sentinel \0 */ 376*7c478bd9Sstevel@tonic-gate nchars = ptrend - ptr; 377*7c478bd9Sstevel@tonic-gate } 378*7c478bd9Sstevel@tonic-gate 379*7c478bd9Sstevel@tonic-gate (void) fwrite(ptr, 1, nchars, stdout); 380*7c478bd9Sstevel@tonic-gate return (0); 381*7c478bd9Sstevel@tonic-gate } 382*7c478bd9Sstevel@tonic-gate 383*7c478bd9Sstevel@tonic-gate static void 384*7c478bd9Sstevel@tonic-gate regerr( 385*7c478bd9Sstevel@tonic-gate int err) 386*7c478bd9Sstevel@tonic-gate { 387*7c478bd9Sstevel@tonic-gate errmsg("grep: RE error %d: ", err); 388*7c478bd9Sstevel@tonic-gate switch (err) { 389*7c478bd9Sstevel@tonic-gate case 11: 390*7c478bd9Sstevel@tonic-gate err = 0; 391*7c478bd9Sstevel@tonic-gate break; 392*7c478bd9Sstevel@tonic-gate case 16: 393*7c478bd9Sstevel@tonic-gate err = 1; 394*7c478bd9Sstevel@tonic-gate break; 395*7c478bd9Sstevel@tonic-gate case 25: 396*7c478bd9Sstevel@tonic-gate err = 2; 397*7c478bd9Sstevel@tonic-gate break; 398*7c478bd9Sstevel@tonic-gate case 41: 399*7c478bd9Sstevel@tonic-gate err = 3; 400*7c478bd9Sstevel@tonic-gate break; 401*7c478bd9Sstevel@tonic-gate case 42: 402*7c478bd9Sstevel@tonic-gate err = 4; 403*7c478bd9Sstevel@tonic-gate break; 404*7c478bd9Sstevel@tonic-gate case 43: 405*7c478bd9Sstevel@tonic-gate err = 5; 406*7c478bd9Sstevel@tonic-gate break; 407*7c478bd9Sstevel@tonic-gate case 44: 408*7c478bd9Sstevel@tonic-gate err = 6; 409*7c478bd9Sstevel@tonic-gate break; 410*7c478bd9Sstevel@tonic-gate case 45: 411*7c478bd9Sstevel@tonic-gate err = 7; 412*7c478bd9Sstevel@tonic-gate break; 413*7c478bd9Sstevel@tonic-gate case 46: 414*7c478bd9Sstevel@tonic-gate err = 8; 415*7c478bd9Sstevel@tonic-gate break; 416*7c478bd9Sstevel@tonic-gate case 49: 417*7c478bd9Sstevel@tonic-gate err = 9; 418*7c478bd9Sstevel@tonic-gate break; 419*7c478bd9Sstevel@tonic-gate case 50: 420*7c478bd9Sstevel@tonic-gate err = 10; 421*7c478bd9Sstevel@tonic-gate break; 422*7c478bd9Sstevel@tonic-gate case 67: 423*7c478bd9Sstevel@tonic-gate err = 11; 424*7c478bd9Sstevel@tonic-gate break; 425*7c478bd9Sstevel@tonic-gate default: 426*7c478bd9Sstevel@tonic-gate err = 12; 427*7c478bd9Sstevel@tonic-gate break; 428*7c478bd9Sstevel@tonic-gate } 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate errmsg("%s\n", gettext(errstr[err])); 431*7c478bd9Sstevel@tonic-gate exit(2); 432*7c478bd9Sstevel@tonic-gate } 433