GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tee/tee.c Lines: 35 51 68.6 %
Date: 2016-12-06 Branches: 20 31 64.5 %

Line Branch Exec Source
1
/*	$OpenBSD: tee.c,v 1.10 2015/10/09 01:37:09 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
struct list {
46
	struct list *next;
47
	int fd;
48
	char *name;
49
};
50
struct list *head;
51
52
static void
53
add(int fd, char *name)
54
1172
{
55
	struct list *p;
56
57
1172
	if ((p = malloc(sizeof(*p))) == NULL)
58
		err(1, NULL);
59
1172
	p->fd = fd;
60
1172
	p->name = name;
61
1172
	p->next = head;
62
1172
	head = p;
63
1172
}
64
65
int
66
main(int argc, char *argv[])
67
524
{
68
	struct list *p;
69
	int fd;
70
	ssize_t n, rval, wval;
71
	char *bp;
72
	int append, ch, exitval;
73
	char buf[8192];
74
75
524
	setlocale(LC_ALL, "");
76
77
524
	if (pledge("stdio wpath cpath rpath", NULL) == -1)
78
		err(1, "pledge");
79
80
524
	append = 0;
81
1572
	while ((ch = getopt(argc, argv, "ai")) != -1) {
82
524
		switch(ch) {
83
		case 'a':
84
524
			append = 1;
85
524
			break;
86
		case 'i':
87
			(void)signal(SIGINT, SIG_IGN);
88
			break;
89
		case '?':
90
		default:
91
			(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
92
			exit(1);
93
		}
94
	}
95
524
	argv += optind;
96
524
	argc -= optind;
97
98
524
	add(STDOUT_FILENO, "stdout");
99
100
524
	exitval = 0;
101
1696
	while (*argv) {
102

648
		if ((fd = open(*argv, O_WRONLY | O_CREAT |
103
		    (append ? O_APPEND : O_TRUNC), DEFFILEMODE)) == -1) {
104
			warn("%s", *argv);
105
			exitval = 1;
106
		} else
107
648
			add(fd, *argv);
108
648
		argv++;
109
	}
110
111
524
	if (pledge("stdio wpath cpath rpath", NULL) == -1)
112
		err(1, "pledge");
113
114
1048
	while ((rval = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
115
1696
		for (p = head; p; p = p->next) {
116
1172
			n = rval;
117
1172
			bp = buf;
118
			do {
119
1172
				if ((wval = write(p->fd, bp, n)) == -1) {
120
					warn("%s", p->name);
121
					exitval = 1;
122
					break;
123
				}
124
1172
				bp += wval;
125
1172
			} while (n -= wval);
126
		}
127
	}
128
524
	if (rval == -1) {
129
		warn("read");
130
		exitval = 1;
131
	}
132
133
1696
	for (p = head; p; p = p->next) {
134
1172
		if (close(p->fd) == -1) {
135
			warn("%s", p->name);
136
			exitval = 1;
137
		}
138
	}
139
140
524
	exit(exitval);
141
}