Home | History | Annotate | Line # | Download | only in tee
tee.c revision 1.6
      1  1.6    lukem /*	$NetBSD: tee.c,v 1.6 1997/10/20 00:37:11 lukem Exp $	*/
      2  1.5      jtc 
      3  1.1      cgd /*
      4  1.4  mycroft  * Copyright (c) 1988, 1993
      5  1.4  mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.6    lukem #include <sys/cdefs.h>
     37  1.1      cgd #ifndef lint
     38  1.6    lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39  1.6    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #ifndef lint
     43  1.5      jtc #if 0
     44  1.5      jtc static char sccsid[] = "@(#)tee.c	8.1 (Berkeley) 6/6/93";
     45  1.5      jtc #endif
     46  1.6    lukem __RCSID("$NetBSD: tee.c,v 1.6 1997/10/20 00:37:11 lukem Exp $");
     47  1.5      jtc #endif
     48  1.1      cgd 
     49  1.1      cgd #include <sys/types.h>
     50  1.1      cgd #include <sys/stat.h>
     51  1.4  mycroft #include <signal.h>
     52  1.4  mycroft #include <errno.h>
     53  1.1      cgd #include <fcntl.h>
     54  1.1      cgd #include <unistd.h>
     55  1.1      cgd #include <stdio.h>
     56  1.1      cgd #include <stdlib.h>
     57  1.1      cgd #include <string.h>
     58  1.3      jtc #include <locale.h>
     59  1.3      jtc #include <err.h>
     60  1.3      jtc 
     61  1.1      cgd typedef struct _list {
     62  1.1      cgd 	struct _list *next;
     63  1.1      cgd 	int fd;
     64  1.1      cgd 	char *name;
     65  1.1      cgd } LIST;
     66  1.1      cgd LIST *head;
     67  1.1      cgd 
     68  1.6    lukem void	add __P((int, char *));
     69  1.6    lukem int	main __P((int, char **));
     70  1.4  mycroft 
     71  1.3      jtc int
     72  1.1      cgd main(argc, argv)
     73  1.1      cgd 	int argc;
     74  1.4  mycroft 	char *argv[];
     75  1.1      cgd {
     76  1.6    lukem 	LIST *p;
     77  1.6    lukem 	int n, fd, rval, wval;
     78  1.6    lukem 	char *bp;
     79  1.1      cgd 	int append, ch, exitval;
     80  1.1      cgd 	char *buf;
     81  1.4  mycroft #define	BSIZE (8 * 1024)
     82  1.3      jtc 
     83  1.3      jtc 	setlocale(LC_ALL, "");
     84  1.1      cgd 
     85  1.1      cgd 	append = 0;
     86  1.3      jtc 	while ((ch = getopt(argc, argv, "ai")) != -1)
     87  1.1      cgd 		switch((char)ch) {
     88  1.1      cgd 		case 'a':
     89  1.1      cgd 			append = 1;
     90  1.1      cgd 			break;
     91  1.1      cgd 		case 'i':
     92  1.1      cgd 			(void)signal(SIGINT, SIG_IGN);
     93  1.1      cgd 			break;
     94  1.1      cgd 		case '?':
     95  1.1      cgd 		default:
     96  1.1      cgd 			(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
     97  1.1      cgd 			exit(1);
     98  1.1      cgd 		}
     99  1.1      cgd 	argv += optind;
    100  1.1      cgd 	argc -= optind;
    101  1.1      cgd 
    102  1.5      jtc 	if ((buf = malloc((size_t)BSIZE)) == NULL)
    103  1.6    lukem 		err(1, "malloc");
    104  1.3      jtc 
    105  1.1      cgd 	add(STDOUT_FILENO, "stdout");
    106  1.4  mycroft 
    107  1.4  mycroft 	for (exitval = 0; *argv; ++argv)
    108  1.1      cgd 		if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
    109  1.4  mycroft 		    O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
    110  1.3      jtc 			warn("%s", *argv);
    111  1.4  mycroft 			exitval = 1;
    112  1.4  mycroft 		} else
    113  1.1      cgd 			add(fd, *argv);
    114  1.3      jtc 
    115  1.4  mycroft 	while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
    116  1.1      cgd 		for (p = head; p; p = p->next) {
    117  1.1      cgd 			n = rval;
    118  1.1      cgd 			bp = buf;
    119  1.1      cgd 			do {
    120  1.1      cgd 				if ((wval = write(p->fd, bp, n)) == -1) {
    121  1.3      jtc 					warn("%s", p->name);
    122  1.1      cgd 					exitval = 1;
    123  1.1      cgd 					break;
    124  1.1      cgd 				}
    125  1.1      cgd 				bp += wval;
    126  1.1      cgd 			} while (n -= wval);
    127  1.1      cgd 		}
    128  1.1      cgd 	if (rval < 0) {
    129  1.3      jtc 		warn("read");
    130  1.3      jtc 		exitval = 1;
    131  1.1      cgd 	}
    132  1.3      jtc 
    133  1.3      jtc 	for (p = head; p; p = p->next) {
    134  1.4  mycroft 		if (close(p->fd) == -1) {
    135  1.3      jtc 			warn("%s", p->name);
    136  1.3      jtc 			exitval = 1;
    137  1.3      jtc 		}
    138  1.3      jtc 	}
    139  1.3      jtc 
    140  1.1      cgd 	exit(exitval);
    141  1.1      cgd }
    142  1.1      cgd 
    143  1.3      jtc void
    144  1.1      cgd add(fd, name)
    145  1.1      cgd 	int fd;
    146  1.1      cgd 	char *name;
    147  1.1      cgd {
    148  1.1      cgd 	LIST *p;
    149  1.1      cgd 
    150  1.5      jtc 	if ((p = malloc((size_t)sizeof(LIST))) == NULL)
    151  1.6    lukem 		err(1, "malloc");
    152  1.1      cgd 	p->fd = fd;
    153  1.1      cgd 	p->name = name;
    154  1.1      cgd 	p->next = head;
    155  1.1      cgd 	head = p;
    156  1.1      cgd }
    157