tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[PATCH] script.c: bubble exit code with -c instead of always exiting 143



Right now, doshell() with -c always falls down to fail(),
which SIGTERMs the process group, so the exit code is always 143:
  $ script -ec 'echo a; exit 12'
  Script started, output file is typescript
  a
  Script done, output file is typescript
  $ echo $?
  143

Instead, handle non-error returns from system(3)
---
christos@ has applied a core subset of my original patches (thanks!),
but not the system(3) replacement bit, which was required to get
good -ec behaviour: this is an alternative that preserves system(3),
but makes it so script -ec 'exit 32' exits 32, not 143

 script.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/script.c b/script.c
index d7ec6c5..d0dd30c 100644
--- a/script.c
+++ b/script.c
@@ -295,8 +295,14 @@ doshell(const char *command)
 		execl(shell, shell, "-i", NULL);
 		warn("execl `%s'", shell);
 	} else {
-		if (system(command) == -1)
-			warn("system `%s'", command);
+		int ret = system(command);
+		if (ret != -1) {
+			if (WIFEXITED(ret))
+				_exit(WEXITSTATUS(ret));
+			else
+				_exit(128 + WTERMSIG(ret));
+		}
+		warn("system `%s'", command);
 	}
 
 	fail();
-- 
2.30.2

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index