Print this page
3546 add support for grep -o option
*** 32,41 ****
--- 32,42 ----
/* Copyright 2012 Nexenta Systems, Inc. All rights reserved. */
/*
* Copyright 2013 Damian Bogel. All rights reserved.
+ * Copyright (c) 2013 Andrew Stormont. All rights reserved.
*/
/*
* grep -- print lines matching (or not matching) a pattern
*
*** 100,109 ****
--- 101,111 ----
static int iflag;
static int wflag;
static int hflag;
static int Hflag;
static int qflag;
+ static int oflag;
static int errflg;
static int nfile;
static long long tln;
static int nsucc;
static int outfn = 0;
*** 128,138 ****
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
! while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
switch (c) {
/* based on options order h or H is set as in GNU grep */
case 'h':
hflag++;
Hflag = 0; /* h excludes H */
--- 130,140 ----
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
! while ((c = getopt(argc, argv, "hHqblcnoRrsviyw")) != -1)
switch (c) {
/* based on options order h or H is set as in GNU grep */
case 'h':
hflag++;
Hflag = 0; /* h excludes H */
*** 152,161 ****
--- 154,166 ----
cflag++;
break;
case 'n':
nflag++;
break;
+ case 'o':
+ oflag++;
+ break;
case 'R':
Rflag++;
/* FALLTHROUGH */
case 'r':
rflag++;
*** 180,190 ****
case '?':
errflg++;
}
if (errflg || (optind >= argc)) {
! errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
--- 185,195 ----
case '?':
errflg++;
}
if (errflg || (optind >= argc)) {
! errmsg("Usage: grep [-c|-l|-q|-o] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
*** 411,422 ****
* Use record as is
*/
lbuf = ptr;
/* lflag only once */
! if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
break;
if (!nlflag)
break;
ptr = next_ptr;
--- 416,436 ----
* Use record as is
*/
lbuf = ptr;
/* lflag only once */
! if (step(lbuf, expbuf) ^ vflag) {
! if (oflag) {
! /*
! * Only store the matching bits
! */
! ptr = loc1;
! ptrend = loc2;
! }
! if (succeed(file) == 1)
break;
+ }
if (!nlflag)
break;
ptr = next_ptr;