/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/*
* Copyright 2013 Damian Bogel. All rights reserved.
*/
/*
* fgrep -- print all lines containing any of a set of keywords
*
* status returns:
* 0 - ok, and some matches
* 1 - ok, but no matches
* 2 - some error
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <euc.h>
#include <fcntl.h>
#include <getwidth.h>
while (ccount--) \
/* Increase the buffer size */ \
fw_lBufsiz += BUFSIZ; \
} \
p = &buf[fw_lBufsiz]; \
} else { \
/* shift the buffer contents down */ \
} \
} \
if (p > &buf[fw_lBufsiz]) { \
<= 0) break; \
break; \
} \
while (cw--) \
/*
* The same() macro and letter() function were inserted to allow for
* the -i option work for the multi-byte environment.
*/
#define same(a, b) \
struct words {
char out;
long long lnum;
int retcode = 0;
int nfile;
long long blkno;
int nsucc;
long long tln;
char *argptr;
void execute(char *);
void cgotofn(void);
void overflo(void);
void cfail(void);
static long fw_lBufsiz = 0;
int
{
int c;
int errflg = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
switch (c) {
case 'q':
case 's': /* Solaris: legacy option */
qflag++;
continue;
case 'H':
Hflag++;
hflag = 0;
continue;
case 'h':
hflag++;
Hflag = 0;
continue;
case 'b':
bflag++;
continue;
case 'i':
case 'y':
iflag++;
continue;
case 'c':
cflag++;
continue;
case 'e':
eflag++;
continue;
case 'f':
fflag++;
gettext("fgrep: can't open %s\n"),
optarg);
exit(2);
}
} else {
gettext("fgrep: can't fstat %s\n"),
optarg);
exit(2);
}
continue;
case 'l':
lflag++;
continue;
case 'n':
nflag++;
continue;
case 'v':
vflag++;
continue;
case 'x':
xflag++;
continue;
case '?':
errflg++;
}
"[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
exit(2);
}
input_size++;
optind++;
argc--;
}
/*
* Normally we need one struct words for each letter in the pattern
* plus one terminating struct words with outp = 1, but when -x option
* is specified we require one more struct words for `\n` character so we
* calculate the input_size as below. We add extra 1 because
* (input_size/2) rounds off odd numbers
*/
if (xflag) {
}
input_size++;
if (w == NULL) {
gettext("fgrep: could not allocate "
"memory for wordlist\n"));
exit(2);
}
(WIDTH3 == 0)) {
/*
* If non EUC-based locale,
* assume WIDTH1 is 1.
*/
WIDTH1 = 1;
}
WIDTH2++;
WIDTH3++;
cgotofn();
cfail();
if (argc <= 0) {
} else
while (--argc >= 0) {
argv++;
}
if (w != NULL) {
free(w);
}
}
void
{
char *p;
struct words *c;
int ccount;
int failed;
char *nlp;
int cw;
fw_lBufsiz = BUFSIZ;
}
}
if (file) {
retcode = 2;
return;
}
} else {
}
ccount = 0;
failed = 0;
lnum = 1;
tln = 0;
blkno = 0;
p = buf;
nlp = p;
c = w;
for (;;) {
if (c == 0)
break;
if (ccount <= 0) {
/* increase the buffer size */
fw_lBufsiz += BUFSIZ;
}
p = &buf[fw_lBufsiz];
} else {
/* shift the buffer down */
- nlp);
}
}
if (p > &buf[fw_lBufsiz]) {
<= 0)
break;
break;
}
c = c->nst;
} else if (c->link != 0) {
c = c->link;
goto nstate;
} else {
c = c->fail;
failed = 1;
if (c == 0) {
c = w;
c = c->nst;
} else if (c->link != 0) {
c = c->link;
goto istate;
}
} else
goto nstate;
}
if (c == 0)
break;
if (c->out) {
while (lc != '\n') {
if (ccount <= 0) {
/* increase buffer size */
fw_lBufsiz += BUFSIZ;
}
p = &buf[fw_lBufsiz];
} else {
/* shift buffer down */
}
}
if (p > &buf[fw_lBufsiz]) {
fptr)) <= 0) break;
}
}
goto nomatch;
nsucc = 1;
if (!qflag)
return;
}
if (cflag) {
tln++;
} else {
if (bflag)
(void) printf("%lld:",
/ BUFSIZ);
if (nflag)
if (p <= nlp) {
}
while (nlp < p)
}
lnum++;
nlp = p;
c = w;
failed = 0;
continue;
}
if (lc == '\n')
if (vflag)
goto succeed;
else {
lnum++;
nlp = p;
c = w;
failed = 0;
}
}
}
}
getargc(void)
{
/* appends a newline to shell quoted argument list so */
/* the list looks like it came from an ed style file */
wchar_t c;
int cw;
int b;
static int endflg;
if (wordf) {
return (EOF);
while (--cw) {
return (EOF);
c = (c << 7) | (b & 0177);
}
}
if (endflg)
return (EOF);
{
while (--cw)
if (c == '\0') {
endflg++;
return ('\n');
}
}
}
void
cgotofn(void)
{
int c;
struct words *s;
s = smax = w;
for (;;) {
c = getargc();
if (c == EOF)
return;
if (c == 0)
goto enter;
if (c == '\n') {
if (xflag) {
for (;;) {
if (s->inp == c) {
s = s->nst;
break;
}
if (s->inp == 0)
goto nenter;
if (s->link == 0) {
overflo();
s = smax;
goto nenter;
}
s = s->link;
}
}
s->out = 1;
s = w;
} else {
loop:
if (s->inp == c) {
s = s->nst;
continue;
}
if (s->inp == 0)
goto enter;
if (s->link == 0) {
overflo();
s = smax;
goto enter;
}
s = s->link;
goto loop;
}
}
do {
s->inp = c;
overflo();
s = smax;
if (xflag) {
s->inp = '\n';
overflo();
}
s = w;
if (c != EOF)
goto nword;
}
/*
* This function is an unexpected condition, since input_size should have been
* calculated correctly before hand.
*/
void
overflo(void)
{
exit(2);
}
void
cfail(void)
{
/*
* front and rear are pointers used to traverse the global words
* structure "w" which contains the data of input pattern file
*/
char c;
struct words *s;
s = w;
== NULL) {
perror("fgrep");
exit(2);
}
init:
if ((s->inp) != 0) {
/*
* Reallocates the queue if the number of distinct starting
* character of patterns exceeds the qsize value
*/
perror("fgrep");
exit(2);
}
}
}
if ((s = s->link) != 0) {
goto init;
}
s = *front++;
if ((c = s->inp) != 0) {
/*
* Reallocate the queue if the rear pointer reaches the end
* queue
*/
perror("fgrep");
exit(2);
}
}
if (state == 0)
state = w;
q->out = 1;
if ((q = q->link) != 0)
goto qloop;
goto floop;
}
if ((s = s->link) != 0)
goto cloop;
}
}
{
if (c >= 'a' && c <= 'z')
return (c);
if (c >= 'A' && c <= 'Z')
return (c + 'a' - 'A');
return (c);
}