34dc614c8744eb56b10b5a7c4d39e84b936392ce
[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 __P ((int nfiles, const char **files));
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 nonzero, display usage information and exit.  */
45 static int show_help;
46
47 /* If nonzero, 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 (int status)
61 {
62   if (status != 0)
63     fprintf (stderr, _("Try `%s --help' for more information.\n"),
64              program_name);
65   else
66     {
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 "));
76     }
77   exit (status);
78 }
79
80 void
81 main (int argc, char **argv)
82 {
83   int errs;
84   int optc;
85
86   program_name = argv[0];
87   append = 0;
88   ignore_interrupts = 0;
89
90   while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
91          != EOF)
92     {
93       switch (optc)
94         {
95         case 0:
96           break;
97
98         case 'a':
99           append = 1;
100           break;
101
102         case 'i':
103           ignore_interrupts = 1;
104           break;
105
106         default:
107           usage (1);
108         }
109     }
110
111   if (show_version)
112     {
113       printf ("tee - %s\n", version_string);
114       exit (0);
115     }
116
117   if (show_help)
118     usage (0);
119
120   if (ignore_interrupts)
121     {
122 #ifdef _POSIX_SOURCE
123       struct sigaction sigact;
124
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 */
132     }
133
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);
140 }
141
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. */
145
146 static int
147 tee (int nfiles, const char **files)
148 {
149   int *descriptors;
150   char buffer[BUFSIZ];
151   register int bytes_read, i, ret = 0, mode;
152
153   descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int));
154
155   mode = O_WRONLY | O_CREAT;
156   if (append)
157     mode |= O_APPEND;
158   else
159     mode |= O_TRUNC;
160
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];
165
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");
170
171   for (i = 1; i <= nfiles; i++)
172     {
173       descriptors[i] = open (files[i], mode, 0666);
174       if (descriptors[i] == -1)
175         {
176           error (0, errno, "%s", files[i]);
177           ret = 1;
178         }
179     }
180
181   while (1)
182     {
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;
190
191       /* Write to all NFILES + 1 descriptors.
192          Standard output is the first one.  */
193       for (i = 0; i <= nfiles; i++)
194         {
195           if (descriptors[i] != -1
196               && full_write (descriptors[i], buffer, bytes_read) < 0)
197             {
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;
204             }
205         }
206     }
207
208   if (bytes_read == -1)
209     {
210       error (0, errno, _("read error"));
211       ret = 1;
212     }
213
214   /* Close the files, but not standard output.  */
215   for (i = 1; i <= nfiles; i++)
216     if (descriptors[i] != -1 && close (descriptors[i]) != 0)
217       {
218         error (0, errno, "%s", files[i]);
219         ret = 1;
220       }
221
222   free (descriptors);
223
224   return ret;
225 }