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 v2 3/3] signal: add str2sig(3) from POSIX.1-2024
Date: Mon,  5 Aug 2024 08:56:07 +0200	[thread overview]
Message-ID: <20240805065607.22897-3-contact@hacktivis.me> (raw)
In-Reply-To: <20240805065607.22897-1-contact@hacktivis.me>

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

---
 include/signal.h     |  1 +
 src/signal/str2sig.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 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..b6a4fc23
--- /dev/null
+++ b/src/signal/str2sig.c
@@ -0,0 +1,45 @@
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+int str2sig(const char *restrict str, int *restrict pnum)
+{
+	if (str[0] == '\0') return -1;
+
+	errno = 0;
+	long signum = strtol(str, NULL, 10);
+	if (errno == 0 && signum < _NSIG) return (*pnum = signum, 0);
+
+	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, 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 (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;
+	}
+
+	return -1;
+}
-- 
2.44.2


  parent reply	other threads:[~2024-08-05  6:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05  6:56 [musl] [PATCH v2 1/3] src/signal/sys_signame.c: create hidden value-name table of signals contact
2024-08-05  6:56 ` [musl] [PATCH v2 2/3] signal: add sig2str(3) from POSIX.1-2024 contact
2024-08-05 18:12   ` Rich Felker
2024-08-05  6:56 ` contact [this message]
2024-08-05 18:26   ` [musl] [PATCH v2 3/3] signal: add str2sig(3) " Rich Felker
2024-08-06  8:21     ` Haelwenn (lanodan) Monnier
2024-08-05  9:13 ` [musl] [PATCH v2 1/3] src/signal/sys_signame.c: create hidden value-name table of signals Joakim Sindholt
2024-08-05 15: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=20240805065607.22897-3-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).