(add_tabstop): Give correct size when reallocating tab_list buffer.
[platform/upstream/coreutils.git] / src / tee.c
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.
3
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.
8
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.
13
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.  */
17
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <getopt.h>
25
26 #include "system.h"
27 #include "version.h"
28 #include "error.h"
29
30 char *xmalloc ();
31 int full_write ();
32
33 static int tee ();
34
35 /* If nonzero, append to output files rather than truncating them. */
36 static int append;
37
38 /* If nonzero, ignore interrupts. */
39 static int ignore_interrupts;
40
41 /* The name that this program was run with. */
42 char *program_name;
43
44 /* If non-zero, display usage information and exit.  */
45 static int show_help;
46
47 /* If non-zero, print the version on standard output and exit.  */
48 static int show_version;
49
50 static struct option const long_options[] =
51 {
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}
57 };
58
59 static void
60 usage (status)
61      int status;
62 {
63   if (status != 0)
64     fprintf (stderr, "Try `%s --help' for more information.\n",
65              program_name);
66   else
67     {
68       printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
69       printf ("\
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 ");
76     }
77   exit (status);
78 }
79
80 void
81 main (argc, argv)
82      int argc;
83      char **argv;
84 {
85   int errs;
86   int optc;
87         
88   program_name = argv[0];
89   append = 0;
90   ignore_interrupts = 0;
91
92   while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
93          != EOF)
94     {
95       switch (optc)
96         {
97         case 0:
98           break;
99
100         case 'a':
101           append = 1;
102           break;
103
104         case 'i':
105           ignore_interrupts = 1;
106           break;
107
108         default:
109           usage (1);
110         }
111     }
112
113   if (show_version)
114     {
115       printf ("tee - %s\n", version_string);
116       exit (0);
117     }
118
119   if (show_help)
120     usage (0);
121
122   if (ignore_interrupts)
123     {
124 #ifdef _POSIX_SOURCE
125       struct sigaction sigact;
126
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 */
134     }
135
136   errs = tee (argc - optind, (const char **) &argv[optind]);
137   if (close (0) != 0)
138     error (1, errno, "standard input");
139   if (close (1) != 0)
140     error (1, errno, "standard output");
141   exit (errs);
142 }
143
144 /* Copy the standard input into each of the NFILES files in FILES
145    and into the standard output.
146    Return 0 if successful, 1 if any errors occur. */
147
148 static int
149 tee (nfiles, files)
150      int nfiles;
151      const char **files;
152 {
153   int *descriptors;
154   char buffer[BUFSIZ];
155   register int bytes_read, i, ret = 0, mode;
156
157   descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
158
159   mode = O_WRONLY | O_CREAT;
160   if (append)
161     mode |= O_APPEND;
162   else
163     mode |= O_TRUNC;
164
165   /* Move all the names `up' one in the argv array to make room for
166      the entry for standard output.  This writes into argv[argc].  */
167   for (i = nfiles; i >= 1; i--)
168     files[i] = files[i - 1];
169
170   /* In the array of NFILES + 1 descriptors, make
171      the first one correspond to standard output.   */
172   descriptors[0] = 1;
173   files[0] = "standard output";
174
175   for (i = 1; i <= nfiles; i++)
176     {
177       descriptors[i] = open (files[i], mode, 0666);
178       if (descriptors[i] == -1)
179         {
180           error (0, errno, "%s", files[i]);
181           ret = 1;
182         }
183     }
184
185   while (1)
186     {
187       bytes_read = read (0, buffer, sizeof buffer);
188 #ifdef EINTR
189       if (bytes_read < 0 && errno == EINTR)
190         continue;
191 #endif
192       if (bytes_read <= 0)
193         break;
194
195       /* Write to all NFILES + 1 descriptors.
196          Standard output is the first one.  */
197       for (i = 0; i <= nfiles; i++)
198         {
199           if (descriptors[i] != -1
200               && full_write (descriptors[i], buffer, bytes_read) < 0)
201             {
202               error (0, errno, "%s", files[i]);
203               /* Don't close stdout.  That's done in main.  */
204               if (descriptors[i] != 1)
205                 close (descriptors[i]);
206               descriptors[i] = -1;
207               ret = 1;
208             }
209         }
210     }
211
212   if (bytes_read == -1)
213     {
214       error (0, errno, "read error");
215       ret = 1;
216     }
217
218   /* Close the files, but not standard output.  */
219   for (i = 1; i <= nfiles; i++)
220     if (descriptors[i] != -1 && close (descriptors[i]) != 0)
221       {
222         error (0, errno, "%s", files[i]);
223         ret = 1;
224       }
225
226   free (descriptors);
227
228   return ret;
229 }