Browse files

Allow zero-length patterns with -x so

  $ grep -x ""

matches empty lines as reported on misc@ by
Martin Marusak <marusak@fhpv.unipo.sk>.
Initial diff by otto@ with tweaks by me.

ok otto
  • Loading branch information...
1 parent d1068d8 commit 77fd98288750f0e7837f48647cab536939647d0f Jared Yanovich committed with Sep 26, 2006
Showing with 7 additions and 6 deletions.
  1. +3 −3 grep.c
  2. +4 −3 util.c
View
6 grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.34 2006/02/09 09:54:46 otto Exp $ */
+/* $OpenBSD: grep.c,v 1.35 2006/03/07 20:59:56 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -168,7 +168,7 @@ struct option long_options[] =
static void
add_pattern(char *pat, size_t len)
{
- if (len == 0 || matchall) {
+ if (!xflag && (len == 0 || matchall)) {
matchall = 1;
return;
}
@@ -183,7 +183,7 @@ add_pattern(char *pat, size_t len)
int bol = 0, eol = 0, extra;
if (pat[0] == '^')
bol = 1;
- if (pat[len - 1] == '$')
+ if (len > 0 && pat[len - 1] == '$')
eol = 1;
extra = Eflag ? 2 : 4;
pattern[patterns] = grep_malloc(len + 15 + extra);
View
7 util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.30 2005/04/03 19:12:40 otto Exp $ */
+/* $OpenBSD: util.c,v 1.31 2006/02/09 09:54:47 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -479,8 +479,9 @@ grep_search(fastgrep_t *fg, unsigned char *data, size_t dataLen, regmatch_t *pma
if (grep_cmp(fg->pattern, data + j, fg->patternLen) == -1) {
pmatch->rm_so = j;
pmatch->rm_eo = j + fg->patternLen;
- if (!fg->wmatch || wmatch(data, dataLen,
- pmatch->rm_so, pmatch->rm_eo)) {
+ if (fg->patternLen == 0 || !fg->wmatch ||
+ wmatch(data, dataLen, pmatch->rm_so,
+ pmatch->rm_eo)) {
rtrnVal = 0;
break;
}

0 comments on commit 77fd982

Please sign in to comment.