{"thread":{"id":"12462","subject":"[PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","startedAt":"2008-03-03T19:06:23Z","lastAt":"2008-03-03T19:06:23Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"70826","messageId":"20080303200623.ac862ed1.chriscool@tuxfamily.org","threadId":"12462","inReplyTo":null,"subject":"[PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2008-03-03T19:06:23Z","receivedAt":"2008-03-03T19:06:23Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: \"Shawn O. Pearce\" <spearce@spearce.org>\n\nWith this patch, in the 'start_command' function after forking\nwe now take care of stderr in the child process before stdout.\n\nThis way if 'start_command' is called with a 'child_process'\nargument like this:\n\n\t.err = -1;\n\t.stdout_to_stderr = 1;\n\nthen stderr will be redirected to a pipe before stdout is\nredirected to stderr. So we can now get the process' stdout\nfrom the pipe (as well as its stderr).\n\nUpdate documentation in 'api-run-command.txt' accordingly.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Documentation/technical/api-run-command.txt |    7 ++++---\n run-command.c                               |   14 +++++++-------\n 2 files changed, 11 insertions(+), 10 deletions(-)\n\n\tThe changes since the previous versions are:\n\n\t\t- added documentation,\n\t\t- added 'From: \"Shawn O. Pearce\" <spearce@spearce.org>',\n\t\t- improved title.\n\ndiff --git a/Documentation/technical/api-run-command.txt b/Documentation/technical/api-run-command.txt\nindex dfbf9ac..c097f8b 100644\n--- a/Documentation/technical/api-run-command.txt\n+++ b/Documentation/technical/api-run-command.txt\n@@ -111,9 +111,10 @@ stderr as follows:\n \t.no_stdin, .no_stdout, .no_stderr: The respective channel is\n \t\tredirected to /dev/null.\n \n-\t.stdout_to_stderr: stdout of the child is redirected to the\n-\t\tparent's stderr (i.e. *not* to what .err or\n-\t\t.no_stderr specify).\n+\t.stdout_to_stderr: stdout of the child is redirected to its\n+\t\tstderr. This happens before stderr is itself\n+\t\tredirected. So stdout will follow stderr to wherever\n+\t\tit is redirected.\n \n To modify the environment of the sub-process, specify an array of\n string pointers (NULL terminated) in .env:\ndiff --git a/run-command.c b/run-command.c\nindex 743757c..44100a7 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -91,6 +91,13 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(cmd->in);\n \t\t}\n \n+\t\tif (cmd->no_stderr)\n+\t\t\tdup_devnull(2);\n+\t\telse if (need_err) {\n+\t\t\tdup2(fderr[1], 2);\n+\t\t\tclose_pair(fderr);\n+\t\t}\n+\n \t\tif (cmd->no_stdout)\n \t\t\tdup_devnull(1);\n \t\telse if (cmd->stdout_to_stderr)\n@@ -103,13 +110,6 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(cmd->out);\n \t\t}\n \n-\t\tif (cmd->no_stderr)\n-\t\t\tdup_devnull(2);\n-\t\telse if (need_err) {\n-\t\t\tdup2(fderr[1], 2);\n-\t\t\tclose_pair(fderr);\n-\t\t}\n"}]}