Use err(3) instead of local redefinition. Add usage().

This commit is contained in:
charnier 1997-08-14 06:42:43 +00:00
parent baddffcb88
commit d72228bb7a
2 changed files with 29 additions and 51 deletions

View File

@ -41,12 +41,12 @@
.Nm tee
.Nd pipe fitting
.Sh SYNOPSIS
.Nm tee
.Nm
.Op Fl ai
.Op Ar file ...
.Op Ar
.Sh DESCRIPTION
The
.Nm tee
.Nm
utility copies standard input to standard output,
making a copy in zero or more files.
The output is unbuffered.
@ -64,25 +64,24 @@ signal.
.Pp
The following operands are available:
.Bl -tag -width file
.It file
.It Ar file
A pathname of an output
.Ar file .
.El
.Pp
The
.Nm tee
.Nm
utility takes the default action for all signals,
except in the event of the
.Fl i
option.
.Pp
The
.Nm tee
.Nm
utility exits 0 on success, and >0 if an error occurs.
.Sh STANDARDS
The
.Nm tee
.Nm
function is expected to be
.Tn POSIX
.St -p1003.2
compatible.

View File

@ -32,24 +32,28 @@
*/
#ifndef lint
static char copyright[] =
static const char copyright[] =
"@(#) Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)tee.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct _list {
struct _list *next;
@ -59,7 +63,7 @@ typedef struct _list {
LIST *head;
void add __P((int, char *));
void err __P((int, const char *, ...));
static void usage __P((void));
int
main(argc, argv)
@ -84,21 +88,20 @@ main(argc, argv)
break;
case '?':
default:
(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
exit(1);
usage();
}
argv += optind;
argc -= optind;
if ((buf = malloc((u_int)BSIZE)) == NULL)
err(1, "%s", strerror(errno));
errx(1, "malloc");
add(STDOUT_FILENO, "stdout");
for (exitval = 0; *argv; ++argv)
if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
err(0, "%s: %s", *argv, strerror(errno));
warn("%s", *argv);
exitval = 1;
} else
add(fd, *argv);
@ -109,8 +112,7 @@ main(argc, argv)
bp = buf;
do {
if ((wval = write(p->fd, bp, n)) == -1) {
err(0, "%s: %s",
p->name, strerror(errno));
warn("%s", p->name);
exitval = 1;
break;
}
@ -118,10 +120,17 @@ main(argc, argv)
} while (n -= wval);
}
if (rval < 0)
err(1, "read: %s", strerror(errno));
err(1, "read");
exit(exitval);
}
static void
usage()
{
(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
exit(1);
}
void
add(fd, name)
int fd;
@ -130,39 +139,9 @@ add(fd, name)
LIST *p;
if ((p = malloc((u_int)sizeof(LIST))) == NULL)
err(1, "%s", strerror(errno));
errx(1, "malloc");
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);
}