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 337c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate /* 367c478bd9Sstevel@tonic-gate * fgrep -- print all lines containing any of a set of keywords 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * status returns: 397c478bd9Sstevel@tonic-gate * 0 - ok, and some matches 407c478bd9Sstevel@tonic-gate * 1 - ok, but no matches 417c478bd9Sstevel@tonic-gate * 2 - some error 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <ctype.h> 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <locale.h> 507c478bd9Sstevel@tonic-gate #include <libintl.h> 517c478bd9Sstevel@tonic-gate #include <euc.h> 52*3f9207dcSpd155743 #include <sys/stat.h> 53*3f9207dcSpd155743 #include <fcntl.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #include <getwidth.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate eucwidth_t WW; 587c478bd9Sstevel@tonic-gate #define WIDTH1 WW._eucw1 597c478bd9Sstevel@tonic-gate #define WIDTH2 WW._eucw2 607c478bd9Sstevel@tonic-gate #define WIDTH3 WW._eucw3 617c478bd9Sstevel@tonic-gate #define MULTI_BYTE WW._multibyte 627c478bd9Sstevel@tonic-gate #define GETONE(lc, p) \ 637c478bd9Sstevel@tonic-gate cw = ISASCII(lc = (unsigned char)*p++) ? 1 : \ 647c478bd9Sstevel@tonic-gate (ISSET2(lc) ? WIDTH2 : \ 657c478bd9Sstevel@tonic-gate (ISSET3(lc) ? WIDTH3 : WIDTH1)); \ 667c478bd9Sstevel@tonic-gate if (--cw > --ccount) { \ 677c478bd9Sstevel@tonic-gate cw -= ccount; \ 687c478bd9Sstevel@tonic-gate while (ccount--) \ 697c478bd9Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177); \ 707c478bd9Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) { \ 717c478bd9Sstevel@tonic-gate if (nlp == buf) { \ 727c478bd9Sstevel@tonic-gate /* Increase the buffer size */ \ 737c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ; \ 747c478bd9Sstevel@tonic-gate if ((buf = realloc(buf, \ 757c478bd9Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) { \ 767c478bd9Sstevel@tonic-gate exit(2); /* out of memory */ \ 777c478bd9Sstevel@tonic-gate } \ 787c478bd9Sstevel@tonic-gate nlp = buf; \ 797c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz]; \ 807c478bd9Sstevel@tonic-gate } else { \ 817c478bd9Sstevel@tonic-gate /* shift the buffer contents down */ \ 827c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp, \ 837c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - nlp);\ 847c478bd9Sstevel@tonic-gate p -= nlp - buf; \ 857c478bd9Sstevel@tonic-gate nlp = buf; \ 867c478bd9Sstevel@tonic-gate } \ 877c478bd9Sstevel@tonic-gate } \ 887c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) { \ 897c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char), \ 907c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr))\ 917c478bd9Sstevel@tonic-gate <= 0) break; \ 927c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, \ 937c478bd9Sstevel@tonic-gate sizeof (char), BUFSIZ, fptr)) <= 0) \ 947c478bd9Sstevel@tonic-gate break; \ 957c478bd9Sstevel@tonic-gate blkno += (long long)ccount; \ 967c478bd9Sstevel@tonic-gate } \ 977c478bd9Sstevel@tonic-gate ccount -= cw; \ 987c478bd9Sstevel@tonic-gate while (cw--) \ 997c478bd9Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177) 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * The same() macro and letter() function were inserted to allow for 1037c478bd9Sstevel@tonic-gate * the -i option work for the multi-byte environment. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate wchar_t letter(); 1067c478bd9Sstevel@tonic-gate #define same(a, b) \ 1077c478bd9Sstevel@tonic-gate (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \ 1087c478bd9Sstevel@tonic-gate letter(a) == letter(b)) 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #define QSIZE 400 1127c478bd9Sstevel@tonic-gate struct words { 1137c478bd9Sstevel@tonic-gate wchar_t inp; 1147c478bd9Sstevel@tonic-gate char out; 1157c478bd9Sstevel@tonic-gate struct words *nst; 1167c478bd9Sstevel@tonic-gate struct words *link; 1177c478bd9Sstevel@tonic-gate struct words *fail; 118*3f9207dcSpd155743 } *w = NULL, *smax, *q; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate FILE *fptr; 1217c478bd9Sstevel@tonic-gate long long lnum; 1227c478bd9Sstevel@tonic-gate int bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag; 1237c478bd9Sstevel@tonic-gate int hflag, iflag; 1247c478bd9Sstevel@tonic-gate int retcode = 0; 1257c478bd9Sstevel@tonic-gate int nfile; 1267c478bd9Sstevel@tonic-gate long long blkno; 1277c478bd9Sstevel@tonic-gate int nsucc; 1287c478bd9Sstevel@tonic-gate long long tln; 1297c478bd9Sstevel@tonic-gate FILE *wordf; 1307c478bd9Sstevel@tonic-gate char *argptr; 131*3f9207dcSpd155743 off_t input_size = 0; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate void execute(char *); 1347c478bd9Sstevel@tonic-gate void cgotofn(void); 1357c478bd9Sstevel@tonic-gate void overflo(void); 1367c478bd9Sstevel@tonic-gate void cfail(void); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate static long fw_lBufsiz = 0; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate int 1417c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1427c478bd9Sstevel@tonic-gate { 1437c478bd9Sstevel@tonic-gate int c; 1447c478bd9Sstevel@tonic-gate int errflg = 0; 145*3f9207dcSpd155743 struct stat file_stat; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1487c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1497c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1507c478bd9Sstevel@tonic-gate #endif 1517c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "hybcie:f:lnvxs")) != EOF) 1547c478bd9Sstevel@tonic-gate switch (c) { 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate case 's': 1577c478bd9Sstevel@tonic-gate sflag++; 1587c478bd9Sstevel@tonic-gate continue; 1597c478bd9Sstevel@tonic-gate case 'h': 1607c478bd9Sstevel@tonic-gate hflag++; 1617c478bd9Sstevel@tonic-gate continue; 1627c478bd9Sstevel@tonic-gate case 'b': 1637c478bd9Sstevel@tonic-gate bflag++; 1647c478bd9Sstevel@tonic-gate continue; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate case 'i': 1677c478bd9Sstevel@tonic-gate case 'y': 1687c478bd9Sstevel@tonic-gate iflag++; 1697c478bd9Sstevel@tonic-gate continue; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate case 'c': 1727c478bd9Sstevel@tonic-gate cflag++; 1737c478bd9Sstevel@tonic-gate continue; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate case 'e': 1767c478bd9Sstevel@tonic-gate eflag++; 1777c478bd9Sstevel@tonic-gate argptr = optarg; 178*3f9207dcSpd155743 input_size = strlen(argptr); 1797c478bd9Sstevel@tonic-gate continue; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate case 'f': 1827c478bd9Sstevel@tonic-gate fflag++; 1837c478bd9Sstevel@tonic-gate wordf = fopen(optarg, "r"); 1847c478bd9Sstevel@tonic-gate if (wordf == NULL) { 1857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1867c478bd9Sstevel@tonic-gate gettext("fgrep: can't open %s\n"), 1877c478bd9Sstevel@tonic-gate optarg); 1887c478bd9Sstevel@tonic-gate exit(2); 1897c478bd9Sstevel@tonic-gate } 190*3f9207dcSpd155743 191*3f9207dcSpd155743 if (fstat(fileno(wordf), &file_stat) == 0) { 192*3f9207dcSpd155743 input_size = file_stat.st_size; 193*3f9207dcSpd155743 } else { 194*3f9207dcSpd155743 (void) fprintf(stderr, 195*3f9207dcSpd155743 gettext("fgrep: can't fstat %s\n"), 196*3f9207dcSpd155743 optarg); 197*3f9207dcSpd155743 exit(2); 198*3f9207dcSpd155743 } 199*3f9207dcSpd155743 2007c478bd9Sstevel@tonic-gate continue; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate case 'l': 2037c478bd9Sstevel@tonic-gate lflag++; 2047c478bd9Sstevel@tonic-gate continue; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate case 'n': 2077c478bd9Sstevel@tonic-gate nflag++; 2087c478bd9Sstevel@tonic-gate continue; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate case 'v': 2117c478bd9Sstevel@tonic-gate vflag++; 2127c478bd9Sstevel@tonic-gate continue; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate case 'x': 2157c478bd9Sstevel@tonic-gate xflag++; 2167c478bd9Sstevel@tonic-gate continue; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate case '?': 2197c478bd9Sstevel@tonic-gate errflg++; 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate argc -= optind; 2237c478bd9Sstevel@tonic-gate if (errflg || ((argc <= 0) && !fflag && !eflag)) { 2247c478bd9Sstevel@tonic-gate (void) printf(gettext("usage: fgrep [ -bchilnsvx ] " 2257c478bd9Sstevel@tonic-gate "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n")); 2267c478bd9Sstevel@tonic-gate exit(2); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate if (!eflag && !fflag) { 2297c478bd9Sstevel@tonic-gate argptr = argv[optind]; 230*3f9207dcSpd155743 input_size = strlen(argptr); 231*3f9207dcSpd155743 input_size++; 2327c478bd9Sstevel@tonic-gate optind++; 2337c478bd9Sstevel@tonic-gate argc--; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 236*3f9207dcSpd155743 /* 237*3f9207dcSpd155743 * Normally we need one struct words for each letter in the pattern 238*3f9207dcSpd155743 * plus one terminating struct words with outp = 1, but when -x option 239*3f9207dcSpd155743 * is specified we require one more struct words for `\n` character so we 240*3f9207dcSpd155743 * calculate the input_size as below. We add extra 1 because 241*3f9207dcSpd155743 * (input_size/2) rounds off odd numbers 242*3f9207dcSpd155743 */ 243*3f9207dcSpd155743 244*3f9207dcSpd155743 if (xflag) { 245*3f9207dcSpd155743 input_size = input_size + (input_size/2) + 1; 246*3f9207dcSpd155743 } 247*3f9207dcSpd155743 248*3f9207dcSpd155743 input_size++; 249*3f9207dcSpd155743 250*3f9207dcSpd155743 w = (struct words *)calloc(input_size, sizeof (struct words)); 251*3f9207dcSpd155743 if (w == NULL) { 252*3f9207dcSpd155743 (void) fprintf(stderr, 253*3f9207dcSpd155743 gettext("fgrep: could not allocate " 254*3f9207dcSpd155743 "memory for wordlist\n")); 255*3f9207dcSpd155743 exit(2); 256*3f9207dcSpd155743 } 257*3f9207dcSpd155743 2587c478bd9Sstevel@tonic-gate getwidth(&WW); 2597c478bd9Sstevel@tonic-gate if ((WIDTH1 == 0) && (WIDTH2 == 0) && 2607c478bd9Sstevel@tonic-gate (WIDTH3 == 0)) { 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * If non EUC-based locale, 2637c478bd9Sstevel@tonic-gate * assume WIDTH1 is 1. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate WIDTH1 = 1; 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate WIDTH2++; 2687c478bd9Sstevel@tonic-gate WIDTH3++; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate cgotofn(); 2717c478bd9Sstevel@tonic-gate cfail(); 2727c478bd9Sstevel@tonic-gate nfile = argc; 2737c478bd9Sstevel@tonic-gate argv = &argv[optind]; 2747c478bd9Sstevel@tonic-gate if (argc <= 0) { 2757c478bd9Sstevel@tonic-gate execute((char *)NULL); 2767c478bd9Sstevel@tonic-gate } else 2777c478bd9Sstevel@tonic-gate while (--argc >= 0) { 2787c478bd9Sstevel@tonic-gate execute(*argv); 2797c478bd9Sstevel@tonic-gate argv++; 2807c478bd9Sstevel@tonic-gate } 281*3f9207dcSpd155743 282*3f9207dcSpd155743 if (w != NULL) { 283*3f9207dcSpd155743 free(w); 284*3f9207dcSpd155743 } 285*3f9207dcSpd155743 2867c478bd9Sstevel@tonic-gate return (retcode != 0 ? retcode : nsucc == 0); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate void 2907c478bd9Sstevel@tonic-gate execute(char *file) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate char *p; 2937c478bd9Sstevel@tonic-gate struct words *c; 2947c478bd9Sstevel@tonic-gate int ccount; 2957c478bd9Sstevel@tonic-gate static char *buf = NULL; 2967c478bd9Sstevel@tonic-gate int failed; 2977c478bd9Sstevel@tonic-gate char *nlp; 2987c478bd9Sstevel@tonic-gate wchar_t lc; 2997c478bd9Sstevel@tonic-gate int cw; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate if (buf == NULL) { 3027c478bd9Sstevel@tonic-gate fw_lBufsiz = BUFSIZ; 3037c478bd9Sstevel@tonic-gate if ((buf = malloc(fw_lBufsiz + BUFSIZ)) == NULL) { 3047c478bd9Sstevel@tonic-gate exit(2); /* out of memory */ 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate if (file) { 3097c478bd9Sstevel@tonic-gate if ((fptr = fopen(file, "r")) == NULL) { 3107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3117c478bd9Sstevel@tonic-gate gettext("fgrep: can't open %s\n"), file); 3127c478bd9Sstevel@tonic-gate retcode = 2; 3137c478bd9Sstevel@tonic-gate return; 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate } else { 3167c478bd9Sstevel@tonic-gate file = "<stdin>"; 3177c478bd9Sstevel@tonic-gate fptr = stdin; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate ccount = 0; 3207c478bd9Sstevel@tonic-gate failed = 0; 3217c478bd9Sstevel@tonic-gate lnum = 1; 3227c478bd9Sstevel@tonic-gate tln = 0; 3237c478bd9Sstevel@tonic-gate blkno = 0; 3247c478bd9Sstevel@tonic-gate p = buf; 3257c478bd9Sstevel@tonic-gate nlp = p; 3267c478bd9Sstevel@tonic-gate c = w; 3277c478bd9Sstevel@tonic-gate for (;;) { 3287c478bd9Sstevel@tonic-gate if (c == 0) 3297c478bd9Sstevel@tonic-gate break; 3307c478bd9Sstevel@tonic-gate if (ccount <= 0) { 3317c478bd9Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) { 3327c478bd9Sstevel@tonic-gate if (nlp == buf) { 3337c478bd9Sstevel@tonic-gate /* increase the buffer size */ 3347c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ; 3357c478bd9Sstevel@tonic-gate if ((buf = realloc(buf, 3367c478bd9Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) { 3377c478bd9Sstevel@tonic-gate exit(2); /* out of memory */ 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate nlp = buf; 3407c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz]; 3417c478bd9Sstevel@tonic-gate } else { 3427c478bd9Sstevel@tonic-gate /* shift the buffer down */ 3437c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp, 3447c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] 3457c478bd9Sstevel@tonic-gate - nlp); 3467c478bd9Sstevel@tonic-gate p -= nlp - buf; 3477c478bd9Sstevel@tonic-gate nlp = buf; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) { 3527c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char), 3537c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) 3547c478bd9Sstevel@tonic-gate <= 0) 3557c478bd9Sstevel@tonic-gate break; 3567c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char), 3577c478bd9Sstevel@tonic-gate BUFSIZ, fptr)) <= 0) 3587c478bd9Sstevel@tonic-gate break; 3597c478bd9Sstevel@tonic-gate blkno += (long long)ccount; 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate GETONE(lc, p); 3627c478bd9Sstevel@tonic-gate nstate: 3637c478bd9Sstevel@tonic-gate if (same(c->inp, lc)) { 3647c478bd9Sstevel@tonic-gate c = c->nst; 3657c478bd9Sstevel@tonic-gate } else if (c->link != 0) { 3667c478bd9Sstevel@tonic-gate c = c->link; 3677c478bd9Sstevel@tonic-gate goto nstate; 3687c478bd9Sstevel@tonic-gate } else { 3697c478bd9Sstevel@tonic-gate c = c->fail; 3707c478bd9Sstevel@tonic-gate failed = 1; 3717c478bd9Sstevel@tonic-gate if (c == 0) { 3727c478bd9Sstevel@tonic-gate c = w; 3737c478bd9Sstevel@tonic-gate istate: 3747c478bd9Sstevel@tonic-gate if (same(c->inp, lc)) { 3757c478bd9Sstevel@tonic-gate c = c->nst; 3767c478bd9Sstevel@tonic-gate } else if (c->link != 0) { 3777c478bd9Sstevel@tonic-gate c = c->link; 3787c478bd9Sstevel@tonic-gate goto istate; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate } else 3817c478bd9Sstevel@tonic-gate goto nstate; 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate if (c == 0) 3857c478bd9Sstevel@tonic-gate break; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if (c->out) { 3887c478bd9Sstevel@tonic-gate while (lc != '\n') { 3897c478bd9Sstevel@tonic-gate if (ccount <= 0) { 3907c478bd9Sstevel@tonic-gate if (p == &buf[fw_lBufsiz + BUFSIZ]) { 3917c478bd9Sstevel@tonic-gate if (nlp == buf) { 3927c478bd9Sstevel@tonic-gate /* increase buffer size */ 3937c478bd9Sstevel@tonic-gate fw_lBufsiz += BUFSIZ; 3947c478bd9Sstevel@tonic-gate if ((buf = realloc(buf, fw_lBufsiz + BUFSIZ)) == NULL) { 3957c478bd9Sstevel@tonic-gate exit(2); /* out of memory */ 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate nlp = buf; 3987c478bd9Sstevel@tonic-gate p = &buf[fw_lBufsiz]; 3997c478bd9Sstevel@tonic-gate } else { 4007c478bd9Sstevel@tonic-gate /* shift buffer down */ 4017c478bd9Sstevel@tonic-gate (void) memmove(buf, nlp, &buf[fw_lBufsiz + BUFSIZ] - nlp); 4027c478bd9Sstevel@tonic-gate p -= nlp - buf; 4037c478bd9Sstevel@tonic-gate nlp = buf; 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) { 4077c478bd9Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char), 4087c478bd9Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) <= 0) break; 4097c478bd9Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char), BUFSIZ, 4107c478bd9Sstevel@tonic-gate fptr)) <= 0) break; 4117c478bd9Sstevel@tonic-gate blkno += (long long)ccount; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate GETONE(lc, p); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate if ((vflag && (failed == 0 || xflag == 0)) || 4167c478bd9Sstevel@tonic-gate (vflag == 0 && xflag && failed)) 4177c478bd9Sstevel@tonic-gate goto nomatch; 4187c478bd9Sstevel@tonic-gate succeed: 4197c478bd9Sstevel@tonic-gate nsucc = 1; 4207c478bd9Sstevel@tonic-gate if (cflag) 4217c478bd9Sstevel@tonic-gate tln++; 4227c478bd9Sstevel@tonic-gate else if (lflag && !sflag) { 4237c478bd9Sstevel@tonic-gate (void) printf("%s\n", file); 4247c478bd9Sstevel@tonic-gate (void) fclose(fptr); 4257c478bd9Sstevel@tonic-gate return; 4267c478bd9Sstevel@tonic-gate } else if (!sflag) { 4277c478bd9Sstevel@tonic-gate if (nfile > 1 && !hflag) 4287c478bd9Sstevel@tonic-gate (void) printf("%s:", file); 4297c478bd9Sstevel@tonic-gate if (bflag) 4307c478bd9Sstevel@tonic-gate (void) printf("%lld:", 4317c478bd9Sstevel@tonic-gate (blkno - (long long)(ccount-1)) 4327c478bd9Sstevel@tonic-gate / BUFSIZ); 4337c478bd9Sstevel@tonic-gate if (nflag) 4347c478bd9Sstevel@tonic-gate (void) printf("%lld:", lnum); 4357c478bd9Sstevel@tonic-gate if (p <= nlp) { 4367c478bd9Sstevel@tonic-gate while (nlp < &buf[fw_lBufsiz + BUFSIZ]) 4377c478bd9Sstevel@tonic-gate (void) putchar(*nlp++); 4387c478bd9Sstevel@tonic-gate nlp = buf; 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate while (nlp < p) 4417c478bd9Sstevel@tonic-gate (void) putchar(*nlp++); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate nomatch: 4447c478bd9Sstevel@tonic-gate lnum++; 4457c478bd9Sstevel@tonic-gate nlp = p; 4467c478bd9Sstevel@tonic-gate c = w; 4477c478bd9Sstevel@tonic-gate failed = 0; 4487c478bd9Sstevel@tonic-gate continue; 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate if (lc == '\n') 4517c478bd9Sstevel@tonic-gate if (vflag) 4527c478bd9Sstevel@tonic-gate goto succeed; 4537c478bd9Sstevel@tonic-gate else { 4547c478bd9Sstevel@tonic-gate lnum++; 4557c478bd9Sstevel@tonic-gate nlp = p; 4567c478bd9Sstevel@tonic-gate c = w; 4577c478bd9Sstevel@tonic-gate failed = 0; 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate (void) fclose(fptr); 4617c478bd9Sstevel@tonic-gate if (cflag) { 4627c478bd9Sstevel@tonic-gate if ((nfile > 1) && !hflag) 4637c478bd9Sstevel@tonic-gate (void) printf("%s:", file); 4647c478bd9Sstevel@tonic-gate (void) printf("%lld\n", tln); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate wchar_t 4707c478bd9Sstevel@tonic-gate getargc(void) 4717c478bd9Sstevel@tonic-gate { 4727c478bd9Sstevel@tonic-gate /* appends a newline to shell quoted argument list so */ 4737c478bd9Sstevel@tonic-gate /* the list looks like it came from an ed style file */ 4747c478bd9Sstevel@tonic-gate wchar_t c; 4757c478bd9Sstevel@tonic-gate int cw; 4767c478bd9Sstevel@tonic-gate int b; 4777c478bd9Sstevel@tonic-gate static int endflg; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate if (wordf) { 4817c478bd9Sstevel@tonic-gate if ((b = getc(wordf)) == EOF) 4827c478bd9Sstevel@tonic-gate return (EOF); 4837c478bd9Sstevel@tonic-gate cw = ISASCII(c = (wchar_t)b) ? 1 : 4847c478bd9Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1)); 4857c478bd9Sstevel@tonic-gate while (--cw) { 4867c478bd9Sstevel@tonic-gate if ((b = getc(wordf)) == EOF) 4877c478bd9Sstevel@tonic-gate return (EOF); 4887c478bd9Sstevel@tonic-gate c = (c << 7) | (b & 0177); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate return (iflag ? letter(c) : c); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate if (endflg) 4947c478bd9Sstevel@tonic-gate return (EOF); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate cw = ISASCII(c = (unsigned char)*argptr++) ? 1 : 4987c478bd9Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1)); 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate while (--cw) 5017c478bd9Sstevel@tonic-gate c = (c << 7) | ((*argptr++) & 0177); 5027c478bd9Sstevel@tonic-gate if (c == '\0') { 5037c478bd9Sstevel@tonic-gate endflg++; 5047c478bd9Sstevel@tonic-gate return ('\n'); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate return (iflag ? letter(c) : c); 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate void 5137c478bd9Sstevel@tonic-gate cgotofn(void) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate int c; 5167c478bd9Sstevel@tonic-gate struct words *s; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate s = smax = w; 5197c478bd9Sstevel@tonic-gate nword: 5207c478bd9Sstevel@tonic-gate for (;;) { 5217c478bd9Sstevel@tonic-gate c = getargc(); 5227c478bd9Sstevel@tonic-gate if (c == EOF) 5237c478bd9Sstevel@tonic-gate return; 5247c478bd9Sstevel@tonic-gate if (c == 0) 5257c478bd9Sstevel@tonic-gate goto enter; 5267c478bd9Sstevel@tonic-gate if (c == '\n') { 5277c478bd9Sstevel@tonic-gate if (xflag) { 5287c478bd9Sstevel@tonic-gate for (;;) { 5297c478bd9Sstevel@tonic-gate if (s->inp == c) { 5307c478bd9Sstevel@tonic-gate s = s->nst; 5317c478bd9Sstevel@tonic-gate break; 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate if (s->inp == 0) 5347c478bd9Sstevel@tonic-gate goto nenter; 5357c478bd9Sstevel@tonic-gate if (s->link == 0) { 536*3f9207dcSpd155743 if (smax >= &w[input_size -1]) 5377c478bd9Sstevel@tonic-gate overflo(); 5387c478bd9Sstevel@tonic-gate s->link = ++smax; 5397c478bd9Sstevel@tonic-gate s = smax; 5407c478bd9Sstevel@tonic-gate goto nenter; 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate s = s->link; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate s->out = 1; 5467c478bd9Sstevel@tonic-gate s = w; 5477c478bd9Sstevel@tonic-gate } else { 5487c478bd9Sstevel@tonic-gate loop: 5497c478bd9Sstevel@tonic-gate if (s->inp == c) { 5507c478bd9Sstevel@tonic-gate s = s->nst; 5517c478bd9Sstevel@tonic-gate continue; 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate if (s->inp == 0) 5547c478bd9Sstevel@tonic-gate goto enter; 5557c478bd9Sstevel@tonic-gate if (s->link == 0) { 556*3f9207dcSpd155743 if (smax >= &w[input_size -1]) 5577c478bd9Sstevel@tonic-gate overflo(); 5587c478bd9Sstevel@tonic-gate s->link = ++smax; 5597c478bd9Sstevel@tonic-gate s = smax; 5607c478bd9Sstevel@tonic-gate goto enter; 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate s = s->link; 5637c478bd9Sstevel@tonic-gate goto loop; 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate enter: 5687c478bd9Sstevel@tonic-gate do { 5697c478bd9Sstevel@tonic-gate s->inp = c; 570*3f9207dcSpd155743 if (smax >= &w[input_size -1]) 5717c478bd9Sstevel@tonic-gate overflo(); 5727c478bd9Sstevel@tonic-gate s->nst = ++smax; 5737c478bd9Sstevel@tonic-gate s = smax; 5747c478bd9Sstevel@tonic-gate } while ((c = getargc()) != '\n' && c != EOF); 5757c478bd9Sstevel@tonic-gate if (xflag) { 5767c478bd9Sstevel@tonic-gate nenter: 5777c478bd9Sstevel@tonic-gate s->inp = '\n'; 578*3f9207dcSpd155743 if (smax >= &w[input_size -1]) 5797c478bd9Sstevel@tonic-gate overflo(); 5807c478bd9Sstevel@tonic-gate s->nst = ++smax; 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate smax->out = 1; 5837c478bd9Sstevel@tonic-gate s = w; 5847c478bd9Sstevel@tonic-gate if (c != EOF) 5857c478bd9Sstevel@tonic-gate goto nword; 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate 588*3f9207dcSpd155743 /* 589*3f9207dcSpd155743 * This function is an unexpected condition, since input_size should have been 590*3f9207dcSpd155743 * calculated correctly before hand. 591*3f9207dcSpd155743 */ 592*3f9207dcSpd155743 5937c478bd9Sstevel@tonic-gate void 5947c478bd9Sstevel@tonic-gate overflo(void) 5957c478bd9Sstevel@tonic-gate { 596*3f9207dcSpd155743 (void) fprintf(stderr, gettext("fgrep: wordlist too large\n")); 5977c478bd9Sstevel@tonic-gate exit(2); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate void 6017c478bd9Sstevel@tonic-gate cfail(void) 6027c478bd9Sstevel@tonic-gate { 6037c478bd9Sstevel@tonic-gate int qsize = QSIZE; 6047c478bd9Sstevel@tonic-gate struct words **queue = NULL; 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * front and rear are pointers used to traverse the global words 6087c478bd9Sstevel@tonic-gate * structure "w" which contains the data of input pattern file 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate struct words **front, **rear; 6117c478bd9Sstevel@tonic-gate struct words *state; 6127c478bd9Sstevel@tonic-gate unsigned long frontoffset = 0, rearoffset = 0; 6137c478bd9Sstevel@tonic-gate char c; 6147c478bd9Sstevel@tonic-gate struct words *s; 6157c478bd9Sstevel@tonic-gate s = w; 6167c478bd9Sstevel@tonic-gate if ((queue = (struct words **)calloc(qsize, sizeof (struct words *))) 6177c478bd9Sstevel@tonic-gate == NULL) { 6187c478bd9Sstevel@tonic-gate perror("fgrep"); 6197c478bd9Sstevel@tonic-gate exit(2); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate front = rear = queue; 6227c478bd9Sstevel@tonic-gate init: 6237c478bd9Sstevel@tonic-gate if ((s->inp) != 0) { 6247c478bd9Sstevel@tonic-gate *rear++ = s->nst; 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * Reallocates the queue if the number of distinct starting 6277c478bd9Sstevel@tonic-gate * character of patterns exceeds the qsize value 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) { 6307c478bd9Sstevel@tonic-gate frontoffset = front - queue; 6317c478bd9Sstevel@tonic-gate rearoffset = rear - queue; 6327c478bd9Sstevel@tonic-gate qsize += QSIZE; 6337c478bd9Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue, 6347c478bd9Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) { 6357c478bd9Sstevel@tonic-gate perror("fgrep"); 6367c478bd9Sstevel@tonic-gate exit(2); 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate front = queue + frontoffset; 6397c478bd9Sstevel@tonic-gate rear = queue + rearoffset; 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate if ((s = s->link) != 0) { 6437c478bd9Sstevel@tonic-gate goto init; 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate while (rear != front) { 6477c478bd9Sstevel@tonic-gate s = *front++; 6487c478bd9Sstevel@tonic-gate cloop: 6497c478bd9Sstevel@tonic-gate if ((c = s->inp) != 0) { 6507c478bd9Sstevel@tonic-gate *rear++ = (q = s->nst); 6517c478bd9Sstevel@tonic-gate /* 6527c478bd9Sstevel@tonic-gate * Reallocate the queue if the rear pointer reaches the end 6537c478bd9Sstevel@tonic-gate * queue 6547c478bd9Sstevel@tonic-gate */ 6557c478bd9Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) { 6567c478bd9Sstevel@tonic-gate frontoffset = front - queue; 6577c478bd9Sstevel@tonic-gate rearoffset = rear - queue; 6587c478bd9Sstevel@tonic-gate qsize += QSIZE; 6597c478bd9Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue, 6607c478bd9Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) { 6617c478bd9Sstevel@tonic-gate perror("fgrep"); 6627c478bd9Sstevel@tonic-gate exit(2); 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate front = queue + frontoffset; 6657c478bd9Sstevel@tonic-gate rear = queue + rearoffset; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate state = s->fail; 6687c478bd9Sstevel@tonic-gate floop: 6697c478bd9Sstevel@tonic-gate if (state == 0) 6707c478bd9Sstevel@tonic-gate state = w; 6717c478bd9Sstevel@tonic-gate if (state->inp == c) { 6727c478bd9Sstevel@tonic-gate qloop: 6737c478bd9Sstevel@tonic-gate q->fail = state->nst; 6747c478bd9Sstevel@tonic-gate if ((state->nst)->out == 1) 6757c478bd9Sstevel@tonic-gate q->out = 1; 6767c478bd9Sstevel@tonic-gate if ((q = q->link) != 0) 6777c478bd9Sstevel@tonic-gate goto qloop; 6787c478bd9Sstevel@tonic-gate } else if ((state = state->link) != 0) 6797c478bd9Sstevel@tonic-gate goto floop; 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate if ((s = s->link) != 0) 6827c478bd9Sstevel@tonic-gate goto cloop; 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate wchar_t 6877c478bd9Sstevel@tonic-gate letter(wchar_t c) 6887c478bd9Sstevel@tonic-gate { 6897c478bd9Sstevel@tonic-gate if (c >= 'a' && c <= 'z') 6907c478bd9Sstevel@tonic-gate return (c); 6917c478bd9Sstevel@tonic-gate if (c >= 'A' && c <= 'Z') 6927c478bd9Sstevel@tonic-gate return (c + 'a' - 'A'); 6937c478bd9Sstevel@tonic-gate return (c); 6947c478bd9Sstevel@tonic-gate } 695