commit 5ba4f37ec3c4dc0d47bc39662d99e8ce005639ba
parent 7627a5069cb14c66d8ba964e4a02ce5b56ff6180
Author: sin <sin@2f30.org>
Date: Thu, 20 Nov 2014 16:45:22 +0000
Handle null BRE/ERE and do not add a pattern to the list if it already exists
Diffstat:
M | grep.c | | | 19 | ++++++++++++++++--- |
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git 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);
}