Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240804124145.30659-2-contact@hacktivis.me>
Date: Sun,  4 Aug 2024 14:41:45 +0200
From: contact@...ktivis.me
To: musl@...ts.openwall.com
Cc: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me>
Subject: [PATCH 2/2] signal: add str2sig(3) from POSIX.1-2024

From: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me>

---
 include/signal.h     |  1 +
 src/signal/str2sig.c | 72 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 src/signal/str2sig.c

diff --git a/include/signal.h b/include/signal.h
index 217cfa08..fd486cd0 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -235,6 +235,7 @@ void psignal(int, const char *);
 
 #define SIG2STR_MAX sizeof("RTMIN+32")
 int sig2str(int signum, char *str);
+int str2sig(const char *restrict str, int *restrict pnum);
 
 #endif
 
diff --git a/src/signal/str2sig.c b/src/signal/str2sig.c
new file mode 100644
index 00000000..6da2d4f4
--- /dev/null
+++ b/src/signal/str2sig.c
@@ -0,0 +1,72 @@
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+int str2sig(const char *restrict str, int *restrict pnum)
+{
+	errno = 0;
+	long signum = strtol(str, NULL, 10);
+	if(errno == 0 && signum < _NSIG)
+	{
+		*pnum = signum;
+		return 0;
+	}
+
+	if (
+		strncmp(str, "RTMIN+", 6) == 0
+		|| strncmp(str, "RTMAX-", 6) == 0
+	)
+	{
+		errno = 0;
+		long sigrt = strtol(str+6, NULL, 10);
+		if(errno != 0 || sigrt < 1 || sigrt > SIGRTMAX - SIGRTMIN) return -1;
+
+		*pnum = str[5] == '+' ? SIGRTMIN + sigrt : SIGRTMAX - sigrt;
+		return 0;
+	}
+
+	int ret = -1;
+	if(strcmp(str, "HUP") == 0) ret = SIGHUP;
+	if(strcmp(str, "INT") == 0) ret = SIGINT;
+	if(strcmp(str, "QUIT") == 0) ret = SIGQUIT;
+	if(strcmp(str, "ILL") == 0) ret = SIGILL;
+	if(strcmp(str, "TRAP") == 0) ret = SIGTRAP;
+	if(strcmp(str, "ABRT") == 0) ret = SIGABRT;
+	if(strcmp(str, "IOT") == 0) ret = SIGIOT;
+	if(strcmp(str, "BUS") == 0) ret = SIGBUS;
+	if(strcmp(str, "FPE") == 0) ret = SIGFPE;
+	if(strcmp(str, "KILL") == 0) ret = SIGKILL;
+	if(strcmp(str, "USR1") == 0) ret = SIGUSR1;
+	if(strcmp(str, "SEGV") == 0) ret = SIGSEGV;
+	if(strcmp(str, "USR2") == 0) ret = SIGUSR2;
+	if(strcmp(str, "PIPE") == 0) ret = SIGPIPE;
+	if(strcmp(str, "ALRM") == 0) ret = SIGALRM;
+	if(strcmp(str, "TERM") == 0) ret = SIGTERM;
+	if(strcmp(str, "STKFLT") == 0) ret = SIGSTKFLT;
+	if(strcmp(str, "CHLD") == 0) ret = SIGCHLD;
+	if(strcmp(str, "CONT") == 0) ret = SIGCONT;
+	if(strcmp(str, "STOP") == 0) ret = SIGSTOP;
+	if(strcmp(str, "TSTP") == 0) ret = SIGTSTP;
+	if(strcmp(str, "TTIN") == 0) ret = SIGTTIN;
+	if(strcmp(str, "TTOU") == 0) ret = SIGTTOU;
+	if(strcmp(str, "URG") == 0) ret = SIGURG;
+	if(strcmp(str, "XCPU") == 0) ret = SIGXCPU;
+	if(strcmp(str, "XFSZ") == 0) ret = SIGXFSZ;
+	if(strcmp(str, "VTALRM") == 0) ret = SIGVTALRM;
+	if(strcmp(str, "PROF") == 0) ret = SIGPROF;
+	if(strcmp(str, "WINCH") == 0) ret = SIGWINCH;
+	if(strcmp(str, "IO") == 0) ret = SIGIO;
+	if(strcmp(str, "POLL") == 0) ret = SIGPOLL;
+	if(strcmp(str, "PWR") == 0) ret = SIGPWR;
+	if(strcmp(str, "SYS") == 0) ret = SIGSYS;
+	if(strcmp(str, "UNUSED") == 0) ret = SIGUNUSED;
+
+	if(strcmp(str, "RTMIN") == 0) ret = SIGRTMIN;
+	if(strcmp(str, "RTMAX") == 0) ret = SIGRTMAX;
+
+	if(ret == -1) return -1;
+
+	*pnum = ret;
+	return 0;
+}
-- 
2.44.2

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.