summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/grep.c
diff options
context:
space:
mode:
authorkili <kili@openbsd.org>2007-02-13 21:48:20 +0000
committerkili <kili@openbsd.org>2007-02-13 21:48:20 +0000
commit2aadc793edc24af0fea206bfd37b27d415900fa7 (patch)
tree24834ab730cbf1883594beb679b54cfa80156f70 /usr.bin/grep/grep.c
parentAdd a delay for ep0 handling. Dunno why, but without debug prints it would (diff)
downloadwireguard-openbsd-2aadc793edc24af0fea206bfd37b27d415900fa7.tar.xz
wireguard-openbsd-2aadc793edc24af0fea206bfd37b27d415900fa7.zip
- Be explicit on command line checking, instead of relying on patterns,
which may be NULL (e.g. -e ''). - let add_pattern() decide how to deal with empty patterns, don't do magic in read_patterns(). This unbreaks stuff like grep -e '', and makes grep -f <file> more POSIX compliant. Semantics for grep -f /dev/null (or any other empty file) may be questionable, but this case isn't specified by POSIX, and matching nothing at all seems to be sane. Thanks to otto@, who mentioned potential problems related to the -x option with the first patch i sent. ok jaredy@ (some time ago), otto@, millert@
Diffstat (limited to 'usr.bin/grep/grep.c')
-rw-r--r--usr.bin/grep/grep.c27
1 files changed, 9 insertions, 18 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index a8a8618b8c4..a8a35e3b702 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.37 2006/11/02 18:00:03 ray Exp $ */
+/* $OpenBSD: grep.c,v 1.38 2007/02/13 21:48:20 kili Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -221,23 +221,11 @@ read_patterns(const char *fn)
FILE *f;
char *line;
size_t len;
- int nl;
if ((f = fopen(fn, "r")) == NULL)
err(2, "%s", fn);
- nl = 0;
- while ((line = fgetln(f, &len)) != NULL) {
- if (*line == '\n') {
- ++nl;
- continue;
- }
- if (nl) {
- matchall = 1;
- break;
- }
- nl = 0;
- add_pattern(line, len);
- }
+ while ((line = fgetln(f, &len)) != NULL)
+ add_pattern(line, *line == '\n' ? 0 : len);
if (ferror(f))
err(2, "%s", fn);
fclose(f);
@@ -246,7 +234,7 @@ read_patterns(const char *fn)
int
main(int argc, char *argv[])
{
- int c, lastc, prevoptind, newarg, i;
+ int c, lastc, prevoptind, newarg, i, needpattern;
struct patfile *patfile, *pf_next;
long l;
char *ep;
@@ -283,6 +271,7 @@ main(int argc, char *argv[])
lastc = '\0';
newarg = 1;
prevoptind = 1;
+ needpattern = 1;
while ((c = getopt_long(argc, argv, optstr,
long_options, NULL)) != -1) {
switch (c) {
@@ -372,11 +361,13 @@ main(int argc, char *argv[])
break;
case 'e':
add_patterns(optarg);
+ needpattern = 0;
break;
case 'f':
patfile = grep_malloc(sizeof(*patfile));
patfile->pf_file = optarg;
SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
+ needpattern = 0;
break;
case 'h':
oflag = 0;
@@ -448,10 +439,10 @@ main(int argc, char *argv[])
free(patfile);
}
- if (argc == 0 && patterns == 0)
+ if (argc == 0 && needpattern)
usage();
- if (patterns == 0) {
+ if (argc != 0 && needpattern) {
add_patterns(*argv);
--argc;
++argv;