From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id 95A0D222EF for ; Mon, 5 Aug 2024 20:12:44 +0200 (CEST) Received: (qmail 20298 invoked by uid 550); 5 Aug 2024 18:12:39 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 20260 invoked from network); 5 Aug 2024 18:12:38 -0000 Date: Mon, 5 Aug 2024 14:12:30 -0400 From: Rich Felker To: contact@hacktivis.me Cc: musl@lists.openwall.com Message-ID: <20240805181227.GB10433@brightrain.aerifal.cx> References: <20240805065607.22897-1-contact@hacktivis.me> <20240805065607.22897-2-contact@hacktivis.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240805065607.22897-2-contact@hacktivis.me> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [PATCH v2 2/3] signal: add sig2str(3) from POSIX.1-2024 On Mon, Aug 05, 2024 at 08:56:06AM +0200, contact@hacktivis.me wrote: > From: "Haelwenn (lanodan) Monnier" > > --- > include/signal.h | 3 +++ > src/signal/sig2str.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 45 insertions(+) > create mode 100644 src/signal/sig2str.c > > diff --git a/include/signal.h b/include/signal.h > index c347f861..217cfa08 100644 > --- a/include/signal.h > +++ b/include/signal.h > @@ -233,6 +233,9 @@ int pthread_kill(pthread_t, int); > void psiginfo(const siginfo_t *, const char *); > void psignal(int, const char *); > > +#define SIG2STR_MAX sizeof("RTMIN+32") This definition is kinda misleading as written, since 32 isn't actually the max; it can be much higher on mips where there are 127 signals. Originally I thought this made the bound wrong, but once you take off the non-RT signals it still fits in 2 digits. It might be better though just to write the literal size (so it's usable at preprocessor level too; not sure if POSIX wants that but it's nice) or even include some extra room just to be safe. > +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..1967159c > --- /dev/null > +++ b/src/signal/sig2str.c > @@ -0,0 +1,42 @@ > +#include > +#include > + > +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 SIGPOLL != SIGIO > + if (sig == SIGPOLL) > + return (strcpy(str, "POLL"), 0); > +#endif Why isn't this one just in the table? It can be there conditional on #if SIGPOLL != SIGIO, no? > + if (sig > SIGRTMIN && sig <= SIGRTMAX) > + { > + strcpy(str, "RTMIN+"); > + int sigrt = sig-SIGRTMIN; > + > + if (sigrt < 10) > + { > + str[6] = '0'+sigrt; > + str[7] = '\0'; > + } > + else > + { > + str[6] = '0'+sigrt/10; > + str[7] = '0'+sigrt%10; > + str[8] = '\0'; > + } > + > + return 0; > + } This might be prettier as something like: if (sigrt>=10) { *s++ = '0' + sigrt/10; sigrt %= 10; } *s++ = '0' + sigrt; but it'd need a pointer (as in my example) or index var to write it that way. I don't have a strong preference of this vs the way you wrote it though, so whatever you like. Rich