From 0d76c6de321ecbf2cfda7d681cfce1ca80420be2 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 8 Mar 2016 06:36:34 +0200 Subject: Improve return value of system. --- builtin.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index a62437a2..375497fa 100644 --- a/builtin.c +++ b/builtin.c @@ -2081,8 +2081,26 @@ do_system(int nargs) os_restore_mode(fileno(stdin)); ret = system(cmd); - if (ret != -1) - ret = WEXITSTATUS(ret); + /* + * 3/2016. What to do with ret? It's never simple. + * POSIX says to use the full return value. BWK awk + * uses just the exit status. That seems more useful to + * me, but then death by signal info gets lost. + * So we compromise as follows: + */ + if (ret != -1) { + if (do_posix) + ; /* leave it alone, full 16 bits */ + else if (WIFEXITED(ret)) + ret = WEXITSTATUS(ret); /* normal exit */ + else if (do_traditional) + ret = 0; /* ignore signal death */ + else if (WIFSIGNALED(ret)) + /* use 256 since exit values are 8 bits */ + ret = WTERMSIG(ret) + 256; + else + ret = 0; /* shouldn't get here */ + } if ((BINMODE & BINMODE_INPUT) != 0) os_setbinmode(fileno(stdin), O_BINARY); -- cgit v1.2.3 From 3952172ea5f501a92c4ccf8595ebaee34d29377d Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 8 Mar 2016 22:39:57 +0200 Subject: Improve algorithm for system() return value. --- builtin.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 375497fa..da664e3a 100644 --- a/builtin.c +++ b/builtin.c @@ -2061,7 +2061,7 @@ NODE * do_system(int nargs) { NODE *tmp; - int ret = 0; + AWKNUM ret = 0; /* floating point on purpose, compat Unix awk */ char *cmd; char save; @@ -2084,17 +2084,17 @@ do_system(int nargs) /* * 3/2016. What to do with ret? It's never simple. * POSIX says to use the full return value. BWK awk - * uses just the exit status. That seems more useful to - * me, but then death by signal info gets lost. + * divides the result by 256. That normally gives the + * exit status but gives a weird result for death-by-signal. * So we compromise as follows: */ if (ret != -1) { if (do_posix) ; /* leave it alone, full 16 bits */ + else if (do_traditional) + ret /= 256.0; else if (WIFEXITED(ret)) ret = WEXITSTATUS(ret); /* normal exit */ - else if (do_traditional) - ret = 0; /* ignore signal death */ else if (WIFSIGNALED(ret)) /* use 256 since exit values are 8 bits */ ret = WTERMSIG(ret) + 256; -- cgit v1.2.3 From e3cc36f1f2f7172ea561664e34bec54c3436297a Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 11 Mar 2016 12:01:44 +0200 Subject: Further improvements in system return value and doc. --- builtin.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index da664e3a..c3a3bb22 100644 --- a/builtin.c +++ b/builtin.c @@ -2064,6 +2064,7 @@ do_system(int nargs) AWKNUM ret = 0; /* floating point on purpose, compat Unix awk */ char *cmd; char save; + int status; if (do_sandbox) fatal(_("'system' function not allowed in sandbox mode")); @@ -2080,7 +2081,7 @@ do_system(int nargs) cmd[tmp->stlen] = '\0'; os_restore_mode(fileno(stdin)); - ret = system(cmd); + status = system(cmd); /* * 3/2016. What to do with ret? It's never simple. * POSIX says to use the full return value. BWK awk @@ -2088,17 +2089,23 @@ do_system(int nargs) * exit status but gives a weird result for death-by-signal. * So we compromise as follows: */ - if (ret != -1) { + ret = status; + if (status != -1) { if (do_posix) ; /* leave it alone, full 16 bits */ else if (do_traditional) - ret /= 256.0; - else if (WIFEXITED(ret)) - ret = WEXITSTATUS(ret); /* normal exit */ - else if (WIFSIGNALED(ret)) + ret = (status / 256.0); + else if (WIFEXITED(status)) + ret = WEXITSTATUS(status); /* normal exit */ + else if (WIFSIGNALED(status)) { + bool coredumped = false; +#ifdef WCOREDUMP + coredumped = WCOREDUMP(status); +#endif /* use 256 since exit values are 8 bits */ - ret = WTERMSIG(ret) + 256; - else + ret = WTERMSIG(status) + + (coredumped ? 512 : 256); + } else ret = 0; /* shouldn't get here */ } if ((BINMODE & BINMODE_INPUT) != 0) -- cgit v1.2.3