doc: clarify the operation of wc -L
[coreutils.git] / src / operand2sig.c
blobbc3b2279ebdfe8cdb07425dc4b2f16ec5e6edf72
1 /* operand2sig.c -- common function for parsing signal specifications
2 Copyright (C) 2008-2015 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 <http://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 with printable representation SIGNAME.
22 Return the signal number, or -1 if 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 "error.h"
31 #include "sig2str.h"
32 #include "operand2sig.h"
34 extern int
35 operand2sig (char const *operand, char *signame)
37 int signum;
39 if (ISDIGIT (*operand))
41 char *endp;
42 long int l = (errno = 0, strtol (operand, &endp, 10));
43 int i = l;
44 signum = (operand == endp || *endp || errno || i != l ? -1
45 : WIFSIGNALED (i) ? WTERMSIG (i) : i);
47 else
49 /* Convert signal to upper case in the C locale, not in the
50 current locale. Don't assume ASCII; it might be EBCDIC. */
51 char *upcased = xstrdup (operand);
52 char *p;
53 for (p = upcased; *p; p++)
54 if (strchr ("abcdefghijklmnopqrstuvwxyz", *p))
55 *p += 'A' - 'a';
57 /* Look for the signal name, possibly prefixed by "SIG",
58 and possibly lowercased. */
59 if (!(str2sig (upcased, &signum) == 0
60 || (upcased[0] == 'S' && upcased[1] == 'I' && upcased[2] == 'G'
61 && str2sig (upcased + 3, &signum) == 0)))
62 signum = -1;
64 free (upcased);
67 if (signum < 0 || sig2str (signum, signame) != 0)
69 error (0, 0, _("%s: invalid signal"), operand);
70 return -1;
73 return signum;