commit 5ba4f37ec3c4dc0d47bc39662d99e8ce005639ba
Author: sin <[email protected]>
Date:   Thu Nov 20 16:45:22 2014 +0000

    Handle null BRE/ERE and do not add a pattern to the list if it already 
exists

diff --git a/grep.c b/grep.c
index 153f8f7..fa3e3bd 100644
--- a/grep.c
+++ b/grep.c
@@ -134,17 +134,30 @@ addpattern(const char *pattern)
        struct pattern *pnode;
        char *tmp;
 
-       pnode = emalloc(sizeof(*pnode));
+       /* a null BRE/ERE matches every line */
+       if (!Fflag)
+               if (pattern[0] == '\0')
+                       pattern = ".";
+
        if (!Fflag && xflag) {
                tmp = emalloc(strlen(pattern) + 3);
                snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
                         pattern[0] == '^' ? "" : "^",
                         pattern,
                         pattern[strlen(pattern) - 1] == '$' ? "" : "$");
-               pnode->pattern = tmp;
        } else {
-               pnode->pattern = estrdup(pattern);
+               tmp = estrdup(pattern);
+       }
+
+       SLIST_FOREACH(pnode, &phead, entry) {
+               if (!strcmp(pnode->pattern, tmp)) {
+                       free(tmp);
+                       return;
+               }
        }
+
+       pnode = emalloc(sizeof(*pnode));
+       pnode->pattern = tmp;
        SLIST_INSERT_HEAD(&phead, pnode, entry);
 }
 


Reply via email to