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)
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 */
22 #include <sys/types.h>
33 static int tee
__P ((int nfiles
, const char **files
));
35 /* If nonzero, append to output files rather than truncating them. */
38 /* If nonzero, ignore interrupts. */
39 static int ignore_interrupts
;
41 /* The name that this program was run with. */
44 /* If nonzero, display usage information and exit. */
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},
63 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
67 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
69 Copy standard input to each FILE, and also to standard output.\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\
81 main (int argc
, char **argv
)
86 program_name
= argv
[0];
88 ignore_interrupts
= 0;
90 while ((optc
= getopt_long (argc
, argv
, "ai", long_options
, (int *) 0))
103 ignore_interrupts
= 1;
113 printf ("tee - %s\n", version_string
);
120 if (ignore_interrupts
)
123 struct sigaction sigact
;
125 sigact
.sa_handler
= SIG_IGN
;
126 sigemptyset (&sigact
.sa_mask
);
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
]);
136 error (1, errno
, _("standard input"));
138 error (1, errno
, _("standard output"));
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. */
147 tee (int nfiles
, const char **files
)
151 register int bytes_read
, i
, ret
= 0, mode
;
153 descriptors
= (int *) xmalloc ((nfiles
+ 1) * sizeof (int));
155 mode
= O_WRONLY
| O_CREAT
;
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. */
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
]);
183 bytes_read
= read (0, buffer
, sizeof buffer
);
185 if (bytes_read
< 0 && errno
== EINTR
)
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
]);
208 if (bytes_read
== -1)
210 error (0, errno
, _("read error"));
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
]);