#define wcstok mtsafe_strtok
#endif
+#define DEFCOLS 25
+#define TAB 8
+#define DEFNUM 1000
+#define MAXLINELEN (LINE_MAX + 1)
+
static void c_columnate __P((void));
static void *emalloc __P((int));
static void input __P((FILE *));
static void print __P((void));
static void r_columnate __P((void));
+typedef struct _tbl {
+ wchar_t **list;
+ int cols, *len;
+} TBL;
+
int termwidth = 80; /* default terminal width */
int entries; /* number of records */
switch(ch) {
case 'h':
case '?':
- usage(EXIT_SUCCESS);
- break;
+ usage(EXIT_SUCCESS);
+ break;
case 'c':
termwidth = atoi(optarg);
break;
break;
default:
usage(EXIT_FAILURE);
- }
+ }
argc -= optind;
argv += optind;
if (!*argv)
input(stdin);
- else for (; *argv; ++argv)
- if ((fp = fopen(*argv, "r")) != NULL) {
- input(fp);
- (void)fclose(fp);
- } else {
- warn("%s", *argv);
- eval = EXIT_FAILURE;
+ else {
+ for (; *argv; ++argv) {
+ if ((fp = fopen(*argv, "r")) != NULL) {
+ input(fp);
+ (void)fclose(fp);
+ } else {
+ warn("%s", *argv);
+ eval = EXIT_FAILURE;
+ }
}
+ }
if (!entries)
exit(eval);
exit(eval);
}
-#define TAB 8
static void
c_columnate()
{
maxlength = (maxlength + TAB) & ~(TAB - 1);
numcols = termwidth / maxlength;
if (!numcols)
- numcols = 1;
+ numcols = 1;
numrows = entries / numcols;
if (entries % numcols)
++numrows;
}
}
-typedef struct _tbl {
- wchar_t **list;
- int cols, *len;
-} TBL;
-#define DEFCOLS 25
-
static void
maketbl()
{
cols = emalloc((maxcols = DEFCOLS) * sizeof(wchar_t *));
lens = emalloc(maxcols * sizeof(int));
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
- for (coloff = 0, p = *lp;
- (cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL;
- p = NULL)
- if (++coloff == maxcols) {
- if (!(cols = realloc(cols, ((u_int)maxcols + DEFCOLS)
- * sizeof(wchar_t *))) ||
- !(lens = realloc(lens, ((u_int)maxcols + DEFCOLS)
- * sizeof(int))))
- err(EXIT_FAILURE, _("out of memory?"));
- memset((char *)lens + maxcols * sizeof(int),
- 0, DEFCOLS * sizeof(int));
- maxcols += DEFCOLS;
+ for (coloff = 0, p = *lp;
+ (cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL;
+ p = NULL) {
+ if (++coloff == maxcols) {
+ cols = realloc(cols, ((u_int)maxcols + DEFCOLS)
+ * sizeof(wchar_t *));
+ lens = realloc(lens, ((u_int)maxcols + DEFCOLS)
+ * sizeof(int));
+ if (!cols || !lens)
+ err(EXIT_FAILURE, _("out of memory?"));
+ memset((char *)lens + maxcols * sizeof(int),
+ 0, DEFCOLS * sizeof(int));
+ maxcols += DEFCOLS;
+ }
+ }
+ t->list = emalloc(coloff * sizeof(wchar_t *));
+ t->len = emalloc(coloff * sizeof(int));
+ for (t->cols = coloff; --coloff >= 0;) {
+ t->list[coloff] = cols[coloff];
+ t->len[coloff] = wcs_width(cols[coloff]);
+ if (t->len[coloff] > lens[coloff])
+ lens[coloff] = t->len[coloff];
}
- t->list = emalloc(coloff * sizeof(wchar_t *));
- t->len = emalloc(coloff * sizeof(int));
- for (t->cols = coloff; --coloff >= 0;) {
- t->list[coloff] = cols[coloff];
- t->len[coloff] = wcs_width(cols[coloff]);
- if (t->len[coloff] > lens[coloff])
- lens[coloff] = t->len[coloff];
- }
}
for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
- for (coloff = 0; coloff < t->cols - 1; ++coloff) {
+ for (coloff = 0; coloff < t->cols - 1; ++coloff) {
+ fputws(t->list[coloff], stdout);
+ for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
+ putwchar(' ');
+ }
fputws(t->list[coloff], stdout);
- for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
- putwchar(' ');
- }
- fputws(t->list[coloff], stdout);
- putwchar('\n');
+ putwchar('\n');
}
}
-#define DEFNUM 1000
-#define MAXLINELEN (LINE_MAX + 1)
-
static void
input(fp)
FILE *fp;
continue;
if (!(p = wcschr(p, '\n')) && !feof(fp)) {
if (reportedline < lineno) {
- warnx(_("line %d is too long, output will be truncated"), lineno);
+ warnx(_("line %d is too long, output will be truncated"),
+ lineno);
reportedline = lineno;
}
eval = EXIT_FAILURE;
maxlength = len;
if (entries == maxentry) {
maxentry += DEFNUM;
- if (!(list = realloc(list,
- (u_int)maxentry * sizeof(wchar_t *))))
+ list = realloc(list, (u_int)maxentry * sizeof(wchar_t *));
+ if (!list)
err(EXIT_FAILURE, _("out of memory?"));
}
list[entries++] = wcsdup(buf);
static void *
emalloc(size)
- int size;
+ int size;
{
char *p;
memset(p, 0, size);
return (p);
}
-
-