Use SLIST_* instead of TAILQ_* in grep(1)

The order of evaluation is unspecified by POSIX so we do not need
to process the patterns in-order.
This commit is contained in:
sin 2014-11-17 10:59:11 +00:00
parent de4a36957e
commit afa2e6ec54

20
grep.c
View File

@ -24,10 +24,10 @@ static char mode = 0;
struct pattern { struct pattern {
char *pattern; char *pattern;
regex_t preg; regex_t preg;
TAILQ_ENTRY(pattern) entry; SLIST_ENTRY(pattern) entry;
}; };
static TAILQ_HEAD(phead, pattern) phead; static SLIST_HEAD(phead, pattern) phead;
static void static void
usage(void) usage(void)
@ -38,11 +38,11 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct pattern *pnode, *tmp; struct pattern *pnode;
int i, m, flags = REG_NOSUB, match = NoMatch; int i, m, flags = REG_NOSUB, match = NoMatch;
FILE *fp; FILE *fp;
TAILQ_INIT(&phead); SLIST_INIT(&phead);
ARGBEGIN { ARGBEGIN {
case 'E': case 'E':
@ -85,9 +85,8 @@ main(int argc, char *argv[])
} }
/* Compile regex for all search patterns */ /* Compile regex for all search patterns */
TAILQ_FOREACH(pnode, &phead, entry) { SLIST_FOREACH(pnode, &phead, entry)
enregcomp(Error, &pnode->preg, pnode->pattern, flags); enregcomp(Error, &pnode->preg, pnode->pattern, flags);
}
many = (argc > 1); many = (argc > 1);
if (argc == 0) { if (argc == 0) {
match = grep(stdin, "<stdin>"); match = grep(stdin, "<stdin>");
@ -104,8 +103,9 @@ main(int argc, char *argv[])
fclose(fp); fclose(fp);
} }
} }
TAILQ_FOREACH_SAFE(pnode, &phead, entry, tmp) { while (!SLIST_EMPTY(&phead)) {
TAILQ_REMOVE(&phead, pnode, entry); pnode = SLIST_FIRST(&phead);
SLIST_REMOVE_HEAD(&phead, entry);
regfree(&pnode->preg); regfree(&pnode->preg);
free(pnode->pattern); free(pnode->pattern);
free(pnode); free(pnode);
@ -120,7 +120,7 @@ addpattern(const char *pattern)
pnode = emalloc(sizeof(*pnode)); pnode = emalloc(sizeof(*pnode));
pnode->pattern = estrdup(pattern); pnode->pattern = estrdup(pattern);
TAILQ_INSERT_TAIL(&phead, pnode, entry); SLIST_INSERT_HEAD(&phead, pnode, entry);
} }
static int static int
@ -136,7 +136,7 @@ grep(FILE *fp, const char *str)
/* Remove the trailing newline if one is present. */ /* Remove the trailing newline if one is present. */
if (len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
TAILQ_FOREACH(pnode, &phead, entry) { SLIST_FOREACH(pnode, &phead, entry) {
if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag) if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
continue; continue;
switch (mode) { switch (mode) {