diff options
| -rw-r--r-- | grep.c | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -48,7 +48,6 @@ static int grep_inverse = 0; static int grep_silent = 0; static int grep_exact = 0; -/* TODO: handle -x */ static int grep_match_re(struct grep_list *head, const char *buf) { for (struct grep_list *c = head; c != NULL; c = c->next) { @@ -176,8 +175,14 @@ static struct grep_list * grep_add_list(struct grep_list *head, char *s) tmp->next = node; } - node->string = strdup(pattern); - node->len = strlen(node->string); + /* reserve space for adding ^ and $ if needed */ + node->len = strlen(pattern); + node->string = malloc(node->len + 3); + if (node->string == NULL) { + perror("grep"); + exit(2); + } + strcpy(node->string, pattern); pattern = strtok(NULL, "\n"); } @@ -283,6 +288,12 @@ int main(int argc, char *argv[]) if (type != FIXED) { for (struct grep_list *c = head; c != NULL; c = c->next) { + if (grep_exact) { + memmove(c->string + 1, c->string, c->len); + c->string[0] = '^'; + c->string[c->len + 1] = '$'; + } + int e = regcomp(&(c->re), c->string, flags); if (e == 0) { free(c->string); |
