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
29 * $FreeBSD: src/usr.bin/column/column.c,v 1.4.6.2 2001/08/02 01:34:19 obrien Exp $
30 * $DragonFly: src/usr.bin/column/column.c,v 1.6 2006/10/08 09:12:32 corecode Exp $
32 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
33 * @(#)column.c 8.4 (Berkeley) 5/4/95
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
49 void c_columnate(void);
53 void r_columnate(void);
56 int termwidth = 80; /* default terminal width */
58 int entries; /* number of records */
59 int eval; /* exit value */
60 int maxlength; /* longest record */
61 char **list; /* array of pointers to records */
62 const char *separator = "\t "; /* field separator for table option */
65 main(int argc, char **argv)
72 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
73 if ((p = getenv("COLUMNS")))
76 termwidth = win.ws_col;
79 while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
82 termwidth = atoi(optarg);
102 else for (; *argv; ++argv)
103 if ((fp = fopen(*argv, "r"))) {
114 maxlength = (maxlength + TAB) & ~(TAB - 1);
117 else if (maxlength >= termwidth)
129 int chcnt, col, cnt, endcol, numcols;
132 numcols = termwidth / maxlength;
134 for (chcnt = col = 0, lp = list;; ++lp) {
135 chcnt += printf("%s", *lp);
138 if (++col == numcols) {
143 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
157 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
159 numcols = termwidth / maxlength;
160 numrows = entries / numcols;
161 if (entries % numcols)
164 for (row = 0; row < numrows; ++row) {
166 for (base = row, chcnt = col = 0; col < numcols; ++col) {
167 chcnt += printf("%s", list[base]);
168 if ((base += numrows) >= entries)
170 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
186 for (cnt = entries, lp = list; cnt--; ++lp)
187 (void)printf("%s\n", *lp);
190 typedef struct _tbl {
206 if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
208 if ((cols = calloc((maxcols = DEFCOLS), sizeof(char *))) == NULL)
210 if ((lens = calloc(maxcols, sizeof(int))) == NULL)
212 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
213 for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
215 if (++coloff == maxcols) {
216 if (!(cols = realloc(cols, ((u_int)maxcols +
217 DEFCOLS) * sizeof(char *))) ||
218 !(lens = realloc(lens,
219 ((u_int)maxcols + DEFCOLS) * sizeof(int))))
221 memset((char *)lens + maxcols * sizeof(int),
222 0, DEFCOLS * sizeof(int));
225 if ((t->list = calloc(coloff, sizeof(char *))) == NULL)
227 if ((t->len = calloc(coloff, sizeof(int))) == NULL)
229 for (t->cols = coloff; --coloff >= 0;) {
230 t->list[coloff] = cols[coloff];
231 t->len[coloff] = strlen(cols[coloff]);
232 if (t->len[coloff] > lens[coloff])
233 lens[coloff] = t->len[coloff];
236 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
237 for (coloff = 0; coloff < t->cols - 1; ++coloff)
238 (void)printf("%s%*s", t->list[coloff],
239 lens[coloff] - t->len[coloff] + 2, " ");
240 (void)printf("%s\n", t->list[coloff]);
245 #define MAXLINELEN (LINE_MAX + 1)
252 char *p, buf[MAXLINELEN];
255 if ((list = calloc((maxentry = DEFNUM), sizeof(char *))) ==
258 while (fgets(buf, MAXLINELEN, fp)) {
259 for (p = buf; *p && isspace(*p); ++p);
262 if (!(p = strchr(p, '\n'))) {
263 warnx("line too long");
271 if (entries == maxentry) {
273 if (!(list = realloc(list,
274 (u_int)maxentry * sizeof(char *))))
277 list[entries++] = strdup(buf);
285 (void)fprintf(stderr,
286 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");