diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2004-02-04 18:38:53 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2004-02-04 18:38:53 +0000 |
commit | 88171b13d5fc1c7b3744c1ffa2499413335bf735 (patch) | |
tree | 162ae7c1ae5273b5b054aa59350aa8c0646bc086 | |
parent | 943a93d1ddeff74815612f2364e74c15c21a6936 (diff) |
Fix anchors (^ or $) in -w mode broken by the last commit's -w overhaul.
With this change we pass the updated regress. Tested and OK by ho@
-rw-r--r-- | usr.bin/grep/grep.c | 13 | ||||
-rw-r--r-- | usr.bin/grep/util.c | 6 |
2 files changed, 13 insertions, 6 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 6e6a8cf6126..6c82ef40891 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.25 2004/01/25 21:36:00 millert Exp $ */ +/* $OpenBSD: grep.c,v 1.26 2004/02/04 18:38:52 millert Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -174,9 +174,16 @@ add_pattern(char *pat, size_t len) --len; /* pat may not be NUL-terminated */ if (wflag) { + int bol = 0, eol = 0; + if (pat[0] == '^') + bol = 1; + if (pat[len - 1] == '$') + eol = 1; pattern[patterns] = grep_malloc(len + 15); - snprintf(pattern[patterns], len + 15, "[[:<:]]%.*s[[:>:]]", - (int)len, pat); + snprintf(pattern[patterns], len + 15, "%s[[:<:]]%.*s[[:>:]]%s", + bol ? "^" : "", (int)len - bol - eol, pat + bol, + eol ? "$" : ""); + len += 14; } else { pattern[patterns] = grep_malloc(len + 1); memcpy(pattern[patterns], pat, len); diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index b0d96f31c17..824ff077bfb 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.23 2004/01/26 14:50:29 millert Exp $ */ +/* $OpenBSD: util.c,v 1.24 2004/02/04 18:38:52 millert Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -266,9 +266,9 @@ fastcomp(fastgrep_t *fg, const char *pattern) } /* Remove enclosing [[:<:]] and [[:>:]] (word match). */ - if (fg->patternLen > 14 + fg->bol + fg->eol && + if (fg->patternLen >= 14 && strncmp(pattern + fg->bol, "[[:<:]]", 7) == 0 && - strncmp(pattern + fg->patternLen - (7 + fg->eol), "[[:>:]]", 7) == 0) { + strncmp(pattern + fg->bol + fg->patternLen - 7, "[[:>:]]", 7) == 0) { fg->patternLen -= 14; fg->wmatch = 7; } |