BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / cat / cat.c
index 4fc6bf5..631f7c4 100644 (file)
@@ -5,7 +5,33 @@
  * This code is derived from software contributed to Berkeley by
  * Kevin Fall.
  *
  * This code is derived from software contributed to Berkeley by
  * Kevin Fall.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -15,27 +41,32 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cat.c      5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)cat.c      5.15 (Berkeley) 5/23/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <sys/file.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 
 #include <ctype.h>
 
-extern int errno;
 int bflag, eflag, nflag, sflag, tflag, vflag;
 int rval;
 char *filename;
 
 int bflag, eflag, nflag, sflag, tflag, vflag;
 int rval;
 char *filename;
 
+void cook_args(), cook_buf(), raw_args(), raw_cat();
+void err __P((int, const char *, ...));
+
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int optind;
        int ch;
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int optind;
        int ch;
-       char *strerror();
 
        while ((ch = getopt(argc, argv, "benstuv")) != EOF)
                switch (ch) {
 
        while ((ch = getopt(argc, argv, "benstuv")) != EOF)
                switch (ch) {
@@ -71,24 +102,25 @@ main(argc, argv)
                cook_args(argv);
        else
                raw_args(argv);
                cook_args(argv);
        else
                raw_args(argv);
+       if (fclose(stdout))
+               err(1, "stdout: %s", strerror(errno));
        exit(rval);
 }
 
        exit(rval);
 }
 
+void
 cook_args(argv)
        char **argv;
 {
        register FILE *fp;
 
        fp = stdin;
 cook_args(argv)
        char **argv;
 {
        register FILE *fp;
 
        fp = stdin;
-       filename = "-";
+       filename = "stdin";
        do {
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fp = stdin;
                        else if (!(fp = fopen(*argv, "r"))) {
        do {
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fp = stdin;
                        else if (!(fp = fopen(*argv, "r"))) {
-                               (void)fprintf(stderr, 
-                                   "cat: %s: %s\n", *argv, strerror(errno));
-                               rval = 1;
+                               err(0, "%s: %s", *argv, strerror(errno));
                                ++argv;
                                continue;
                        }
                                ++argv;
                                continue;
                        }
@@ -100,6 +132,7 @@ cook_args(argv)
        } while (*argv);
 }
 
        } while (*argv);
 }
 
+void
 cook_buf(fp)
        register FILE *fp;
 {
 cook_buf(fp)
        register FILE *fp;
 {
@@ -155,74 +188,90 @@ cook_buf(fp)
                        break;
        }
        if (ferror(fp)) {
                        break;
        }
        if (ferror(fp)) {
-               (void)fprintf(stderr, "cat: %s: read error\n", filename);
-               rval = 1;
-       }
-       if (ferror(stdout)) {
-               clearerr(stdout);
-               (void)fprintf(stderr, "cat: stdout: write error\n");
-               rval = 1;
+               err(0, "%s: %s", strerror(errno));
+               clearerr(fp);
        }
        }
+       if (ferror(stdout))
+               err(1, "stdout: %s", strerror(errno));
 }
 
 }
 
+void
 raw_args(argv)
        char **argv;
 {
        register int fd;
 
        fd = fileno(stdin);
 raw_args(argv)
        char **argv;
 {
        register int fd;
 
        fd = fileno(stdin);
-       filename = "-";
+       filename = "stdin";
        do {
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fd = fileno(stdin);
                        else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
        do {
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fd = fileno(stdin);
                        else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
-                               (void)fprintf(stderr, "cat: %s: %s\n",
-                                   *argv, strerror(errno));
-                               rval = 1;
+                               err(0, "%s: %s", *argv, strerror(errno));
                                ++argv;
                                continue;
                        }
                        filename = *argv++;
                }
                                ++argv;
                                continue;
                        }
                        filename = *argv++;
                }
-               rval |= raw_cat(fd);
+               raw_cat(fd);
                if (fd != fileno(stdin))
                        (void)close(fd);
        } while (*argv);
 }
 
                if (fd != fileno(stdin))
                        (void)close(fd);
        } while (*argv);
 }
 
-raw_cat(fd)
-       register int fd;
+void
+raw_cat(rfd)
+       register int rfd;
 {
 {
-       register int nr, nw, off;
+       register int nr, nw, off, wfd;
        static int bsize;
        static char *buf;
        struct stat sbuf;
        static int bsize;
        static char *buf;
        struct stat sbuf;
-       char *malloc(), *strerror();
 
 
+       wfd = fileno(stdout);
        if (!buf) {
        if (!buf) {
-               if (fstat(fileno(stdout), &sbuf)) {
-                       (void)fprintf(stderr, "cat: %s: %s\n", filename,
-                           strerror(errno));
-                       return(1);
-               }
+               if (fstat(wfd, &sbuf))
+                       err(1, "%s: %s", filename, strerror(errno));
                bsize = MAX(sbuf.st_blksize, 1024);
                bsize = MAX(sbuf.st_blksize, 1024);
-               if (!(buf = malloc((u_int)bsize))) {
-                       (void)fprintf(stderr, "cat: %s: no memory.\n",
-                           filename);
-                       return(1);
-               }
+               if (!(buf = malloc((u_int)bsize)))
+                       err(1, "%s", strerror(errno));
        }
        }
-       while ((nr = read(fd, buf, bsize)) > 0)
+       while ((nr = read(rfd, buf, bsize)) > 0)
                for (off = 0; off < nr; nr -= nw, off += nw)
                for (off = 0; off < nr; nr -= nw, off += nw)
-                       if ((nw = write(fileno(stdout), buf + off, nr)) < 0) {
-                               perror("cat: stdout");
-                               return(1);
-                       }
-       if (nr < 0) {
-               (void)fprintf(stderr, "cat: %s: %s\n", filename,
-                   strerror(errno));
-               return(1);
-       }
-       return(0);
+                       if ((nw = write(wfd, buf + off, nr)) < 0)
+                               err(1, "stdout");
+       if (nr < 0)
+               err(0, "%s: %s", filename, strerror(errno));
+}
+
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(int ex, const char *fmt, ...)
+#else
+err(ex, fmt, va_alist)
+       int ex;
+       char *fmt;
+        va_dcl
+#endif
+{
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "cat: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
+       if (ex)
+               exit(1);
+       rval = 1;
 }
 }