[PATCH 1/2] libc: Added implementations for sig2str/str2sig

Matt Joyce mfjoyce2004 at gmail.com
Wed Jul 7 10:46:36 UTC 2021


Added function implementations for sig2str() and str2sig() in libc/signal in order
to improve POSIX compliance.
---
 newlib/libc/signal/sig2str.c | 156 +++++++++++++++++++++++++++++++++++
 1 file changed, 156 insertions(+)
 create mode 100644 newlib/libc/signal/sig2str.c

diff --git a/newlib/libc/signal/sig2str.c b/newlib/libc/signal/sig2str.c
new file mode 100644
index 000000000..a07fa2a38
--- /dev/null
+++ b/newlib/libc/signal/sig2str.c
@@ -0,0 +1,156 @@
+/* Placeholder  */
+//#define __GNU_VISIBLE // defining it to have access to SIG2STR_MAX
+
+#include <signal.h>
+#include <string.h>
+#include <stdio.h>
+
+typedef struct sig_name_and_num {
+  const char *sig_name;
+  const int  sig_num; 
+} sig_name_and_num;
+
+static sig_name_and_num sig_array[] = {
+    #ifdef EXIT
+    { "EXIT", 0 },
+    #endif
+    #ifdef SIGHUP
+    { "HUP", SIGHUP},
+    #endif
+    #ifdef SIGINT
+    { "INT", SIGINT },
+    #endif
+    #ifdef SIGQUIT
+    { "QUIT", SIGQUIT },
+    #endif
+    #ifdef SIGILL
+    { "ILL", SIGILL },
+    #endif
+    #ifdef SIGTRAP
+    { "TRAP", SIGTRAP },
+    #endif
+    #ifdef SIGABRT
+    { "ABRT", SIGABRT },
+    #endif
+    #ifdef SIGIOT
+    { "IOT", SIGIOT},
+    #endif
+    #ifdef SIGEMT
+    { "EMT", SIGEMT },
+    #endif
+    #ifdef SIGFPE
+    { "FPE", SIGFPE },
+    #endif
+    #ifdef SIGKILL
+    { "KILL", SIGKILL },
+    #endif
+    #ifdef SIGBUS
+    { "BUS", SIGBUS },
+    #endif
+    #ifdef SIGSEGV
+    { "SEGV", SIGSEGV },
+    #endif
+    #ifdef SIGSYS
+    { "SYS", SIGSYS },
+    #endif
+    #ifdef SIGPIPE
+    { "PIPE", SIGPIPE },
+    #endif
+    #ifdef SIGALRM
+    { "ALRM", SIGALRM },
+    #endif
+    #ifdef SIGTERM
+    { "TERM", SIGTERM },
+    #endif
+    #ifdef SIGURG
+    { "URG", SIGURG },
+    #endif
+    #ifdef SIGSTOP
+    { "STOP", SIGSTOP },
+    #endif
+    #ifdef SIGTSTP
+    { "TSTP", SIGTSTP },
+    #endif
+    #ifdef SIGCONT
+    { "CONT", SIGCONT },
+    #endif
+    #ifdef SIGCHLD
+    { "CHLD", SIGCHLD },
+    #endif
+    #ifdef SIGCLD
+    { "CLD", SIGCLD },
+    #endif
+    #ifdef SIGTTIN
+    { "TTIN", SIGTTIN },
+    #endif
+    #ifdef SIGTTOU
+    { "TTOU", SIGTTOU },
+    #endif
+    #ifdef SIGIO
+    { "IO", SIGIO },
+    #endif
+    #ifdef SIGPOLL
+    { "POLL", SIGPOLL },
+    #endif
+    #ifdef SIGWINCH
+    { "WINCH", SIGWINCH },
+    #endif
+    #ifdef SIGUSR1
+    { "USR1", SIGUSR1 },
+    #endif
+    #ifdef SIGUSR2
+    { "USR2", SIGUSR2 },
+    #endif
+    // #ifdef SIGRTMIN
+    // { "RTMIN", SIGRTMIN },
+    // #endif
+    // #ifdef SIGRTMAX
+    // { "RTMAX", SIGRTMAX },
+    // #endif
+    #ifdef SIGPWR
+    { "PWR", SIGPWR },
+    #endif
+    #ifdef SIGXCPU
+    { "XCPU", SIGXCPU },
+    #endif
+    #ifdef SIGXFSZ
+    { "XFSZ", SIGXFSZ },
+    #endif
+    #ifdef SIGVTALRM
+    { "VTALRM", SIGVTALRM },
+    #endif
+    #ifdef SIGPROF
+    { "PROF", SIGPROF },
+    #endif
+    #ifdef SIGLOST
+    { "LOST", SIGLOST }
+    #endif
+}; 
+
+#define NUM_OF_SIGS (sizeof(sig_array) / sizeof(sig_name_and_num))
+
+int
+sig2str(int signum, char *str)
+{
+  
+  for (sig_name_and_num *i = sig_array; i < &sig_array[NUM_OF_SIGS]; i++){
+    if (i->sig_num == signum){
+        strcpy(str, i->sig_name);
+        return 0; 
+    }    
+  }
+  sprintf(str, "Unknown signal %d", signum); 
+  return -1; 
+}
+
+int
+str2sig(const char *__restrict str, int *__restrict pnum)
+{
+  for (sig_name_and_num *i = sig_array; i < &sig_array[NUM_OF_SIGS]; i++){
+    if (strcmp(i->sig_name, str) == 0){
+        *pnum = i->sig_num;
+        return 0; 
+    }    
+  }
+  return -1; 
+}
-- 
2.31.1



More information about the devel mailing list