1992-11-01 05:44:29 +00:00
|
|
|
/* tee - read from standard input and write to standard output and files.
|
2005-04-11 20:12:17 +00:00
|
|
|
Copyright (C) 85,1990-2005 Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
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
|
1996-03-24 18:33:12 +00:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 07:58:31 +00:00
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
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 <stdio.h>
|
|
|
|
|
#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"
|
2005-04-11 20:12:17 +00:00
|
|
|
#include "stdio-safer.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"
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
#define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
|
1999-03-31 04:16:08 +00:00
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
static bool tee (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
|
|
|
|
|
|
|
|
/* The name that this program was run with. */
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
1992-11-12 04:14:54 +00: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"),
|
1993-10-17 03:57:04 +00:00
|
|
|
program_name);
|
|
|
|
|
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);
|
2002-07-02 09:09:10 +00:00
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
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);
|
1992-11-01 05:44:29 +00:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
case 'a':
|
2004-08-03 20:06:29 +00:00
|
|
|
append = true;
|
1992-11-01 05:44:29 +00:00
|
|
|
break;
|
1993-10-12 01:52:24 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
case 'i':
|
2004-08-03 20:06:29 +00:00
|
|
|
ignore_interrupts = true;
|
1992-11-01 05:44:29 +00:00
|
|
|
break;
|
1993-10-12 01:52:24 +00:00
|
|
|
|
1999-03-31 05:52:46 +00:00
|
|
|
case_GETOPT_HELP_CHAR;
|
|
|
|
|
|
2003-10-18 10:05:47 +00:00
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
1999-03-31 05:52:46 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
default:
|
2002-08-31 08:52:10 +00:00
|
|
|
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
|
|
|
|
2004-08-03 20:06:29 +00:00
|
|
|
ok = tee (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
|
1996-01-06 11:44:05 +00:00
|
|
|
tee (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;
|
1999-07-26 07:11:27 +00:00
|
|
|
const char *mode_string = (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];
|
|
|
|
|
|
2000-08-01 07:10:57 +00:00
|
|
|
SET_BINARY2 (0, 1);
|
|
|
|
|
|
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");
|
1999-07-26 07:11:27 +00: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], "-")
|
|
|
|
|
? stdout
|
2005-04-11 20:12:17 +00:00
|
|
|
: fopen_safer (files[i], mode_string));
|
1999-07-26 07:11:27 +00:00
|
|
|
if (descriptors[i] == NULL)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
error (0, errno, "%s", files[i]);
|
2004-08-03 20:06:29 +00:00
|
|
|
ok = false;
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
1999-07-26 07:11:27 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SETVBUF (descriptors[i], NULL, _IONBF, 0);
|
2000-08-01 07:10:57 +00:00
|
|
|
SET_BINARY (fileno (descriptors[i]));
|
1999-07-26 07:11:27 +00:00
|
|
|
}
|
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);
|
|
|
|
|
#ifdef EINTR
|
|
|
|
|
if (bytes_read < 0 && errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
#endif
|
|
|
|
|
if (bytes_read <= 0)
|
|
|
|
|
break;
|
1993-12-28 23:22:03 +00:00
|
|
|
|
|
|
|
|
/* Write to all NFILES + 1 descriptors.
|
1993-12-28 23:46:12 +00:00
|
|
|
Standard output is the first one. */
|
1993-12-28 23:22:03 +00:00
|
|
|
for (i = 0; i <= nfiles; i++)
|
2003-09-22 16:00:49 +00:00
|
|
|
if (descriptors[i]
|
2003-09-23 07:51:02 +00:00
|
|
|
&& fwrite (buffer, 1, bytes_read, descriptors[i]) != bytes_read)
|
2003-09-22 16:00:49 +00:00
|
|
|
{
|
|
|
|
|
error (0, errno, "%s", files[i]);
|
|
|
|
|
descriptors[i] = NULL;
|
2004-08-03 20:06:29 +00:00
|
|
|
ok = false;
|
2003-09-22 16:00:49 +00:00
|
|
|
}
|
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], "-")
|
|
|
|
|
&& descriptors[i] && fclose (descriptors[i]) != 0)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
error (0, errno, "%s", files[i]);
|
2004-08-03 20:06:29 +00:00
|
|
|
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
|
|
|
}
|