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 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */
317c478bd9Sstevel@tonic-gate /* All Rights Reserved */
327c478bd9Sstevel@tonic-gate
33*41599e9fSDamian Bogel /*
34*41599e9fSDamian Bogel * Copyright 2013 Damian Bogel. All rights reserved.
35*41599e9fSDamian Bogel */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * fgrep -- print all lines containing any of a set of keywords
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * status returns:
417c478bd9Sstevel@tonic-gate * 0 - ok, and some matches
427c478bd9Sstevel@tonic-gate * 1 - ok, but no matches
437c478bd9Sstevel@tonic-gate * 2 - some error
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <locale.h>
527c478bd9Sstevel@tonic-gate #include <libintl.h>
537c478bd9Sstevel@tonic-gate #include <euc.h>
543f9207dcSpd155743 #include <sys/stat.h>
553f9207dcSpd155743 #include <fcntl.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #include <getwidth.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate eucwidth_t WW;
607c478bd9Sstevel@tonic-gate #define WIDTH1 WW._eucw1
617c478bd9Sstevel@tonic-gate #define WIDTH2 WW._eucw2
627c478bd9Sstevel@tonic-gate #define WIDTH3 WW._eucw3
637c478bd9Sstevel@tonic-gate #define MULTI_BYTE WW._multibyte
647c478bd9Sstevel@tonic-gate #define GETONE(lc, p) \
657c478bd9Sstevel@tonic-gate cw = ISASCII(lc = (unsigned char)*p++) ? 1 : \
667c478bd9Sstevel@tonic-gate (ISSET2(lc) ? WIDTH2 : \
677c478bd9Sstevel@tonic-gate (ISSET3(lc) ? WIDTH3 : WIDTH1)); \
687c478bd9Sstevel@tonic-gate if (--cw > --ccount) { \
697c478bd9Sstevel@tonic-gate cw -= ccount; \
707c478bd9Sstevel@tonic-gate while (ccount--) \
717c478bd9Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177); \
727c478bd9Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) { \
737c478bd9Sstevel@tonic-gate if (nlp == buf) { \
747c478bd9Sstevel@tonic-gate /* Increase the buffer size */ \
757c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ; \
767c478bd9Sstevel@tonic-gate if ((buf = realloc(buf, \
777c478bd9Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) { \
787c478bd9Sstevel@tonic-gate exit(2); /* out of memory */ \
797c478bd9Sstevel@tonic-gate } \
807c478bd9Sstevel@tonic-gate nlp = buf; \
817c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz]; \
827c478bd9Sstevel@tonic-gate } else { \
837c478bd9Sstevel@tonic-gate /* shift the buffer contents down */ \
847c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp, \
857c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - nlp);\
867c478bd9Sstevel@tonic-gate p -= nlp - buf; \
877c478bd9Sstevel@tonic-gate nlp = buf; \
887c478bd9Sstevel@tonic-gate } \
897c478bd9Sstevel@tonic-gate } \
907c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) { \
917c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char), \
927c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr))\
937c478bd9Sstevel@tonic-gate <= 0) break; \
947c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, \
957c478bd9Sstevel@tonic-gate sizeof (char), BUFSIZ, fptr)) <= 0) \
967c478bd9Sstevel@tonic-gate break; \
977c478bd9Sstevel@tonic-gate blkno += (long long)ccount; \
987c478bd9Sstevel@tonic-gate } \
997c478bd9Sstevel@tonic-gate ccount -= cw; \
1007c478bd9Sstevel@tonic-gate while (cw--) \
1017c478bd9Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177)
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * The same() macro and letter() function were inserted to allow for
1057c478bd9Sstevel@tonic-gate * the -i option work for the multi-byte environment.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate wchar_t letter();
1087c478bd9Sstevel@tonic-gate #define same(a, b) \
1097c478bd9Sstevel@tonic-gate (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
1107c478bd9Sstevel@tonic-gate letter(a) == letter(b))
1117c478bd9Sstevel@tonic-gate
112*41599e9fSDamian Bogel #define STDIN_FILENAME gettext("(standard input)")
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate #define QSIZE 400
1157c478bd9Sstevel@tonic-gate struct words {
1167c478bd9Sstevel@tonic-gate wchar_t inp;
1177c478bd9Sstevel@tonic-gate char out;
1187c478bd9Sstevel@tonic-gate struct words *nst;
1197c478bd9Sstevel@tonic-gate struct words *link;
1207c478bd9Sstevel@tonic-gate struct words *fail;
1213f9207dcSpd155743 } *w = NULL, *smax, *q;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate FILE *fptr;
1247c478bd9Sstevel@tonic-gate long long lnum;
125*41599e9fSDamian Bogel int bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, qflag;
126*41599e9fSDamian Bogel int Hflag, hflag, iflag;
1277c478bd9Sstevel@tonic-gate int retcode = 0;
1287c478bd9Sstevel@tonic-gate int nfile;
1297c478bd9Sstevel@tonic-gate long long blkno;
1307c478bd9Sstevel@tonic-gate int nsucc;
1317c478bd9Sstevel@tonic-gate long long tln;
1327c478bd9Sstevel@tonic-gate FILE *wordf;
1337c478bd9Sstevel@tonic-gate char *argptr;
1343f9207dcSpd155743 off_t input_size = 0;
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate void execute(char *);
1377c478bd9Sstevel@tonic-gate void cgotofn(void);
1387c478bd9Sstevel@tonic-gate void overflo(void);
1397c478bd9Sstevel@tonic-gate void cfail(void);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate static long fw_lBufsiz = 0;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1447c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate int c;
1477c478bd9Sstevel@tonic-gate int errflg = 0;
1483f9207dcSpd155743 struct stat file_stat;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1517c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1527c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1557c478bd9Sstevel@tonic-gate
156*41599e9fSDamian Bogel while ((c = getopt(argc, argv, "Hhybcie:f:lnvxqs")) != EOF)
1577c478bd9Sstevel@tonic-gate switch (c) {
1587c478bd9Sstevel@tonic-gate
159*41599e9fSDamian Bogel case 'q':
160*41599e9fSDamian Bogel case 's': /* Solaris: legacy option */
161*41599e9fSDamian Bogel qflag++;
162*41599e9fSDamian Bogel continue;
163*41599e9fSDamian Bogel case 'H':
164*41599e9fSDamian Bogel Hflag++;
165*41599e9fSDamian Bogel hflag = 0;
1667c478bd9Sstevel@tonic-gate continue;
1677c478bd9Sstevel@tonic-gate case 'h':
1687c478bd9Sstevel@tonic-gate hflag++;
169*41599e9fSDamian Bogel Hflag = 0;
1707c478bd9Sstevel@tonic-gate continue;
1717c478bd9Sstevel@tonic-gate case 'b':
1727c478bd9Sstevel@tonic-gate bflag++;
1737c478bd9Sstevel@tonic-gate continue;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate case 'i':
1767c478bd9Sstevel@tonic-gate case 'y':
1777c478bd9Sstevel@tonic-gate iflag++;
1787c478bd9Sstevel@tonic-gate continue;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate case 'c':
1817c478bd9Sstevel@tonic-gate cflag++;
1827c478bd9Sstevel@tonic-gate continue;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate case 'e':
1857c478bd9Sstevel@tonic-gate eflag++;
1867c478bd9Sstevel@tonic-gate argptr = optarg;
1873f9207dcSpd155743 input_size = strlen(argptr);
1887c478bd9Sstevel@tonic-gate continue;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate case 'f':
1917c478bd9Sstevel@tonic-gate fflag++;
1927c478bd9Sstevel@tonic-gate wordf = fopen(optarg, "r");
1937c478bd9Sstevel@tonic-gate if (wordf == NULL) {
1947c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1957c478bd9Sstevel@tonic-gate gettext("fgrep: can't open %s\n"),
1967c478bd9Sstevel@tonic-gate optarg);
1977c478bd9Sstevel@tonic-gate exit(2);
1987c478bd9Sstevel@tonic-gate }
1993f9207dcSpd155743
2003f9207dcSpd155743 if (fstat(fileno(wordf), &file_stat) == 0) {
2013f9207dcSpd155743 input_size = file_stat.st_size;
2023f9207dcSpd155743 } else {
2033f9207dcSpd155743 (void) fprintf(stderr,
2043f9207dcSpd155743 gettext("fgrep: can't fstat %s\n"),
2053f9207dcSpd155743 optarg);
2063f9207dcSpd155743 exit(2);
2073f9207dcSpd155743 }
2083f9207dcSpd155743
2097c478bd9Sstevel@tonic-gate continue;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate case 'l':
2127c478bd9Sstevel@tonic-gate lflag++;
2137c478bd9Sstevel@tonic-gate continue;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate case 'n':
2167c478bd9Sstevel@tonic-gate nflag++;
2177c478bd9Sstevel@tonic-gate continue;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate case 'v':
2207c478bd9Sstevel@tonic-gate vflag++;
2217c478bd9Sstevel@tonic-gate continue;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate case 'x':
2247c478bd9Sstevel@tonic-gate xflag++;
2257c478bd9Sstevel@tonic-gate continue;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate case '?':
2287c478bd9Sstevel@tonic-gate errflg++;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate argc -= optind;
2327c478bd9Sstevel@tonic-gate if (errflg || ((argc <= 0) && !fflag && !eflag)) {
233*41599e9fSDamian Bogel (void) printf(gettext("usage: fgrep [ -bcHhilnqsvx ] "
2347c478bd9Sstevel@tonic-gate "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
2357c478bd9Sstevel@tonic-gate exit(2);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate if (!eflag && !fflag) {
2387c478bd9Sstevel@tonic-gate argptr = argv[optind];
2393f9207dcSpd155743 input_size = strlen(argptr);
2403f9207dcSpd155743 input_size++;
2417c478bd9Sstevel@tonic-gate optind++;
2427c478bd9Sstevel@tonic-gate argc--;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2453f9207dcSpd155743 /*
2463f9207dcSpd155743 * Normally we need one struct words for each letter in the pattern
2473f9207dcSpd155743 * plus one terminating struct words with outp = 1, but when -x option
2483f9207dcSpd155743 * is specified we require one more struct words for `\n` character so we
2493f9207dcSpd155743 * calculate the input_size as below. We add extra 1 because
2503f9207dcSpd155743 * (input_size/2) rounds off odd numbers
2513f9207dcSpd155743 */
2523f9207dcSpd155743
2533f9207dcSpd155743 if (xflag) {
2543f9207dcSpd155743 input_size = input_size + (input_size/2) + 1;
2553f9207dcSpd155743 }
2563f9207dcSpd155743
2573f9207dcSpd155743 input_size++;
2583f9207dcSpd155743
2593f9207dcSpd155743 w = (struct words *)calloc(input_size, sizeof (struct words));
2603f9207dcSpd155743 if (w == NULL) {
2613f9207dcSpd155743 (void) fprintf(stderr,
2623f9207dcSpd155743 gettext("fgrep: could not allocate "
2633f9207dcSpd155743 "memory for wordlist\n"));
2643f9207dcSpd155743 exit(2);
2653f9207dcSpd155743 }
2663f9207dcSpd155743
2677c478bd9Sstevel@tonic-gate getwidth(&WW);
2687c478bd9Sstevel@tonic-gate if ((WIDTH1 == 0) && (WIDTH2 == 0) &&
2697c478bd9Sstevel@tonic-gate (WIDTH3 == 0)) {
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * If non EUC-based locale,
2727c478bd9Sstevel@tonic-gate * assume WIDTH1 is 1.
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate WIDTH1 = 1;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate WIDTH2++;
2777c478bd9Sstevel@tonic-gate WIDTH3++;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate cgotofn();
2807c478bd9Sstevel@tonic-gate cfail();
2817c478bd9Sstevel@tonic-gate nfile = argc;
2827c478bd9Sstevel@tonic-gate argv = &argv[optind];
2837c478bd9Sstevel@tonic-gate if (argc <= 0) {
2847c478bd9Sstevel@tonic-gate execute((char *)NULL);
2857c478bd9Sstevel@tonic-gate } else
2867c478bd9Sstevel@tonic-gate while (--argc >= 0) {
2877c478bd9Sstevel@tonic-gate execute(*argv);
2887c478bd9Sstevel@tonic-gate argv++;
2897c478bd9Sstevel@tonic-gate }
2903f9207dcSpd155743
2913f9207dcSpd155743 if (w != NULL) {
2923f9207dcSpd155743 free(w);
2933f9207dcSpd155743 }
2943f9207dcSpd155743
2957c478bd9Sstevel@tonic-gate return (retcode != 0 ? retcode : nsucc == 0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate void
execute(char * file)2997c478bd9Sstevel@tonic-gate execute(char *file)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate char *p;
3027c478bd9Sstevel@tonic-gate struct words *c;
3037c478bd9Sstevel@tonic-gate int ccount;
3047c478bd9Sstevel@tonic-gate static char *buf = NULL;
3057c478bd9Sstevel@tonic-gate int failed;
3067c478bd9Sstevel@tonic-gate char *nlp;
3077c478bd9Sstevel@tonic-gate wchar_t lc;
3087c478bd9Sstevel@tonic-gate int cw;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate if (buf == NULL) {
3117c478bd9Sstevel@tonic-gate fw_lBufsiz = BUFSIZ;
3127c478bd9Sstevel@tonic-gate if ((buf = malloc(fw_lBufsiz + BUFSIZ)) == NULL) {
3137c478bd9Sstevel@tonic-gate exit(2); /* out of memory */
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate if (file) {
3187c478bd9Sstevel@tonic-gate if ((fptr = fopen(file, "r")) == NULL) {
3197c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3207c478bd9Sstevel@tonic-gate gettext("fgrep: can't open %s\n"), file);
3217c478bd9Sstevel@tonic-gate retcode = 2;
3227c478bd9Sstevel@tonic-gate return;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate } else {
3257c478bd9Sstevel@tonic-gate fptr = stdin;
326*41599e9fSDamian Bogel file = STDIN_FILENAME;
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate ccount = 0;
3297c478bd9Sstevel@tonic-gate failed = 0;
3307c478bd9Sstevel@tonic-gate lnum = 1;
3317c478bd9Sstevel@tonic-gate tln = 0;
3327c478bd9Sstevel@tonic-gate blkno = 0;
3337c478bd9Sstevel@tonic-gate p = buf;
3347c478bd9Sstevel@tonic-gate nlp = p;
3357c478bd9Sstevel@tonic-gate c = w;
3367c478bd9Sstevel@tonic-gate for (;;) {
3377c478bd9Sstevel@tonic-gate if (c == 0)
3387c478bd9Sstevel@tonic-gate break;
3397c478bd9Sstevel@tonic-gate if (ccount <= 0) {
3407c478bd9Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) {
3417c478bd9Sstevel@tonic-gate if (nlp == buf) {
3427c478bd9Sstevel@tonic-gate /* increase the buffer size */
3437c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ;
3447c478bd9Sstevel@tonic-gate if ((buf = realloc(buf,
3457c478bd9Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) {
3467c478bd9Sstevel@tonic-gate exit(2); /* out of memory */
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate nlp = buf;
3497c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz];
3507c478bd9Sstevel@tonic-gate } else {
3517c478bd9Sstevel@tonic-gate /* shift the buffer down */
3527c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp,
3537c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ]
3547c478bd9Sstevel@tonic-gate - nlp);
3557c478bd9Sstevel@tonic-gate p -= nlp - buf;
3567c478bd9Sstevel@tonic-gate nlp = buf;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) {
3617c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char),
3627c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr))
3637c478bd9Sstevel@tonic-gate <= 0)
3647c478bd9Sstevel@tonic-gate break;
3657c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char),
3667c478bd9Sstevel@tonic-gate BUFSIZ, fptr)) <= 0)
3677c478bd9Sstevel@tonic-gate break;
3687c478bd9Sstevel@tonic-gate blkno += (long long)ccount;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate GETONE(lc, p);
3717c478bd9Sstevel@tonic-gate nstate:
3727c478bd9Sstevel@tonic-gate if (same(c->inp, lc)) {
3737c478bd9Sstevel@tonic-gate c = c->nst;
3747c478bd9Sstevel@tonic-gate } else if (c->link != 0) {
3757c478bd9Sstevel@tonic-gate c = c->link;
3767c478bd9Sstevel@tonic-gate goto nstate;
3777c478bd9Sstevel@tonic-gate } else {
3787c478bd9Sstevel@tonic-gate c = c->fail;
3797c478bd9Sstevel@tonic-gate failed = 1;
3807c478bd9Sstevel@tonic-gate if (c == 0) {
3817c478bd9Sstevel@tonic-gate c = w;
3827c478bd9Sstevel@tonic-gate istate:
3837c478bd9Sstevel@tonic-gate if (same(c->inp, lc)) {
3847c478bd9Sstevel@tonic-gate c = c->nst;
3857c478bd9Sstevel@tonic-gate } else if (c->link != 0) {
3867c478bd9Sstevel@tonic-gate c = c->link;
3877c478bd9Sstevel@tonic-gate goto istate;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate } else
3907c478bd9Sstevel@tonic-gate goto nstate;
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate if (c == 0)
3947c478bd9Sstevel@tonic-gate break;
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate if (c->out) {
3977c478bd9Sstevel@tonic-gate while (lc != '\n') {
3987c478bd9Sstevel@tonic-gate if (ccount <= 0) {
3997c478bd9Sstevel@tonic-gate if (p == &buf[fw_lBufsiz + BUFSIZ]) {
4007c478bd9Sstevel@tonic-gate if (nlp == buf) {
4017c478bd9Sstevel@tonic-gate /* increase buffer size */
4027c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ;
4037c478bd9Sstevel@tonic-gate if ((buf = realloc(buf, fw_lBufsiz + BUFSIZ)) == NULL) {
4047c478bd9Sstevel@tonic-gate exit(2); /* out of memory */
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate nlp = buf;
4077c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz];
4087c478bd9Sstevel@tonic-gate } else {
4097c478bd9Sstevel@tonic-gate /* shift buffer down */
4107c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp, &buf[fw_lBufsiz + BUFSIZ] - nlp);
4117c478bd9Sstevel@tonic-gate p -= nlp - buf;
4127c478bd9Sstevel@tonic-gate nlp = buf;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) {
4167c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char),
4177c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) <= 0) break;
4187c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char), BUFSIZ,
4197c478bd9Sstevel@tonic-gate fptr)) <= 0) break;
4207c478bd9Sstevel@tonic-gate blkno += (long long)ccount;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate GETONE(lc, p);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate if ((vflag && (failed == 0 || xflag == 0)) ||
4257c478bd9Sstevel@tonic-gate (vflag == 0 && xflag && failed))
4267c478bd9Sstevel@tonic-gate goto nomatch;
4277c478bd9Sstevel@tonic-gate succeed:
4287c478bd9Sstevel@tonic-gate nsucc = 1;
429*41599e9fSDamian Bogel if (lflag || qflag) {
430*41599e9fSDamian Bogel if (!qflag)
4317c478bd9Sstevel@tonic-gate (void) printf("%s\n", file);
4327c478bd9Sstevel@tonic-gate (void) fclose(fptr);
4337c478bd9Sstevel@tonic-gate return;
434*41599e9fSDamian Bogel }
435*41599e9fSDamian Bogel if (cflag) {
436*41599e9fSDamian Bogel tln++;
437*41599e9fSDamian Bogel } else {
438*41599e9fSDamian Bogel if (Hflag || (nfile > 1 && !hflag))
4397c478bd9Sstevel@tonic-gate (void) printf("%s:", file);
4407c478bd9Sstevel@tonic-gate if (bflag)
4417c478bd9Sstevel@tonic-gate (void) printf("%lld:",
4427c478bd9Sstevel@tonic-gate (blkno - (long long)(ccount-1))
4437c478bd9Sstevel@tonic-gate / BUFSIZ);
4447c478bd9Sstevel@tonic-gate if (nflag)
4457c478bd9Sstevel@tonic-gate (void) printf("%lld:", lnum);
4467c478bd9Sstevel@tonic-gate if (p <= nlp) {
4477c478bd9Sstevel@tonic-gate while (nlp < &buf[fw_lBufsiz + BUFSIZ])
4487c478bd9Sstevel@tonic-gate (void) putchar(*nlp++);
4497c478bd9Sstevel@tonic-gate nlp = buf;
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate while (nlp < p)
4527c478bd9Sstevel@tonic-gate (void) putchar(*nlp++);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate nomatch:
4557c478bd9Sstevel@tonic-gate lnum++;
4567c478bd9Sstevel@tonic-gate nlp = p;
4577c478bd9Sstevel@tonic-gate c = w;
4587c478bd9Sstevel@tonic-gate failed = 0;
4597c478bd9Sstevel@tonic-gate continue;
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate if (lc == '\n')
4627c478bd9Sstevel@tonic-gate if (vflag)
4637c478bd9Sstevel@tonic-gate goto succeed;
4647c478bd9Sstevel@tonic-gate else {
4657c478bd9Sstevel@tonic-gate lnum++;
4667c478bd9Sstevel@tonic-gate nlp = p;
4677c478bd9Sstevel@tonic-gate c = w;
4687c478bd9Sstevel@tonic-gate failed = 0;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate (void) fclose(fptr);
472*41599e9fSDamian Bogel if (cflag && !qflag) {
473*41599e9fSDamian Bogel if (Hflag || (nfile > 1 && !hflag))
4747c478bd9Sstevel@tonic-gate (void) printf("%s:", file);
4757c478bd9Sstevel@tonic-gate (void) printf("%lld\n", tln);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate wchar_t
getargc(void)4817c478bd9Sstevel@tonic-gate getargc(void)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate /* appends a newline to shell quoted argument list so */
4847c478bd9Sstevel@tonic-gate /* the list looks like it came from an ed style file */
4857c478bd9Sstevel@tonic-gate wchar_t c;
4867c478bd9Sstevel@tonic-gate int cw;
4877c478bd9Sstevel@tonic-gate int b;
4887c478bd9Sstevel@tonic-gate static int endflg;
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate if (wordf) {
4927c478bd9Sstevel@tonic-gate if ((b = getc(wordf)) == EOF)
4937c478bd9Sstevel@tonic-gate return (EOF);
4947c478bd9Sstevel@tonic-gate cw = ISASCII(c = (wchar_t)b) ? 1 :
4957c478bd9Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
4967c478bd9Sstevel@tonic-gate while (--cw) {
4977c478bd9Sstevel@tonic-gate if ((b = getc(wordf)) == EOF)
4987c478bd9Sstevel@tonic-gate return (EOF);
4997c478bd9Sstevel@tonic-gate c = (c << 7) | (b & 0177);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate return (iflag ? letter(c) : c);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate if (endflg)
5057c478bd9Sstevel@tonic-gate return (EOF);
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate cw = ISASCII(c = (unsigned char)*argptr++) ? 1 :
5097c478bd9Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate while (--cw)
5127c478bd9Sstevel@tonic-gate c = (c << 7) | ((*argptr++) & 0177);
5137c478bd9Sstevel@tonic-gate if (c == '\0') {
5147c478bd9Sstevel@tonic-gate endflg++;
5157c478bd9Sstevel@tonic-gate return ('\n');
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate return (iflag ? letter(c) : c);
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate void
cgotofn(void)5247c478bd9Sstevel@tonic-gate cgotofn(void)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate int c;
5277c478bd9Sstevel@tonic-gate struct words *s;
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate s = smax = w;
5307c478bd9Sstevel@tonic-gate nword:
5317c478bd9Sstevel@tonic-gate for (;;) {
5327c478bd9Sstevel@tonic-gate c = getargc();
5337c478bd9Sstevel@tonic-gate if (c == EOF)
5347c478bd9Sstevel@tonic-gate return;
5357c478bd9Sstevel@tonic-gate if (c == 0)
5367c478bd9Sstevel@tonic-gate goto enter;
5377c478bd9Sstevel@tonic-gate if (c == '\n') {
5387c478bd9Sstevel@tonic-gate if (xflag) {
5397c478bd9Sstevel@tonic-gate for (;;) {
5407c478bd9Sstevel@tonic-gate if (s->inp == c) {
5417c478bd9Sstevel@tonic-gate s = s->nst;
5427c478bd9Sstevel@tonic-gate break;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate if (s->inp == 0)
5457c478bd9Sstevel@tonic-gate goto nenter;
5467c478bd9Sstevel@tonic-gate if (s->link == 0) {
5473f9207dcSpd155743 if (smax >= &w[input_size -1])
5487c478bd9Sstevel@tonic-gate overflo();
5497c478bd9Sstevel@tonic-gate s->link = ++smax;
5507c478bd9Sstevel@tonic-gate s = smax;
5517c478bd9Sstevel@tonic-gate goto nenter;
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate s = s->link;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate s->out = 1;
5577c478bd9Sstevel@tonic-gate s = w;
5587c478bd9Sstevel@tonic-gate } else {
5597c478bd9Sstevel@tonic-gate loop:
5607c478bd9Sstevel@tonic-gate if (s->inp == c) {
5617c478bd9Sstevel@tonic-gate s = s->nst;
5627c478bd9Sstevel@tonic-gate continue;
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate if (s->inp == 0)
5657c478bd9Sstevel@tonic-gate goto enter;
5667c478bd9Sstevel@tonic-gate if (s->link == 0) {
5673f9207dcSpd155743 if (smax >= &w[input_size -1])
5687c478bd9Sstevel@tonic-gate overflo();
5697c478bd9Sstevel@tonic-gate s->link = ++smax;
5707c478bd9Sstevel@tonic-gate s = smax;
5717c478bd9Sstevel@tonic-gate goto enter;
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate s = s->link;
5747c478bd9Sstevel@tonic-gate goto loop;
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate enter:
5797c478bd9Sstevel@tonic-gate do {
5807c478bd9Sstevel@tonic-gate s->inp = c;
5813f9207dcSpd155743 if (smax >= &w[input_size -1])
5827c478bd9Sstevel@tonic-gate overflo();
5837c478bd9Sstevel@tonic-gate s->nst = ++smax;
5847c478bd9Sstevel@tonic-gate s = smax;
5857c478bd9Sstevel@tonic-gate } while ((c = getargc()) != '\n' && c != EOF);
5867c478bd9Sstevel@tonic-gate if (xflag) {
5877c478bd9Sstevel@tonic-gate nenter:
5887c478bd9Sstevel@tonic-gate s->inp = '\n';
5893f9207dcSpd155743 if (smax >= &w[input_size -1])
5907c478bd9Sstevel@tonic-gate overflo();
5917c478bd9Sstevel@tonic-gate s->nst = ++smax;
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate smax->out = 1;
5947c478bd9Sstevel@tonic-gate s = w;
5957c478bd9Sstevel@tonic-gate if (c != EOF)
5967c478bd9Sstevel@tonic-gate goto nword;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate
5993f9207dcSpd155743 /*
6003f9207dcSpd155743 * This function is an unexpected condition, since input_size should have been
6013f9207dcSpd155743 * calculated correctly before hand.
6023f9207dcSpd155743 */
6033f9207dcSpd155743
6047c478bd9Sstevel@tonic-gate void
overflo(void)6057c478bd9Sstevel@tonic-gate overflo(void)
6067c478bd9Sstevel@tonic-gate {
6073f9207dcSpd155743 (void) fprintf(stderr, gettext("fgrep: wordlist too large\n"));
6087c478bd9Sstevel@tonic-gate exit(2);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate void
cfail(void)6127c478bd9Sstevel@tonic-gate cfail(void)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate int qsize = QSIZE;
6157c478bd9Sstevel@tonic-gate struct words **queue = NULL;
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate * front and rear are pointers used to traverse the global words
6197c478bd9Sstevel@tonic-gate * structure "w" which contains the data of input pattern file
6207c478bd9Sstevel@tonic-gate */
6217c478bd9Sstevel@tonic-gate struct words **front, **rear;
6227c478bd9Sstevel@tonic-gate struct words *state;
6237c478bd9Sstevel@tonic-gate unsigned long frontoffset = 0, rearoffset = 0;
6247c478bd9Sstevel@tonic-gate char c;
6257c478bd9Sstevel@tonic-gate struct words *s;
6267c478bd9Sstevel@tonic-gate s = w;
6277c478bd9Sstevel@tonic-gate if ((queue = (struct words **)calloc(qsize, sizeof (struct words *)))
6287c478bd9Sstevel@tonic-gate == NULL) {
6297c478bd9Sstevel@tonic-gate perror("fgrep");
6307c478bd9Sstevel@tonic-gate exit(2);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate front = rear = queue;
6337c478bd9Sstevel@tonic-gate init:
6347c478bd9Sstevel@tonic-gate if ((s->inp) != 0) {
6357c478bd9Sstevel@tonic-gate *rear++ = s->nst;
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * Reallocates the queue if the number of distinct starting
6387c478bd9Sstevel@tonic-gate * character of patterns exceeds the qsize value
6397c478bd9Sstevel@tonic-gate */
6407c478bd9Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) {
6417c478bd9Sstevel@tonic-gate frontoffset = front - queue;
6427c478bd9Sstevel@tonic-gate rearoffset = rear - queue;
6437c478bd9Sstevel@tonic-gate qsize += QSIZE;
6447c478bd9Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue,
6457c478bd9Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) {
6467c478bd9Sstevel@tonic-gate perror("fgrep");
6477c478bd9Sstevel@tonic-gate exit(2);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate front = queue + frontoffset;
6507c478bd9Sstevel@tonic-gate rear = queue + rearoffset;
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate if ((s = s->link) != 0) {
6547c478bd9Sstevel@tonic-gate goto init;
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate while (rear != front) {
6587c478bd9Sstevel@tonic-gate s = *front++;
6597c478bd9Sstevel@tonic-gate cloop:
6607c478bd9Sstevel@tonic-gate if ((c = s->inp) != 0) {
6617c478bd9Sstevel@tonic-gate *rear++ = (q = s->nst);
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate * Reallocate the queue if the rear pointer reaches the end
6647c478bd9Sstevel@tonic-gate * queue
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) {
6677c478bd9Sstevel@tonic-gate frontoffset = front - queue;
6687c478bd9Sstevel@tonic-gate rearoffset = rear - queue;
6697c478bd9Sstevel@tonic-gate qsize += QSIZE;
6707c478bd9Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue,
6717c478bd9Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) {
6727c478bd9Sstevel@tonic-gate perror("fgrep");
6737c478bd9Sstevel@tonic-gate exit(2);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate front = queue + frontoffset;
6767c478bd9Sstevel@tonic-gate rear = queue + rearoffset;
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate state = s->fail;
6797c478bd9Sstevel@tonic-gate floop:
6807c478bd9Sstevel@tonic-gate if (state == 0)
6817c478bd9Sstevel@tonic-gate state = w;
6827c478bd9Sstevel@tonic-gate if (state->inp == c) {
6837c478bd9Sstevel@tonic-gate qloop:
6847c478bd9Sstevel@tonic-gate q->fail = state->nst;
6857c478bd9Sstevel@tonic-gate if ((state->nst)->out == 1)
6867c478bd9Sstevel@tonic-gate q->out = 1;
6877c478bd9Sstevel@tonic-gate if ((q = q->link) != 0)
6887c478bd9Sstevel@tonic-gate goto qloop;
6897c478bd9Sstevel@tonic-gate } else if ((state = state->link) != 0)
6907c478bd9Sstevel@tonic-gate goto floop;
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate if ((s = s->link) != 0)
6937c478bd9Sstevel@tonic-gate goto cloop;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate wchar_t
letter(wchar_t c)6987c478bd9Sstevel@tonic-gate letter(wchar_t c)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate if (c >= 'a' && c <= 'z')
7017c478bd9Sstevel@tonic-gate return (c);
7027c478bd9Sstevel@tonic-gate if (c >= 'A' && c <= 'Z')
7037c478bd9Sstevel@tonic-gate return (c + 'a' - 'A');
7047c478bd9Sstevel@tonic-gate return (c);
7057c478bd9Sstevel@tonic-gate }
706