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