Convert codebase to use emalloc.c utility-functions
This also definitely increases readability and makes OOM-conditions more consistent.
This commit is contained in:
parent
045fc62028
commit
e17b9cdd0a
3
cut.c
3
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;
|
||||
|
|
6
grep.c
6
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;
|
||||
}
|
||||
|
|
10
ls.c
10
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);
|
||||
}
|
||||
|
|
6
paste.c
6
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)
|
||||
|
|
5
tail.c
5
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))
|
||||
|
|
3
tee.c
3
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")))
|
||||
|
|
4
uniq.c
4
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,5 @@ apathmax(char **p, long *size)
|
|||
eprintf("pathconf:");
|
||||
}
|
||||
}
|
||||
|
||||
if (!(*p = malloc(*size)))
|
||||
eprintf("malloc:");
|
||||
*p = emalloc(*size);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
10
xargs.c
10
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user