Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make the processing of patterns collected from files specified by -f

delayed so options that affect pattern-building (such as -w) can be
applied evenly to all such patterns.

ok and help otto, ok millert
  • Loading branch information...
commit ae3e6e47819ab0a19e4f7f1d4de4fb33b6a27410 1 parent 53dc13a
Jared Yanovich authored committed
Showing with 21 additions and 3 deletions.
  1. +21 −3 grep.c
View
24 grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.31 2004/10/03 19:23:02 otto Exp $ */
+/* $OpenBSD: grep.c,v 1.32 2005/04/03 19:12:40 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <sys/limits.h>
#include <sys/stat.h>
+#include <sys/queue.h>
#include <ctype.h>
#include <err.h>
@@ -98,6 +99,12 @@ int first; /* flag whether or not this is our first match */
int tail; /* lines left to print */
int lead; /* number of lines in leading context queue */
+struct patfile {
+ const char *pf_file;
+ SLIST_ENTRY(patfile) pf_next;
+};
+SLIST_HEAD(, patfile) patfilelh;
+
extern char *__progname;
static void
@@ -199,7 +206,7 @@ add_pattern(char *pat, size_t len)
}
static void
-read_patterns(char *fn)
+read_patterns(const char *fn)
{
FILE *f;
char *line;
@@ -230,9 +237,11 @@ int
main(int argc, char *argv[])
{
int c, lastc, prevoptind, newarg, i;
+ struct patfile *patfile, *pf_next;
long l;
char *ep;
+ SLIST_INIT(&patfilelh);
switch (__progname[0]) {
case 'e':
Eflag++;
@@ -355,7 +364,9 @@ main(int argc, char *argv[])
add_pattern(optarg, strlen(optarg));
break;
case 'f':
- read_patterns(optarg);
+ patfile = grep_malloc(sizeof(*patfile));
+ patfile->pf_file = optarg;
+ SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
break;
case 'h':
oflag = 0;
@@ -420,6 +431,13 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
+ for (patfile = SLIST_FIRST(&patfilelh); patfile != NULL;
+ patfile = pf_next) {
+ pf_next = SLIST_NEXT(patfile, pf_next);
+ read_patterns(patfile->pf_file);
+ free(patfile);
+ }
+
if (argc == 0 && patterns == 0)
usage();
Please sign in to comment.
Something went wrong with that request. Please try again.