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