tee.c revision 1.11 1 1.11 yamt /* $NetBSD: tee.c,v 1.11 2013/03/06 11:44:11 yamt 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.8 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 1.8 lukem The Regents of the University of California. All rights reserved.");
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.11 yamt __RCSID("$NetBSD: tee.c,v 1.11 2013/03/06 11:44:11 yamt 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.9 lukem const char *name;
61 1.1 cgd } LIST;
62 1.1 cgd LIST *head;
63 1.1 cgd
64 1.10 matt void add(int, const char *);
65 1.10 matt int main(int, char **);
66 1.4 mycroft
67 1.3 jtc int
68 1.10 matt main(int argc, char *argv[])
69 1.1 cgd {
70 1.6 lukem LIST *p;
71 1.11 yamt ssize_t rval;
72 1.11 yamt int fd;
73 1.1 cgd int append, ch, exitval;
74 1.1 cgd char *buf;
75 1.4 mycroft #define BSIZE (8 * 1024)
76 1.3 jtc
77 1.3 jtc setlocale(LC_ALL, "");
78 1.1 cgd
79 1.1 cgd append = 0;
80 1.3 jtc while ((ch = getopt(argc, argv, "ai")) != -1)
81 1.1 cgd switch((char)ch) {
82 1.1 cgd case 'a':
83 1.1 cgd append = 1;
84 1.1 cgd break;
85 1.1 cgd case 'i':
86 1.1 cgd (void)signal(SIGINT, SIG_IGN);
87 1.1 cgd break;
88 1.1 cgd case '?':
89 1.1 cgd default:
90 1.1 cgd (void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
91 1.1 cgd exit(1);
92 1.1 cgd }
93 1.1 cgd argv += optind;
94 1.1 cgd argc -= optind;
95 1.1 cgd
96 1.11 yamt if ((buf = malloc(BSIZE)) == NULL)
97 1.6 lukem err(1, "malloc");
98 1.3 jtc
99 1.1 cgd add(STDOUT_FILENO, "stdout");
100 1.4 mycroft
101 1.4 mycroft for (exitval = 0; *argv; ++argv)
102 1.1 cgd if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
103 1.4 mycroft O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
104 1.3 jtc warn("%s", *argv);
105 1.4 mycroft exitval = 1;
106 1.4 mycroft } else
107 1.1 cgd add(fd, *argv);
108 1.3 jtc
109 1.4 mycroft while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
110 1.1 cgd for (p = head; p; p = p->next) {
111 1.11 yamt const char *bp = buf;
112 1.11 yamt size_t n = rval;
113 1.11 yamt ssize_t wval;
114 1.11 yamt
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.10 matt add(int fd, const char *name)
141 1.1 cgd {
142 1.1 cgd LIST *p;
143 1.1 cgd
144 1.11 yamt if ((p = malloc(sizeof(LIST))) == NULL)
145 1.6 lukem err(1, "malloc");
146 1.1 cgd p->fd = fd;
147 1.1 cgd p->name = name;
148 1.1 cgd p->next = head;
149 1.1 cgd head = p;
150 1.1 cgd }
151