pipeln

Pipeline creation tool
git clone https://git.sinitax.com/sinitax/pipeln
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 0e7a061a6592b3dae5c7519a73d32579860b3560
parent 8afeed1a607f70606a81ab39cac8d684964fdab1
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 16 Oct 2023 13:59:50 +0200

Only return status code of last command in pipeline

Diffstat:
Mpipeln.c | 30++++++++++++++++++------------
1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/pipeln.c b/pipeln.c @@ -68,7 +68,7 @@ findbin(const char *name) die("findbin %s", name); } -void +pid_t run(int *in, int *out, const char **argv) { pid_t child; @@ -100,24 +100,29 @@ run(int *in, int *out, const char **argv) if (in) close(in[0]); if (out) close(out[1]); + + return child; } void -waitall(void) +waitall(pid_t tpid) { - int rc, status, exitcode; + int status, exitcode; + pid_t pid; errno = 0; exitcode = 0; do { - rc = waitpid(-1, &status, 0); - if (rc < 0 && errno != EINTR && errno != ECHILD) + pid = waitpid(-1, &status, 0); + if (pid < 0 && errno != EINTR && errno != ECHILD) die("waitpid:"); - if (!exitcode && WIFEXITED(status)) - exitcode = WEXITSTATUS(status); - else if (!exitcode && WIFSIGNALED(status)) - exitcode = 128 + WTERMSIG(status); - } while (rc >= 0 && errno != ECHILD); + if (pid == tpid) { + if (!exitcode && WIFEXITED(status)) + exitcode = WEXITSTATUS(status); + else if (!exitcode && WIFSIGNALED(status)) + exitcode = 128 + WTERMSIG(status); + } + } while (pid >= 0 && errno != ECHILD); exit(exitcode); } @@ -128,6 +133,7 @@ main(int argc, const char **argv) const char **arg, **cmdargs; int pipe1[2], pipe2[2]; int *in, *out; + pid_t pid; /* first cmd from stdin */ in = NULL; @@ -142,8 +148,8 @@ main(int argc, const char **argv) /* run piped cmd */ *arg = NULL; - run(in, out, cmdargs); - if (!out) waitall(); + pid = run(in, out, cmdargs); + if (!out) waitall(pid); cmdargs = arg + 1; /* out => in */