* Copyright (c) 1989 The Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)column.c 5.1 (Berkeley) %G%";
int termwidth
= 80; /* default terminal width */
char **list
; /* array of pointers to records */
int entries
; /* number of records */
int maxlength
; /* longest record */
extern int errno
, optind
;
if (ioctl(1, TIOCGWINSZ
, &win
) == -1 || !win
.ws_col
) {
if (p
= getenv("COLUMNS"))
while ((ch
= getopt(argc
, argv
, "c:x")) != EOF
)
termwidth
= atoi(optarg
);
if (!(fp
= fopen(*argv
, "r"))) {
(void)fprintf(stderr
, "column: %s: %s\n",
if (maxlength
>= termwidth
)
register int chcnt
, col
, cnt
, numcols
;
maxlength
= (maxlength
+ TAB
) & ~(TAB
- 1);
numcols
= termwidth
/ maxlength
;
for (chcnt
= col
= 0, lp
= list
;; ++lp
) {
chcnt
+= printf("%s", *lp
);
while ((cnt
= (chcnt
+ TAB
& ~(TAB
- 1))) <= endcol
) {
register int base
, chcnt
, cnt
, col
;
int endcol
, numcols
, numrows
, row
;
maxlength
= (maxlength
+ TAB
) & ~(TAB
- 1);
numcols
= termwidth
/ maxlength
;
numrows
= entries
/ numcols
;
for (row
= 0; row
< numrows
; ++row
) {
for (base
= row
, chcnt
= col
= 0; col
< numcols
; ++col
) {
chcnt
+= printf("%s", list
[base
]);
if ((base
+= numrows
) >= entries
)
while ((cnt
= (chcnt
+ TAB
& ~(TAB
- 1))) <= endcol
) {
for (cnt
= entries
, lp
= list
; cnt
--; ++lp
)
#define MAXLINELEN (2048 + 1)
char buf
[MAXLINELEN
], *malloc(), *realloc();
!(list
= (char **)malloc((maxentry
= DEFNUM
) * sizeof(char *))))
while (fgets(buf
, MAXLINELEN
, fp
)) {
if (entries
== maxentry
) {
(char **)realloc(list
, maxentry
* sizeof(char *))))
if (p
= index(buf
, '\n')) {
list
[entries
++] = strdup(buf
);
(void)fprintf(stderr
, "column: out of memory.\n");
"usage: column [-x] [-c columns] [file ...]\n");