|
Message-ID: <20240809153423.30829-3-contact@hacktivis.me> Date: Fri, 9 Aug 2024 17:34:23 +0200 From: contact@...ktivis.me To: musl@...ts.openwall.com Cc: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me> Subject: [PATCH v3 3/3] signal: add str2sig(3) from POSIX.1-2024 From: "Haelwenn (lanodan) Monnier" <contact@...ktivis.me> --- include/signal.h | 1 + src/signal/str2sig.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/signal/str2sig.c diff --git a/include/signal.h b/include/signal.h index 94ac29b1..5451424d 100644 --- a/include/signal.h +++ b/include/signal.h @@ -237,6 +237,7 @@ void psignal(int, const char *); // Bumped to 13 to be safe if a case like "SIGRTMIN+nnn" happens #define SIG2STR_MAX 13 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..e4c17c57 --- /dev/null +++ b/src/signal/str2sig.c @@ -0,0 +1,54 @@ +#include <signal.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> +#include <ctype.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) + { + if(!isdigit(str[6])) return -1; + + int sigrt = str[6]-'0'; + + if(str[7] != '\0') + { + if(!isdigit(str[7])) return -1; + + sigrt *= 10; + sigrt += str[7]-'0'; + } + + *pnum = str[5] == '+' ? SIGRTMIN + sigrt : SIGRTMAX - sigrt; + return 0; + } + + return -1; +} -- 2.44.2
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.