xref: /netbsd-src/lib/libc/gen/signalnumber.c (revision 631837b7c67f3d61ff67f8290890cc9fade1f929)
1 /* $NetBSD: signalnumber.c,v 1.3 2020/08/20 22:56:56 kre Exp $ */
2 
3 /*
4  * Software available to all and sundry without limitations
5  * but without warranty of fitness for any purpose whatever.
6  *
7  * Licensed for any use, including redistribution in source
8  * and binary forms, with or without modifications, subject
9  * the following agreement:
10  *
11  * Licensee agrees to indemnify licensor, and distributor, for
12  * the full amount of any any claim made by the licensee against
13  * the licensor or distributor, for any action that results from
14  * any use or redistribution of this software, plus any costs
15  * incurred by licensor or distributor resulting from that claim.
16  *
17  * This licence must be retained with the software.
18  */
19 
20 #include "namespace.h"
21 
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 
27 /*
28  * signalnumber()
29  *
30  *	Converts the signal named "name" to its
31  *	signal number.
32  *
33  *	"name" can be the signal name with or without
34  *	the leading "SIG" prefix, and character comparisons
35  *	are done in a case independent manner.
36  *	(ie: SIGINT == INT == int == Int == SiGiNt == (etc).)
37  *
38  * Returns:
39  *	0 on error (invalid signal name)
40  *	otherwise the signal number that corresponds to "name"
41  */
42 
43 int
signalnumber(const char * name)44 signalnumber(const char *name)
45 {
46 	int i;
47 #ifndef SMALL
48 	long offs;
49 	char *ep;
50 #endif
51 
52 	if (strncasecmp(name, "sig", 3) == 0)
53 		name += 3;
54 
55 	for (i = 1; i < NSIG; ++i)
56 		if (sys_signame[i] != NULL &&
57 		    strcasecmp(name, sys_signame[i]) == 0)
58 			return i;
59 
60 #ifndef SMALL
61 	if (strncasecmp(name, "rtm", 3) == 0) {
62 		name += 3;
63 		if (strncasecmp(name, "ax", 2) == 0)
64 			i = SIGRTMAX;
65 		else if (strncasecmp(name, "in", 2) == 0)
66 			i = SIGRTMIN;
67 		else
68 			return 0;
69 		name += 2;
70 		if (name[0] == '\0')
71 			return i;
72 		if (i == SIGRTMAX && name[0] != '-')
73 			return 0;
74 		if (i == SIGRTMIN && name[0] != '+')
75 			return 0;
76 		if (!isdigit((unsigned char)name[1]))
77 			return 0;
78 		offs = strtol(name+1, &ep, 10);
79 		if (ep == name+1 || *ep != '\0' ||
80 		    offs < 0 || offs > SIGRTMAX-SIGRTMIN)
81 			return 0;
82 		if (name[0] == '+')
83 			i += (int)offs;
84 		else
85 			i -= (int)offs;
86 		if (i >= SIGRTMIN && i <= SIGRTMAX)
87 			return i;
88 	}
89 #endif
90 	return 0;
91 }
92