-/* $OpenBSD: column.c,v 1.17 2014/01/22 09:39:38 jsg Exp $ */
+/* $OpenBSD: column.c,v 1.18 2014/05/17 20:05:07 espie Exp $ */
/* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */
/*
#include <unistd.h>
void c_columnate(void);
-void *emalloc(size_t);
-void *erealloc(void *, size_t);
+void *ereallocarray(void *, size_t, size_t);
+void *ecalloc(size_t, size_t);
void input(FILE *);
void maketbl(void);
void print(void);
TBL *tbl;
char **cols;
- t = tbl = emalloc(entries * sizeof(TBL));
- cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
- lens = emalloc(maxcols * sizeof(int));
+ t = tbl = ecalloc(entries, sizeof(TBL));
+ cols = ecalloc((maxcols = DEFCOLS), sizeof(char *));
+ lens = ecalloc(maxcols, sizeof(int));
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
p = NULL)
if (++coloff == maxcols) {
maxcols += DEFCOLS;
- cols = erealloc(cols, maxcols * sizeof(char *));
- lens = erealloc(lens, maxcols * sizeof(int));
+ cols = ereallocarray(cols, maxcols,
+ sizeof(char *));
+ lens = ereallocarray(lens, maxcols,
+ sizeof(int));
memset(lens + coloff, 0, DEFCOLS * sizeof(int));
}
if (coloff == 0)
continue;
- t->list = emalloc(coloff * sizeof(char *));
- t->len = emalloc(coloff * sizeof(int));
+ t->list = ecalloc(coloff, sizeof(char *));
+ t->len = ecalloc(coloff, sizeof(int));
for (t->cols = coloff; --coloff >= 0;) {
t->list[coloff] = cols[coloff];
t->len[coloff] = strlen(cols[coloff]);
char *p, buf[MAXLINELEN];
if (!list)
- list = emalloc((maxentry = DEFNUM) * sizeof(char *));
+ list = ecalloc((maxentry = DEFNUM), sizeof(char *));
while (fgets(buf, MAXLINELEN, fp)) {
for (p = buf; isspace((unsigned char)*p); ++p);
if (!*p)
maxlength = len;
if (entries == maxentry) {
maxentry += DEFNUM;
- list = erealloc(list, maxentry * sizeof(char *));
+ list = ereallocarray(list, maxentry, sizeof(char *));
}
if (!(list[entries++] = strdup(buf)))
err(1, NULL);
}
void *
-emalloc(size_t size)
+ereallocarray(void *oldp, size_t sz1, size_t sz2)
{
void *p;
- if (!(p = malloc(size)))
+ if (!(p = reallocarray(oldp, sz1, sz2)))
err(1, NULL);
- memset(p, 0, size);
return (p);
}
void *
-erealloc(void *oldp, size_t size)
+ecalloc(size_t sz1, size_t sz2)
{
void *p;
- if (!(p = realloc(oldp, size)))
+ if (!(p = calloc(sz1, sz2)))
err(1, NULL);
return (p);
}