Annotation of src/usr.bin/column/column.c, Revision 1.22
1.22 ! mmcc 1: /* $OpenBSD: column.c,v 1.21 2015/10/09 01:37:07 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.
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.21 deraadt 79: if (pledge("stdio rpath", NULL) == -1)
80: err(1, "pledge");
1.20 deraadt 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: }
1.22 ! mmcc 119:
1.21 deraadt 120: if (pledge("stdio", NULL) == -1)
121: err(1, "pledge");
1.1 deraadt 122:
123: if (!entries)
124: exit(eval);
125:
126: if (tflag)
127: maketbl();
128: else if (maxlength >= termwidth)
129: print();
130: else if (xflag)
131: c_columnate();
132: else
133: r_columnate();
134: exit(eval);
135: }
136:
137: #define TAB 8
138: void
1.9 deraadt 139: c_columnate(void)
1.1 deraadt 140: {
141: int chcnt, col, cnt, endcol, numcols;
142: char **lp;
143:
144: maxlength = (maxlength + TAB) & ~(TAB - 1);
145: numcols = termwidth / maxlength;
146: endcol = maxlength;
147: for (chcnt = col = 0, lp = list;; ++lp) {
148: chcnt += printf("%s", *lp);
149: if (!--entries)
150: break;
151: if (++col == numcols) {
152: chcnt = col = 0;
153: endcol = maxlength;
154: putchar('\n');
155: } else {
1.6 deraadt 156: while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1 deraadt 157: (void)putchar('\t');
158: chcnt = cnt;
159: }
160: endcol += maxlength;
161: }
162: }
163: if (chcnt)
164: putchar('\n');
165: }
166:
167: void
1.9 deraadt 168: r_columnate(void)
1.1 deraadt 169: {
170: int base, chcnt, cnt, col, endcol, numcols, numrows, row;
171:
172: maxlength = (maxlength + TAB) & ~(TAB - 1);
173: numcols = termwidth / maxlength;
1.5 millert 174: if (numcols == 0)
175: numcols = 1;
1.1 deraadt 176: numrows = entries / numcols;
177: if (entries % numcols)
178: ++numrows;
179:
180: for (row = 0; row < numrows; ++row) {
181: endcol = maxlength;
182: for (base = row, chcnt = col = 0; col < numcols; ++col) {
183: chcnt += printf("%s", list[base]);
184: if ((base += numrows) >= entries)
185: break;
1.6 deraadt 186: while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1 deraadt 187: (void)putchar('\t');
188: chcnt = cnt;
189: }
190: endcol += maxlength;
191: }
192: putchar('\n');
193: }
194: }
195:
196: void
1.9 deraadt 197: print(void)
1.1 deraadt 198: {
199: int cnt;
200: char **lp;
201:
202: for (cnt = entries, lp = list; cnt--; ++lp)
203: (void)printf("%s\n", *lp);
204: }
205:
206: typedef struct _tbl {
207: char **list;
208: int cols, *len;
209: } TBL;
210: #define DEFCOLS 25
211:
212: void
1.9 deraadt 213: maketbl(void)
1.1 deraadt 214: {
215: TBL *t;
216: int coloff, cnt;
217: char *p, **lp;
1.19 millert 218: int *lens, maxcols = DEFCOLS;
1.1 deraadt 219: TBL *tbl;
1.14 millert 220: char **cols;
1.1 deraadt 221:
1.18 espie 222: t = tbl = ecalloc(entries, sizeof(TBL));
1.19 millert 223: cols = ereallocarray(NULL, maxcols, sizeof(char *));
1.18 espie 224: lens = ecalloc(maxcols, sizeof(int));
1.1 deraadt 225: for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
1.6 deraadt 226: for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
1.1 deraadt 227: p = NULL)
228: if (++coloff == maxcols) {
229: maxcols += DEFCOLS;
1.18 espie 230: cols = ereallocarray(cols, maxcols,
231: sizeof(char *));
232: lens = ereallocarray(lens, maxcols,
233: sizeof(int));
1.14 millert 234: memset(lens + coloff, 0, DEFCOLS * sizeof(int));
1.1 deraadt 235: }
1.14 millert 236: if (coloff == 0)
237: continue;
1.18 espie 238: t->list = ecalloc(coloff, sizeof(char *));
239: t->len = ecalloc(coloff, sizeof(int));
1.1 deraadt 240: for (t->cols = coloff; --coloff >= 0;) {
241: t->list[coloff] = cols[coloff];
242: t->len[coloff] = strlen(cols[coloff]);
243: if (t->len[coloff] > lens[coloff])
244: lens[coloff] = t->len[coloff];
245: }
246: }
247: for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
1.14 millert 248: if (t->cols > 0) {
249: for (coloff = 0; coloff < t->cols - 1; ++coloff)
250: (void)printf("%s%*s", t->list[coloff],
251: lens[coloff] - t->len[coloff] + 2, " ");
252: (void)printf("%s\n", t->list[coloff]);
253: }
1.1 deraadt 254: }
1.19 millert 255: free(tbl);
1.17 jsg 256: free(lens);
257: free(cols);
1.1 deraadt 258: }
259:
260: #define DEFNUM 1000
261: #define MAXLINELEN (LINE_MAX + 1)
262:
263: void
1.9 deraadt 264: input(FILE *fp)
1.1 deraadt 265: {
1.19 millert 266: static size_t maxentry = DEFNUM;
1.1 deraadt 267: int len;
268: char *p, buf[MAXLINELEN];
269:
270: if (!list)
1.19 millert 271: list = ecalloc(maxentry, sizeof(char *));
1.1 deraadt 272: while (fgets(buf, MAXLINELEN, fp)) {
1.16 deraadt 273: for (p = buf; isspace((unsigned char)*p); ++p);
1.1 deraadt 274: if (!*p)
275: continue;
276: if (!(p = strchr(p, '\n'))) {
277: warnx("line too long");
278: eval = 1;
279: continue;
280: }
281: *p = '\0';
282: len = p - buf;
283: if (maxlength < len)
284: maxlength = len;
285: if (entries == maxentry) {
1.14 millert 286: maxentry += DEFNUM;
1.18 espie 287: list = ereallocarray(list, maxentry, sizeof(char *));
1.19 millert 288: memset(list + entries, 0, DEFNUM * sizeof(char *));
1.1 deraadt 289: }
1.11 otto 290: if (!(list[entries++] = strdup(buf)))
291: err(1, NULL);
1.1 deraadt 292: }
293: }
294:
295: void *
1.18 espie 296: ereallocarray(void *oldp, size_t sz1, size_t sz2)
1.1 deraadt 297: {
1.11 otto 298: void *p;
1.1 deraadt 299:
1.18 espie 300: if (!(p = reallocarray(oldp, sz1, sz2)))
1.1 deraadt 301: err(1, NULL);
1.14 millert 302: return (p);
303: }
304:
305: void *
1.18 espie 306: ecalloc(size_t sz1, size_t sz2)
1.14 millert 307: {
308: void *p;
309:
1.18 espie 310: if (!(p = calloc(sz1, sz2)))
1.14 millert 311: err(1, NULL);
1.1 deraadt 312: return (p);
313: }
314:
315: void
1.9 deraadt 316: usage(void)
1.1 deraadt 317: {
318:
319: (void)fprintf(stderr,
1.4 deraadt 320: "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
1.1 deraadt 321: exit(1);
322: }