|
|
Message-ID: <20260403092911.4336-4-contact@hacktivis.me>
Date: Fri, 3 Apr 2026 11:29:11 +0200
From: contact@...ktivis.me
To: musl@...ts.openwall.com
Cc: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me>
Subject: [PATCH v4 3/3] signal: add str2sig(3) from POSIX.1-2024
From: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me>
---
include/signal.h | 1 +
src/signal/str2sig.c | 63 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
create mode 100644 src/signal/str2sig.c
diff --git a/include/signal.h b/include/signal.h
index c997cb08..c5e925bd 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -237,6 +237,7 @@ void psignal(int, const char *);
// Bumped to 13 to be safe if a case like "SIGRTMIN+nnn" happens
#define SIG2STR_MAX 13
int sig2str(int, char *);
+int str2sig(const char *__restrict, int *__restrict);
#endif
diff --git a/src/signal/str2sig.c b/src/signal/str2sig.c
new file mode 100644
index 00000000..53ef3cc7
--- /dev/null
+++ b/src/signal/str2sig.c
@@ -0,0 +1,63 @@
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+int str2sig(const char *restrict str, int *restrict pnum)
+{
+ if (str[0] == '\0') return -1;
+
+ if (isdigit(str[0]))
+ {
+ char *end = NULL;
+ unsigned long signum = strtoul(str, &end, 10);
+
+ if (end != NULL && *end != '\0')
+ return -1;
+
+ if (signum > 0 && signum <= SIGRTMAX)
+ return (*pnum = signum, 0);
+
+ return -1;
+ }
+
+ if (strnlen(str, sizeof *__sys_signame) <= sizeof *__sys_signame)
+ for (int i = 0; i < sizeof __sys_signame/sizeof *__sys_signame; i++)
+ if (strncmp(str, __sys_signame[i], sizeof *__sys_signame) == 0)
+ return (*pnum = i+1, 0);
+
+ // signal aliases
+ if (strcmp(str, "IOT") == 0)
+ return (*pnum = SIGIOT, 0);
+ if (strcmp(str, "UNUSED") == 0)
+ return (*pnum = SIGUNUSED, 0);
+#if SIGPOLL == SIGIO
+ if (strcmp(str, "POLL") == 0)
+ return (*pnum = SIGPOLL, 0);
+#endif
+
+ if (strcmp(str, "RTMIN") == 0)
+ return (*pnum = SIGRTMIN, 0);
+ if (strcmp(str, "RTMAX") == 0)
+ return (*pnum = SIGRTMAX, 0);
+
+ if (memcmp(str, "RTMIN+", 6) == 0 || memcmp(str, "RTMAX-", 6) == 0)
+ {
+ char *end = NULL;
+ errno = 0;
+ unsigned long sigrt = strtoul(str+6, &end, 10);
+
+ if (errno != 0 || (end != NULL && *end != '\0'))
+ return -1;
+
+ sigrt = str[5] == '+' ? SIGRTMIN+sigrt : SIGRTMAX-sigrt;
+
+ if (sigrt >= SIGRTMIN && sigrt <= SIGRTMAX)
+ return (*pnum = sigrt, 0);
+
+ return -1;
+ }
+
+ return -1;
+}
--
2.52.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.