From 7dc71b1ff87913cba31a26f081b65d85b429186f Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 5 Oct 2016 16:37:23 +0300 Subject: Fix redirection of process stdout to stderr in POSIX implementation --- butl/process.cxx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'butl/process.cxx') diff --git a/butl/process.cxx b/butl/process.cxx index 38948a1..64366ba 100644 --- a/butl/process.cxx +++ b/butl/process.cxx @@ -332,11 +332,27 @@ namespace butl if (in != STDIN_FILENO) duplicate (STDIN_FILENO, in, out_fd); - if (out != STDOUT_FILENO) - duplicate (STDOUT_FILENO, out, in_ofd); + // If stdout is redirected to stderr (out == 2) we need to duplicate it + // after duplicating stderr to pickup the proper fd. Otherwise keep the + // "natual" order of duplicate() calls, so if stderr is redirected to + // stdout it picks up the proper fd as well. + // + if (out == STDERR_FILENO) + { + if (err != STDERR_FILENO) + duplicate (STDERR_FILENO, err, in_efd); - if (err != STDERR_FILENO) - duplicate (STDERR_FILENO, err, in_efd); + if (out != STDOUT_FILENO) + duplicate (STDOUT_FILENO, out, in_ofd); + } + else + { + if (out != STDOUT_FILENO) + duplicate (STDOUT_FILENO, out, in_ofd); + + if (err != STDERR_FILENO) + duplicate (STDERR_FILENO, err, in_efd); + } // Change current working directory if requested. // -- cgit v1.1