Annotation of src/usr.bin/column/column.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: column.c,v 1.19 2014/05/22 19:50:34 millert 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: #include <sys/types.h>
34: #include <sys/ioctl.h>
35:
36: #include <ctype.h>
37: #include <err.h>
38: #include <limits.h>
39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
42: #include <unistd.h>
43:
1.7 millert 44: void c_columnate(void);
1.18 espie 45: void *ereallocarray(void *, size_t, size_t);
46: void *ecalloc(size_t, size_t);
1.7 millert 47: void input(FILE *);
48: void maketbl(void);
49: void print(void);
50: void r_columnate(void);
51: void usage(void);
1.1 deraadt 52:
53: int termwidth = 80; /* default terminal width */
54:
55: int entries; /* number of records */
56: int eval; /* exit value */
57: int maxlength; /* longest record */
58: char **list; /* array of pointers to records */
59: char *separator = "\t "; /* field separator for table option */
60:
61: int
1.9 deraadt 62: main(int argc, char *argv[])
1.1 deraadt 63: {
64: struct winsize win;
65: FILE *fp;
66: int ch, tflag, xflag;
67: char *p;
1.13 jdixon 68: const char *errstr;
1.1 deraadt 69:
70: if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
1.13 jdixon 71: if ((p = getenv("COLUMNS")) && *p != '\0') {
72: termwidth = strtonum(p, 1, INT_MAX, &errstr);
73: if (errstr != NULL)
74: errx(1, "%s: %s", errstr, p);
75: }
1.1 deraadt 76: } else
77: termwidth = win.ws_col;
78:
1.20 ! deraadt 79: if (tame("stdio rpath", NULL) == -1)
! 80: err(1, "tame");
! 81:
1.1 deraadt 82: tflag = xflag = 0;
1.3 millert 83: while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
1.1 deraadt 84: switch(ch) {
85: case 'c':
1.13 jdixon 86: termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
87: if (errstr != NULL)
88: errx(1, "%s: %s", errstr, optarg);
1.1 deraadt 89: break;
90: case 's':
91: separator = optarg;
92: break;
93: case 't':
94: tflag = 1;
95: break;
96: case 'x':
97: xflag = 1;
98: break;
99: case '?':
100: default:
101: usage();
102: }
103: argc -= optind;
104: argv += optind;
105:
1.20 ! deraadt 106: if (!*argv) {
1.1 deraadt 107: input(stdin);
1.20 ! deraadt 108: } else {
! 109: for (; *argv; ++argv) {
! 110: if ((fp = fopen(*argv, "r"))) {
! 111: input(fp);
! 112: (void)fclose(fp);
! 113: } else {
! 114: warn("%s", *argv);
! 115: eval = 1;
! 116: }
1.1 deraadt 117: }
1.20 ! deraadt 118: }
! 119: if (tame("stdio", NULL) == -1)
! 120: err(1, "tame");
1.1 deraadt 121:
122: if (!entries)
123: exit(eval);
124:
125: if (tflag)
126: maketbl();
127: else if (maxlength >= termwidth)
128: print();
129: else if (xflag)
130: c_columnate();
131: else
132: r_columnate();
133: exit(eval);
134: }
135:
136: #define TAB 8
137: void
1.9 deraadt 138: c_columnate(void)
1.1 deraadt 139: {
140: int chcnt, col, cnt, endcol, numcols;
141: char **lp;
142:
143: maxlength = (maxlength + TAB) & ~(TAB - 1);
144: numcols = termwidth / maxlength;
145: endcol = maxlength;
146: for (chcnt = col = 0, lp = list;; ++lp) {
147: chcnt += printf("%s", *lp);
148: if (!--entries)
149: break;
150: if (++col == numcols) {
151: chcnt = col = 0;
152: endcol = maxlength;
153: putchar('\n');
154: } else {
1.6 deraadt 155: while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1 deraadt 156: (void)putchar('\t');
157: chcnt = cnt;
158: }
159: endcol += maxlength;
160: }
161: }
162: if (chcnt)
163: putchar('\n');
164: }
165:
166: void
1.9 deraadt 167: r_columnate(void)
1.1 deraadt 168: {
169: int base, chcnt, cnt, col, endcol, numcols, numrows, row;
170:
171: maxlength = (maxlength + TAB) & ~(TAB - 1);
172: numcols = termwidth / maxlength;
1.5 millert 173: if (numcols == 0)
174: numcols = 1;
1.1 deraadt 175: numrows = entries / numcols;
176: if (entries % numcols)
177: ++numrows;
178:
179: for (row = 0; row < numrows; ++row) {
180: endcol = maxlength;
181: for (base = row, chcnt = col = 0; col < numcols; ++col) {
182: chcnt += printf("%s", list[base]);
183: if ((base += numrows) >= entries)
184: break;
1.6 deraadt 185: while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1 deraadt 186: (void)putchar('\t');
187: chcnt = cnt;
188: }
189: endcol += maxlength;
190: }
191: putchar('\n');
192: }
193: }
194:
195: void
1.9 deraadt 196: print(void)
1.1 deraadt 197: {
198: int cnt;
199: char **lp;
200:
201: for (cnt = entries, lp = list; cnt--; ++lp)
202: (void)printf("%s\n", *lp);
203: }
204:
205: typedef struct _tbl {
206: char **list;
207: int cols, *len;
208: } TBL;
209: #define DEFCOLS 25
210:
211: void
1.9 deraadt 212: maketbl(void)
1.1 deraadt 213: {
214: TBL *t;
215: int coloff, cnt;
216: char *p, **lp;
1.19 millert 217: int *lens, maxcols = DEFCOLS;
1.1 deraadt 218: TBL *tbl;
1.14 millert 219: char **cols;
1.1 deraadt 220:
1.18 espie 221: t = tbl = ecalloc(entries, sizeof(TBL));
1.19 millert 222: cols = ereallocarray(NULL, maxcols, sizeof(char *));
1.18 espie 223: lens = ecalloc(maxcols, sizeof(int));
1.1 deraadt 224: for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
1.6 deraadt 225: for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
1.1 deraadt 226: p = NULL)
227: if (++coloff == maxcols) {
228: maxcols += DEFCOLS;
1.18 espie 229: cols = ereallocarray(cols, maxcols,
230: sizeof(char *));
231: lens = ereallocarray(lens, maxcols,
232: sizeof(int));
1.14 millert 233: memset(lens + coloff, 0, DEFCOLS * sizeof(int));
1.1 deraadt 234: }
1.14 millert 235: if (coloff == 0)
236: continue;
1.18 espie 237: t->list = ecalloc(coloff, sizeof(char *));
238: t->len = ecalloc(coloff, sizeof(int));
1.1 deraadt 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) {
1.14 millert 247: if (t->cols > 0) {
248: for (coloff = 0; coloff < t->cols - 1; ++coloff)
249: (void)printf("%s%*s", t->list[coloff],
250: lens[coloff] - t->len[coloff] + 2, " ");
251: (void)printf("%s\n", t->list[coloff]);
252: }
1.1 deraadt 253: }
1.19 millert 254: free(tbl);
1.17 jsg 255: free(lens);
256: free(cols);
1.1 deraadt 257: }
258:
259: #define DEFNUM 1000
260: #define MAXLINELEN (LINE_MAX + 1)
261:
262: void
1.9 deraadt 263: input(FILE *fp)
1.1 deraadt 264: {
1.19 millert 265: static size_t maxentry = DEFNUM;
1.1 deraadt 266: int len;
267: char *p, buf[MAXLINELEN];
268:
269: if (!list)
1.19 millert 270: list = ecalloc(maxentry, sizeof(char *));
1.1 deraadt 271: while (fgets(buf, MAXLINELEN, fp)) {
1.16 deraadt 272: for (p = buf; isspace((unsigned char)*p); ++p);
1.1 deraadt 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) {
1.14 millert 285: maxentry += DEFNUM;
1.18 espie 286: list = ereallocarray(list, maxentry, sizeof(char *));
1.19 millert 287: memset(list + entries, 0, DEFNUM * sizeof(char *));
1.1 deraadt 288: }
1.11 otto 289: if (!(list[entries++] = strdup(buf)))
290: err(1, NULL);
1.1 deraadt 291: }
292: }
293:
294: void *
1.18 espie 295: ereallocarray(void *oldp, size_t sz1, size_t sz2)
1.1 deraadt 296: {
1.11 otto 297: void *p;
1.1 deraadt 298:
1.18 espie 299: if (!(p = reallocarray(oldp, sz1, sz2)))
1.1 deraadt 300: err(1, NULL);
1.14 millert 301: return (p);
302: }
303:
304: void *
1.18 espie 305: ecalloc(size_t sz1, size_t sz2)
1.14 millert 306: {
307: void *p;
308:
1.18 espie 309: if (!(p = calloc(sz1, sz2)))
1.14 millert 310: err(1, NULL);
1.1 deraadt 311: return (p);
312: }
313:
314: void
1.9 deraadt 315: usage(void)
1.1 deraadt 316: {
317:
318: (void)fprintf(stderr,
1.4 deraadt 319: "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
1.1 deraadt 320: exit(1);
321: }