summaryrefslogtreecommitdiff
path: root/usr.bin/grep/grep.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-05-07 14:51:43 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-05-07 14:51:43 +0000
commit73336cf4fb0979d4d55d8dec48b6336562cbace5 (patch)
tree528e13afcd3b1d15cb94b7e84d3ea8790ab91c71 /usr.bin/grep/grep.c
parente8a1d5db468a4c9891094f658236c006e9af206c (diff)
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@
Diffstat (limited to 'usr.bin/grep/grep.c')
-rw-r--r--usr.bin/grep/grep.c45
1 files changed, 14 insertions, 31 deletions
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);
}