1 /* $OpenBSD: tee.c,v 1.12 2017/07/11 13:14:59 bluhm Exp $ */
2 /* $NetBSD: tee.c,v 1.5 1994/12/09 01:43:39 jtc Exp $ */
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/types.h>
35 #include <sys/queue.h>
47 SLIST_ENTRY(list
) next
;
51 SLIST_HEAD(, list
) head
;
54 add(int fd
, char *name
)
58 if ((p
= malloc(sizeof(*p
))) == NULL
)
62 SLIST_INSERT_HEAD(&head
, p
, next
);
66 main(int argc
, char *argv
[])
70 ssize_t n
, rval
, wval
;
72 int append
, ch
, exitval
;
75 if (pledge("stdio wpath cpath", NULL
) == -1)
81 while ((ch
= getopt(argc
, argv
, "ai")) != -1) {
87 (void)signal(SIGINT
, SIG_IGN
);
90 (void)fprintf(stderr
, "usage: tee [-ai] [file ...]\n");
97 add(STDOUT_FILENO
, "stdout");
101 if ((fd
= open(*argv
, O_WRONLY
| O_CREAT
|
102 (append
? O_APPEND
: O_TRUNC
), DEFFILEMODE
)) == -1) {
110 if (pledge("stdio", NULL
) == -1)
113 while ((rval
= read(STDIN_FILENO
, buf
, sizeof(buf
))) > 0) {
114 SLIST_FOREACH(p
, &head
, next
) {
118 if ((wval
= write(p
->fd
, bp
, n
)) == -1) {
132 SLIST_FOREACH(p
, &head
, next
) {
133 if (close(p
->fd
) == -1) {