commit e17b9cdd0a1496d35a5492683531fe7d7e81a7cf
Author: FRIGN <[email protected]>
Date:   Sun Nov 16 11:07:26 2014 +0100

    Convert codebase to use emalloc.c utility-functions
    
    This also definitely increases readability and makes OOM-conditions
    more consistent.

diff --git a/cut.c b/cut.c
index c20a236..97d2f2a 100644
--- a/cut.c
+++ b/cut.c
@@ -63,8 +63,7 @@ parselist(char *str)
                if (*s == ',')
                        n++;
        }
-       if (!(r = malloc(n * sizeof(Range))))
-               eprintf("malloc:");
+       r = emalloc(n * sizeof(Range));
        for (s = str; n; n--, s++) {
                r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
                r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
diff --git a/grep.c b/grep.c
index 7352aae..edc565a 100644
--- a/grep.c
+++ b/grep.c
@@ -111,10 +111,8 @@ addpattern(const char *pattern)
 {
        struct plist *pnode;
 
-       if (!(pnode = malloc(sizeof(*pnode))))
-               eprintf("malloc:");
-       if (!(pnode->pattern = strdup(pattern)))
-               eprintf("strdup:");
+       pnode = emalloc(sizeof(*pnode));
+       pnode->pattern = estrdup(pattern);
        pnode->next = phead;
        phead = pnode;
 }
diff --git a/ls.c b/ls.c
index 2af7d45..97af5f9 100644
--- a/ls.c
+++ b/ls.c
@@ -92,8 +92,8 @@ main(int argc, char *argv[])
        if (argc == 0)
                *--argv = ".", argc++;
 
-       if (!(ents = malloc(argc * sizeof *ents)))
-               eprintf("malloc:");
+       ents = emalloc(argc * sizeof(*ents));
+
        for (i = 0; i < argc; i++)
                mkent(&ents[i], argv[i], 1);
        qsort(ents, argc, sizeof *ents, entcmp);
@@ -154,10 +154,8 @@ lsdir(const char *path)
                        mkent(&ent, d->d_name, Fflag || lflag || iflag);
                        output(&ent);
                } else {
-                       if (!(ents = realloc(ents, ++n * sizeof *ents)))
-                               eprintf("realloc:");
-                       if (!(p = malloc((sz = strlen(d->d_name)+1))))
-                               eprintf("malloc:");
+                       ents = erealloc(ents, ++n * sizeof *ents);
+                       p = emalloc((sz = strlen(d->d_name)+1));
                        memcpy(p, d->d_name, sz);
                        mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag);
                }
diff --git a/paste.c b/paste.c
index 918dea0..382ad79 100644
--- a/paste.c
+++ b/paste.c
@@ -59,8 +59,7 @@ main(int argc, char *argv[])
        if (len == (size_t) - 1)
                eprintf("invalid delimiter\n");
 
-       if (!(delim = malloc((len + 1) * sizeof(*delim))))
-               eprintf("out of memory\n");
+       delim = emalloc((len + 1) * sizeof(*delim));
 
        mbstowcs(delim, adelim, len);
        len = unescape(delim);
@@ -68,8 +67,7 @@ main(int argc, char *argv[])
                eprintf("no delimiters specified\n");
 
        /* populate file list */
-       if (!(dsc = malloc(argc * sizeof(*dsc))))
-               eprintf("out of memory\n");
+       dsc = emalloc(argc * sizeof(*dsc));
 
        for (i = 0; i < argc; i++) {
                if (strcmp(argv[i], "-") == 0)
diff --git a/tail.c b/tail.c
index b87b83f..b53ec8d 100644
--- a/tail.c
+++ b/tail.c
@@ -75,8 +75,9 @@ taketail(FILE *fp, const char *str, long n)
        long i, j;
        size_t *size = NULL;
 
-       if (!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof 
*size)))
-               eprintf("calloc:");
+       ring = ecalloc(n, sizeof *ring);
+       size = ecalloc(n, sizeof *size);
+
        for (i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) 
% n)
                ;
        if (ferror(fp))
diff --git a/tee.c b/tee.c
index 638e59f..bdd8222 100644
--- a/tee.c
+++ b/tee.c
@@ -29,8 +29,7 @@ main(int argc, char *argv[])
        } ARGEND;
 
        nfps = argc + 1;
-       if (!(fps = calloc(nfps, sizeof *fps)))
-               eprintf("calloc:");
+       fps = ecalloc(nfps, sizeof *fps);
 
        for (i = 0; argc > 0; argc--, argv++, i++)
                if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
diff --git a/uniq.c b/uniq.c
index f07c40a..709a913 100644
--- a/uniq.c
+++ b/uniq.c
@@ -81,8 +81,8 @@ uniqline(char *l)
                prevline = NULL;
        }
 
-       if (l && !(prevline = strdup(l)))
-               eprintf("strdup:");
+       if (l)
+               prevline = estrdup(l);
        prevlinecount = 1;
 }
 
diff --git a/util/apathmax.c b/util/apathmax.c
index 348df4c..c570329 100644
--- a/util/apathmax.c
+++ b/util/apathmax.c
@@ -18,7 +18,5 @@ apathmax(char **p, long *size)
                        eprintf("pathconf:");
                }
        }
-
-       if (!(*p = malloc(*size)))
-               eprintf("malloc:");
+       *p = emalloc(*size);
 }
diff --git a/util/getlines.c b/util/getlines.c
index ee9457f..df64544 100644
--- a/util/getlines.c
+++ b/util/getlines.c
@@ -16,14 +16,11 @@ getlines(FILE *fp, struct linebuf *b)
        while ((len = agetline(&line, &size, fp)) != -1) {
                if (++b->nlines > b->capacity) {
                        b->capacity += 512;
-                       nline = realloc(b->lines, b->capacity * 
sizeof(*b->lines));
-                       if (!nline)
-                               eprintf("realloc:");
+                       nline = erealloc(b->lines, b->capacity * 
sizeof(*b->lines));
                        b->lines = nline;
                }
                linelen = len + 1;
-               if (!(b->lines[b->nlines-1] = malloc(linelen)))
-                       eprintf("malloc:");
+               b->lines[b->nlines-1] = emalloc(linelen);
                memcpy(b->lines[b->nlines-1], line, linelen);
        }
        free(line);
diff --git a/xargs.c b/xargs.c
index 05abf2f..d972a34 100644
--- a/xargs.c
+++ b/xargs.c
@@ -72,11 +72,11 @@ main(int argc, char *argv[])
                argsz = 0; i = 0; a = 0;
                if (argc > 0) {
                        for (; i < argc; i++) {
-                               cmd[i] = strdup(argv[i]);
+                               cmd[i] = estrdup(argv[i]);
                                argsz += strlen(cmd[i]) + 1;
                        }
                } else {
-                       cmd[i] = strdup("/bin/echo");
+                       cmd[i] = estrdup("/bin/echo");
                        argsz += strlen(cmd[i]) + 1;
                        i++;
                }
@@ -88,7 +88,7 @@ main(int argc, char *argv[])
                                leftover = 1;
                                break;
                        }
-                       cmd[i] = strdup(arg);
+                       cmd[i] = estrdup(arg);
                        argsz += strlen(cmd[i]) + 1;
                        i++;
                        a++;
@@ -134,9 +134,7 @@ fillargbuf(int ch)
 {
        if (argbpos >= argbsz) {
                argbsz = argbpos == 0 ? 1 : argbsz * 2;
-               argb = realloc(argb, argbsz);
-               if (!argb)
-                       eprintf("realloc:");
+               argb = erealloc(argb, argbsz);
        }
        argb[argbpos] = ch;
 }


Reply via email to