1
## regex-glibc-getopt.patch by Ryan Finnie <ryan@finnie.org>
3
This patch contains two modifications. First, grepcidr.c is modified
4
to use glibc's built-in getopt, rather than the getopt provided with
5
the tarball. Second, regular expression support is added. A
6
description of the regex patch by the original patch author is
9
These two patches were combined because patch(1) does not seem to be
10
able to handle the two "conflicting" changes when separated.
12
----------------------------------------------------------------------
14
Date: Fri, 29 Apr 2005 19:44:05 -0700
15
From: Jeff Makey <jeff@sdsc.edu>
17
The patch included below adds regular expression processing to allow
18
grepcidr to find IP addresses anywhere on the input. Tested against
19
the README file shipped with grepcidr-1.3, the first command below
20
shows one line of output and the second correctly shows none:
22
grepcidr 192.168.0.0/20 README
23
grepcidr 192.168.16.0/20 README
25
My changes work on Red Hat Linux 9.0, Sun Solaris, SGI IRIX, and IBM
26
AIX. It should be possible to use regular expression processing and
27
avoid calling sscanf() in the ip_to_uint() function, but it may not be
30
Note that for a number the source lines the only thing I changed is
31
the indenting. After applying the patch use "diff -w" to see just the
34
Thanks for writing grepcidr. Feel free to include this change in the
40
diff -ruN grepcidr-1.3-orig/grepcidr.c grepcidr-1.3/grepcidr.c
41
--- grepcidr-1.3-orig/grepcidr.c 2005-04-23 15:00:16.000000000 -0700
42
+++ grepcidr-1.3/grepcidr.c 2006-10-05 01:42:24.000000000 -0700
52
#define EXIT_NOMATCH 1
56
int anymatch = 0; /* did anything match? for exit code */
57
+ static regex_t preg; /* compiled regular expression for IPs */
65
+ /* Compile the regular expression for matching IP addresses */
66
+ if (regcomp(&preg, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", REG_EXTENDED) != 0)
68
+ (void)fputs("regcomp() failed\n", stderr);
72
/* Match IPs from input stream to network patterns */
73
while (fgets(line, sizeof(line), inp_stream))
76
- if ((key.min=ip_to_uint(line)))
79
+ for (offset = 0; regexec(&preg, &line[offset], 1, &pmatch, 0) == 0; offset += pmatch.rm_eo)
82
- if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
85
+ if ((key.min=ip_to_uint(&line[offset + pmatch.rm_so])))
93
+ if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
101
+ printf("%s", line);
114
printf("%u\n", counting-1);