[PATCH 3/3] make use of en*alloc functions

From: Jakob Kramer <jakob.kramer_AT_gmx.de>
Date: Wed, 11 Feb 2015 02:08:17 +0100

---
 expr.c |  8 ++------
 grep.c | 16 ++++------------
 sort.c |  9 ++-------
 3 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/expr.c b/expr.c
index 04cbee6..c36e56a 100644
--- a/expr.c
+++ b/expr.c
_AT_@ -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);
_AT_@ -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
index 11937e9..4db2c09 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -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] == '^')
_AT_@ -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
index f917bb9..6184b0b 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -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))
_AT_@ -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++)
_AT_@ -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);
 }
-- 
1.9.1
--------------040100040607090404070005--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Wed Feb 11 2015 - 05:48:04 CET