ANSI stuff, use err() routine so error messages are right; nothing important
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Tue, 17 Dec 1991 07:40:06 +0000 (23:40 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Tue, 17 Dec 1991 07:40:06 +0000 (23:40 -0800)
SCCS-vsn: usr.bin/tee/tee.c 5.13

usr/src/usr.bin/tee/tee.c

index 57d1787..69ba4d3 100644 (file)
@@ -12,13 +12,14 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)tee.c      5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)tee.c      5.13 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <signal.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,17 +32,19 @@ typedef struct _list {
 } LIST;
 LIST *head;
 
 } LIST;
 LIST *head;
 
+void add __P((int, char *));
+void err __P((int, const char *, ...));
+
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
-       extern int errno, optind;
        register LIST *p;
        register int n, fd, rval, wval;
        register char *bp;
        int append, ch, exitval;
        char *buf;
        register LIST *p;
        register int n, fd, rval, wval;
        register char *bp;
        int append, ch, exitval;
        char *buf;
-       off_t lseek();
 #define        BSIZE (8 * 1024)
 
        append = 0;
 #define        BSIZE (8 * 1024)
 
        append = 0;
@@ -61,26 +64,26 @@ main(argc, argv)
        argv += optind;
        argc -= optind;
 
        argv += optind;
        argc -= optind;
 
-       if (!(buf = malloc((u_int)BSIZE))) {
-               (void)fprintf(stderr, "tee: out of space.\n");
-               exit(1);
-       }
+       if ((buf = malloc((u_int)BSIZE)) == NULL)
+               err(1, "%s", strerror(errno));
+
        add(STDOUT_FILENO, "stdout");
        add(STDOUT_FILENO, "stdout");
-       for (; *argv; ++argv)
+
+       for (exitval = 0; *argv; ++argv)
                if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
                if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
-                   O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0)
-                       (void)fprintf(stderr, "tee: %s: %s.\n",
-                           *argv, strerror(errno));
-               else
+                   O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
+                       err(0, "%s: %s", *argv, strerror(errno));
+                       exitval = 1;
+               else
                        add(fd, *argv);
                        add(fd, *argv);
-       exitval = 0;
+
        while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
                for (p = head; p; p = p->next) {
                        n = rval;
                        bp = buf;
                        do {
                                if ((wval = write(p->fd, bp, n)) == -1) {
        while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
                for (p = head; p; p = p->next) {
                        n = rval;
                        bp = buf;
                        do {
                                if ((wval = write(p->fd, bp, n)) == -1) {
-                                       (void)fprintf(stderr, "tee: %s: %s.\n",
+                                       err(0, "%s: %s",
                                            p->name, strerror(errno));
                                        exitval = 1;
                                        break;
                                            p->name, strerror(errno));
                                        exitval = 1;
                                        break;
@@ -88,25 +91,52 @@ main(argc, argv)
                                bp += wval;
                        } while (n -= wval);
                }
                                bp += wval;
                        } while (n -= wval);
                }
-       if (rval < 0) {
-               (void)fprintf(stderr, "tee: read: %s\n", strerror(errno));
-               exit(1);
-       }
+       if (rval < 0)
+               err(1, "read: %s", strerror(errno));
        exit(exitval);
 }
 
        exit(exitval);
 }
 
+void
 add(fd, name)
        int fd;
        char *name;
 {
        LIST *p;
 
 add(fd, name)
        int fd;
        char *name;
 {
        LIST *p;
 
-       if (!(p = malloc((u_int)sizeof(LIST)))) {
-               (void)fprintf(stderr, "tee: out of space.\n");
-               exit(1);
-       }
+       if ((p = malloc((u_int)sizeof(LIST))) == NULL)
+               err(1, "%s", strerror(errno));
        p->fd = fd;
        p->name = name;
        p->next = head;
        head = p;
 }
        p->fd = fd;
        p->name = name;
        p->next = head;
        head = p;
 }
+
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(int doexit, const char *fmt, ...)
+#else
+err(doexit, fmt, va_alist)
+       int doexit;
+       char *fmt;
+        va_dcl
+#endif
+{
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "tee: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
+       if (doexit)
+               exit(1);
+}