commit 0fcad66c75d8495a22e36669e6933f7507e9f2fc
parent c0a3c66a84f38bd7b7a73d19cf0b4d0003d28b3d
Author: Jakob Kramer <jakob.kramer@gmx.de>
Date: Wed, 11 Feb 2015 02:08:17 +0100
make use of en*alloc functions
Diffstat:
3 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/expr.c b/expr.c
@@ -115,9 +115,7 @@ match(Val vstr, Val vregx)
str = valstr(vstr, buf1, sizeof(buf1));
regx = valstr(vregx, buf2, sizeof(buf2));
- anchreg = malloc(strlen(regx) + 2);
- if (!anchreg)
- enprintf(3, "malloc:");
+ anchreg = enmalloc(3, strlen(regx) + 2);
snprintf(anchreg, strlen(regx) + 2, "^%s", regx);
enregcomp(3, &re, anchreg, 0);
@@ -131,9 +129,7 @@ match(Val vstr, Val vregx)
if (re.re_nsub) {
regfree(&re);
len = matches[1].rm_eo - matches[1].rm_so + 1;
- ret = malloc(len);
- if (!ret)
- enprintf(3, "malloc:");
+ ret = enmalloc(3, len);
strlcpy(ret, str + matches[1].rm_so, len);
d = strtoimax(ret, &p, 10);
if (*ret && !*p) {
diff --git a/grep.c b/grep.c
@@ -162,18 +162,14 @@ addpattern(const char *pattern)
pattern = ".";
if (!Fflag && xflag) {
- tmp = malloc(strlen(pattern) + 3);
- if (!tmp)
- enprintf(Error, "malloc:");
+ tmp = enmalloc(Error, strlen(pattern) + 3);
snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
pattern[0] == '^' ? "" : "^",
pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$");
} else if (!Fflag && wflag) {
len = strlen(pattern) + 5 + (Eflag ? 2 : 4);
- tmp = malloc(len);
- if (!tmp)
- enprintf(Error, "malloc:");
+ tmp = enmalloc(Error, len);
bol = eol = 0;
if (pattern[0] == '^')
@@ -188,14 +184,10 @@ addpattern(const char *pattern)
Eflag ? ")" : "\\)",
eol ? "$" : "");
} else {
- tmp = strdup(pattern);
- if (!tmp)
- enprintf(Error, "strdup:");
+ tmp = enstrdup(Error, pattern);
}
- pnode = malloc(sizeof(*pnode));
- if (!pnode)
- enprintf(Error, "malloc:");
+ pnode = enmalloc(Error, sizeof(*pnode));
pnode->pattern = tmp;
SLIST_INSERT_HEAD(&phead, pnode, entry);
}
diff --git a/sort.c b/sort.c
@@ -131,9 +131,7 @@ addkeydef(char *def, int flags)
{
struct kdlist *node;
- node = malloc(sizeof(*node));
- if (!node)
- enprintf(2, "malloc:");
+ node = enmalloc(2, sizeof(*node));
if (!head)
head = node;
if (parse_keydef(&node->keydef, def, flags))
@@ -282,7 +280,6 @@ static char *
columns(char *line, const struct keydef *kd)
{
char *start, *end;
- char *res;
int i;
for (i = 1, start = line; i < kd->start_column; i++)
@@ -305,7 +302,5 @@ columns(char *line, const struct keydef *kd)
end = strchr(line, '\0');
}
- if (!(res = strndup(start, end - start)))
- enprintf(2, "strndup:");
- return res;
+ return enstrndup(2, start, end - start);
}