mailing list of musl libc
 help / color / mirror / code / Atom feed
From: contact@hacktivis.me
To: musl@lists.openwall.com
Cc: "Haelwenn (lanodan) Monnier" <contact@hacktivis.me>
Subject: [musl] [PATCH 2/2] signal: add str2sig(3) from POSIX.1-2024
Date: Sun,  4 Aug 2024 14:41:45 +0200	[thread overview]
Message-ID: <20240804124145.30659-2-contact@hacktivis.me> (raw)
In-Reply-To: <20240804124145.30659-1-contact@hacktivis.me>

From: "Haelwenn (lanodan) Monnier" <contact@hacktivis.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


  reply	other threads:[~2024-08-04 12:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-04 12:41 [musl] [PATCH 1/2] signal: add sig2str(3) " contact
2024-08-04 12:41 ` contact [this message]
2024-08-04 13:39   ` [musl] [PATCH 2/2] signal: add str2sig(3) " Pedro Falcato
2024-08-04 14:02     ` Joakim Sindholt
2024-08-04 14:23 ` [musl] [PATCH 1/2] signal: add sig2str(3) " Joakim Sindholt
2024-08-04 14:32   ` Rich Felker
2024-08-04 14:29 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240804124145.30659-2-contact@hacktivis.me \
    --to=contact@hacktivis.me \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).