1992-11-01 05:44:29 +00:00
|
|
|
/* tee - read from standard input and write to standard output and files.
|
2012-01-01 09:47:10 +01:00
|
|
|
Copyright (C) 1985, 1990-2006, 2008-2012 Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
1992-11-01 05:44:29 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-07-23 14:35:58 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
/* Mike Parker, Richard M. Stallman, and David MacKenzie */
|
|
|
|
|
|
1993-10-12 14:49:11 +00:00
|
|
|
#include <config.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <getopt.h>
|
1993-10-12 01:52:24 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include "system.h"
|
1994-12-20 05:26:44 +00:00
|
|
|
#include "error.h"
|
2010-07-20 18:51:01 +01:00
|
|
|
#include "fadvise.h"
|
2005-07-03 07:22:50 +00:00
|
|
|
#include "stdio--.h"
|
2008-10-12 14:50:02 +02:00
|
|
|
#include "xfreopen.h"
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1999-03-31 04:16:08 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "tee"
|
|
|
|
|
|
convert 3-author programs to use proper_name
g grep -E -l 'define AUTHORS "[^,]+", "[^,]+", "[^,]+"$'|xargs perl -pi -e \
's/(define AUTHORS) ("[^,]+"), ("[^,]+"), ("[^,]+")$/$1 \\\n proper_name ($2), \\\n proper_name ($3), \\\n proper_name ($4)/'
2008-05-19 16:28:14 +02:00
|
|
|
#define AUTHORS \
|
|
|
|
|
proper_name ("Mike Parker"), \
|
|
|
|
|
proper_name ("Richard M. Stallman"), \
|
|
|
|
|
proper_name ("David MacKenzie")
|
1999-03-31 04:16:08 +00:00
|
|
|
|
2006-06-22 12:50:32 +00:00
|
|
|
static bool tee_files (int nfiles, const char **files);
|
1992-11-12 04:14:54 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
/* If true, append to output files rather than truncating them. */
|
|
|
|
|
static bool append;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
/* If true, ignore interrupts. */
|
|
|
|
|
static bool ignore_interrupts;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
remove redundant const directives
In 1463824d8e7f72c31f1d803d7cfe2b608ccafc5c, I added some
missing "const" directives, as well as some new, redundant ones.
This removes the redundant ones. Pointed out by Eric Blake.
* base64.c, cat.c, chcon.c, chgrp.c, chmod.c, chown.c, comm.c:
* cp.c, csplit.c, cut.c, date.c, dd.c, df.c, dircolors.c, du.c:
* env.c, expand.c, fmt.c, fold.c, groups.c, head.c, id.c:
* install.c, join.c, kill.c, ln.c, ls.c, md5sum.c, mkdir.c:
* mkfifo.c, mknod.c, mktemp.c, mv.c, nice.c, nl.c, od.c:
* paste.c, pathchk.c, pinky.c, pr.c, ptx.c, readlink.c, rm.c:
* rmdir.c, runcon.c, seq.c, shred.c, shuf.c, sort.c, split.c:
* stat.c, stty.c, su.c, sum.c, tac.c, tail.c, tee.c, timeout.c:
* touch.c, tr.c, truncate.c, tty.c, uname.c, unexpand.c, uniq.c:
* wc.c, who.c: Remove redundant const directives.
* maint.mk (sc_const_long_option): Don't require redundant "const".
2008-06-16 14:55:06 +02:00
|
|
|
static struct option const long_options[] =
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
1992-12-02 18:51:53 +00:00
|
|
|
{"append", no_argument, NULL, 'a'},
|
|
|
|
|
{"ignore-interrupts", no_argument, NULL, 'i'},
|
1999-03-31 05:52:46 +00:00
|
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
|
|
|
{GETOPT_VERSION_OPTION_DECL},
|
1992-11-01 05:44:29 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
};
|
|
|
|
|
|
1999-01-25 14:33:38 +00:00
|
|
|
void
|
1996-01-06 11:44:05 +00:00
|
|
|
usage (int status)
|
1993-10-12 01:52:24 +00:00
|
|
|
{
|
2004-01-21 23:46:53 +00:00
|
|
|
if (status != EXIT_SUCCESS)
|
1995-08-08 04:37:34 +00:00
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
2009-08-22 18:56:06 +02:00
|
|
|
program_name);
|
1993-10-17 03:57:04 +00:00
|
|
|
else
|
1993-10-26 00:11:14 +00:00
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
|
2001-12-15 20:46:30 +00:00
|
|
|
fputs (_("\
|
1995-05-15 04:53:56 +00:00
|
|
|
Copy standard input to each FILE, and also to standard output.\n\
|
1993-10-17 03:57:04 +00:00
|
|
|
\n\
|
|
|
|
|
-a, --append append to the given FILEs, do not overwrite\n\
|
|
|
|
|
-i, --ignore-interrupts ignore interrupt signals\n\
|
2001-12-15 20:46:30 +00:00
|
|
|
"), stdout);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2004-08-10 22:08:58 +00:00
|
|
|
fputs (_("\
|
|
|
|
|
\n\
|
|
|
|
|
If a FILE is -, copy again to standard output.\n\
|
|
|
|
|
"), stdout);
|
2009-09-18 23:06:21 +01:00
|
|
|
emit_ancillary_info ();
|
1993-10-26 00:11:14 +00:00
|
|
|
}
|
1993-10-17 03:57:04 +00:00
|
|
|
exit (status);
|
1993-10-12 01:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
2004-08-03 20:06:29 +00:00
|
|
|
bool ok;
|
1992-11-01 05:44:29 +00:00
|
|
|
int optc;
|
1995-11-27 05:34:31 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
2008-06-03 08:34:09 +02:00
|
|
|
set_program_name (argv[0]);
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
|
2000-05-07 14:46:11 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
append = false;
|
|
|
|
|
ignore_interrupts = false;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1997-02-01 03:05:36 +00:00
|
|
|
while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
switch (optc)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
case 'a':
|
|
|
|
|
append = true;
|
|
|
|
|
break;
|
1993-10-12 01:52:24 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case 'i':
|
|
|
|
|
ignore_interrupts = true;
|
|
|
|
|
break;
|
1993-10-12 01:52:24 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_HELP_CHAR;
|
1999-03-31 05:52:46 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
1999-03-31 05:52:46 +00:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
default:
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
}
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ignore_interrupts)
|
2004-04-20 15:10:07 +00:00
|
|
|
signal (SIGINT, SIG_IGN);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2000-08-14 21:40:30 +00:00
|
|
|
/* Do *not* warn if tee is given no file arguments.
|
|
|
|
|
POSIX requires that it work when given no arguments. */
|
1999-07-26 07:11:27 +00:00
|
|
|
|
2006-06-22 12:50:32 +00:00
|
|
|
ok = tee_files (argc - optind, (const char **) &argv[optind]);
|
2002-08-25 14:32:02 +00:00
|
|
|
if (close (STDIN_FILENO) != 0)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("standard input"));
|
1999-01-14 15:37:04 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Copy the standard input into each of the NFILES files in FILES
|
|
|
|
|
and into the standard output.
|
2004-08-03 20:06:29 +00:00
|
|
|
Return true if successful. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
static bool
|
2006-06-22 12:50:32 +00:00
|
|
|
tee_files (int nfiles, const char **files)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
1999-07-26 07:11:27 +00:00
|
|
|
FILE **descriptors;
|
1992-11-01 05:44:29 +00:00
|
|
|
char buffer[BUFSIZ];
|
2004-08-03 20:06:29 +00:00
|
|
|
ssize_t bytes_read;
|
|
|
|
|
int i;
|
|
|
|
|
bool ok = true;
|
2005-07-11 18:26:52 +00:00
|
|
|
char const *mode_string =
|
|
|
|
|
(O_BINARY
|
|
|
|
|
? (append ? "ab" : "wb")
|
|
|
|
|
: (append ? "a" : "w"));
|
1993-12-28 23:22:03 +00:00
|
|
|
|
2004-01-04 21:07:40 +00:00
|
|
|
descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1993-12-28 23:46:12 +00:00
|
|
|
/* Move all the names `up' one in the argv array to make room for
|
|
|
|
|
the entry for standard output. This writes into argv[argc]. */
|
|
|
|
|
for (i = nfiles; i >= 1; i--)
|
|
|
|
|
files[i] = files[i - 1];
|
|
|
|
|
|
2005-07-11 18:26:52 +00:00
|
|
|
if (O_BINARY && ! isatty (STDIN_FILENO))
|
2008-10-12 14:50:02 +02:00
|
|
|
xfreopen (NULL, "rb", stdin);
|
2005-07-11 18:26:52 +00:00
|
|
|
if (O_BINARY && ! isatty (STDOUT_FILENO))
|
2008-10-12 14:50:02 +02:00
|
|
|
xfreopen (NULL, "wb", stdout);
|
2000-08-01 07:10:57 +00:00
|
|
|
|
2010-07-20 18:51:01 +01:00
|
|
|
fadvise (stdin, FADVISE_SEQUENTIAL);
|
|
|
|
|
|
1993-12-28 23:46:12 +00:00
|
|
|
/* In the array of NFILES + 1 descriptors, make
|
|
|
|
|
the first one correspond to standard output. */
|
1999-07-26 07:11:27 +00:00
|
|
|
descriptors[0] = stdout;
|
1995-08-08 04:37:34 +00:00
|
|
|
files[0] = _("standard output");
|
2006-12-12 14:34:23 +01:00
|
|
|
setvbuf (stdout, NULL, _IONBF, 0);
|
1993-12-28 23:46:12 +00:00
|
|
|
|
|
|
|
|
for (i = 1; i <= nfiles; i++)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
2004-08-10 22:08:58 +00:00
|
|
|
descriptors[i] = (STREQ (files[i], "-")
|
2009-08-22 18:56:06 +02:00
|
|
|
? stdout
|
|
|
|
|
: fopen (files[i], mode_string));
|
1999-07-26 07:11:27 +00:00
|
|
|
if (descriptors[i] == NULL)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
error (0, errno, "%s", files[i]);
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
1999-07-26 07:11:27 +00:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
setvbuf (descriptors[i], NULL, _IONBF, 0);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
1993-12-28 21:18:17 +00:00
|
|
|
while (1)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
1993-12-28 21:18:17 +00:00
|
|
|
bytes_read = read (0, buffer, sizeof buffer);
|
|
|
|
|
if (bytes_read < 0 && errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
if (bytes_read <= 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
break;
|
1993-12-28 23:22:03 +00:00
|
|
|
|
|
|
|
|
/* Write to all NFILES + 1 descriptors.
|
2009-08-22 18:56:06 +02:00
|
|
|
Standard output is the first one. */
|
1993-12-28 23:22:03 +00:00
|
|
|
for (i = 0; i <= nfiles; i++)
|
2009-08-22 18:56:06 +02:00
|
|
|
if (descriptors[i]
|
|
|
|
|
&& fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
|
|
|
|
|
{
|
|
|
|
|
error (0, errno, "%s", files[i]);
|
|
|
|
|
descriptors[i] = NULL;
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
1993-12-28 23:22:03 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
if (bytes_read == -1)
|
|
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
error (0, errno, _("read error"));
|
2004-08-03 20:06:29 +00:00
|
|
|
ok = false;
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
1993-12-28 23:46:12 +00:00
|
|
|
/* Close the files, but not standard output. */
|
|
|
|
|
for (i = 1; i <= nfiles; i++)
|
2005-04-05 11:40:53 +00:00
|
|
|
if (!STREQ (files[i], "-")
|
2009-08-22 18:56:06 +02:00
|
|
|
&& descriptors[i] && fclose (descriptors[i]) != 0)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
2009-08-22 18:56:06 +02:00
|
|
|
error (0, errno, "%s", files[i]);
|
|
|
|
|
ok = false;
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
1993-12-28 23:22:03 +00:00
|
|
|
free (descriptors);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
return ok;
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|