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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1989, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <sys/param.h>
63 static void c_columnate(void);
64 static void input(FILE *);
65 static void maketbl(void);
66 static void print(void);
67 static void r_columnate(void);
68 static void usage(void);
69 static int width(const wchar_t *);
71 static int termwidth = 80; /* default terminal width */
73 static int entries; /* number of records */
74 static int eval; /* exit value */
75 static int maxlength; /* longest record */
76 static wchar_t **list; /* array of pointers to records */
77 static const wchar_t *separator = L"\t "; /* field separator for table option */
80 main(int argc, char **argv)
90 setlocale(LC_ALL, "");
92 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
93 if ((p = getenv("COLUMNS")))
96 termwidth = win.ws_col;
99 while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
102 termwidth = atoi(optarg);
106 seplen = mbsrtowcs(NULL, &src, 0, NULL);
107 if (seplen == (size_t)-1)
108 err(1, "bad separator");
109 newsep = malloc((seplen + 1) * sizeof(wchar_t));
112 mbsrtowcs(newsep, &src, seplen + 1, NULL);
130 else for (; *argv; ++argv)
131 if ((fp = fopen(*argv, "r"))) {
142 maxlength = roundup(maxlength + 1, TAB);
145 else if (maxlength >= termwidth)
157 int chcnt, col, cnt, endcol, numcols;
160 numcols = termwidth / maxlength;
162 for (chcnt = col = 0, lp = list;; ++lp) {
163 wprintf(L"%ls", *lp);
167 if (++col == numcols) {
172 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
173 (void)putwchar('\t');
186 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
188 numcols = termwidth / maxlength;
189 numrows = entries / numcols;
190 if (entries % numcols)
193 for (row = 0; row < numrows; ++row) {
195 for (base = row, chcnt = col = 0; col < numcols; ++col) {
196 wprintf(L"%ls", list[base]);
197 chcnt += width(list[base]);
198 if ((base += numrows) >= entries)
200 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
201 (void)putwchar('\t');
216 for (cnt = entries, lp = list; cnt--; ++lp)
217 (void)wprintf(L"%ls\n", *lp);
220 typedef struct _tbl {
237 if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
239 if ((cols = calloc((maxcols = DEFCOLS), sizeof(*cols))) == NULL)
241 if ((lens = calloc(maxcols, sizeof(int))) == NULL)
243 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
244 for (coloff = 0, p = *lp;
245 (cols[coloff] = wcstok(p, separator, &last));
247 if (++coloff == maxcols) {
248 if (!(cols = realloc(cols, ((u_int)maxcols +
249 DEFCOLS) * sizeof(wchar_t *))) ||
250 !(lens = realloc(lens,
251 ((u_int)maxcols + DEFCOLS) * sizeof(int))))
253 memset((char *)lens + maxcols * sizeof(int),
254 0, DEFCOLS * sizeof(int));
257 if ((t->list = calloc(coloff, sizeof(*t->list))) == NULL)
259 if ((t->len = calloc(coloff, sizeof(int))) == NULL)
261 for (t->cols = coloff; --coloff >= 0;) {
262 t->list[coloff] = cols[coloff];
263 t->len[coloff] = width(cols[coloff]);
264 if (t->len[coloff] > lens[coloff])
265 lens[coloff] = t->len[coloff];
268 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
269 for (coloff = 0; coloff < t->cols - 1; ++coloff)
270 (void)wprintf(L"%ls%*ls", t->list[coloff],
271 lens[coloff] - t->len[coloff] + 2, L" ");
272 (void)wprintf(L"%ls\n", t->list[coloff]);
282 #define MAXLINELEN (LINE_MAX + 1)
289 wchar_t *p, buf[MAXLINELEN];
292 if ((list = calloc((maxentry = DEFNUM), sizeof(*list))) ==
295 while (fgetws(buf, MAXLINELEN, fp)) {
296 for (p = buf; *p && iswspace(*p); ++p);
299 if (!(p = wcschr(p, L'\n'))) {
300 warnx("line too long");
308 if (entries == maxentry) {
310 if (!(list = realloc(list,
311 (u_int)maxentry * sizeof(*list))))
314 list[entries] = malloc((wcslen(buf) + 1) * sizeof(wchar_t));
315 if (list[entries] == NULL)
317 wcscpy(list[entries], buf);
322 /* Like wcswidth(), but ignores non-printing characters. */
324 width(const wchar_t *wcs)
328 for (w = 0; *wcs != L'\0'; wcs++)
329 if ((cw = wcwidth(*wcs)) > 0)
338 (void)fprintf(stderr,
339 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");