prettyness police
[unix-history] / usr / src / usr.bin / column / column.c
index 6e88f39..e3081f8 100644 (file)
@@ -1,35 +1,37 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * 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.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1989, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)column.c   5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)column.c   8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
-#include <ctype.h>
+
+void  c_columnate __P((void));
+void *emalloc __P((int));
+void  input __P((FILE *));
+void  maketbl __P((void));
+void  print __P((void));
+void  r_columnate __P((void));
+void  usage __P((void));
 
 int termwidth = 80;            /* default terminal width */
 
 
 int termwidth = 80;            /* default terminal width */
 
@@ -39,16 +41,15 @@ int maxlength;                      /* longest record */
 char **list;                   /* array of pointers to records */
 char *separator = "\t ";       /* field separator for table option */
 
 char **list;                   /* array of pointers to records */
 char *separator = "\t ";       /* field separator for table option */
 
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       extern char *optarg;
-       extern int errno, optind;
        struct winsize win;
        FILE *fp;
        int ch, tflag, xflag;
        struct winsize win;
        FILE *fp;
        int ch, tflag, xflag;
-       char *p, *getenv();
+       char *p;
 
        if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
                if (p = getenv("COLUMNS"))
 
        if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
                if (p = getenv("COLUMNS"))
@@ -56,7 +57,7 @@ main(argc, argv)
        } else
                termwidth = win.ws_col;
 
        } else
                termwidth = win.ws_col;
 
-       xflag = 0;
+       tflag = xflag = 0;
        while ((ch = getopt(argc, argv, "c:s:tx")) != EOF)
                switch(ch) {
                case 'c':
        while ((ch = getopt(argc, argv, "c:s:tx")) != EOF)
                switch(ch) {
                case 'c':
@@ -85,8 +86,7 @@ main(argc, argv)
                        input(fp);
                        (void)fclose(fp);
                } else {
                        input(fp);
                        (void)fclose(fp);
                } else {
-                       (void)fprintf(stderr, "column: %s: %s\n", *argv,
-                           strerror(errno));
+                       warn("%s", *argv);
                        eval = 1;
                }
 
                        eval = 1;
                }
 
@@ -95,22 +95,20 @@ main(argc, argv)
 
        if (tflag)
                maketbl();
 
        if (tflag)
                maketbl();
-       else {
-               if (maxlength >= termwidth)
-                       print();
-               if (xflag)
-                       c_columnate();
-               else
-                       r_columnate();
-       }
+       else if (maxlength >= termwidth)
+               print();
+       else if (xflag)
+               c_columnate();
+       else
+               r_columnate();
        exit(eval);
 }
 
 #define        TAB     8
        exit(eval);
 }
 
 #define        TAB     8
+void
 c_columnate()
 {
 c_columnate()
 {
-       register int chcnt, col, cnt, numcols;
-       int endcol;
+       int chcnt, col, cnt, endcol, numcols;
        char **lp;
 
        maxlength = (maxlength + TAB) & ~(TAB - 1);
        char **lp;
 
        maxlength = (maxlength + TAB) & ~(TAB - 1);
@@ -136,10 +134,10 @@ c_columnate()
                putchar('\n');
 }
 
                putchar('\n');
 }
 
+void
 r_columnate()
 {
 r_columnate()
 {
-       register int base, chcnt, cnt, col;
-       int endcol, numcols, numrows, row;
+       int base, chcnt, cnt, col, endcol, numcols, numrows, row;
 
        maxlength = (maxlength + TAB) & ~(TAB - 1);
        numcols = termwidth / maxlength;
 
        maxlength = (maxlength + TAB) & ~(TAB - 1);
        numcols = termwidth / maxlength;
@@ -163,10 +161,11 @@ r_columnate()
        }
 }
 
        }
 }
 
+void
 print()
 {
 print()
 {
-       register int cnt;
-       register char **lp;
+       int cnt;
+       char **lp;
 
        for (cnt = entries, lp = list; cnt--; ++lp)
                (void)printf("%s\n", *lp);
 
        for (cnt = entries, lp = list; cnt--; ++lp)
                (void)printf("%s\n", *lp);
@@ -178,33 +177,34 @@ typedef struct _tbl {
 } TBL;
 #define        DEFCOLS 25
 
 } TBL;
 #define        DEFCOLS 25
 
+void
 maketbl()
 {
 maketbl()
 {
-       register TBL *t;
-       register int coloff, cnt;
-       register char *p, **lp;
+       TBL *t;
+       int coloff, cnt;
+       char *p, **lp;
        int *lens, maxcols;
        TBL *tbl;
        int *lens, maxcols;
        TBL *tbl;
-       char **cols, *emalloc(), *realloc();
+       char **cols;
 
 
-       t = tbl = (TBL *)emalloc(entries * sizeof(TBL));
-       cols = (char **)emalloc((maxcols = DEFCOLS) * sizeof(char *));
-       lens = (int *)emalloc(maxcols * sizeof(int));
+       t = tbl = emalloc(entries * sizeof(TBL));
+       cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
+       lens = emalloc(maxcols * sizeof(int));
        for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
                for (coloff = 0, p = *lp; cols[coloff] = strtok(p, separator);
                    p = NULL)
                        if (++coloff == maxcols) {
        for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
                for (coloff = 0, p = *lp; cols[coloff] = strtok(p, separator);
                    p = NULL)
                        if (++coloff == maxcols) {
-                               if (!(cols = (char **)realloc((char *)cols,
-                                   (u_int)maxcols + DEFCOLS * sizeof(char *))) ||
-                                   !(lens = (int *)realloc((char *)lens,
+                               if (!(cols = realloc(cols, (u_int)maxcols +
+                                   DEFCOLS * sizeof(char *))) ||
+                                   !(lens = realloc(lens,
                                    (u_int)maxcols + DEFCOLS * sizeof(int))))
                                    (u_int)maxcols + DEFCOLS * sizeof(int))))
-                                       nomem();
-                               bzero((char *)lens + maxcols * sizeof(int),
-                                   DEFCOLS * sizeof(int));
+                                       err(1, NULL);
+                               memset((char *)lens + maxcols * sizeof(int),
+                                   0, DEFCOLS * sizeof(int));
                                maxcols += DEFCOLS;
                        }
                                maxcols += DEFCOLS;
                        }
-               t->list = (char **)emalloc(coloff * sizeof(char *));
-               t->len = (int *)emalloc(coloff * sizeof(int));
+               t->list = emalloc(coloff * sizeof(char *));
+               t->len = emalloc(coloff * sizeof(int));
                for (t->cols = coloff; --coloff >= 0;) {
                        t->list[coloff] = cols[coloff];
                        t->len[coloff] = strlen(cols[coloff]);
                for (t->cols = coloff; --coloff >= 0;) {
                        t->list[coloff] = cols[coloff];
                        t->len[coloff] = strlen(cols[coloff]);
@@ -221,24 +221,24 @@ maketbl()
 }
 
 #define        DEFNUM          1000
 }
 
 #define        DEFNUM          1000
-#define        MAXLINELEN      (2048 + 1)
+#define        MAXLINELEN      (LINE_MAX + 1)
 
 
+void
 input(fp)
 input(fp)
-       register FILE *fp;
+       FILE *fp;
 {
        static int maxentry;
 {
        static int maxentry;
-       register int len;
-       register char *p;
-       char buf[MAXLINELEN], *emalloc(), *realloc();
+       int len;
+       char *p, buf[MAXLINELEN];
 
        if (!list)
 
        if (!list)
-               list = (char **)emalloc((maxentry = DEFNUM) * sizeof(char *));
+               list = emalloc((maxentry = DEFNUM) * sizeof(char *));
        while (fgets(buf, MAXLINELEN, fp)) {
                for (p = buf; *p && isspace(*p); ++p);
                if (!*p)
                        continue;
        while (fgets(buf, MAXLINELEN, fp)) {
                for (p = buf; *p && isspace(*p); ++p);
                if (!*p)
                        continue;
-               if (!(p = index(p, '\n'))) {
-                       (void)fprintf(stderr, "column: line too long.\n");
+               if (!(p = strchr(p, '\n'))) {
+                       warnx("line too long");
                        eval = 1;
                        continue;
                }
                        eval = 1;
                        continue;
                }
@@ -248,36 +248,30 @@ input(fp)
                        maxlength = len;
                if (entries == maxentry) {
                        maxentry += DEFNUM;
                        maxlength = len;
                if (entries == maxentry) {
                        maxentry += DEFNUM;
-                       if (!(list =
-                           (char **)realloc((char *)list,
+                       if (!(list = realloc(list,
                            (u_int)maxentry * sizeof(char *))))
                            (u_int)maxentry * sizeof(char *))))
-                               nomem();
+                               err(1, NULL);
                }
                list[entries++] = strdup(buf);
        }
 }
 
                }
                list[entries++] = strdup(buf);
        }
 }
 
-char *
+void *
 emalloc(size)
        int size;
 {
 emalloc(size)
        int size;
 {
-       char *p, *malloc();
-
-       /* NOSTRICT */
-       if (!(p = malloc((u_int)size)))
-               nomem();
-       bzero(p, size);
-       return(p);
-}
+       char *p;
 
 
-nomem()
-{
-       (void)fprintf(stderr, "column: out of memory.\n");
-       exit(1);
+       if (!(p = malloc(size)))
+               err(1, NULL);
+       memset(p, 0, size);
+       return (p);
 }
 
 }
 
+void
 usage()
 {
 usage()
 {
+
        (void)fprintf(stderr,
            "usage: column [-tx] [-c columns] [file ...]\n");
        exit(1);
        (void)fprintf(stderr,
            "usage: column [-tx] [-c columns] [file ...]\n");
        exit(1);