Importing usr.bin/tee

Replaces commands/tee. No Minix-specific changes needed.

Change-Id: I31481facfa9cd44860bf2b59ec779605dd8d2f2d
This commit is contained in:
Thomas Cort 2013-10-06 14:08:45 -04:00 committed by Gerrit Code Review
parent fb69480b9a
commit 916b872033
10 changed files with 246 additions and 110 deletions

View File

@ -25,7 +25,7 @@ SUBDIR= add_route arp ash at backup btrace \
rotate rsh rshd service setup shar \
sleep slip spell split sprofalyze sprofdiff srccrc \
stty svclog svrctl swifi synctree sysenv \
syslogd tail tcpd tcpdp tcpstat tee telnet \
syslogd tail tcpd tcpdp tcpstat telnet \
telnetd term termcap tget time touch \
truncate tty udpstat umount uname \
unstack update uud uue version vol \

View File

@ -1,4 +0,0 @@
PROG= tee
MAN=
.include <bsd.prog.mk>

View File

@ -1,66 +0,0 @@
/* tee - pipe fitting Author: Paul Polderman */
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <minix/minlib.h>
#define MAXFD 18
#define CHUNK_SIZE 4096
int fd[MAXFD];
int main(int argc, char **argv);
int main(argc, argv)
int argc;
char **argv;
{
char iflag = 0, aflag = 0;
char buf[CHUNK_SIZE];
int i, s, n;
argv++;
--argc;
while (argc > 0 && argv[0][0] == '-') {
switch (argv[0][1]) {
case 'i': /* Interrupt turned off. */
iflag++;
break;
case 'a': /* Append to outputfile(s), instead of
* overwriting them. */
aflag++;
break;
default:
std_err("Usage: tee [-i] [-a] [files].\n");
exit(1);
}
argv++;
--argc;
}
fd[0] = 1; /* Always output to stdout. */
for (s = 1; s < MAXFD && argc > 0; --argc, argv++, s++) {
if (aflag && (fd[s] = open(*argv, O_RDWR)) >= 0) {
lseek(fd[s], 0L, SEEK_END);
continue;
} else {
if ((fd[s] = creat(*argv, 0666)) >= 0) continue;
}
std_err("Cannot open output file: ");
std_err(*argv);
std_err("\n");
exit(2);
}
if (iflag) signal(SIGINT, SIG_IGN);
while ((n = read(0, buf, CHUNK_SIZE)) > 0) {
for (i = 0; i < s; i++) write(fd[i], buf, n);
}
for (i = 0; i < s; i++) /* Close all fd's */
close(fd[i]);
return(0);
}

View File

@ -17,7 +17,7 @@ MAN= ash.1 at.1 \
remsync.1 rget.1 rlogin.1 rsh.1 rz.1 \
shar.1 sleep.1 spell.1 \
split.1 stty.1 svc.1 svrctl.1 \
synctree.1 sysenv.1 sz.1 tail.1 tee.1 telnet.1 template.1 \
synctree.1 sysenv.1 sz.1 tail.1 telnet.1 template.1 \
term.1 termcap.1 tget.1 time.1 true.1 \
truncate.1 tty.1 umount.1 uname.1 \
uud.1 uue.1 vol.1 whereis.1 which.1 \

View File

@ -1,37 +0,0 @@
.TH TEE 1
.SH NAME
tee \- divert stdin to a file
.SH SYNOPSIS
\fBtee\fR [\fB\-ai\fR] \fIfile\fR ...\fR
.br
.de FL
.TP
\\fB\\$1\\fR
\\$2
..
.de EX
.TP 20
\\fB\\$1\\fR
# \\$2
..
.SH OPTIONS
.TP 5
.B \-a
# Append to the files, rather than overwriting
.TP 5
.B \-i
# Ignore interrupts
.SH EXAMPLES
.TP 20
.B cat file1 file2 | tee x
# Save and display two files
.TP 20
.B pr file | tee x | lp
# Save the output of \fIpr\fP on \fIx\fP
.SH DESCRIPTION
.PP
.I Tee
copies \fIstdin\fR to standard output.
It also makes copies on all the files listed as arguments.
.SH "SEE ALSO"
.BR cat (1).

View File

@ -189,6 +189,7 @@
2012/10/17 12:00:00,usr.bin/sort
2011/01/15 22:54:10,usr.bin/stat
2012/02/10 16:16:12,usr.bin/su
2013/10/06 12:00:00,usr.bin/tee
2012/06/01 12:08:40,usr.bin/tic
2012/10/17 12:00:00,usr.bin/tput
2012/10/17 12:00:00,usr.bin/tr

View File

@ -25,7 +25,7 @@ SUBDIR= \
\
shuffle sed seq \
sort stat su \
tic tput \
tee tic tput \
tr tsort unexpand \
toproto \
uniq \

6
usr.bin/tee/Makefile Normal file
View File

@ -0,0 +1,6 @@
# $NetBSD: Makefile,v 1.4 1994/12/09 01:43:36 jtc Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= tee
.include <bsd.prog.mk>

86
usr.bin/tee/tee.1 Normal file
View File

@ -0,0 +1,86 @@
.\" $NetBSD: tee.1,v 1.7 2003/08/07 11:16:07 agc Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" 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. 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.
.\"
.\" @(#)tee.1 8.1 (Berkeley) 6/6/93
.\"
.Dd June 6, 1993
.Dt TEE 1
.Os
.Sh NAME
.Nm tee
.Nd pipe fitting
.Sh SYNOPSIS
.Nm
.Op Fl ai
.Op Ar file ...
.Sh DESCRIPTION
The
.Nm
utility copies standard input to standard output,
making a copy in zero or more files.
The output is unbuffered.
.Pp
The following options are available:
.Bl -tag -width Ds
.It Fl a
Append the output to the files rather than
overwriting them.
.It Fl i
Ignore the
.Dv SIGINT
signal.
.El
.Pp
The following operands are available:
.Bl -tag -width file
.It file
A pathname of an output
.Ar file .
.El
.Pp
The
.Nm
utility takes the default action for all signals,
except in the event of the
.Fl i
option.
.Pp
The
.Nm
utility exits 0 on success, and \*[Gt]0 if an error occurs.
.Sh STANDARDS
The
.Nm
function is expected to be
.Tn POSIX
.St -p1003.2
compatible.

150
usr.bin/tee/tee.c Normal file
View File

@ -0,0 +1,150 @@
/* $NetBSD: tee.c,v 1.11 2013/03/06 11:44:11 yamt Exp $ */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* 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. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)tee.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: tee.c,v 1.11 2013/03/06 11:44:11 yamt Exp $");
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <err.h>
typedef struct _list {
struct _list *next;
int fd;
const char *name;
} LIST;
LIST *head;
void add(int, const char *);
int main(int, char **);
int
main(int argc, char *argv[])
{
LIST *p;
ssize_t rval;
int fd;
int append, ch, exitval;
char *buf;
#define BSIZE (8 * 1024)
setlocale(LC_ALL, "");
append = 0;
while ((ch = getopt(argc, argv, "ai")) != -1)
switch((char)ch) {
case 'a':
append = 1;
break;
case 'i':
(void)signal(SIGINT, SIG_IGN);
break;
case '?':
default:
(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
exit(1);
}
argv += optind;
argc -= optind;
if ((buf = malloc(BSIZE)) == NULL)
err(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) {
warn("%s", *argv);
exitval = 1;
} else
add(fd, *argv);
while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
for (p = head; p; p = p->next) {
const char *bp = buf;
size_t n = rval;
ssize_t wval;
do {
if ((wval = write(p->fd, bp, n)) == -1) {
warn("%s", p->name);
exitval = 1;
break;
}
bp += wval;
} while (n -= wval);
}
if (rval < 0) {
warn("read");
exitval = 1;
}
for (p = head; p; p = p->next) {
if (close(p->fd) == -1) {
warn("%s", p->name);
exitval = 1;
}
}
exit(exitval);
}
void
add(int fd, const char *name)
{
LIST *p;
if ((p = malloc(sizeof(LIST))) == NULL)
err(1, "malloc");
p->fd = fd;
p->name = name;
p->next = head;
head = p;
}