~ubuntu-branches/ubuntu/vivid/grepcidr/vivid

« back to all changes in this revision

Viewing changes to debian/patches/02_regex-glibc-getopt.patch

  • Committer: Package Import Robot
  • Author(s): Ryan Finnie
  • Date: 2011-09-04 13:59:04 UTC
  • Revision ID: package-import@ubuntu.com-20110904135904-p2q9mbcihmwmjfay
Tags: 1.3-5
* Housekeeping release (no user-visible changes)
* Switch to dpkg-source 3.0 (quilt) format
* Converted debian/rules to unified dh
* Bumped debian/compat to 7
* Adjusted debian/copyright (removed '(C)')
* Added debian/watch file
* Removed Build-Depends: docbook-to-man; debian/grepcidr.1 is now 
  pre-compiled (with included source and README for manual 
  regeneration)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## regex-glibc-getopt.patch by Ryan Finnie <ryan@finnie.org>
 
2
 
 
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
 
7
provided below.
 
8
 
 
9
These two patches were combined because patch(1) does not seem to be
 
10
able to handle the two "conflicting" changes when separated.
 
11
 
 
12
----------------------------------------------------------------------
 
13
 
 
14
Date: Fri, 29 Apr 2005 19:44:05 -0700
 
15
From: Jeff Makey <jeff@sdsc.edu>
 
16
 
 
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:
 
21
 
 
22
 grepcidr 192.168.0.0/20 README
 
23
 grepcidr 192.168.16.0/20 README
 
24
 
 
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
 
28
worth the effort.
 
29
 
 
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
 
32
coding changes.
 
33
 
 
34
Thanks for writing grepcidr.  Feel free to include this change in the
 
35
next release.
 
36
 
 
37
                          :: Jeff Makey
 
38
                             jeff@sdsc.edu
 
39
 
 
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
 
43
@@ -25,7 +25,8 @@
 
44
 #include <stdio.h>
 
45
 #include <stdlib.h>
 
46
 #include <string.h>
 
47
-#include "getopt.h"
 
48
+#include <getopt.h>
 
49
+#include <regex.h>
 
50
 
 
51
 #define EXIT_OK                0
 
52
 #define EXIT_NOMATCH   1
 
53
@@ -164,6 +165,7 @@
 
54
        char line[MAXFIELD];
 
55
        int foundopt;
 
56
        int anymatch = 0;                       /* did anything match? for exit code */
 
57
+       static regex_t preg;                    /* compiled regular expression for IPs */
 
58
 
 
59
        if (argc == 1)
 
60
        {
 
61
@@ -272,22 +274,35 @@
 
62
                }
 
63
        }
 
64
        
 
65
+       /* Compile the regular expression for matching IP addresses */
 
66
+       if (regcomp(&preg, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", REG_EXTENDED) != 0)
 
67
+       {
 
68
+               (void)fputs("regcomp() failed\n", stderr);
 
69
+               return EXIT_ERROR;
 
70
+       }
 
71
+
 
72
        /* Match IPs from input stream to network patterns */
 
73
        while (fgets(line, sizeof(line), inp_stream))
 
74
        {
 
75
                struct netspec key;
 
76
-               if ((key.min=ip_to_uint(line)))
 
77
+               regoff_t offset;
 
78
+               regmatch_t pmatch;
 
79
+               for (offset = 0; regexec(&preg, &line[offset], 1, &pmatch, 0) == 0; offset += pmatch.rm_eo)
 
80
                {
 
81
-                       int match = 0;
 
82
-                       if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
 
83
-                               match = 1;
 
84
-                       if (invert ^ match)
 
85
+                       if ((key.min=ip_to_uint(&line[offset + pmatch.rm_so])))
 
86
                        {
 
87
-                               anymatch = 1;
 
88
-                               if (counting)
 
89
-                                       counting++;
 
90
-                               else
 
91
-                                       printf("%s", line);
 
92
+                               int match = 0;
 
93
+                               if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
 
94
+                                       match = 1;
 
95
+                               if (invert ^ match)
 
96
+                               {
 
97
+                                       anymatch = 1;
 
98
+                                       if (counting)
 
99
+                                               counting++;
 
100
+                                       else
 
101
+                                               printf("%s", line);
 
102
+                                       break;
 
103
+                               }
 
104
                        }
 
105
                }
 
106
        }
 
107
@@ -297,6 +312,7 @@
 
108
                fclose(inp_stream);
 
109
        if (array)
 
110
                free(array);
 
111
+       regfree(&preg);
 
112
 
 
113
        if (counting)
 
114
                printf("%u\n", counting-1);