2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/param.h>
61 static void c_columnate(void);
62 static void input(FILE *);
63 static void maketbl(void);
64 static void print(void);
65 static void r_columnate(void);
66 static void usage(void);
67 static int width(const wchar_t *);
69 static int termwidth = 80; /* default terminal width */
71 static int entries; /* number of records */
72 static int eval; /* exit value */
73 static int maxlength; /* longest record */
74 static wchar_t **list; /* array of pointers to records */
75 static const wchar_t *separator = L"\t "; /* field separator for table option */
78 main(int argc, char **argv)
88 setlocale(LC_ALL, "");
90 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
91 if ((p = getenv("COLUMNS")))
94 termwidth = win.ws_col;
97 while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
100 termwidth = atoi(optarg);
104 seplen = mbsrtowcs(NULL, &src, 0, NULL);
105 if (seplen == (size_t)-1)
106 err(1, "bad separator");
107 newsep = malloc((seplen + 1) * sizeof(wchar_t));
110 mbsrtowcs(newsep, &src, seplen + 1, NULL);
128 else for (; *argv; ++argv)
129 if ((fp = fopen(*argv, "r"))) {
140 maxlength = roundup(maxlength + 1, TAB);
143 else if (maxlength >= termwidth)
155 int chcnt, col, cnt, endcol, numcols;
158 numcols = termwidth / maxlength;
160 for (chcnt = col = 0, lp = list;; ++lp) {
161 wprintf(L"%ls", *lp);
165 if (++col == numcols) {
170 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
171 (void)putwchar('\t');
184 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
186 numcols = termwidth / maxlength;
187 numrows = entries / numcols;
188 if (entries % numcols)
191 for (row = 0; row < numrows; ++row) {
193 for (base = row, chcnt = col = 0; col < numcols; ++col) {
194 wprintf(L"%ls", list[base]);
195 chcnt += width(list[base]);
196 if ((base += numrows) >= entries)
198 while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
199 (void)putwchar('\t');
214 for (cnt = entries, lp = list; cnt--; ++lp)
215 (void)wprintf(L"%ls\n", *lp);
218 typedef struct _tbl {
235 if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
236 err(1, (char *)NULL);
237 if ((cols = calloc((maxcols = DEFCOLS), sizeof(*cols))) == NULL)
238 err(1, (char *)NULL);
239 if ((lens = calloc(maxcols, sizeof(int))) == NULL)
240 err(1, (char *)NULL);
241 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
242 for (coloff = 0, p = *lp;
243 (cols[coloff] = wcstok(p, separator, &last));
245 if (++coloff == maxcols) {
246 if (!(cols = realloc(cols, ((u_int)maxcols +
247 DEFCOLS) * sizeof(char *))) ||
248 !(lens = realloc(lens,
249 ((u_int)maxcols + DEFCOLS) * sizeof(int))))
251 memset((char *)lens + maxcols * sizeof(int),
252 0, DEFCOLS * sizeof(int));
255 if ((t->list = calloc(coloff, sizeof(*t->list))) == NULL)
256 err(1, (char *)NULL);
257 if ((t->len = calloc(coloff, sizeof(int))) == NULL)
258 err(1, (char *)NULL);
259 for (t->cols = coloff; --coloff >= 0;) {
260 t->list[coloff] = cols[coloff];
261 t->len[coloff] = width(cols[coloff]);
262 if (t->len[coloff] > lens[coloff])
263 lens[coloff] = t->len[coloff];
266 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
267 for (coloff = 0; coloff < t->cols - 1; ++coloff)
268 (void)wprintf(L"%ls%*ls", t->list[coloff],
269 lens[coloff] - t->len[coloff] + 2, L" ");
270 (void)wprintf(L"%ls\n", t->list[coloff]);
275 #define MAXLINELEN (LINE_MAX + 1)
282 wchar_t *p, buf[MAXLINELEN];
285 if ((list = calloc((maxentry = DEFNUM), sizeof(*list))) ==
287 err(1, (char *)NULL);
288 while (fgetws(buf, MAXLINELEN, fp)) {
289 for (p = buf; *p && iswspace(*p); ++p);
292 if (!(p = wcschr(p, L'\n'))) {
293 warnx("line too long");
301 if (entries == maxentry) {
303 if (!(list = realloc(list,
304 (u_int)maxentry * sizeof(*list))))
307 list[entries] = malloc((wcslen(buf) + 1) * sizeof(wchar_t));
308 if (list[entries] == NULL)
310 wcscpy(list[entries], buf);
315 /* Like wcswidth(), but ignores non-printing characters. */
317 width(const wchar_t *wcs)
321 for (w = 0; *wcs != L'\0'; wcs++)
322 if ((cw = wcwidth(*wcs)) > 0)
331 (void)fprintf(stderr,
332 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");