1 /* $NetBSD: column.c,v 1.20 2008/05/24 14:45:46 mlelstv Exp $ */
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: column.c,v 1.20 2008/05/24 14:45:46 mlelstv Exp $");
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
59 #define TABROUND(l) (((l) + TAB) & ~(TAB - 1))
61 static void c_columnate(void);
62 static void input(FILE *);
63 static void maketbl(void);
64 static void print(void);
65 static void r_columnate(void);
66 static void usage(void) __dead
;
68 static int termwidth
= 80; /* default terminal width */
70 static int entries
; /* number of records */
71 static int eval
; /* exit value */
72 static int maxlength
; /* longest record */
73 static char **list
; /* array of pointers to records */
74 static const char *separator
= "\t "; /* field separator for table option */
77 main(int argc
, char **argv
)
86 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) == -1 || !win
.ws_col
) {
87 if ((p
= getenv("COLUMNS")) != NULL
)
90 termwidth
= win
.ws_col
;
93 while ((ch
= getopt(argc
, argv
, "c:s:tx")) != -1)
96 termwidth
= atoi(optarg
);
116 else for (; *argv
; ++argv
)
117 if ((fp
= fopen(*argv
, "r")) != NULL
) {
121 warn("Cannot open `%s'", *argv
);
128 maxlength
= TABROUND(maxlength
);
131 else if (maxlength
>= termwidth
)
143 int chcnt
, col
, cnt
, endcol
, numcols
;
146 numcols
= termwidth
/ maxlength
;
148 for (chcnt
= col
= 0, lp
= list
;; ++lp
) {
149 chcnt
+= printf("%s", *lp
);
152 if (++col
== numcols
) {
157 while ((cnt
= TABROUND(chcnt
)) <= endcol
) {
171 int base
, chcnt
, cnt
, col
, endcol
, numcols
, numrows
, row
;
173 numcols
= termwidth
/ maxlength
;
174 numrows
= entries
/ numcols
;
175 if (entries
% numcols
)
178 for (row
= 0; row
< numrows
; ++row
) {
180 for (base
= row
, chcnt
= col
= 0; col
< numcols
; ++col
) {
181 chcnt
+= printf("%s", list
[base
]);
182 if ((base
+= numrows
) >= entries
)
184 while ((cnt
= TABROUND(chcnt
)) <= endcol
) {
200 for (cnt
= entries
, lp
= list
; cnt
--; ++lp
)
201 (void)printf("%s\n", *lp
);
204 typedef struct _tbl
{
216 int *lens
, *nlens
, maxcols
;
218 char **cols
, **ncols
;
220 t
= tbl
= ecalloc(entries
, sizeof(*t
));
221 cols
= ecalloc((maxcols
= DEFCOLS
), sizeof(*cols
));
222 lens
= ecalloc(maxcols
, sizeof(*lens
));
223 for (cnt
= 0, lp
= list
; cnt
< entries
; ++cnt
, ++lp
, ++t
) {
224 for (coloff
= 0, p
= *lp
;
225 (cols
[coloff
] = strtok(p
, separator
)) != NULL
; p
= NULL
)
226 if (++coloff
== maxcols
) {
227 ncols
= erealloc(cols
, (maxcols
+
228 DEFCOLS
) * sizeof(*ncols
));
229 nlens
= erealloc(lens
, (maxcols
+
230 DEFCOLS
) * sizeof(*nlens
));
233 (void)memset(cols
+ maxcols
, 0,
234 DEFCOLS
* sizeof(*cols
));
235 (void)memset(lens
+ maxcols
, 0,
236 DEFCOLS
* sizeof(*lens
));
239 t
->list
= ecalloc(coloff
, sizeof(*(t
->list
)));
240 t
->len
= ecalloc(coloff
, sizeof(*(t
->len
)));
241 for (t
->cols
= coloff
; --coloff
>= 0;) {
242 t
->list
[coloff
] = cols
[coloff
];
243 t
->len
[coloff
] = strlen(cols
[coloff
]);
244 if (t
->len
[coloff
] > lens
[coloff
])
245 lens
[coloff
] = t
->len
[coloff
];
248 for (cnt
= 0, t
= tbl
; cnt
< entries
; ++cnt
, ++t
) {
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
]);
271 list
= ecalloc((maxentry
= DEFNUM
), sizeof(*list
));
272 while ((buf
= fgetln(fp
, &blen
)) != NULL
) {
273 buf
= estrndup(buf
, blen
);
274 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
);
279 if (!(p
= strchr(p
, '\n'))) {
280 warnx("line too long");
289 if (entries
== maxentry
) {
290 n
= erealloc(list
, (maxentry
+ DEFNUM
) * sizeof(*n
));
291 (void)memset(n
+ maxentry
, 0, sizeof(*n
) * DEFNUM
);
295 list
[entries
++] = buf
;
303 (void)fprintf(stderr
,
304 "Usage: %s [-tx] [-c columns] [-s sep] [file ...]\n",