(memcasecmp): Declare local I to be unsigned to avoid warning from gcc -Wall.
[coreutils.git] / src / tee.c
blob34dc614c8744eb56b10b5a7c4d39e84b936392ce
1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85, 90, 91, 92, 93, 94, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "version.h"
28 #include "error.h"
30 char *xmalloc ();
31 int full_write ();
33 static int tee __P ((int nfiles, const char **files));
35 /* If nonzero, append to output files rather than truncating them. */
36 static int append;
38 /* If nonzero, ignore interrupts. */
39 static int ignore_interrupts;
41 /* The name that this program was run with. */
42 char *program_name;
44 /* If nonzero, display usage information and exit. */
45 static int show_help;
47 /* If nonzero, print the version on standard output and exit. */
48 static int show_version;
50 static struct option const long_options[] =
52 {"append", no_argument, NULL, 'a'},
53 {"help", no_argument, &show_help, 1},
54 {"ignore-interrupts", no_argument, NULL, 'i'},
55 {"version", no_argument, &show_version, 1},
56 {NULL, 0, NULL, 0}
59 static void
60 usage (int status)
62 if (status != 0)
63 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 program_name);
65 else
67 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
68 printf (_("\
69 Copy standard input to each FILE, and also to standard output.\n\
70 \n\
71 -a, --append append to the given FILEs, do not overwrite\n\
72 -i, --ignore-interrupts ignore interrupt signals\n\
73 --help display this help and exit\n\
74 --version output version information and exit\n\
75 "));
77 exit (status);
80 void
81 main (int argc, char **argv)
83 int errs;
84 int optc;
86 program_name = argv[0];
87 append = 0;
88 ignore_interrupts = 0;
90 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
91 != EOF)
93 switch (optc)
95 case 0:
96 break;
98 case 'a':
99 append = 1;
100 break;
102 case 'i':
103 ignore_interrupts = 1;
104 break;
106 default:
107 usage (1);
111 if (show_version)
113 printf ("tee - %s\n", version_string);
114 exit (0);
117 if (show_help)
118 usage (0);
120 if (ignore_interrupts)
122 #ifdef _POSIX_SOURCE
123 struct sigaction sigact;
125 sigact.sa_handler = SIG_IGN;
126 sigemptyset (&sigact.sa_mask);
127 sigact.sa_flags = 0;
128 sigaction (SIGINT, &sigact, NULL);
129 #else /* !_POSIX_SOURCE */
130 signal (SIGINT, SIG_IGN);
131 #endif /* _POSIX_SOURCE */
134 errs = tee (argc - optind, (const char **) &argv[optind]);
135 if (close (0) != 0)
136 error (1, errno, _("standard input"));
137 if (close (1) != 0)
138 error (1, errno, _("standard output"));
139 exit (errs);
142 /* Copy the standard input into each of the NFILES files in FILES
143 and into the standard output.
144 Return 0 if successful, 1 if any errors occur. */
146 static int
147 tee (int nfiles, const char **files)
149 int *descriptors;
150 char buffer[BUFSIZ];
151 register int bytes_read, i, ret = 0, mode;
153 descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
155 mode = O_WRONLY | O_CREAT;
156 if (append)
157 mode |= O_APPEND;
158 else
159 mode |= O_TRUNC;
161 /* Move all the names `up' one in the argv array to make room for
162 the entry for standard output. This writes into argv[argc]. */
163 for (i = nfiles; i >= 1; i--)
164 files[i] = files[i - 1];
166 /* In the array of NFILES + 1 descriptors, make
167 the first one correspond to standard output. */
168 descriptors[0] = 1;
169 files[0] = _("standard output");
171 for (i = 1; i <= nfiles; i++)
173 descriptors[i] = open (files[i], mode, 0666);
174 if (descriptors[i] == -1)
176 error (0, errno, "%s", files[i]);
177 ret = 1;
181 while (1)
183 bytes_read = read (0, buffer, sizeof buffer);
184 #ifdef EINTR
185 if (bytes_read < 0 && errno == EINTR)
186 continue;
187 #endif
188 if (bytes_read <= 0)
189 break;
191 /* Write to all NFILES + 1 descriptors.
192 Standard output is the first one. */
193 for (i = 0; i <= nfiles; i++)
195 if (descriptors[i] != -1
196 && full_write (descriptors[i], buffer, bytes_read) < 0)
198 error (0, errno, "%s", files[i]);
199 /* Don't close stdout. That's done in main. */
200 if (descriptors[i] != 1)
201 close (descriptors[i]);
202 descriptors[i] = -1;
203 ret = 1;
208 if (bytes_read == -1)
210 error (0, errno, _("read error"));
211 ret = 1;
214 /* Close the files, but not standard output. */
215 for (i = 1; i <= nfiles; i++)
216 if (descriptors[i] != -1 && close (descriptors[i]) != 0)
218 error (0, errno, "%s", files[i]);
219 ret = 1;
222 free (descriptors);
224 return ret;