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

Joel Sherrill joel at rtems.org
Wed Jul 7 14:04:16 UTC 2021


On Wed, Jul 7, 2021 at 5:46 AM Matt Joyce <mfjoyce2004 at gmail.com> wrote:
>
> 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  */

Why placeholder? Seems like an odd comment.
> +//#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[] = {

This is also const.

> +    #ifdef EXIT
> +    { "EXIT", 0 },
> +    #endif

I don't think this should be included. And I don't know where you saw it.

> +    #ifdef SIGHUP
> +    { "HUP", SIGHUP},
> +    #endif

Indent the middle line (include the ifdef)

> +    #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
> +};
> +

Is this the entire set used across all newlib signal.h files?

> +#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++){

Declare the variable at the top of the function. I am not sure newlib
has gone to assuming
a new enough version of C for this to work on all target
configurations. This is also in the
other method.

> +    if (i->sig_num == signum){

) {

needs a space. This occurs multiple times.

> +        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++){

Dec
> +    if (strcmp(i->sig_name, str) == 0){
> +        *pnum = i->sig_num;
> +        return 0;
> +    }
> +  }
> +  return -1;
> +}
> --
> 2.31.1
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list