[BACK]Return to column.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / column

Annotation of src/usr.bin/column/column.c, Revision 1.8

1.8     ! millert     1: /*     $OpenBSD: column.c,v 1.7 2002/02/16 21:27:45 millert 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: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1989, 1993, 1994\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)column.c   8.4 (Berkeley) 5/4/95";
                     42: #endif
1.8     ! millert    43: static char rcsid[] = "$OpenBSD: column.c,v 1.7 2002/02/16 21:27:45 millert Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <sys/types.h>
                     47: #include <sys/ioctl.h>
                     48:
                     49: #include <ctype.h>
                     50: #include <err.h>
                     51: #include <limits.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55: #include <unistd.h>
                     56:
1.7       millert    57: void  c_columnate(void);
                     58: void *emalloc(int);
                     59: void  input(FILE *);
                     60: void  maketbl(void);
                     61: void  print(void);
                     62: void  r_columnate(void);
                     63: void  usage(void);
1.1       deraadt    64:
                     65: int termwidth = 80;            /* default terminal width */
                     66:
                     67: int entries;                   /* number of records */
                     68: int eval;                      /* exit value */
                     69: int maxlength;                 /* longest record */
                     70: char **list;                   /* array of pointers to records */
                     71: char *separator = "\t ";       /* field separator for table option */
                     72:
                     73: int
                     74: main(argc, argv)
                     75:        int argc;
                     76:        char **argv;
                     77: {
                     78:        struct winsize win;
                     79:        FILE *fp;
                     80:        int ch, tflag, xflag;
                     81:        char *p;
                     82:
                     83:        if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
1.6       deraadt    84:                if ((p = getenv("COLUMNS")))
1.1       deraadt    85:                        termwidth = atoi(p);
                     86:        } else
                     87:                termwidth = win.ws_col;
                     88:
                     89:        tflag = xflag = 0;
1.3       millert    90:        while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
1.1       deraadt    91:                switch(ch) {
                     92:                case 'c':
                     93:                        termwidth = atoi(optarg);
                     94:                        break;
                     95:                case 's':
                     96:                        separator = optarg;
                     97:                        break;
                     98:                case 't':
                     99:                        tflag = 1;
                    100:                        break;
                    101:                case 'x':
                    102:                        xflag = 1;
                    103:                        break;
                    104:                case '?':
                    105:                default:
                    106:                        usage();
                    107:                }
                    108:        argc -= optind;
                    109:        argv += optind;
                    110:
                    111:        if (!*argv)
                    112:                input(stdin);
                    113:        else for (; *argv; ++argv)
1.6       deraadt   114:                if ((fp = fopen(*argv, "r"))) {
1.1       deraadt   115:                        input(fp);
                    116:                        (void)fclose(fp);
                    117:                } else {
                    118:                        warn("%s", *argv);
                    119:                        eval = 1;
                    120:                }
                    121:
                    122:        if (!entries)
                    123:                exit(eval);
                    124:
                    125:        if (tflag)
                    126:                maketbl();
                    127:        else if (maxlength >= termwidth)
                    128:                print();
                    129:        else if (xflag)
                    130:                c_columnate();
                    131:        else
                    132:                r_columnate();
                    133:        exit(eval);
                    134: }
                    135:
                    136: #define        TAB     8
                    137: void
                    138: c_columnate()
                    139: {
                    140:        int chcnt, col, cnt, endcol, numcols;
                    141:        char **lp;
                    142:
                    143:        maxlength = (maxlength + TAB) & ~(TAB - 1);
                    144:        numcols = termwidth / maxlength;
                    145:        endcol = maxlength;
                    146:        for (chcnt = col = 0, lp = list;; ++lp) {
                    147:                chcnt += printf("%s", *lp);
                    148:                if (!--entries)
                    149:                        break;
                    150:                if (++col == numcols) {
                    151:                        chcnt = col = 0;
                    152:                        endcol = maxlength;
                    153:                        putchar('\n');
                    154:                } else {
1.6       deraadt   155:                        while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1       deraadt   156:                                (void)putchar('\t');
                    157:                                chcnt = cnt;
                    158:                        }
                    159:                        endcol += maxlength;
                    160:                }
                    161:        }
                    162:        if (chcnt)
                    163:                putchar('\n');
                    164: }
                    165:
                    166: void
                    167: r_columnate()
                    168: {
                    169:        int base, chcnt, cnt, col, endcol, numcols, numrows, row;
                    170:
                    171:        maxlength = (maxlength + TAB) & ~(TAB - 1);
                    172:        numcols = termwidth / maxlength;
1.5       millert   173:        if (numcols == 0)
                    174:                numcols = 1;
1.1       deraadt   175:        numrows = entries / numcols;
                    176:        if (entries % numcols)
                    177:                ++numrows;
                    178:
                    179:        for (row = 0; row < numrows; ++row) {
                    180:                endcol = maxlength;
                    181:                for (base = row, chcnt = col = 0; col < numcols; ++col) {
                    182:                        chcnt += printf("%s", list[base]);
                    183:                        if ((base += numrows) >= entries)
                    184:                                break;
1.6       deraadt   185:                        while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1       deraadt   186:                                (void)putchar('\t');
                    187:                                chcnt = cnt;
                    188:                        }
                    189:                        endcol += maxlength;
                    190:                }
                    191:                putchar('\n');
                    192:        }
                    193: }
                    194:
                    195: void
                    196: print()
                    197: {
                    198:        int cnt;
                    199:        char **lp;
                    200:
                    201:        for (cnt = entries, lp = list; cnt--; ++lp)
                    202:                (void)printf("%s\n", *lp);
                    203: }
                    204:
                    205: typedef struct _tbl {
                    206:        char **list;
                    207:        int cols, *len;
                    208: } TBL;
                    209: #define        DEFCOLS 25
                    210:
                    211: void
                    212: maketbl()
                    213: {
                    214:        TBL *t;
                    215:        int coloff, cnt;
                    216:        char *p, **lp;
                    217:        int *lens, maxcols;
                    218:        TBL *tbl;
                    219:        char **cols;
                    220:
                    221:        t = tbl = emalloc(entries * sizeof(TBL));
                    222:        cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
                    223:        lens = emalloc(maxcols * sizeof(int));
                    224:        for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
1.6       deraadt   225:                for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
1.1       deraadt   226:                    p = NULL)
                    227:                        if (++coloff == maxcols) {
                    228:                                if (!(cols = realloc(cols, (u_int)maxcols +
                    229:                                    DEFCOLS * sizeof(char *))) ||
                    230:                                    !(lens = realloc(lens,
                    231:                                    (u_int)maxcols + DEFCOLS * sizeof(int))))
                    232:                                        err(1, NULL);
                    233:                                memset((char *)lens + maxcols * sizeof(int),
                    234:                                    0, DEFCOLS * sizeof(int));
                    235:                                maxcols += DEFCOLS;
                    236:                        }
                    237:                t->list = emalloc(coloff * sizeof(char *));
                    238:                t->len = emalloc(coloff * sizeof(int));
                    239:                for (t->cols = coloff; --coloff >= 0;) {
                    240:                        t->list[coloff] = cols[coloff];
                    241:                        t->len[coloff] = strlen(cols[coloff]);
                    242:                        if (t->len[coloff] > lens[coloff])
                    243:                                lens[coloff] = t->len[coloff];
                    244:                }
                    245:        }
                    246:        for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
                    247:                for (coloff = 0; coloff < t->cols  - 1; ++coloff)
                    248:                        (void)printf("%s%*s", t->list[coloff],
                    249:                            lens[coloff] - t->len[coloff] + 2, " ");
                    250:                (void)printf("%s\n", t->list[coloff]);
                    251:        }
                    252: }
                    253:
                    254: #define        DEFNUM          1000
                    255: #define        MAXLINELEN      (LINE_MAX + 1)
                    256:
                    257: void
                    258: input(fp)
                    259:        FILE *fp;
                    260: {
                    261:        static int maxentry;
                    262:        int len;
                    263:        char *p, buf[MAXLINELEN];
                    264:
                    265:        if (!list)
                    266:                list = emalloc((maxentry = DEFNUM) * sizeof(char *));
                    267:        while (fgets(buf, MAXLINELEN, fp)) {
                    268:                for (p = buf; *p && isspace(*p); ++p);
                    269:                if (!*p)
                    270:                        continue;
                    271:                if (!(p = strchr(p, '\n'))) {
                    272:                        warnx("line too long");
                    273:                        eval = 1;
                    274:                        continue;
                    275:                }
                    276:                *p = '\0';
                    277:                len = p - buf;
                    278:                if (maxlength < len)
                    279:                        maxlength = len;
                    280:                if (entries == maxentry) {
                    281:                        maxentry += DEFNUM;
                    282:                        if (!(list = realloc(list,
                    283:                            (u_int)maxentry * sizeof(char *))))
                    284:                                err(1, NULL);
                    285:                }
                    286:                list[entries++] = strdup(buf);
                    287:        }
                    288: }
                    289:
                    290: void *
                    291: emalloc(size)
                    292:        int size;
                    293: {
                    294:        char *p;
                    295:
                    296:        if (!(p = malloc(size)))
                    297:                err(1, NULL);
                    298:        memset(p, 0, size);
                    299:        return (p);
                    300: }
                    301:
                    302: void
                    303: usage()
                    304: {
                    305:
                    306:        (void)fprintf(stderr,
1.4       deraadt   307:            "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
1.1       deraadt   308:        exit(1);
                    309: }