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 == ',')
|
if (*s == ',')
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
if (!(r = malloc(n * sizeof(Range))))
|
r = emalloc(n * sizeof(Range));
|
||||||
eprintf("malloc:");
|
|
||||||
for (s = str; n; n--, s++) {
|
for (s = str; n; n--, s++) {
|
||||||
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
|
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
|
||||||
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
|
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;
|
struct plist *pnode;
|
||||||
|
|
||||||
if (!(pnode = malloc(sizeof(*pnode))))
|
pnode = emalloc(sizeof(*pnode));
|
||||||
eprintf("malloc:");
|
pnode->pattern = estrdup(pattern);
|
||||||
if (!(pnode->pattern = strdup(pattern)))
|
|
||||||
eprintf("strdup:");
|
|
||||||
pnode->next = phead;
|
pnode->next = phead;
|
||||||
phead = pnode;
|
phead = pnode;
|
||||||
}
|
}
|
||||||
|
|
10
ls.c
10
ls.c
|
@ -92,8 +92,8 @@ main(int argc, char *argv[])
|
||||||
if (argc == 0)
|
if (argc == 0)
|
||||||
*--argv = ".", argc++;
|
*--argv = ".", argc++;
|
||||||
|
|
||||||
if (!(ents = malloc(argc * sizeof *ents)))
|
ents = emalloc(argc * sizeof(*ents));
|
||||||
eprintf("malloc:");
|
|
||||||
for (i = 0; i < argc; i++)
|
for (i = 0; i < argc; i++)
|
||||||
mkent(&ents[i], argv[i], 1);
|
mkent(&ents[i], argv[i], 1);
|
||||||
qsort(ents, argc, sizeof *ents, entcmp);
|
qsort(ents, argc, sizeof *ents, entcmp);
|
||||||
|
@ -154,10 +154,8 @@ lsdir(const char *path)
|
||||||
mkent(&ent, d->d_name, Fflag || lflag || iflag);
|
mkent(&ent, d->d_name, Fflag || lflag || iflag);
|
||||||
output(&ent);
|
output(&ent);
|
||||||
} else {
|
} else {
|
||||||
if (!(ents = realloc(ents, ++n * sizeof *ents)))
|
ents = erealloc(ents, ++n * sizeof *ents);
|
||||||
eprintf("realloc:");
|
p = emalloc((sz = strlen(d->d_name)+1));
|
||||||
if (!(p = malloc((sz = strlen(d->d_name)+1))))
|
|
||||||
eprintf("malloc:");
|
|
||||||
memcpy(p, d->d_name, sz);
|
memcpy(p, d->d_name, sz);
|
||||||
mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag);
|
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)
|
if (len == (size_t) - 1)
|
||||||
eprintf("invalid delimiter\n");
|
eprintf("invalid delimiter\n");
|
||||||
|
|
||||||
if (!(delim = malloc((len + 1) * sizeof(*delim))))
|
delim = emalloc((len + 1) * sizeof(*delim));
|
||||||
eprintf("out of memory\n");
|
|
||||||
|
|
||||||
mbstowcs(delim, adelim, len);
|
mbstowcs(delim, adelim, len);
|
||||||
len = unescape(delim);
|
len = unescape(delim);
|
||||||
|
@ -68,8 +67,7 @@ main(int argc, char *argv[])
|
||||||
eprintf("no delimiters specified\n");
|
eprintf("no delimiters specified\n");
|
||||||
|
|
||||||
/* populate file list */
|
/* populate file list */
|
||||||
if (!(dsc = malloc(argc * sizeof(*dsc))))
|
dsc = emalloc(argc * sizeof(*dsc));
|
||||||
eprintf("out of memory\n");
|
|
||||||
|
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
if (strcmp(argv[i], "-") == 0)
|
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;
|
long i, j;
|
||||||
size_t *size = NULL;
|
size_t *size = NULL;
|
||||||
|
|
||||||
if (!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof *size)))
|
ring = ecalloc(n, sizeof *ring);
|
||||||
eprintf("calloc:");
|
size = ecalloc(n, sizeof *size);
|
||||||
|
|
||||||
for (i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n)
|
for (i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n)
|
||||||
;
|
;
|
||||||
if (ferror(fp))
|
if (ferror(fp))
|
||||||
|
|
3
tee.c
3
tee.c
|
@ -29,8 +29,7 @@ main(int argc, char *argv[])
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
nfps = argc + 1;
|
nfps = argc + 1;
|
||||||
if (!(fps = calloc(nfps, sizeof *fps)))
|
fps = ecalloc(nfps, sizeof *fps);
|
||||||
eprintf("calloc:");
|
|
||||||
|
|
||||||
for (i = 0; argc > 0; argc--, argv++, i++)
|
for (i = 0; argc > 0; argc--, argv++, i++)
|
||||||
if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
|
if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
|
||||||
|
|
4
uniq.c
4
uniq.c
|
@ -81,8 +81,8 @@ uniqline(char *l)
|
||||||
prevline = NULL;
|
prevline = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (l && !(prevline = strdup(l)))
|
if (l)
|
||||||
eprintf("strdup:");
|
prevline = estrdup(l);
|
||||||
prevlinecount = 1;
|
prevlinecount = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,5 @@ apathmax(char **p, long *size)
|
||||||
eprintf("pathconf:");
|
eprintf("pathconf:");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*p = emalloc(*size);
|
||||||
if (!(*p = malloc(*size)))
|
|
||||||
eprintf("malloc:");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,11 @@ getlines(FILE *fp, struct linebuf *b)
|
||||||
while ((len = agetline(&line, &size, fp)) != -1) {
|
while ((len = agetline(&line, &size, fp)) != -1) {
|
||||||
if (++b->nlines > b->capacity) {
|
if (++b->nlines > b->capacity) {
|
||||||
b->capacity += 512;
|
b->capacity += 512;
|
||||||
nline = realloc(b->lines, b->capacity * sizeof(*b->lines));
|
nline = erealloc(b->lines, b->capacity * sizeof(*b->lines));
|
||||||
if (!nline)
|
|
||||||
eprintf("realloc:");
|
|
||||||
b->lines = nline;
|
b->lines = nline;
|
||||||
}
|
}
|
||||||
linelen = len + 1;
|
linelen = len + 1;
|
||||||
if (!(b->lines[b->nlines-1] = malloc(linelen)))
|
b->lines[b->nlines-1] = emalloc(linelen);
|
||||||
eprintf("malloc:");
|
|
||||||
memcpy(b->lines[b->nlines-1], line, linelen);
|
memcpy(b->lines[b->nlines-1], line, linelen);
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
|
|
10
xargs.c
10
xargs.c
|
@ -72,11 +72,11 @@ main(int argc, char *argv[])
|
||||||
argsz = 0; i = 0; a = 0;
|
argsz = 0; i = 0; a = 0;
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
for (; i < argc; i++) {
|
for (; i < argc; i++) {
|
||||||
cmd[i] = strdup(argv[i]);
|
cmd[i] = estrdup(argv[i]);
|
||||||
argsz += strlen(cmd[i]) + 1;
|
argsz += strlen(cmd[i]) + 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cmd[i] = strdup("/bin/echo");
|
cmd[i] = estrdup("/bin/echo");
|
||||||
argsz += strlen(cmd[i]) + 1;
|
argsz += strlen(cmd[i]) + 1;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ main(int argc, char *argv[])
|
||||||
leftover = 1;
|
leftover = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
cmd[i] = strdup(arg);
|
cmd[i] = estrdup(arg);
|
||||||
argsz += strlen(cmd[i]) + 1;
|
argsz += strlen(cmd[i]) + 1;
|
||||||
i++;
|
i++;
|
||||||
a++;
|
a++;
|
||||||
|
@ -134,9 +134,7 @@ fillargbuf(int ch)
|
||||||
{
|
{
|
||||||
if (argbpos >= argbsz) {
|
if (argbpos >= argbsz) {
|
||||||
argbsz = argbpos == 0 ? 1 : argbsz * 2;
|
argbsz = argbpos == 0 ? 1 : argbsz * 2;
|
||||||
argb = realloc(argb, argbsz);
|
argb = erealloc(argb, argbsz);
|
||||||
if (!argb)
|
|
||||||
eprintf("realloc:");
|
|
||||||
}
|
}
|
||||||
argb[argbpos] = ch;
|
argb[argbpos] = ch;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user