summaryrefslogtreecommitdiff
path: root/lib/libc/signal/str2sig.c
blob: 288948d79a90a199bfb39db74c98d7503a0000f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <__signal.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <asm-generic/signal.h>

int str2sig(const char *restrict str, int *restrict pnum)
{
	for (size_t i = SIGHUP; i <= SIGSYS; ++i) {
		if (!strcmp(str, __sys_signame[i])) {
			*pnum = i;
			return 0;
		}
	}

	if (strcmp(str, "RTMIN") == 0) {
		*pnum = SIGRTMIN;
		return 0;
	}

	if (strcmp(str, "RTMAX") == 0) {
		*pnum = SIGRTMAX;
		return 0;
	}

	int base = 0;
	if (strncmp(str, "RTMIN+", 6) == 0) {
		base = SIGRTMIN;
		str += 5;
	} else if (strncmp(str, "RTMAX-", 6) == 0) {
		base = SIGRTMAX;
		str += 5;
	}

	char *end = NULL;
	errno = 0;
	int offset = strtol(str, &end, 10);
	if (errno || *end) {
		return -1;
	}

	int result = base + offset;
	bool regular = (base == 0 && result >= SIGHUP && result <= SIGSYS);
	bool realtime = (result >= SIGRTMIN && result <= SIGRTMAX);
	if (!regular && !realtime) {
		return -1;
	}

	*pnum = result;
	return 0;
}