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.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 ED58E284B5 for ; Sun, 4 Aug 2024 16:03:01 +0200 (CEST) Received: (qmail 28356 invoked by uid 550); 4 Aug 2024 14:02:57 -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 28318 invoked from network); 4 Aug 2024 14:02:57 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zhasha.com; s=wirbelwind; t=1722780164; bh=I2w9Oegi5eHWrABW1EB0caeCgSKTf+bTKDEHCMzGaVw=; h=Date:From:To:Subject:In-Reply-To:References; b=qzSzq1OU/eWSyTjRIxvHMVBZpjL6J+6Q184en5uSHdKkWVHWEr1bjBV2plM7riNYM FZzyIRc8Ar5k8dO/3GqqmUX3Bb+O5DWouaMAoWaf3+Soq3wDusUGS5mCXEqF/x+xAQ AU45bzo2T8CvSwtfi0HHoGLGM8X5pG9vhGnO6jl8= Date: Sun, 4 Aug 2024 16:02:44 +0200 From: Joakim Sindholt To: musl@lists.openwall.com Message-ID: <20240804160244.16aaa35b@eclair> In-Reply-To: References: <20240804124145.30659-1-contact@hacktivis.me> <20240804124145.30659-2-contact@hacktivis.me> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-musl) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/T4I7ooY=q/T.NNTPEC22fx+" Subject: Re: [musl] [PATCH 2/2] signal: add str2sig(3) from POSIX.1-2024 --MP_/T4I7ooY=q/T.NNTPEC22fx+ Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline On Sun, 4 Aug 2024 14:39:21 +0100 Pedro Falcato wrote: > To me it looks like the best way to implement these two functions is > to create a table of some sorts (indexed by signal, so array[SIGINT] = > "INT") and then use it. > str2sig would just iterate the whole array with strcmps, which is a > _little_ slow but this is probably not performance critical anyway. > > Special casing would be required for RT signals, but that's no biggie. > What do you think? I was thinking the same thing for the sake of size. There are a few more special cases though as some signal names refer to the same signal numbers. --MP_/T4I7ooY=q/T.NNTPEC22fx+ Content-Type: text/x-c++src Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=sig2str.c #include #include #include #include #define SIG(s) [SIG##s] = #s static const char signames[][8] = { SIG(HUP), SIG(INT), SIG(QUIT), SIG(ILL), SIG(TRAP), SIG(ABRT), SIG(BUS), SIG(FPE), SIG(KILL), SIG(USR1), SIG(SEGV), SIG(USR2), SIG(PIPE), SIG(ALRM), SIG(TERM), SIG(STKFLT), SIG(CHLD), SIG(CONT), SIG(STOP), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(URG), SIG(XCPU), SIG(XFSZ), SIG(VTALRM), SIG(PROF), SIG(WINCH), SIG(POLL), SIG(PWR), SIG(SYS), [32] = "TIMER", [33] = "CANCEL", [34] = "SYNCCALL", [35] = "RTMIN", #define RTMIN 35 #define RTMAX SIGRTMAX }; int sig2str(int sig, char *str) { const char *name; int i, num; if (sig < sizeof signames/sizeof *signames) { for (i = 0; i < sizeof *signames; i++) str[i] = signames[sig][i]; str[sizeof *signames] = 0; return 0; } if (sig == RTMAX) { for (i = 0; i < 6; i++) str[i] = "RTMAX"[i]; return 0; } if (sig > RTMIN && sig <= RTMAX) { if (sig <= (RTMIN+RTMAX)/2) { name = "RTMIN+"; num = sig-RTMIN; } else { name = sig==RTMAX?"RTMAX":"RTMAX-"; num = RTMAX-sig; } for (i = 0; i < 6; i++) str[i] = name[i]; if (num > 10) str[i++] = '0'+num/10; if (num > 0) str[i++] = '0'+num%10; str[i] = 0; return 0; } errno = EINVAL; return -1; } int str2sig(const char *restrict str, int *restrict sig) { int i, num; if (strnlen(str, sizeof *signames) <= sizeof *signames) for (i = 0; i < sizeof signames/sizeof *signames; i++) if (strncmp(str, signames[i], sizeof *signames) == 0) return (*sig = i, 0); if (strcmp(str, "IO") == 0) return (*sig = SIGIO, 0); if (strcmp(str, "IOT") == 0) return (*sig = SIGIOT, 0); if (strcmp(str, "UNUSED") == 0) return (*sig = SIGUNUSED, 0); if (strcmp(str, "RTMAX") == 0) return (*sig = RTMAX, 0); if (strncmp(str, "RTMIN+", 6) == 0 || strncmp(str, "RTMAX-", 6) == 0) i = 6; else if (isdigit(*str)) i = 0; else return -1; if (str[i] < '1' || str[i] > '9') return -1; num = str[i++]-'0'; if (isdigit(str[i])) num = num*10+str[i++]-'0'; if (str[i]) return -1; if (*str != 'R') return num<=RTMAX?(*sig = num, 0):-1; else if (num < RTMAX-RTMIN) return (*sig = str[5]=='+'?RTMIN+num:RTMAX-num, 0); else return -1; } --MP_/T4I7ooY=q/T.NNTPEC22fx+--