Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/script 1. restore the previous finish() logic to make:



details:   https://anonhg.NetBSD.org/src/rev/db14b7dab783
branches:  trunk
changeset: 361557:db14b7dab783
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Feb 13 19:40:14 2022 +0000

description:
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:

 usr.bin/script/script.c |  30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)

diffs (69 lines):

diff -r c403cc4a3097 -r db14b7dab783 usr.bin/script/script.c
--- a/usr.bin/script/script.c   Sun Feb 13 19:21:21 2022 +0000
+++ b/usr.bin/script/script.c   Sun Feb 13 19:40:14 2022 +0000
@@ -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 @@
 #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 @@
        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 @@
                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();



Home | Main Index | Thread Index | Old Index