From 916b8720337ab8bb96db43eb3e7131da276278e9 Mon Sep 17 00:00:00 2001 From: Thomas Cort Date: Sun, 6 Oct 2013 14:08:45 -0400 Subject: [PATCH] Importing usr.bin/tee Replaces commands/tee. No Minix-specific changes needed. Change-Id: I31481facfa9cd44860bf2b59ec779605dd8d2f2d --- commands/Makefile | 2 +- commands/tee/Makefile | 4 -- commands/tee/tee.c | 66 ------------------ man/man1/Makefile | 2 +- man/man1/tee.1 | 37 ---------- releasetools/nbsd_ports | 1 + usr.bin/Makefile | 2 +- usr.bin/tee/Makefile | 6 ++ usr.bin/tee/tee.1 | 86 +++++++++++++++++++++++ usr.bin/tee/tee.c | 150 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 246 insertions(+), 110 deletions(-) delete mode 100644 commands/tee/Makefile delete mode 100644 commands/tee/tee.c delete mode 100644 man/man1/tee.1 create mode 100644 usr.bin/tee/Makefile create mode 100644 usr.bin/tee/tee.1 create mode 100644 usr.bin/tee/tee.c diff --git a/commands/Makefile b/commands/Makefile index 4913f4c40..6a9f98bef 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -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 \ diff --git a/commands/tee/Makefile b/commands/tee/Makefile deleted file mode 100644 index 494913542..000000000 --- a/commands/tee/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= tee -MAN= - -.include diff --git a/commands/tee/tee.c b/commands/tee/tee.c deleted file mode 100644 index 54fadb20e..000000000 --- a/commands/tee/tee.c +++ /dev/null @@ -1,66 +0,0 @@ -/* tee - pipe fitting Author: Paul Polderman */ - -#include -#include -#include -#include -#include -#include - -#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); -} diff --git a/man/man1/Makefile b/man/man1/Makefile index 2e7f2b231..c3898ee99 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -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 \ diff --git a/man/man1/tee.1 b/man/man1/tee.1 deleted file mode 100644 index 6f01f199d..000000000 --- a/man/man1/tee.1 +++ /dev/null @@ -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). diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index a8e4158d7..1832622e3 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -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 diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 07d512f68..fdc0de695 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -25,7 +25,7 @@ SUBDIR= \ \ shuffle sed seq \ sort stat su \ - tic tput \ + tee tic tput \ tr tsort unexpand \ toproto \ uniq \ diff --git a/usr.bin/tee/Makefile b/usr.bin/tee/Makefile new file mode 100644 index 000000000..a238fe4ec --- /dev/null +++ b/usr.bin/tee/Makefile @@ -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 diff --git a/usr.bin/tee/tee.1 b/usr.bin/tee/tee.1 new file mode 100644 index 000000000..a02b868a2 --- /dev/null +++ b/usr.bin/tee/tee.1 @@ -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. diff --git a/usr.bin/tee/tee.c b/usr.bin/tee/tee.c new file mode 100644 index 000000000..9c3872b97 --- /dev/null +++ b/usr.bin/tee/tee.c @@ -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 +#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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}