ash: allow "trap NUM [SIG]..." syntax
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Jul 2017 18:06:17 +0000 (20:06 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Jul 2017 18:06:17 +0000 (20:06 +0200)
While at it, make get_signum() return -1 for numeric strings >= NSIG.

function                                             old     new   delta
trapcmd                                              292     306     +14
get_signum                                           295     300      +5
builtin_trap                                         413     412      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 19/-1)              Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/u_signal_names.c
procps/kill.c
shell/ash.c
shell/hush.c

index bf984a44e2b2bff0e74b9d70712822d3daeb81ca..b82a706d875d8a60017901d0ff9a1340bca9e6d4 100644 (file)
@@ -143,7 +143,7 @@ int FAST_FUNC get_signum(const char *name)
        unsigned i;
 
        i = bb_strtou(name, NULL, 10);
-       if (!errno)
+       if (!errno && i < NSIG) /* for shells, we allow 0 too */
                return i;
        if (strncasecmp(name, "SIG", 3) == 0)
                name += 3;
index 5cff24475c551f6de2a468f1c209df1893c4e32b..09beefb2d378771d28e8c88277e05ab0f56f062d 100644 (file)
@@ -188,7 +188,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
                arg = *++argv;
        } /* else it must be -SIG */
        signo = get_signum(arg);
-       if (signo < 0) { /* || signo > MAX_SIGNUM ? */
+       if (signo < 0) {
                bb_error_msg("bad signal name '%s'", arg);
                return EXIT_FAILURE;
        }
index b4b0d525374b308bd8cad75b6e463b60c0f8490c..42e14cbc8d2e737e2072a16c910e7ebc3765ddae 100644 (file)
@@ -12981,13 +12981,18 @@ trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
                return 0;
        }
 
+       /* Why the second check?
+        * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..."
+        * In this case, NUM is signal no, not an action.
+        */
        action = NULL;
-       if (ap[1])
+       if (ap[1] && !is_number(ap[0]))
                action = *ap++;
+
        exitcode = 0;
        while (*ap) {
                signo = get_signum(*ap);
-               if (signo < 0 || signo >= NSIG) {
+               if (signo < 0) {
                        /* Mimic bash message exactly */
                        ash_msg("%s: invalid signal specification", *ap);
                        exitcode = 1;
index f9dad074ff5446599d7b3c21ddef753ef02eb94b..11b33f40a4c4f9baed6ba2353d76b38c2fe1848e 100644 (file)
@@ -9745,7 +9745,7 @@ static int FAST_FUNC builtin_trap(char **argv)
                        sighandler_t handler;
 
                        sig = get_signum(*argv++);
-                       if (sig < 0 || sig >= NSIG) {
+                       if (sig < 0) {
                                ret = EXIT_FAILURE;
                                /* Mimic bash message exactly */
                                bb_error_msg("trap: %s: invalid signal specification", argv[-1]);