tee.c revision 102944
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 102944 2002-09-04 23:29:10Z 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
6592922Simpvoid add(int, const char *);
6692922Simpstatic void usage(void);
671590Srgrimes
681590Srgrimesint
69102944Sdwmalonemain(int argc, char *argv[])
701590Srgrimes{
71102944Sdwmalone	LIST *p;
72102944Sdwmalone	int n, fd, rval, wval;
73102944Sdwmalone	char *bp;
741590Srgrimes	int append, ch, exitval;
751590Srgrimes	char *buf;
761590Srgrimes#define	BSIZE (8 * 1024)
771590Srgrimes
781590Srgrimes	append = 0;
7924360Simp	while ((ch = getopt(argc, argv, "ai")) != -1)
801590Srgrimes		switch((char)ch) {
811590Srgrimes		case 'a':
821590Srgrimes			append = 1;
831590Srgrimes			break;
841590Srgrimes		case 'i':
851590Srgrimes			(void)signal(SIGINT, SIG_IGN);
861590Srgrimes			break;
871590Srgrimes		case '?':
881590Srgrimes		default:
8928199Scharnier			usage();
901590Srgrimes		}
911590Srgrimes	argv += optind;
921590Srgrimes	argc -= optind;
931590Srgrimes
9496776Sjmallett	if ((buf = malloc(BSIZE)) == NULL)
9599433Stjr		err(1, "malloc");
961590Srgrimes
971590Srgrimes	add(STDOUT_FILENO, "stdout");
981590Srgrimes
991590Srgrimes	for (exitval = 0; *argv; ++argv)
1001590Srgrimes		if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
1011590Srgrimes		    O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
10228199Scharnier			warn("%s", *argv);
1031590Srgrimes			exitval = 1;
1041590Srgrimes		} else
1051590Srgrimes			add(fd, *argv);
1061590Srgrimes
1071590Srgrimes	while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
1081590Srgrimes		for (p = head; p; p = p->next) {
1091590Srgrimes			n = rval;
1101590Srgrimes			bp = buf;
1111590Srgrimes			do {
1121590Srgrimes				if ((wval = write(p->fd, bp, n)) == -1) {
11328199Scharnier					warn("%s", p->name);
1141590Srgrimes					exitval = 1;
1151590Srgrimes					break;
1161590Srgrimes				}
1171590Srgrimes				bp += wval;
1181590Srgrimes			} while (n -= wval);
1191590Srgrimes		}
1201590Srgrimes	if (rval < 0)
12128199Scharnier		err(1, "read");
12296803Sjmallett	exit(exitval);
1231590Srgrimes}
1241590Srgrimes
12528199Scharnierstatic void
126102944Sdwmaloneusage(void)
12728199Scharnier{
12828199Scharnier	(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
12928199Scharnier	exit(1);
13028199Scharnier}
13128199Scharnier
1321590Srgrimesvoid
133102944Sdwmaloneadd(int fd, const char *name)
1341590Srgrimes{
1351590Srgrimes	LIST *p;
1361590Srgrimes
13796776Sjmallett	if ((p = malloc(sizeof(LIST))) == NULL)
13899433Stjr		err(1, "malloc");
1391590Srgrimes	p->fd = fd;
1401590Srgrimes	p->name = name;
1411590Srgrimes	p->next = head;
1421590Srgrimes	head = p;
1431590Srgrimes}
144