Print this page
3546 add support for grep -o option
@@ -32,10 +32,11 @@
/* 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,10 +101,11 @@
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,11 +130,11 @@
#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)
+ 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,10 +154,13 @@
cflag++;
break;
case 'n':
nflag++;
break;
+ case 'o':
+ oflag++;
+ break;
case 'R':
Rflag++;
/* FALLTHROUGH */
case 'r':
rflag++;
@@ -180,11 +185,11 @@
case '?':
errflg++;
}
if (errflg || (optind >= argc)) {
- errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
+ errmsg("Usage: grep [-c|-l|-q|-o] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
}
@@ -411,12 +416,21 @@
* Use record as is
*/
lbuf = ptr;
/* lflag only once */
- if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
+ 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;