grep.c revision 55f916226cb9db7816760f31b13a3c5c6609aef3
/*
* 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 */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* 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>
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!!",
};
#define BLKSIZE 512
#define GBUFSIZ 8192
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 vflag;
static int sflag;
static int iflag;
static int wflag;
static int hflag;
static int errflg;
static int nfile;
static long long tln;
static int nsucc;
static int nlflag;
static char *expbuf;
static void execute(char *);
static void regerr(int);
static int succeed(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) {
case 'h':
hflag++;
break;
case 'v':
vflag++;
break;
case 'c':
cflag++;
break;
case 'n':
nflag++;
break;
case 'b':
bflag++;
break;
case 's':
sflag++;
break;
case 'l':
lflag++;
break;
case 'y':
case 'i':
iflag++;
break;
case 'w':
wflag++;
break;
case '?':
errflg++;
}
errmsg("Usage: grep -hblcnsviw 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
{
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 (cflag) {
}
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;
}
if (cflag) {
}
}
static int
succeed(char *f)
{
int nchars;
if (f == NULL)
f = "<stdin>";
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);
}