2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/param.h>
48 static void c_columnate(void);
49 static void input(FILE *);
50 static void maketbl(void);
51 static void print(void);
52 static void r_columnate(void);
53 static void usage(void);
54 static int width(const wchar_t *);
56 static int termwidth = 80; /* default terminal width */
58 static int entries; /* number of records */
59 static int eval; /* exit value */
60 static int maxlength; /* longest record */
61 static wchar_t **list; /* array of pointers to records */
62 static const wchar_t *separator = L"\t "; /* field separator for table option */
65 main(int argc, char **argv)
75 setlocale(LC_ALL, "");
77 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
78 if ((p = getenv("COLUMNS")))
81 termwidth = win.ws_col;
84 while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
87 termwidth = atoi(optarg);
91 seplen = mbsrtowcs(NULL, &src, 0, NULL);
92 if (seplen == (size_t)-1)
93 err(1, "bad separator");
94 newsep = malloc((seplen + 1) * sizeof(wchar_t));
97 mbsrtowcs(newsep, &src, seplen + 1, NULL);
115 else for (; *argv; ++argv)
116 if ((fp = fopen(*argv, "r"))) {
127 maxlength = roundup(maxlength + 1, TAB);
130 else if (maxlength >= termwidth)
142 int chcnt, col, cnt, endcol, numcols;
145 numcols = termwidth / maxlength;
147 for (chcnt = col = 0, lp = list;; ++lp) {
148 wprintf(L"%ls", *lp);
152 if (++col == numcols) {
157 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
158 (void)putwchar('\t');
171 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
173 numcols = termwidth / maxlength;
174 numrows = entries / numcols;
175 if (entries % numcols)
178 for (row = 0; row < numrows; ++row) {
180 for (base = row, chcnt = col = 0; col < numcols; ++col) {
181 wprintf(L"%ls", list[base]);
182 chcnt += width(list[base]);
183 if ((base += numrows) >= entries)
185 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
186 (void)putwchar('\t');
201 for (cnt = entries, lp = list; cnt--; ++lp)
202 (void)wprintf(L"%ls\n", *lp);
205 typedef struct _tbl {
222 if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
224 if ((cols = calloc((maxcols = DEFCOLS), sizeof(*cols))) == NULL)
226 if ((lens = calloc(maxcols, sizeof(int))) == NULL)
228 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
229 for (coloff = 0, p = *lp;
230 (cols[coloff] = wcstok(p, separator, &last));
232 if (++coloff == maxcols) {
233 if (!(cols = realloc(cols, ((u_int)maxcols +
234 DEFCOLS) * sizeof(wchar_t *))) ||
235 !(lens = realloc(lens,
236 ((u_int)maxcols + DEFCOLS) * sizeof(int))))
238 memset((char *)lens + maxcols * sizeof(int),
239 0, DEFCOLS * sizeof(int));
242 if ((t->list = calloc(coloff, sizeof(*t->list))) == NULL)
244 if ((t->len = calloc(coloff, sizeof(int))) == NULL)
246 for (t->cols = coloff; --coloff >= 0;) {
247 t->list[coloff] = cols[coloff];
248 t->len[coloff] = width(cols[coloff]);
249 if (t->len[coloff] > lens[coloff])
250 lens[coloff] = t->len[coloff];
253 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
254 for (coloff = 0; coloff < t->cols - 1; ++coloff)
255 (void)wprintf(L"%ls%*ls", t->list[coloff],
256 lens[coloff] - t->len[coloff] + 2, L" ");
257 (void)wprintf(L"%ls\n", t->list[coloff]);
267 #define MAXLINELEN (LINE_MAX + 1)
274 wchar_t *p, buf[MAXLINELEN];
277 if ((list = calloc((maxentry = DEFNUM), sizeof(*list))) ==
280 while (fgetws(buf, MAXLINELEN, fp)) {
281 for (p = buf; *p && iswspace(*p); ++p);
284 if (!(p = wcschr(p, L'\n'))) {
285 warnx("line too long");
293 if (entries == maxentry) {
295 if (!(list = realloc(list,
296 (u_int)maxentry * sizeof(*list))))
299 list[entries] = malloc((wcslen(buf) + 1) * sizeof(wchar_t));
300 if (list[entries] == NULL)
302 wcscpy(list[entries], buf);
307 /* Like wcswidth(), but ignores non-printing characters. */
309 width(const wchar_t *wcs)
313 for (w = 0; *wcs != L'\0'; wcs++)
314 if ((cw = wcwidth(*wcs)) > 0)
323 (void)fprintf(stderr,
324 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");