Respect exit status in grep(1)

This commit is contained in:
sin 2014-11-21 11:43:53 +00:00
parent 228e8c4a0b
commit 2b39f20675

14
grep.c
View File

@ -160,16 +160,22 @@ addpattern(const char *pattern)
pattern = "."; pattern = ".";
if (!Fflag && xflag) { if (!Fflag && xflag) {
tmp = emalloc(strlen(pattern) + 3); tmp = malloc(strlen(pattern) + 3);
if (!tmp)
enprintf(Error, "malloc:");
snprintf(tmp, strlen(pattern) + 3, "%s%s%s", snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
pattern[0] == '^' ? "" : "^", pattern[0] == '^' ? "" : "^",
pattern, pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$"); pattern[strlen(pattern) - 1] == '$' ? "" : "$");
} else { } else {
tmp = estrdup(pattern); tmp = strdup(pattern);
if (!tmp)
enprintf(Error, "strdup:");
} }
pnode = emalloc(sizeof(*pnode)); pnode = malloc(sizeof(*pnode));
if (!pnode)
enprintf(Error, "malloc:");
pnode->pattern = tmp; pnode->pattern = tmp;
SLIST_INSERT_HEAD(&phead, pnode, entry); SLIST_INSERT_HEAD(&phead, pnode, entry);
} }
@ -186,6 +192,8 @@ addpatternfile(FILE *fp)
addpattern(buf); addpattern(buf);
} }
free(buf); free(buf);
if (ferror(fp))
enprintf(Error, "read error:");
} }
static int static int