From 73336cf4fb0979d4d55d8dec48b6336562cbace5 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 7 May 2004 14:51:43 +0000 Subject: Add a new past path for fgrep that is just a simplified version of fastcomp. This makes fgrep faster and fixes the -w flag w/ fgrep. Also remove free_patterns() since calling free right before exit is silly. Problem noticed by espie@ --- usr.bin/grep/grep.c | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) (limited to 'usr.bin/grep/grep.c') diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 6980d68cafa..aa10b0a2cb4 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.27 2004/04/02 18:39:36 otto Exp $ */ +/* $OpenBSD: grep.c,v 1.28 2004/05/07 14:51:42 millert Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -176,7 +176,7 @@ add_pattern(char *pat, size_t len) if (pat[len - 1] == '\n') --len; /* pat may not be NUL-terminated */ - if (wflag) { + if (wflag && !Fflag) { int bol = 0, eol = 0; if (pat[0] == '^') bol = 1; @@ -226,24 +226,6 @@ read_patterns(char *fn) fclose(f); } -static void -free_patterns(void) -{ - int i; - - for (i = 0; i < patterns; i++) { - if (fg_pattern[i].pattern) - free(fg_pattern[i].pattern); - else - regfree(&r_pattern[i]); - free(pattern[i]); - } - - free(fg_pattern); - free(r_pattern); - free(pattern); -} - int main(int argc, char *argv[]) { @@ -449,18 +431,21 @@ main(int argc, char *argv[]) if (Eflag) cflags |= REG_EXTENDED; - else if (Fflag) - cflags |= REG_NOSPEC; fg_pattern = grep_malloc(patterns * sizeof(*fg_pattern)); r_pattern = grep_malloc(patterns * sizeof(*r_pattern)); for (i = 0; i < patterns; ++i) { - /* Check if cheating is allowed */ - if (fastcomp(&fg_pattern[i], pattern[i])) { - /* Fall back to full regex library */ - if ((c = regcomp(&r_pattern[i], pattern[i], cflags))) { - regerror(c, &r_pattern[i], re_error, - RE_ERROR_BUF); - errx(2, "%s", re_error); + /* Check if cheating is allowed (always is for fgrep). */ + if (Fflag) { + fgrepcomp(&fg_pattern[i], pattern[i]); + } else { + if (fastcomp(&fg_pattern[i], pattern[i])) { + /* Fall back to full regex library */ + c = regcomp(&r_pattern[i], pattern[i], cflags); + if (c != 0) { + regerror(c, &r_pattern[i], re_error, + RE_ERROR_BUF); + errx(2, "%s", re_error); + } } } } @@ -480,7 +465,5 @@ main(int argc, char *argv[]) for (c = 0; argc--; ++argv) c += procfile(*argv); - free_patterns(); - exit(!c); } -- cgit v1.2.3