1 /* tee - duplicate standard input */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #include "bashtypes.h"
26 #include "posixstat.h"
31 #if defined (HAVE_UNISTD_H)
42 #include "bashgetopt.h"
49 typedef struct flist
{
55 static FLIST
*tee_flist
;
57 #define TEE_BUFSIZE 8192
59 extern int interrupt_immediately
;
61 extern char *strerror ();
66 int opt
, append
, nointr
, rval
, fd
, fflags
;
73 reset_internal_getopt ();
75 tee_flist
= (FLIST
*)NULL
;
76 while ((opt
= internal_getopt (list
, "ai")) != -1)
94 interrupt_immediately
++;
96 buf
= xmalloc (TEE_BUFSIZE
);
98 /* Initialize output file list. */
99 fl
= tee_flist
= (FLIST
*)xmalloc (sizeof(FLIST
));
101 tee_flist
->fname
= "stdout";
102 tee_flist
->next
= (FLIST
*)NULL
;
104 /* Add file arguments to list of output files. */
105 fflags
= append
? O_WRONLY
|O_CREAT
|O_APPEND
: O_WRONLY
|O_CREAT
|O_TRUNC
;
106 for (rval
= EXECUTION_SUCCESS
; list
; list
= list
->next
)
108 fd
= open (list
->word
->word
, fflags
, 0666);
111 builtin_error ("%s: cannot open: %s", list
->word
->word
, strerror (errno
));
112 rval
= EXECUTION_FAILURE
;
116 fl
->next
= (FLIST
*)xmalloc (sizeof(FLIST
));
118 fl
->next
->fname
= list
->word
->word
;
120 fl
->next
= (FLIST
*)NULL
;
124 while ((nr
= read(0, buf
, TEE_BUFSIZE
)) > 0)
125 for (fl
= tee_flist
; fl
; fl
= fl
->next
)
131 if ((nw
= write (fl
->fd
, bp
, n
)) == -1)
133 builtin_error ("%s: write error: %s", fl
->fname
, strerror (errno
));
134 rval
= EXECUTION_FAILURE
;
142 builtin_error ("read error: %s", strerror (errno
));
144 /* Deallocate resources -- this is a builtin command. */
145 tee_flist
= tee_flist
->next
; /* skip bogus close of stdout */
149 if (close (fl
->fd
) < 0)
151 builtin_error ("%s: close_error: %s", fl
->fname
, strerror (errno
));
152 rval
= EXECUTION_FAILURE
;
154 tee_flist
= tee_flist
->next
;
162 "Duplicate standard output.",
164 "Copy standard input to standard output, making a copy in each",
165 "filename argument. If the `-a' option is gived, the specified",
166 "files are appended to, otherwise they are overwritten. If the",
167 "`-i' option is supplied, tee ignores interrupts.",
171 struct builtin tee_struct
= {
172 "tee", /* builtin name */
173 tee_builtin
, /* function implementing the builtin */
174 BUILTIN_ENABLED
, /* initial flags for builtin */
175 tee_doc
, /* array of long documentation strings. */
176 "tee [-ai] [file ...]", /* usage synopsis; becomes short_doc */
177 0 /* reserved for internal use */