SCCS-vsn: usr.bin/tee/tee.c 5.13
#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 <errno.h>
+#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+void add __P((int, char *));
+void err __P((int, const char *, ...));
+
+int
main(argc, argv)
int argc;
main(argc, argv)
int argc;
- 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;
#define BSIZE (8 * 1024)
append = 0;
#define BSIZE (8 * 1024)
append = 0;
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 (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
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",
p->name, strerror(errno));
exitval = 1;
break;
p->name, strerror(errno));
exitval = 1;
break;
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));
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);
+}