diff options
author | Andreas Gruenbacher <agruen@suse.de> | 2009-06-05 21:52:47 +0200 |
---|---|---|
committer | Andreas Gruenbacher <agruen@suse.de> | 2009-06-05 21:54:58 +0200 |
commit | dee5d61753f4ba75c48fb5caba4e6a85d32796ff (patch) | |
tree | 8573979b6564d1df24dcaf3be4a42740a58403d8 | |
parent | 4027b46b0f2e0c6bb79923864c7df655f1668f5e (diff) | |
download | patch-dee5d61753f4ba75c48fb5caba4e6a85d32796ff.tar.gz |
To redirect messages, instead of assigning stderr to stdout, use dup2
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | gl/lib/dup2.c | 57 | ||||
-rw-r--r-- | gl/m4/dup2.m4 | 15 | ||||
-rw-r--r-- | src/patch.c | 8 |
4 files changed, 83 insertions, 2 deletions
@@ -5,6 +5,11 @@ * Makefile.in (GETOPT_H): Include subdirectory (gl/lib/) when using the gnulib version. + * src/patch.c: When sending output to stdout, use dup2 to redirect + messages to stderr; simply assigning stderr to stdout doesn't work on + some platforms. + * gl/lib/dup2.c, gl/m4/dup2.m4: Import from gnulib. + 2009-05-12 Andreas Gruenbacher <agruen@suse.de> * src/merge.c (locate_merge): Make sure to return a line number diff --git a/gl/lib/dup2.c b/gl/lib/dup2.c new file mode 100644 index 0000000..0999082 --- /dev/null +++ b/gl/lib/dup2.c @@ -0,0 +1,57 @@ +/* Duplicate an open file descriptor to a specified file descriptor. + + Copyright (C) 1999, 2004, 2005, 2006, 2007 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 + the Free Software Foundation; either version 3 of the License, 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 + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* written by Paul Eggert */ + +#include <config.h> + +/* Specification. */ +#include <unistd.h> + +#include <errno.h> +#include <fcntl.h> + +#ifndef F_DUPFD +static int +dupfd (int fd, int desired_fd) +{ + int duplicated_fd = dup (fd); + if (duplicated_fd < 0 || duplicated_fd == desired_fd) + return duplicated_fd; + else + { + int r = dupfd (fd, desired_fd); + int e = errno; + close (duplicated_fd); + errno = e; + return r; + } +} +#endif + +int +dup2 (int fd, int desired_fd) +{ + if (fd == desired_fd) + return fd; + close (desired_fd); +#ifdef F_DUPFD + return fcntl (fd, F_DUPFD, desired_fd); +#else + return dupfd (fd, desired_fd); +#endif +} diff --git a/gl/m4/dup2.m4 b/gl/m4/dup2.m4 new file mode 100644 index 0000000..0549823 --- /dev/null +++ b/gl/m4/dup2.m4 @@ -0,0 +1,15 @@ +#serial 5 +dnl Copyright (C) 2002, 2005, 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_DUP2], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_CHECK_FUNCS_ONCE([dup2]) + if test $ac_cv_func_dup2 = no; then + HAVE_DUP2=0 + AC_LIBOBJ([dup2]) + fi +]) diff --git a/src/patch.c b/src/patch.c index 79718a7..897e796 100644 --- a/src/patch.c +++ b/src/patch.c @@ -1349,8 +1349,12 @@ init_output (char const *name, int open_flags, struct outstate *outstate) outstate->ofp = create_output_file (name, open_flags); else { - outstate->ofp = stdout; - stdout = stderr; + int stdout_dup = dup (fileno (stdout)); + outstate->ofp = fdopen (stdout_dup, "a"); + if (stdout_dup == -1 || ! outstate->ofp) + pfatal ("Failed to duplicate standard output"); + if (dup2 (fileno (stderr), fileno (stdout)) == -1) + pfatal ("Failed to redirect messages to standard error"); } outstate->after_newline = true; |