2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: head/usr.bin/column/column.c 292864 2015-12-29 11:24:41Z uqs $
31 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
32 * @(#)column.c 8.4 (Berkeley) 5/4/95
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
51 static void c_columnate(void);
52 static void input(FILE *);
53 static void maketbl(void);
54 static void print(void);
55 static void r_columnate(void);
56 static void usage(void);
57 static int width(const wchar_t *);
59 static int termwidth
= 80; /* default terminal width */
61 static int entries
; /* number of records */
62 static int eval
; /* exit value */
63 static int maxlength
; /* longest record */
64 static wchar_t **list
; /* array of pointers to records */
65 static const wchar_t *separator
= L
"\t "; /* field separator for table option */
68 main(int argc
, char **argv
)
78 setlocale(LC_ALL
, "");
80 if (ioctl(1, TIOCGWINSZ
, &win
) == -1 || !win
.ws_col
) {
81 if ((p
= getenv("COLUMNS")))
84 termwidth
= win
.ws_col
;
87 while ((ch
= getopt(argc
, argv
, "c:s:tx")) != -1)
90 termwidth
= atoi(optarg
);
94 seplen
= mbsrtowcs(NULL
, &src
, 0, NULL
);
95 if (seplen
== (size_t)-1)
96 err(1, "bad separator");
97 newsep
= malloc((seplen
+ 1) * sizeof(wchar_t));
100 mbsrtowcs(newsep
, &src
, seplen
+ 1, NULL
);
118 else for (; *argv
; ++argv
)
119 if ((fp
= fopen(*argv
, "r"))) {
130 maxlength
= roundup(maxlength
+ 1, TAB
);
133 else if (maxlength
>= termwidth
)
145 int chcnt
, col
, cnt
, endcol
, numcols
;
148 numcols
= termwidth
/ maxlength
;
150 for (chcnt
= col
= 0, lp
= list
;; ++lp
) {
151 wprintf(L
"%ls", *lp
);
155 if (++col
== numcols
) {
160 while ((cnt
= roundup(chcnt
+ 1, TAB
)) <= endcol
) {
161 (void)putwchar('\t');
174 int base
, chcnt
, cnt
, col
, endcol
, numcols
, numrows
, row
;
176 numcols
= termwidth
/ maxlength
;
177 numrows
= entries
/ numcols
;
178 if (entries
% numcols
)
181 for (row
= 0; row
< numrows
; ++row
) {
183 for (base
= row
, chcnt
= col
= 0; col
< numcols
; ++col
) {
184 wprintf(L
"%ls", list
[base
]);
185 chcnt
+= width(list
[base
]);
186 if ((base
+= numrows
) >= entries
)
188 while ((cnt
= roundup(chcnt
+ 1, TAB
)) <= endcol
) {
189 (void)putwchar('\t');
204 for (cnt
= entries
, lp
= list
; cnt
--; ++lp
)
205 (void)wprintf(L
"%ls\n", *lp
);
208 typedef struct _tbl
{
225 if ((t
= tbl
= calloc(entries
, sizeof(TBL
))) == NULL
)
227 if ((cols
= calloc((maxcols
= DEFCOLS
), sizeof(*cols
))) == NULL
)
229 if ((lens
= calloc(maxcols
, sizeof(int))) == NULL
)
231 for (cnt
= 0, lp
= list
; cnt
< entries
; ++cnt
, ++lp
, ++t
) {
232 for (coloff
= 0, p
= *lp
;
233 (cols
[coloff
] = wcstok(p
, separator
, &last
));
235 if (++coloff
== maxcols
) {
236 if (!(cols
= realloc(cols
, ((u_int
)maxcols
+
237 DEFCOLS
) * sizeof(wchar_t *))) ||
238 !(lens
= realloc(lens
,
239 ((u_int
)maxcols
+ DEFCOLS
) * sizeof(int))))
241 memset((char *)lens
+ maxcols
* sizeof(int),
242 0, DEFCOLS
* sizeof(int));
245 if ((t
->list
= calloc(coloff
, sizeof(*t
->list
))) == NULL
)
247 if ((t
->len
= calloc(coloff
, sizeof(int))) == NULL
)
249 for (t
->cols
= coloff
; --coloff
>= 0;) {
250 t
->list
[coloff
] = cols
[coloff
];
251 t
->len
[coloff
] = width(cols
[coloff
]);
252 if (t
->len
[coloff
] > lens
[coloff
])
253 lens
[coloff
] = t
->len
[coloff
];
256 for (cnt
= 0, t
= tbl
; cnt
< entries
; ++cnt
, ++t
) {
257 for (coloff
= 0; coloff
< t
->cols
- 1; ++coloff
)
258 (void)wprintf(L
"%ls%*ls", t
->list
[coloff
],
259 lens
[coloff
] - t
->len
[coloff
] + 2, L
" ");
260 (void)wprintf(L
"%ls\n", t
->list
[coloff
]);
265 #define MAXLINELEN (LINE_MAX + 1)
272 wchar_t *p
, buf
[MAXLINELEN
];
275 if ((list
= calloc((maxentry
= DEFNUM
), sizeof(*list
))) ==
278 while (fgetws(buf
, MAXLINELEN
, fp
)) {
279 for (p
= buf
; *p
&& iswspace(*p
); ++p
);
282 if (!(p
= wcschr(p
, L
'\n'))) {
283 warnx("line too long");
291 if (entries
== maxentry
) {
293 if (!(list
= realloc(list
,
294 (u_int
)maxentry
* sizeof(*list
))))
297 list
[entries
] = malloc((wcslen(buf
) + 1) * sizeof(wchar_t));
298 if (list
[entries
] == NULL
)
300 wcscpy(list
[entries
], buf
);
305 /* Like wcswidth(), but ignores non-printing characters. */
307 width(const wchar_t *wcs
)
311 for (w
= 0; *wcs
!= L
'\0'; wcs
++)
312 if ((cw
= wcwidth(*wcs
)) > 0)
321 (void)fprintf(stderr
,
322 "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");