From: Andre Noll Date: Mon, 13 May 2024 21:14:53 +0000 (+0200) Subject: exec: Simplify fd handling. X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=8318f9ff5977d75f0b48d3523e7dc41ec3308833;p=paraslash.git exec: Simplify fd handling. The new code is equivalent, but a bit shorter. --- diff --git a/exec.c b/exec.c index 85dbaf94..38f445ee 100644 --- a/exec.c +++ b/exec.c @@ -46,29 +46,26 @@ static int para_exec(pid_t *pid, const char *file, char *const *const args, int } *pid = ret; if (!(*pid)) { /* child */ - if (fds[0] >= 0) { - if (fds[0]) { - close(in[1]); - if (in[0] != STDIN_FILENO) - dup2(in[0], STDIN_FILENO); - } else - dup2(null, STDIN_FILENO); + if (fds[0] == 0) + dup2(null, STDIN_FILENO); + else if (fds[0] > 0) { + close(in[1]); + if (in[0] != STDIN_FILENO) + dup2(in[0], STDIN_FILENO); } - if (fds[1] >= 0) { - if (fds[1]) { - close(out[0]); - if (out[1] != STDOUT_FILENO) - dup2(out[1], STDOUT_FILENO); - } else - dup2(null, STDOUT_FILENO); + if (fds[1] == 0) + dup2(null, STDOUT_FILENO); + else if (fds[1] > 0) { + close(out[0]); + if (out[1] != STDOUT_FILENO) + dup2(out[1], STDOUT_FILENO); } - if (fds[2] >= 0) { - if (fds[2]) { - close(err[0]); - if (err[1] != STDERR_FILENO) - dup2(err[1], STDERR_FILENO); - } else - dup2(null, STDERR_FILENO); + if (fds[2] == 0) + dup2(null, STDERR_FILENO); + else if (fds[2] > 0) { + close(err[0]); + if (err[1] != STDERR_FILENO) + dup2(err[1], STDERR_FILENO); } if (null >= 0) close(null);