GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/column/column.c Lines: 0 138 0.0 %
Date: 2016-12-06 Branches: 0 107 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: column.c,v 1.23 2016/03/17 05:27:10 bentley Exp $	*/
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. Neither the name of the University nor the names of its contributors
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
44
void  c_columnate(void);
45
void *ereallocarray(void *, size_t, size_t);
46
void *ecalloc(size_t, size_t);
47
void  input(FILE *);
48
void  maketbl(void);
49
void  print(void);
50
void  r_columnate(void);
51
void  usage(void);
52
53
int termwidth;			/* 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
62
main(int argc, char *argv[])
63
{
64
	struct winsize win;
65
	FILE *fp;
66
	int ch, tflag, xflag;
67
	char *p;
68
	const char *errstr;
69
70
	termwidth = 0;
71
	if ((p = getenv("COLUMNS")) != NULL)
72
		termwidth = strtonum(p, 1, INT_MAX, NULL);
73
	if (termwidth == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
74
	    win.ws_col > 0)
75
		termwidth = win.ws_col;
76
	if (termwidth == 0)
77
		termwidth = 80;
78
79
	if (pledge("stdio rpath wpath cpath", NULL) == -1)
80
		err(1, "pledge");
81
82
	tflag = xflag = 0;
83
	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
84
		switch(ch) {
85
		case 'c':
86
			termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
87
			if (errstr != NULL)
88
				errx(1, "%s: %s", errstr, optarg);
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
106
	if (!*argv) {
107
		input(stdin);
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
			}
117
		}
118
	}
119
120
	if (pledge("stdio wpath cpath rpath", NULL) == -1)
121
		err(1, "pledge");
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
139
c_columnate(void)
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 {
156
			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
157
				(void)putchar('\t');
158
				chcnt = cnt;
159
			}
160
			endcol += maxlength;
161
		}
162
	}
163
	if (chcnt)
164
		putchar('\n');
165
}
166
167
void
168
r_columnate(void)
169
{
170
	int base, chcnt, cnt, col, endcol, numcols, numrows, row;
171
172
	maxlength = (maxlength + TAB) & ~(TAB - 1);
173
	numcols = termwidth / maxlength;
174
	if (numcols == 0)
175
		numcols = 1;
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;
186
			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
187
				(void)putchar('\t');
188
				chcnt = cnt;
189
			}
190
			endcol += maxlength;
191
		}
192
		putchar('\n');
193
	}
194
}
195
196
void
197
print(void)
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
213
maketbl(void)
214
{
215
	TBL *t;
216
	int coloff, cnt;
217
	char *p, **lp;
218
	int *lens, maxcols = DEFCOLS;
219
	TBL *tbl;
220
	char **cols;
221
222
	t = tbl = ecalloc(entries, sizeof(TBL));
223
	cols = ereallocarray(NULL, maxcols, sizeof(char *));
224
	lens = ecalloc(maxcols, sizeof(int));
225
	for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
226
		for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
227
		    p = NULL)
228
			if (++coloff == maxcols) {
229
				maxcols += DEFCOLS;
230
				cols = ereallocarray(cols, maxcols,
231
				    sizeof(char *));
232
				lens = ereallocarray(lens, maxcols,
233
				    sizeof(int));
234
				memset(lens + coloff, 0, DEFCOLS * sizeof(int));
235
			}
236
		if (coloff == 0)
237
			continue;
238
		t->list = ecalloc(coloff, sizeof(char *));
239
		t->len = ecalloc(coloff, sizeof(int));
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) {
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
		}
254
	}
255
	free(tbl);
256
	free(lens);
257
	free(cols);
258
}
259
260
#define	DEFNUM		1000
261
#define	MAXLINELEN	(LINE_MAX + 1)
262
263
void
264
input(FILE *fp)
265
{
266
	static size_t maxentry = DEFNUM;
267
	int len;
268
	char *p, buf[MAXLINELEN];
269
270
	if (!list)
271
		list = ecalloc(maxentry, sizeof(char *));
272
	while (fgets(buf, MAXLINELEN, fp)) {
273
		for (p = buf; isspace((unsigned char)*p); ++p);
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) {
286
			maxentry += DEFNUM;
287
			list = ereallocarray(list, maxentry, sizeof(char *));
288
			memset(list + entries, 0, DEFNUM * sizeof(char *));
289
		}
290
		if (!(list[entries++] = strdup(buf)))
291
			err(1, NULL);
292
	}
293
}
294
295
void *
296
ereallocarray(void *oldp, size_t sz1, size_t sz2)
297
{
298
	void *p;
299
300
	if (!(p = reallocarray(oldp, sz1, sz2)))
301
		err(1, NULL);
302
	return (p);
303
}
304
305
void *
306
ecalloc(size_t sz1, size_t sz2)
307
{
308
	void *p;
309
310
	if (!(p = calloc(sz1, sz2)))
311
		err(1, NULL);
312
	return (p);
313
}
314
315
void
316
usage(void)
317
{
318
319
	(void)fprintf(stderr,
320
	    "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
321
	exit(1);
322
}