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 v3 2/3] signal: add sig2str(3) from POSIX.1-2024
Date: Fri,  9 Aug 2024 17:34:22 +0200	[thread overview]
Message-ID: <20240809153423.30829-2-contact@hacktivis.me> (raw)
In-Reply-To: <20240809153423.30829-1-contact@hacktivis.me>

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

---
 include/signal.h     |  5 +++++
 src/signal/sig2str.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 src/signal/sig2str.c

diff --git a/include/signal.h b/include/signal.h
index c347f861..94ac29b1 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -233,6 +233,11 @@ int pthread_kill(pthread_t, int);
 void psiginfo(const siginfo_t *, const char *);
 void psignal(int, const char *);
 
+// SIG2STR_MAX needs to at least fit "RTMIN+nn" (9 bytes)
+// Bumped to 13 to be safe if a case like "SIGRTMIN+nnn" happens
+#define SIG2STR_MAX 13
+int sig2str(int signum, char *str);
+
 #endif
 
 #if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
diff --git a/src/signal/sig2str.c b/src/signal/sig2str.c
new file mode 100644
index 00000000..989d3645
--- /dev/null
+++ b/src/signal/sig2str.c
@@ -0,0 +1,34 @@
+#include <signal.h>
+#include <string.h>
+
+int sig2str(int sig, char *str)
+{
+	if (sig <= 0) return -1;
+
+	if (sig <= SIGSYS)
+		return (strcpy(str, __sys_signame[sig]), 0);
+
+	if (sig == SIGRTMIN)
+		return (strcpy(str, "RTMIN"), 0);
+	if (sig == SIGRTMAX)
+		return (strcpy(str, "RTMAX"), 0);
+
+	if (sig > SIGRTMIN && sig <= SIGRTMAX)
+	{
+		strcpy(str, "RTMIN+");
+		int sigrt = sig-SIGRTMIN;
+
+		char *s = str+6;
+		if (sigrt>=10)
+		{
+			*s++ = '0' + sigrt/10;
+			sigrt %= 10;
+		}
+		*s++ = '0' + sigrt;
+		*s = '\0';
+
+		return 0;
+	}
+
+	return -1;
+}
-- 
2.44.2


  reply	other threads:[~2024-08-09 15:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-09 15:34 [musl] [PATCH v3 1/3] src/signal/sys_signame.c: create hidden value-name table of signals contact
2024-08-09 15:34 ` contact [this message]
2024-08-09 15:34 ` [musl] [PATCH v3 3/3] signal: add str2sig(3) from POSIX.1-2024 contact
2024-08-11  0:43   ` Rich Felker
2024-08-11  2:54     ` 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=20240809153423.30829-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).