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