tests: avoid triggering obsolete tail option processing
[coreutils.git] / src / operand2sig.c
blobb46cb1bede2cee8d66eb2bd17c253b3f5de8dbc8
1 /* operand2sig.c -- common function for parsing signal specifications
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Extracted from kill.c/timeout.c by Pádraig Brady.
18 FIXME: Move this to gnulib/str2sig.c */
21 /* Convert OPERAND to a signal number. Return the signal number, or -1 if
22 unsuccessful. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
29 #include "system.h"
30 #include "quote.h"
31 #include "sig2str.h"
32 #include "operand2sig.h"
34 extern int
35 operand2sig (char const *operand)
37 int signum;
39 if (ISDIGIT (*operand))
41 /* Note we don't put a limit on the maximum value passed,
42 because we're checking shell $? values here, and ksh for
43 example will add 256 to the signal value, thus being wider
44 than the number of WEXITSTATUS bits.
45 We could validate that values were not above say
46 ((WEXITSTATUS (~0) << 1) + 1), which would cater for ksh.
47 But some shells may use other adjustments in future to be
48 (forward) compatible with systems that support
49 wider exit status values as discussed at
50 https://austingroupbugs.net/view.php?id=947 */
52 char *endp;
53 long int l = (errno = 0, strtol (operand, &endp, 10));
54 int i = l;
55 signum = (operand == endp || *endp || errno || i != l ? -1 : i);
57 if (signum != -1)
59 /* Note AIX uses a different bit pattern for status returned
60 from shell and wait(), so we can't use WTERMSIG etc. here.
61 Also ksh returns 0xFF + signal number. */
62 signum &= signum >= 0xFF ? 0xFF : 0x7F;
65 else
67 /* Convert signal to upper case in the C locale, not in the
68 current locale. Don't assume ASCII; it might be EBCDIC. */
69 char *upcased = xstrdup (operand);
70 char *p;
71 for (p = upcased; *p; p++)
72 if (strchr ("abcdefghijklmnopqrstuvwxyz", *p))
73 *p += 'A' - 'a';
75 /* Look for the signal name, possibly prefixed by "SIG",
76 and possibly lowercased. */
77 if (!(str2sig (upcased, &signum) == 0
78 || (upcased[0] == 'S' && upcased[1] == 'I' && upcased[2] == 'G'
79 && str2sig (upcased + 3, &signum) == 0)))
80 signum = -1;
82 free (upcased);
85 if (0 > signum || signum > SIGNUM_BOUND)
87 error (0, 0, _("%s: invalid signal"), quote (operand));
88 return -1;
91 return signum;