/*
* 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 2012 Nexenta Systems, Inc. All rights reserved. */
/*
* Copyright 2013 Damian Bogel. All rights reserved.
*/
/*
* grep -- print lines matching (or not matching) a pattern
*
* status returns:
* 0 - ok, and some matches
* 1 - ok, but no matches
* 2 - some error
*/
#include <ctype.h>
#include <fcntl.h>
#include <locale.h>
#include <memory.h>
#include <regexpr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ftw.h>
#include <limits.h>
static const char *errstr[] = {
"Range endpoint too large.",
"Bad number.",
"``\\digit'' out of range.",
"No remembered search string.",
"\\( \\) imbalance.",
"Too many \\(.",
"More than 2 numbers given in \\{ \\}.",
"} expected after \\.",
"First number exceeds second in \\{ \\}.",
"[ ] imbalance.",
"Regular expression overflow.",
"Illegal byte sequence.",
"Unknown regexp error code!!",
};
static int temp;
static long long lnum;
static char *linebuf;
static long fw_lPrntBufLen = 0;
static int nflag;
static int bflag;
static int lflag;
static int cflag;
static int rflag;
static int Rflag;
static int vflag;
static int sflag;
static int iflag;
static int wflag;
static int hflag;
static int Hflag;
static int qflag;
static int errflg;
static int nfile;
static long long tln;
static int nsucc;
static int outfn = 0;
static int nlflag;
static char *expbuf;
static void execute(const char *, int);
static void regerr(int);
static void prepare(const char *);
static int succeed(const char *);
int
{
int c;
char *arg;
extern int optind;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
switch (c) {
/* based on options order h or H is set as in GNU grep */
case 'h':
hflag++;
Hflag = 0; /* h excludes H */
break;
case 'H':
if (!lflag) /* H is excluded by l */
Hflag++;
hflag = 0; /* H excludes h */
break;
case 'q': /* POSIX: quiet: status only */
qflag++;
break;
case 'v':
vflag++;
break;
case 'c':
cflag++;
break;
case 'n':
nflag++;
break;
case 'R':
Rflag++;
/* FALLTHROUGH */
case 'r':
rflag++;
break;
case 'b':
bflag++;
break;
case 's':
sflag++;
break;
case 'l':
lflag++;
Hflag = 0; /* l excludes H */
break;
case 'y':
case 'i':
iflag++;
break;
case 'w':
wflag++;
break;
case '?':
errflg++;
}
errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
regerr(41);
if (iflag) {
}
if (wflag) {
unsigned int wordlen;
char *wordbuf;
exit(2);
}
}
if (regerrno)
if (--argc == 0)
else
while (argc-- > 0)
}
static void
{
if (rflag) {
outfn = 1;
/*
* Add trailing slash if arg
* is directory, to resolve symlinks.
*/
}
/*
* Search through subdirs if path is directory.
* Don't follow symlinks if Rflag is not set.
*/
if (!Rflag)
if (!sflag)
nsucc = 2;
}
return;
}
}
}
static int
{
/*
* process files and follow symlinks if Rflag set.
*/
if (!sflag &&
/* report broken symlinks and unreadable files */
}
return (0);
}
/* skip devices and pipes if Rflag is not set */
return (0);
/* pass offset to relative name from FTW_CHDIR */
return (0);
}
static void
{
char *lbuf, *p;
long count;
long offset = 0;
long next_count = 0;
tln = 0;
}
}
}
temp = 0;
if (!sflag)
nsucc = 2;
return;
}
/* read in first block of bytes */
if (!rflag)
}
return;
}
lnum = 0;
for (;;) {
/* look for next newline */
/*
* shift unused data to the beginning of the buffer
*/
}
/*
* re-allocate a larger buffer if this one is full
*/
/*
* allocate a new buffer and preserve the
* contents...
*/
fw_lPrntBufLen)) == NULL)
exit(2);
/*
* set up a bigger linebuffer (this is only used
* for case insensitive operations). Contents do
* not have to be preserved.
*/
exit(2);
}
continue;
if (offset == 0)
/* end of file already reached */
break;
/* last line of file has no newline */
nlflag = 0;
} else {
nlflag = 1;
}
lnum++;
*ptrend = '\0';
if (iflag) {
/*
* Make a lower case copy of the record
*/
p = ptr;
(unsigned char)*p++);
*lbuf = '\0';
} else
/*
* Use record as is
*/
/* lflag only once */
break;
if (!nlflag)
break;
count = next_count;
offset = 0;
}
}
}
static int
succeed(const char *f)
{
int nchars;
if (qflag) {
/* no need to continue */
return (1);
}
if (cflag) {
tln++;
return (0);
}
if (lflag) {
return (1);
}
/* print filename */
}
if (bflag)
/* print block number */
if (nflag)
/* print line number */
if (nlflag) {
/* newline at end of line */
*ptrend = '\n';
} else {
/* don't write sentinel \0 */
}
return (0);
}
static void
{
switch (err) {
case 11:
err = 0;
break;
case 16:
err = 1;
break;
case 25:
err = 2;
break;
case 41:
err = 3;
break;
case 42:
err = 4;
break;
case 43:
err = 5;
break;
case 44:
err = 6;
break;
case 45:
err = 7;
break;
case 46:
err = 8;
break;
case 49:
err = 9;
break;
case 50:
err = 10;
break;
case 67:
err = 11;
break;
default:
err = 12;
break;
}
exit(2);
}