Patch-ID: bash40-021
[bash.git] / examples / loadables / tee.c
blob8b5715f25f413f8fcf645be38c533d82e1862503
1 /* tee - duplicate standard input */
3 /* See Makefile for compilation details. */
5 /*
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/>.
23 #include "config.h"
25 #include "bashtypes.h"
26 #include "posixstat.h"
27 #include "filecntl.h"
29 #include <signal.h>
31 #if defined (HAVE_UNISTD_H)
32 # include <unistd.h>
33 #endif
35 #include "bashansi.h"
37 #include <stdio.h>
38 #include <errno.h>
40 #include "builtins.h"
41 #include "shell.h"
42 #include "bashgetopt.h"
43 #include "common.h"
45 #if !defined (errno)
46 extern int errno;
47 #endif
49 typedef struct flist {
50 struct flist *next;
51 int fd;
52 char *fname;
53 } FLIST;
55 static FLIST *tee_flist;
57 #define TEE_BUFSIZE 8192
59 extern int interrupt_immediately;
61 extern char *strerror ();
63 tee_builtin (list)
64 WORD_LIST *list;
66 int opt, append, nointr, rval, fd, fflags;
67 int n, nr, nw;
68 FLIST *fl;
69 char *buf, *bp;
71 char *t;
73 reset_internal_getopt ();
74 append = nointr = 0;
75 tee_flist = (FLIST *)NULL;
76 while ((opt = internal_getopt (list, "ai")) != -1)
78 switch (opt)
80 case 'a':
81 append = 1;
82 break;
83 case 'i':
84 nointr = 1;
85 break;
86 default:
87 builtin_usage ();
88 return (EX_USAGE);
91 list = loptend;
93 if (nointr == 0)
94 interrupt_immediately++;
96 buf = xmalloc (TEE_BUFSIZE);
98 /* Initialize output file list. */
99 fl = tee_flist = (FLIST *)xmalloc (sizeof(FLIST));
100 tee_flist->fd = 1;
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);
109 if (fd < 0)
111 builtin_error ("%s: cannot open: %s", list->word->word, strerror (errno));
112 rval = EXECUTION_FAILURE;
114 else
116 fl->next = (FLIST *)xmalloc (sizeof(FLIST));
117 fl->next->fd = fd;
118 fl->next->fname = list->word->word;
119 fl = fl->next;
120 fl->next = (FLIST *)NULL;
124 while ((nr = read(0, buf, TEE_BUFSIZE)) > 0)
125 for (fl = tee_flist; fl; fl = fl->next)
127 n = nr;
128 bp = buf;
131 if ((nw = write (fl->fd, bp, n)) == -1)
133 builtin_error ("%s: write error: %s", fl->fname, strerror (errno));
134 rval = EXECUTION_FAILURE;
135 break;
137 bp += nw;
139 while (n -= nw);
141 if (nr < 0)
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 */
146 while (tee_flist)
148 fl = tee_flist;
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;
155 free (fl);
158 return (rval);
161 char *tee_doc[] = {
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.",
168 (char *)NULL
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 */