tee.c revision 87303
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 * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3528199Scharnierstatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1988, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 4128199Scharnier#if 0 421590Srgrimesstatic char sccsid[] = "@(#)tee.c 8.1 (Berkeley) 6/6/93"; 4328199Scharnier#endif 4428199Scharnierstatic const char rcsid[] = 4550477Speter "$FreeBSD: head/usr.bin/tee/tee.c 87303 2001-12-03 21:37:35Z dwmalone $"; 461590Srgrimes#endif /* not lint */ 471590Srgrimes 481590Srgrimes#include <sys/types.h> 491590Srgrimes#include <sys/stat.h> 5028199Scharnier#include <err.h> 5128199Scharnier#include <fcntl.h> 521590Srgrimes#include <signal.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <stdlib.h> 551590Srgrimes#include <string.h> 5628199Scharnier#include <unistd.h> 571590Srgrimes 581590Srgrimestypedef struct _list { 591590Srgrimes struct _list *next; 601590Srgrimes int fd; 6187303Sdwmalone const char *name; 621590Srgrimes} LIST; 631590SrgrimesLIST *head; 641590Srgrimes 6587303Sdwmalonevoid add __P((int, const char *)); 6628199Scharnierstatic void usage __P((void)); 671590Srgrimes 681590Srgrimesint 691590Srgrimesmain(argc, argv) 701590Srgrimes int argc; 711590Srgrimes char *argv[]; 721590Srgrimes{ 731590Srgrimes register LIST *p; 741590Srgrimes register int n, fd, rval, wval; 751590Srgrimes register char *bp; 761590Srgrimes int append, ch, exitval; 771590Srgrimes char *buf; 781590Srgrimes#define BSIZE (8 * 1024) 791590Srgrimes 801590Srgrimes append = 0; 8124360Simp while ((ch = getopt(argc, argv, "ai")) != -1) 821590Srgrimes switch((char)ch) { 831590Srgrimes case 'a': 841590Srgrimes append = 1; 851590Srgrimes break; 861590Srgrimes case 'i': 871590Srgrimes (void)signal(SIGINT, SIG_IGN); 881590Srgrimes break; 891590Srgrimes case '?': 901590Srgrimes default: 9128199Scharnier usage(); 921590Srgrimes } 931590Srgrimes argv += optind; 941590Srgrimes argc -= optind; 951590Srgrimes 961590Srgrimes if ((buf = malloc((u_int)BSIZE)) == NULL) 9728199Scharnier errx(1, "malloc"); 981590Srgrimes 991590Srgrimes add(STDOUT_FILENO, "stdout"); 1001590Srgrimes 1011590Srgrimes for (exitval = 0; *argv; ++argv) 1021590Srgrimes if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND : 1031590Srgrimes O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) { 10428199Scharnier warn("%s", *argv); 1051590Srgrimes exitval = 1; 1061590Srgrimes } else 1071590Srgrimes add(fd, *argv); 1081590Srgrimes 1091590Srgrimes while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0) 1101590Srgrimes for (p = head; p; p = p->next) { 1111590Srgrimes n = rval; 1121590Srgrimes bp = buf; 1131590Srgrimes do { 1141590Srgrimes if ((wval = write(p->fd, bp, n)) == -1) { 11528199Scharnier warn("%s", p->name); 1161590Srgrimes exitval = 1; 1171590Srgrimes break; 1181590Srgrimes } 1191590Srgrimes bp += wval; 1201590Srgrimes } while (n -= wval); 1211590Srgrimes } 1221590Srgrimes if (rval < 0) 12328199Scharnier err(1, "read"); 1241590Srgrimes exit(exitval); 1251590Srgrimes} 1261590Srgrimes 12728199Scharnierstatic void 12828199Scharnierusage() 12928199Scharnier{ 13028199Scharnier (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n"); 13128199Scharnier exit(1); 13228199Scharnier} 13328199Scharnier 1341590Srgrimesvoid 1351590Srgrimesadd(fd, name) 1361590Srgrimes int fd; 13787303Sdwmalone const char *name; 1381590Srgrimes{ 1391590Srgrimes LIST *p; 1401590Srgrimes 1411590Srgrimes if ((p = malloc((u_int)sizeof(LIST))) == NULL) 14228199Scharnier errx(1, "malloc"); 1431590Srgrimes p->fd = fd; 1441590Srgrimes p->name = name; 1451590Srgrimes p->next = head; 1461590Srgrimes head = p; 1471590Srgrimes} 148