summaryrefslogtreecommitdiff
path: root/src/tee.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tee.c')
-rw-r--r--src/tee.c95
1 files changed, 79 insertions, 16 deletions
diff --git a/src/tee.c b/src/tee.c
index be365604..3c39a4ab 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -1,5 +1,5 @@
/* tee - read from standard input and write to standard output and files.
- Copyright (C) 1985-2014 Free Software Foundation, Inc.
+ Copyright (C) 1985-2015 Free Software Foundation, Inc.
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
@@ -22,6 +22,7 @@
#include <getopt.h>
#include "system.h"
+#include "argmatch.h"
#include "error.h"
#include "fadvise.h"
#include "stdio--.h"
@@ -43,15 +44,38 @@ static bool append;
/* If true, ignore interrupts. */
static bool ignore_interrupts;
+enum output_error
+ {
+ output_error_sigpipe, /* traditional behavior, sigpipe enabled. */
+ output_error_warn, /* warn on EPIPE, but continue. */
+ output_error_warn_nopipe, /* ignore EPIPE, continue. */
+ output_error_exit, /* exit on any output error. */
+ output_error_exit_nopipe /* exit on any output error except EPIPE. */
+ };
+
+static enum output_error output_error;
+
static struct option const long_options[] =
{
{"append", no_argument, NULL, 'a'},
{"ignore-interrupts", no_argument, NULL, 'i'},
+ {"output-error", optional_argument, NULL, 'p'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
+static char const *const output_error_args[] =
+{
+ "warn", "warn-nopipe", "exit", "exit-nopipe", NULL
+};
+static enum output_error const output_error_types[] =
+{
+ output_error_warn, output_error_warn_nopipe,
+ output_error_exit, output_error_exit_nopipe
+};
+ARGMATCH_VERIFY (output_error_args, output_error_types);
+
void
usage (int status)
{
@@ -66,13 +90,24 @@ Copy standard input to each FILE, and also to standard output.\n\
-a, --append append to the given FILEs, do not overwrite\n\
-i, --ignore-interrupts ignore interrupt signals\n\
"), stdout);
+ fputs (_("\
+ -p, --output-error[=MODE] behavior on write error. See MODE details below\n\
+"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
-If a FILE is -, copy again to standard output.\n\
+MODE determines behavior with write errors on the outputs:\n\
+ 'warn' diagnose errors writing to any output\n\
+ 'warn-nopipe' diagnose errors writing to any output not a pipe\n\
+ 'exit' exit on error writing to any output\n\
+ 'exit-nopipe' exit on error writing to any output not a pipe\n\
+The default MODE for the -p option is 'warn-nopipe'.\n\
+The default operation when --output-error is not specified, is to\n\
+exit immediately on error writing to a pipe, and diagnose errors\n\
+writing to non pipe outputs.\n\
"), stdout);
- emit_ancillary_info ();
+ emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
@@ -94,7 +129,7 @@ main (int argc, char **argv)
append = false;
ignore_interrupts = false;
- while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
+ while ((optc = getopt_long (argc, argv, "aip", long_options, NULL)) != -1)
{
switch (optc)
{
@@ -106,6 +141,14 @@ main (int argc, char **argv)
ignore_interrupts = true;
break;
+ case 'p':
+ if (optarg)
+ output_error = XARGMATCH ("--output-error", optarg,
+ output_error_args, output_error_types);
+ else
+ output_error = output_error_warn_nopipe;
+ break;
+
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -118,6 +161,9 @@ main (int argc, char **argv)
if (ignore_interrupts)
signal (SIGINT, SIG_IGN);
+ if (output_error != output_error_sigpipe)
+ signal (SIGPIPE, SIG_IGN);
+
/* Do *not* warn if tee is given no file arguments.
POSIX requires that it work when given no arguments. */
@@ -125,7 +171,7 @@ main (int argc, char **argv)
if (close (STDIN_FILENO) != 0)
error (EXIT_FAILURE, errno, _("standard input"));
- exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+ return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* Copy the standard input into each of the NFILES files in FILES
@@ -135,9 +181,10 @@ main (int argc, char **argv)
static bool
tee_files (int nfiles, const char **files)
{
+ size_t n_outputs = 0;
FILE **descriptors;
char buffer[BUFSIZ];
- ssize_t bytes_read;
+ ssize_t bytes_read = 0;
int i;
bool ok = true;
char const *mode_string =
@@ -164,22 +211,27 @@ tee_files (int nfiles, const char **files)
descriptors[0] = stdout;
files[0] = _("standard output");
setvbuf (stdout, NULL, _IONBF, 0);
+ n_outputs++;
for (i = 1; i <= nfiles; i++)
{
- descriptors[i] = (STREQ (files[i], "-")
- ? stdout
- : fopen (files[i], mode_string));
+ /* Do not treat "-" specially - as mandated by POSIX. */
+ descriptors[i] = fopen (files[i], mode_string);
if (descriptors[i] == NULL)
{
- error (0, errno, "%s", files[i]);
+ error (output_error == output_error_exit
+ || output_error == output_error_exit_nopipe,
+ errno, "%s", files[i]);
ok = false;
}
else
- setvbuf (descriptors[i], NULL, _IONBF, 0);
+ {
+ setvbuf (descriptors[i], NULL, _IONBF, 0);
+ n_outputs++;
+ }
}
- while (1)
+ while (n_outputs)
{
bytes_read = read (0, buffer, sizeof buffer);
if (bytes_read < 0 && errno == EINTR)
@@ -193,9 +245,21 @@ tee_files (int nfiles, const char **files)
if (descriptors[i]
&& fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
{
- error (0, errno, "%s", files[i]);
+ int w_errno = errno;
+ bool fail = errno != EPIPE || (output_error == output_error_exit
+ || output_error == output_error_warn);
+ if (descriptors[i] == stdout)
+ clearerr (stdout); /* Avoid redundant close_stdout diagnostic. */
+ if (fail)
+ {
+ error (output_error == output_error_exit
+ || output_error == output_error_exit_nopipe,
+ w_errno, "%s", files[i]);
+ }
descriptors[i] = NULL;
- ok = false;
+ if (fail)
+ ok = false;
+ n_outputs--;
}
}
@@ -207,8 +271,7 @@ tee_files (int nfiles, const char **files)
/* Close the files, but not standard output. */
for (i = 1; i <= nfiles; i++)
- if (!STREQ (files[i], "-")
- && descriptors[i] && fclose (descriptors[i]) != 0)
+ if (descriptors[i] && fclose (descriptors[i]) != 0)
{
error (0, errno, "%s", files[i]);
ok = false;