summaryrefslogtreecommitdiff
path: root/usr.bin/script/script.c
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2022-02-13 19:40:14 +0000
committerchristos <christos@NetBSD.org>2022-02-13 19:40:14 +0000
commit3388fd83f87bb49dd817c358815dcfb91e9c7547 (patch)
tree4751cd4186c7e9a856f508fc2ac815c890f16eb1 /usr.bin/script/script.c
parentaac1345b86940f153676eeb5b969f6edd5cabf4c (diff)
1. restore the previous finish() logic to make:
script -e -c /usr/bin/true script -e -c /usr/bin/false exit with the proper exit code. 2. handle system return value correctly (nabijaczleweli) 3. factor out the conversion of wait status -> shell return code.
Diffstat (limited to 'usr.bin/script/script.c')
-rw-r--r--usr.bin/script/script.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/usr.bin/script/script.c b/usr.bin/script/script.c
index f06a01ee1eb..275a0de951e 100644
--- a/usr.bin/script/script.c
+++ b/usr.bin/script/script.c
@@ -1,4 +1,4 @@
-/* $NetBSD: script.c,v 1.32 2022/02/12 23:03:52 rillig Exp $ */
+/* $NetBSD: script.c,v 1.33 2022/02/13 19:40:14 christos Exp $ */
/*
* Copyright (c) 1980, 1992, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
#if 0
static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
#endif
-__RCSID("$NetBSD: script.c,v 1.32 2022/02/12 23:03:52 rillig Exp $");
+__RCSID("$NetBSD: script.c,v 1.33 2022/02/13 19:40:14 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@@ -231,23 +231,30 @@ xsignal(int signo, sig_t handler)
return osa.sa_handler;
}
+static int
+getshellstatus(int status)
+{
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ if (WIFSIGNALED(status))
+ return 128 + WTERMSIG(status);
+ return EXIT_FAILURE;
+}
+
static void
finish(int signo)
{
int pid, status;
+ die = 0;
while ((pid = wait(&status)) > 0)
if (pid == child) {
- cstat = status;
die = 1;
}
- if (!eflag)
- cstat = EXIT_SUCCESS;
- else if (WIFEXITED(cstat))
- cstat = WEXITSTATUS(cstat);
- else
- cstat = 128 + WTERMSIG(cstat);
+ if (!die)
+ return;
+ done(eflag ? getshellstatus(status) : EXIT_SUCCESS);
}
static void
@@ -312,8 +319,11 @@ doshell(const char *command)
execl(shell, shell, "-i", NULL);
warn("execl `%s'", shell);
} else {
- if (system(command) == -1)
+ int ret = system(command);
+ if (ret == -1)
warn("system `%s'", command);
+ else
+ exit(eflag ? getshellstatus(ret) : EXIT_FAILURE);
}
fail();