1 /* $OpenBSD: tee.c,v 1.6 2003/06/10 22:20:53 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/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 __SCCSID("@(#)tee.c 8.1 (Berkeley) 6/6/93");
37 __RCSID("$MirOS: src/usr.bin/tee/tee.c,v 1.2 2007/07/05 23:09:43 tg Exp $");
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <locale.h>
49 #include <err.h>
50
51 typedef struct _list {
52 struct _list *next;
53 int fd;
54 char *name;
55 } LIST;
56 LIST *head;
57
58 void add(int, char *);
59
60 int
main(int argc,char * argv[])61 main(int argc, char *argv[])
62 {
63 LIST *p;
64 int n, fd, rval, wval;
65 char *bp;
66 int append, ch, exitval;
67 char *buf;
68 #define BSIZE (8 * 1024)
69
70 #ifndef __MirBSD__
71 setlocale(LC_ALL, "");
72 #endif
73
74 append = 0;
75 while ((ch = getopt(argc, argv, "ai")) != -1)
76 switch((char)ch) {
77 case 'a':
78 append = 1;
79 break;
80 case 'i':
81 (void)signal(SIGINT, SIG_IGN);
82 break;
83 case '?':
84 default:
85 (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
86 exit(1);
87 }
88 argv += optind;
89 argc -= optind;
90
91 if ((buf = malloc((size_t)BSIZE)) == NULL)
92 err(1, NULL);
93
94 add(STDOUT_FILENO, "stdout");
95
96 for (exitval = 0; *argv; ++argv)
97 if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
98 O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
99 warn("%s", *argv);
100 exitval = 1;
101 } else
102 add(fd, *argv);
103
104 while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
105 for (p = head; p; p = p->next) {
106 n = rval;
107 bp = buf;
108 do {
109 if ((wval = write(p->fd, bp, n)) == -1) {
110 warn("%s", p->name);
111 exitval = 1;
112 break;
113 }
114 bp += wval;
115 } while (n -= wval);
116 }
117 if (rval < 0) {
118 warn("read");
119 exitval = 1;
120 }
121
122 for (p = head; p; p = p->next) {
123 if (close(p->fd) == -1) {
124 warn("%s", p->name);
125 exitval = 1;
126 }
127 }
128
129 exit(exitval);
130 }
131
132 void
add(int fd,char * name)133 add(int fd, char *name)
134 {
135 LIST *p;
136
137 if ((p = malloc((size_t)sizeof(LIST))) == NULL)
138 err(1, NULL);
139 p->fd = fd;
140 p->name = name;
141 p->next = head;
142 head = p;
143 }
144