Annotation of src/usr.bin/column/column.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: column.c,v 1.4 1997/06/30 11:46:21 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1989, 1993, 1994\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
46: #endif
1.5 ! millert 47: static char rcsid[] = "$OpenBSD: column.c,v 1.4 1997/06/30 11:46:21 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/ioctl.h>
52:
53: #include <ctype.h>
54: #include <err.h>
55: #include <limits.h>
56: #include <stdio.h>
57: #include <stdlib.h>
58: #include <string.h>
59: #include <unistd.h>
60:
61: void c_columnate __P((void));
62: void *emalloc __P((int));
63: void input __P((FILE *));
64: void maketbl __P((void));
65: void print __P((void));
66: void r_columnate __P((void));
67: void usage __P((void));
68:
69: int termwidth = 80; /* default terminal width */
70:
71: int entries; /* number of records */
72: int eval; /* exit value */
73: int maxlength; /* longest record */
74: char **list; /* array of pointers to records */
75: char *separator = "\t "; /* field separator for table option */
76:
77: int
78: main(argc, argv)
79: int argc;
80: char **argv;
81: {
82: struct winsize win;
83: FILE *fp;
84: int ch, tflag, xflag;
85: char *p;
86:
87: if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
88: if (p = getenv("COLUMNS"))
89: termwidth = atoi(p);
90: } else
91: termwidth = win.ws_col;
92:
93: tflag = xflag = 0;
1.3 millert 94: while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
1.1 deraadt 95: switch(ch) {
96: case 'c':
97: termwidth = atoi(optarg);
98: break;
99: case 's':
100: separator = optarg;
101: break;
102: case 't':
103: tflag = 1;
104: break;
105: case 'x':
106: xflag = 1;
107: break;
108: case '?':
109: default:
110: usage();
111: }
112: argc -= optind;
113: argv += optind;
114:
115: if (!*argv)
116: input(stdin);
117: else for (; *argv; ++argv)
118: if (fp = fopen(*argv, "r")) {
119: input(fp);
120: (void)fclose(fp);
121: } else {
122: warn("%s", *argv);
123: eval = 1;
124: }
125:
126: if (!entries)
127: exit(eval);
128:
129: if (tflag)
130: maketbl();
131: else if (maxlength >= termwidth)
132: print();
133: else if (xflag)
134: c_columnate();
135: else
136: r_columnate();
137: exit(eval);
138: }
139:
140: #define TAB 8
141: void
142: c_columnate()
143: {
144: int chcnt, col, cnt, endcol, numcols;
145: char **lp;
146:
147: maxlength = (maxlength + TAB) & ~(TAB - 1);
148: numcols = termwidth / maxlength;
149: endcol = maxlength;
150: for (chcnt = col = 0, lp = list;; ++lp) {
151: chcnt += printf("%s", *lp);
152: if (!--entries)
153: break;
154: if (++col == numcols) {
155: chcnt = col = 0;
156: endcol = maxlength;
157: putchar('\n');
158: } else {
159: while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
160: (void)putchar('\t');
161: chcnt = cnt;
162: }
163: endcol += maxlength;
164: }
165: }
166: if (chcnt)
167: putchar('\n');
168: }
169:
170: void
171: r_columnate()
172: {
173: int base, chcnt, cnt, col, endcol, numcols, numrows, row;
174:
175: maxlength = (maxlength + TAB) & ~(TAB - 1);
176: numcols = termwidth / maxlength;
1.5 ! millert 177: if (numcols == 0)
! 178: numcols = 1;
1.1 deraadt 179: numrows = entries / numcols;
180: if (entries % numcols)
181: ++numrows;
182:
183: for (row = 0; row < numrows; ++row) {
184: endcol = maxlength;
185: for (base = row, chcnt = col = 0; col < numcols; ++col) {
186: chcnt += printf("%s", list[base]);
187: if ((base += numrows) >= entries)
188: break;
189: while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
190: (void)putchar('\t');
191: chcnt = cnt;
192: }
193: endcol += maxlength;
194: }
195: putchar('\n');
196: }
197: }
198:
199: void
200: print()
201: {
202: int cnt;
203: char **lp;
204:
205: for (cnt = entries, lp = list; cnt--; ++lp)
206: (void)printf("%s\n", *lp);
207: }
208:
209: typedef struct _tbl {
210: char **list;
211: int cols, *len;
212: } TBL;
213: #define DEFCOLS 25
214:
215: void
216: maketbl()
217: {
218: TBL *t;
219: int coloff, cnt;
220: char *p, **lp;
221: int *lens, maxcols;
222: TBL *tbl;
223: char **cols;
224:
225: t = tbl = emalloc(entries * sizeof(TBL));
226: cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
227: lens = emalloc(maxcols * sizeof(int));
228: for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
229: for (coloff = 0, p = *lp; cols[coloff] = strtok(p, separator);
230: p = NULL)
231: if (++coloff == maxcols) {
232: if (!(cols = realloc(cols, (u_int)maxcols +
233: DEFCOLS * sizeof(char *))) ||
234: !(lens = realloc(lens,
235: (u_int)maxcols + DEFCOLS * sizeof(int))))
236: err(1, NULL);
237: memset((char *)lens + maxcols * sizeof(int),
238: 0, DEFCOLS * sizeof(int));
239: maxcols += DEFCOLS;
240: }
241: t->list = emalloc(coloff * sizeof(char *));
242: t->len = emalloc(coloff * sizeof(int));
243: for (t->cols = coloff; --coloff >= 0;) {
244: t->list[coloff] = cols[coloff];
245: t->len[coloff] = strlen(cols[coloff]);
246: if (t->len[coloff] > lens[coloff])
247: lens[coloff] = t->len[coloff];
248: }
249: }
250: for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
251: for (coloff = 0; coloff < t->cols - 1; ++coloff)
252: (void)printf("%s%*s", t->list[coloff],
253: lens[coloff] - t->len[coloff] + 2, " ");
254: (void)printf("%s\n", t->list[coloff]);
255: }
256: }
257:
258: #define DEFNUM 1000
259: #define MAXLINELEN (LINE_MAX + 1)
260:
261: void
262: input(fp)
263: FILE *fp;
264: {
265: static int maxentry;
266: int len;
267: char *p, buf[MAXLINELEN];
268:
269: if (!list)
270: list = emalloc((maxentry = DEFNUM) * sizeof(char *));
271: while (fgets(buf, MAXLINELEN, fp)) {
272: for (p = buf; *p && isspace(*p); ++p);
273: if (!*p)
274: continue;
275: if (!(p = strchr(p, '\n'))) {
276: warnx("line too long");
277: eval = 1;
278: continue;
279: }
280: *p = '\0';
281: len = p - buf;
282: if (maxlength < len)
283: maxlength = len;
284: if (entries == maxentry) {
285: maxentry += DEFNUM;
286: if (!(list = realloc(list,
287: (u_int)maxentry * sizeof(char *))))
288: err(1, NULL);
289: }
290: list[entries++] = strdup(buf);
291: }
292: }
293:
294: void *
295: emalloc(size)
296: int size;
297: {
298: char *p;
299:
300: if (!(p = malloc(size)))
301: err(1, NULL);
302: memset(p, 0, size);
303: return (p);
304: }
305:
306: void
307: usage()
308: {
309:
310: (void)fprintf(stderr,
1.4 deraadt 311: "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
1.1 deraadt 312: exit(1);
313: }