.
[coreutils.git] / src / tee.c
blob302c4b73498d383abca207a7fdbc35a736dcc701
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,90,91,92,93,94,95,96,1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "error.h"
29 int full_write ();
31 static int tee PARAMS ((int nfiles, const char **files));
33 /* If nonzero, append to output files rather than truncating them. */
34 static int append;
36 /* If nonzero, ignore interrupts. */
37 static int ignore_interrupts;
39 /* The name that this program was run with. */
40 char *program_name;
42 /* If nonzero, display usage information and exit. */
43 static int show_help;
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version;
48 static struct option const long_options[] =
50 {"append", no_argument, NULL, 'a'},
51 {"help", no_argument, &show_help, 1},
52 {"ignore-interrupts", no_argument, NULL, 'i'},
53 {"version", no_argument, &show_version, 1},
54 {NULL, 0, NULL, 0}
57 static void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
66 printf (_("\
67 Copy standard input to each FILE, and also to standard output.\n\
68 \n\
69 -a, --append append to the given FILEs, do not overwrite\n\
70 -i, --ignore-interrupts ignore interrupt signals\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
73 "));
74 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
76 exit (status);
79 int
80 main (int argc, char **argv)
82 int errs;
83 int optc;
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
90 append = 0;
91 ignore_interrupts = 0;
93 while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
95 switch (optc)
97 case 0:
98 break;
100 case 'a':
101 append = 1;
102 break;
104 case 'i':
105 ignore_interrupts = 1;
106 break;
108 default:
109 usage (1);
113 if (show_version)
115 printf ("tee (%s) %s\n", GNU_PACKAGE, VERSION);
116 exit (0);
119 if (show_help)
120 usage (0);
122 if (ignore_interrupts)
124 #ifdef _POSIX_SOURCE
125 struct sigaction sigact;
127 sigact.sa_handler = SIG_IGN;
128 sigemptyset (&sigact.sa_mask);
129 sigact.sa_flags = 0;
130 sigaction (SIGINT, &sigact, NULL);
131 #else /* !_POSIX_SOURCE */
132 signal (SIGINT, SIG_IGN);
133 #endif /* _POSIX_SOURCE */
136 /* Don't let us be killed if one of the output files is a pipe that
137 doesn't consume all its input. */
138 #ifdef _POSIX_SOURCE
140 struct sigaction sigact;
142 sigact.sa_handler = SIG_IGN;
143 sigemptyset (&sigact.sa_mask);
144 sigact.sa_flags = 0;
145 sigaction (SIGPIPE, &sigact, NULL);
147 #else
148 signal (SIGPIPE, SIG_IGN);
149 #endif
151 errs = tee (argc - optind, (const char **) &argv[optind]);
152 if (close (0) != 0)
153 error (1, errno, _("standard input"));
154 if (close (1) != 0)
155 error (1, errno, _("standard output"));
156 exit (errs);
159 /* Copy the standard input into each of the NFILES files in FILES
160 and into the standard output.
161 Return 0 if successful, 1 if any errors occur. */
163 static int
164 tee (int nfiles, const char **files)
166 int *descriptors;
167 char buffer[BUFSIZ];
168 register int bytes_read, i, ret = 0, mode;
170 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
172 mode = O_WRONLY | O_CREAT;
173 if (append)
174 mode |= O_APPEND;
175 else
176 mode |= O_TRUNC;
178 /* Move all the names `up' one in the argv array to make room for
179 the entry for standard output. This writes into argv[argc]. */
180 for (i = nfiles; i >= 1; i--)
181 files[i] = files[i - 1];
183 /* In the array of NFILES + 1 descriptors, make
184 the first one correspond to standard output. */
185 descriptors[0] = 1;
186 files[0] = _("standard output");
188 for (i = 1; i <= nfiles; i++)
190 descriptors[i] = open (files[i], mode, 0666);
191 if (descriptors[i] == -1)
193 error (0, errno, "%s", files[i]);
194 ret = 1;
198 while (1)
200 bytes_read = read (0, buffer, sizeof buffer);
201 #ifdef EINTR
202 if (bytes_read < 0 && errno == EINTR)
203 continue;
204 #endif
205 if (bytes_read <= 0)
206 break;
208 /* Write to all NFILES + 1 descriptors.
209 Standard output is the first one. */
210 for (i = 0; i <= nfiles; i++)
212 if (descriptors[i] != -1
213 && full_write (descriptors[i], buffer, bytes_read) < 0)
215 error (0, errno, "%s", files[i]);
216 /* Don't close stdout. That's done in main. */
217 if (descriptors[i] != 1)
218 close (descriptors[i]);
219 descriptors[i] = -1;
220 ret = 1;
225 if (bytes_read == -1)
227 error (0, errno, _("read error"));
228 ret = 1;
231 /* Close the files, but not standard output. */
232 for (i = 1; i <= nfiles; i++)
233 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
235 error (0, errno, "%s", files[i]);
236 ret = 1;
239 free (descriptors);
241 return ret;