Skip to content
Browse files

- 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@
  • Loading branch information...
1 parent 2771f28 commit d4d4f790337f0662e898147ce8205e86290dac48 @mkilian mkilian committed with Feb 13, 2007
Showing with 17 additions and 20 deletions.
  1. +8 −2 grep.1
  2. +9 −18 grep.c
View
10 grep.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: grep.1,v 1.31 2005/06/05 19:40:22 jmc Exp $
+.\" $OpenBSD: grep.1,v 1.32 2006/03/07 12:23:50 jmc Exp $
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@@ -170,7 +170,11 @@ to behave as
.It Fl f Ar file
Read one or more newline separated patterns from
.Ar file .
+Empty pattern lines match every input line.
Newlines are not considered part of a pattern.
+If
+.Ar file
+is empty, nothing is matched.
.It Fl G
Interpret
.Ar pattern
@@ -364,7 +368,9 @@ specification.
.Pp
The flags
.Op Fl AaBbCGHhILoPRSUVwZ
-are extensions to that specification.
+are extensions to that specification, and the behaviour of the
+.Fl f
+flag when used with an empty pattern file is left undefined.
.Pp
All long options are provided for compatibility with
GNU versions of this utility.
View
27 grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.36 2006/09/26 15:55:17 jaredy Exp $ */
+/* $OpenBSD: grep.c,v 1.37 2006/11/02 18:00:03 ray 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;

0 comments on commit d4d4f79

Please sign in to comment.
Something went wrong with that request. Please try again.