diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2016-10-05 16:37:23 +0300 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2016-10-17 16:21:46 +0200 |
commit | 7dc71b1ff87913cba31a26f081b65d85b429186f (patch) | |
tree | 874262c6dd84f95dbd8e44ba1f97edab51560fa9 | |
parent | 95e5d0ca8e4769b87f4c547696b42f42c399e127 (diff) |
Fix redirection of process stdout to stderr in POSIX implementation
-rw-r--r-- | butl/process.cxx | 24 |
1 files changed, 20 insertions, 4 deletions
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. // |