sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit e17b9cdd0a1496d35a5492683531fe7d7e81a7cf
parent 045fc62028a6636d044cab371fb0e9553cf7b1e8
Author: FRIGN <dev@frign.de>
Date:   Sun Nov 16 11:07:26 +0100

Convert codebase to use emalloc.c utility-functions

This also definitely increases readability and makes OOM-conditions
more consistent.

Diffstat:
cut.c | 3+--
grep.c | 6++----
ls.c | 10++++------
paste.c | 6++----
tail.c | 5+++--
tee.c | 3+--
uniq.c | 4++--
util/apathmax.c | 4+---
util/getlines.c | 7++-----
xargs.c | 10++++------
10 files changed, 22 insertions(+), 36 deletions(-)
diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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; }