summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/grep.c
diff options
context:
space:
mode:
authorpirofti <pirofti@openbsd.org>2017-12-09 18:38:37 +0000
committerpirofti <pirofti@openbsd.org>2017-12-09 18:38:37 +0000
commitb5430e4d8844f8588034f4c5399e4dc5d6ca5cd9 (patch)
tree5a11435dd1fae13b7b24b2235b74b9a807480e9c /usr.bin/grep/grep.c
parentMake tls_config_parse_protocols() work correctly when passed a NULL pointer (diff)
downloadwireguard-openbsd-b5430e4d8844f8588034f4c5399e4dc5d6ca5cd9.tar.xz
wireguard-openbsd-b5430e4d8844f8588034f4c5399e4dc5d6ca5cd9.zip
Add support for the non-standard grep -m extension.
grep -m num stops after a maximum of num matches are found. We support -m0 to match GNU behaviour, but we do not allow negative numbers. Manpage help from jmc@, OK deraadt@.
Diffstat (limited to 'usr.bin/grep/grep.c')
-rw-r--r--usr.bin/grep/grep.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index 907246caa5d..401ed503cbb 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.55 2015/11/28 01:17:12 gsoares Exp $ */
+/* $OpenBSD: grep.c,v 1.56 2017/12/09 18:38:37 pirofti Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -71,6 +71,9 @@ int cflag; /* -c: only show a count of matching lines */
int hflag; /* -h: don't print filename headers */
int iflag; /* -i: ignore case */
int lflag; /* -l: only show names of files with matches */
+int mflag; /* -m x: stop reading the files after x matches */
+long long mcount; /* count for -m */
+long long mlimit; /* requested value for -m */
int nflag; /* -n: show line numbers in front of matching lines */
int oflag; /* -o: print each match */
int qflag; /* -q: quiet mode (don't output anything) */
@@ -111,15 +114,16 @@ usage(void)
#else
"usage: %s [-abcEFGHhIiLlnoqRsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
#endif
- "\t[-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n"
- "\t[--line-buffered] [pattern] [file ...]\n", __progname);
+ "\t[-e pattern] [-f file] [-m num] [--binary-files=value]\n"
+ "\t[--context[=num]] [--line-buffered] [pattern] [file ...]\n",
+ __progname);
exit(2);
}
#ifdef NOZ
-static const char optstr[] = "0123456789A:B:CEFGHILRUVabce:f:hilnoqrsuvwxy";
+static const char optstr[] = "0123456789A:B:CEFGHILRUVabce:f:hilm:noqrsuvwxy";
#else
-static const char optstr[] = "0123456789A:B:CEFGHILRUVZabce:f:hilnoqrsuvwxy";
+static const char optstr[] = "0123456789A:B:CEFGHILRUVZabce:f:hilm:noqrsuvwxy";
#endif
static const struct option long_options[] =
@@ -147,6 +151,7 @@ static const struct option long_options[] =
{"ignore-case", no_argument, NULL, 'i'},
{"files-without-match", no_argument, NULL, 'L'},
{"files-with-matches", no_argument, NULL, 'l'},
+ {"max-count", required_argument, NULL, 'm'},
{"line-number", no_argument, NULL, 'n'},
{"quiet", no_argument, NULL, 'q'},
{"silent", no_argument, NULL, 'q'},
@@ -376,6 +381,14 @@ main(int argc, char *argv[])
Lflag = 0;
lflag = qflag = 1;
break;
+ case 'm':
+ mflag = 1;
+ mlimit = mcount = strtonum(optarg, 0, LLONG_MAX,
+ &errstr);
+ if (errstr != NULL)
+ errx(2, "invalid max-count %s: %s",
+ optarg, errstr);
+ break;
case 'n':
nflag = 1;
break;