summaryrefslogtreecommitdiff
path: root/lib/libc/signal/str2sig.c
diff options
context:
space:
mode:
authorKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
committerKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
commitfc00c656c96528112d05cf0edf8631bd5eaea446 (patch)
treea6e0e6c588191a8bd1c64afc3b7a258e3e66c236 /lib/libc/signal/str2sig.c
Add build system scaffolding and libc headers
Diffstat (limited to 'lib/libc/signal/str2sig.c')
-rw-r--r--lib/libc/signal/str2sig.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/libc/signal/str2sig.c b/lib/libc/signal/str2sig.c
new file mode 100644
index 00000000..288948d7
--- /dev/null
+++ b/lib/libc/signal/str2sig.c
@@ -0,0 +1,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;
+}