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