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