11590Srgrimes/* 21590Srgrimes * Copyright (c) 1988, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3128199Scharnierstatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1988, 1993\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 361590Srgrimes#ifndef lint 3728199Scharnier#if 0 381590Srgrimesstatic char sccsid[] = "@(#)tee.c 8.1 (Berkeley) 6/6/93"; 3928199Scharnier#endif 4028199Scharnierstatic const char rcsid[] = 4150477Speter "$FreeBSD$"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 441590Srgrimes#include <sys/types.h> 451590Srgrimes#include <sys/stat.h> 4628199Scharnier#include <err.h> 4728199Scharnier#include <fcntl.h> 481590Srgrimes#include <signal.h> 491590Srgrimes#include <stdio.h> 501590Srgrimes#include <stdlib.h> 51200462Sdelphij#include <string.h> 5228199Scharnier#include <unistd.h> 531590Srgrimes 541590Srgrimestypedef struct _list { 551590Srgrimes struct _list *next; 561590Srgrimes int fd; 5787303Sdwmalone const char *name; 581590Srgrimes} LIST; 59227187Sedstatic LIST *head; 601590Srgrimes 61227187Sedstatic void add(int, const char *); 6292922Simpstatic void usage(void); 631590Srgrimes 641590Srgrimesint 65102944Sdwmalonemain(int argc, char *argv[]) 661590Srgrimes{ 67102944Sdwmalone LIST *p; 68102944Sdwmalone int n, fd, rval, wval; 69102944Sdwmalone char *bp; 701590Srgrimes int append, ch, exitval; 711590Srgrimes char *buf; 721590Srgrimes#define BSIZE (8 * 1024) 731590Srgrimes 741590Srgrimes append = 0; 7524360Simp while ((ch = getopt(argc, argv, "ai")) != -1) 761590Srgrimes switch((char)ch) { 771590Srgrimes case 'a': 781590Srgrimes append = 1; 791590Srgrimes break; 801590Srgrimes case 'i': 811590Srgrimes (void)signal(SIGINT, SIG_IGN); 821590Srgrimes break; 831590Srgrimes case '?': 841590Srgrimes default: 8528199Scharnier usage(); 861590Srgrimes } 871590Srgrimes argv += optind; 881590Srgrimes argc -= optind; 891590Srgrimes 9096776Sjmallett if ((buf = malloc(BSIZE)) == NULL) 9199433Stjr err(1, "malloc"); 921590Srgrimes 931590Srgrimes add(STDOUT_FILENO, "stdout"); 941590Srgrimes 951590Srgrimes for (exitval = 0; *argv; ++argv) 961590Srgrimes if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND : 971590Srgrimes O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) { 9828199Scharnier warn("%s", *argv); 991590Srgrimes exitval = 1; 1001590Srgrimes } else 1011590Srgrimes add(fd, *argv); 1021590Srgrimes 1031590Srgrimes while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0) 1041590Srgrimes for (p = head; p; p = p->next) { 1051590Srgrimes n = rval; 1061590Srgrimes bp = buf; 1071590Srgrimes do { 1081590Srgrimes if ((wval = write(p->fd, bp, n)) == -1) { 109231524Scracauer warn("%s", p->name); 110231524Scracauer exitval = 1; 111231524Scracauer break; 1121590Srgrimes } 1131590Srgrimes bp += wval; 1141590Srgrimes } while (n -= wval); 1151590Srgrimes } 1161590Srgrimes if (rval < 0) 11728199Scharnier err(1, "read"); 11896803Sjmallett exit(exitval); 1191590Srgrimes} 1201590Srgrimes 12128199Scharnierstatic void 122102944Sdwmaloneusage(void) 12328199Scharnier{ 12428199Scharnier (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n"); 12528199Scharnier exit(1); 12628199Scharnier} 12728199Scharnier 128227187Sedstatic void 129102944Sdwmaloneadd(int fd, const char *name) 1301590Srgrimes{ 1311590Srgrimes LIST *p; 1321590Srgrimes 13396776Sjmallett if ((p = malloc(sizeof(LIST))) == NULL) 13499433Stjr err(1, "malloc"); 1351590Srgrimes p->fd = fd; 1361590Srgrimes p->name = name; 1371590Srgrimes p->next = head; 1381590Srgrimes head = p; 1391590Srgrimes} 140