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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
55 void c_columnate __P((void));
56 void *emalloc __P((int));
57 void input __P((FILE *));
58 void maketbl __P((void));
59 void print __P((void));
60 void r_columnate __P((void));
61 void usage __P((void));
63 int termwidth = 80; /* default terminal width */
65 int entries; /* number of records */
66 int eval; /* exit value */
67 int maxlength; /* longest record */
68 char **list; /* array of pointers to records */
69 char *separator = "\t "; /* field separator for table option */
81 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
82 if ((p = getenv("COLUMNS")))
85 termwidth = win.ws_col;
88 while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
91 termwidth = atoi(optarg);
111 else for (; *argv; ++argv)
112 if ((fp = fopen(*argv, "r"))) {
125 else if (maxlength >= termwidth)
138 int chcnt, col, cnt, endcol, numcols;
141 maxlength = (maxlength + TAB) & ~(TAB - 1);
142 numcols = termwidth / maxlength;
144 for (chcnt = col = 0, lp = list;; ++lp) {
145 chcnt += printf("%s", *lp);
148 if (++col == numcols) {
153 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
167 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
169 maxlength = (maxlength + TAB) & ~(TAB - 1);
170 numcols = termwidth / maxlength;
171 numrows = entries / numcols;
172 if (entries % numcols)
175 for (row = 0; row < numrows; ++row) {
177 for (base = row, chcnt = col = 0; col < numcols; ++col) {
178 chcnt += printf("%s", list[base]);
179 if ((base += numrows) >= entries)
181 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
197 for (cnt = entries, lp = list; cnt--; ++lp)
198 (void)printf("%s\n", *lp);
201 typedef struct _tbl {
217 t = tbl = emalloc(entries * sizeof(TBL));
218 cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
219 lens = emalloc(maxcols * sizeof(int));
220 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
221 for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
223 if (++coloff == maxcols) {
224 if (!(cols = realloc(cols, (u_int)maxcols +
225 DEFCOLS * sizeof(char *))) ||
226 !(lens = realloc(lens,
227 (u_int)maxcols + DEFCOLS * sizeof(int))))
229 memset((char *)lens + maxcols * sizeof(int),
230 0, DEFCOLS * sizeof(int));
233 t->list = emalloc(coloff * sizeof(char *));
234 t->len = emalloc(coloff * sizeof(int));
235 for (t->cols = coloff; --coloff >= 0;) {
236 t->list[coloff] = cols[coloff];
237 t->len[coloff] = strlen(cols[coloff]);
238 if (t->len[coloff] > lens[coloff])
239 lens[coloff] = t->len[coloff];
242 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
243 for (coloff = 0; coloff < t->cols - 1; ++coloff)
244 (void)printf("%s%*s", t->list[coloff],
245 lens[coloff] - t->len[coloff] + 2, " ");
246 (void)printf("%s\n", t->list[coloff]);
251 #define MAXLINELEN (LINE_MAX + 1)
259 char *p, buf[MAXLINELEN];
262 list = emalloc((maxentry = DEFNUM) * sizeof(char *));
263 while (fgets(buf, MAXLINELEN, fp)) {
264 for (p = buf; *p && isspace(*p); ++p);
267 if (!(p = strchr(p, '\n'))) {
268 warnx("line too long");
276 if (entries == maxentry) {
278 if (!(list = realloc(list,
279 (u_int)maxentry * sizeof(char *))))
282 list[entries++] = strdup(buf);
292 if (!(p = malloc(size)))
302 (void)fprintf(stderr,
303 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");