Signal catching

Hi!
I want to catch all signals that my program receives print their name and then execute the default handler.
Can you help me on that?

I've tried the following code:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void (*hnd[32])(int i);

char signals[32][10] = 
{
    "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", 
    "SIGABRT", "SIGEMT", "SIGFPE", "SIGKILL", "SIGBUS", 
    "SIGSEGV", "SIGSYS", "SIGPIPE", "SIGALRM", "SIGTERM", 
    "SIGURG", "SIGSTOP", "SIGTSTP", "SIGCONT", "SIGCHLD", 
    "SIGTTIN", "SIGTTOU", "SIGIO", "SIGXCPU", "SIGXFSZ", 
    "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGINFO", "SIGUSR1", 
    "SIGUSR2", "SIGTHR" 
}; //SIGTHR?

void handle(int i)
{
    printf("I've received the signal named: %s\n", signals[i-1]);
    hnd(i);
}

int main()
{

    int i;
    for ( i = 1; i < 32; i++ )
        hnd = signal(i, handle);    

    for (;;) pause();

    return 0;
}

This works somehow, but after receiving a signal it displays the message and then receives a segmentation fault signal. Any ideas?

If i define the array of handlers that i save as

void (*hnd[32])();

and i call it as

hnd();

it works!

You need to read up on what signal returns. What happens if there is no handler already installed for a signal, or if no handler can be installed?
Your code can crash because in handle you are dereferencing a function pointer without checking its value first.

The other issue is that a lot of signals's default action is to terminate the program.

Rather than the code we see, what are you trying to do? What I see does not make a lot of sense to me.

You are not filling your array with function pointers! What did you hope they would point to?

Yeah thanks, I could check for some errors that may occur.

I know know that some signals are not catchable. I only care for the catchable ones.
This is a small exercise, I know it doesn't make a lot of sense.

Why not?
I believe that in hnd I have the pointers to the default handlers.

Here is why not -
The signal() call masked off the default action of the signal. You have to raise() the signal again. This is because once the signal is received, the orignal mask is removed. The only way to get the effect of the signal - if it can be blocked - is to raise() it again.

The big problem is that many signals end up terminating the process one way or another.
So your process will dump core and exit or whatever. It would make more sense to pick up a copy of Steven's 'Advanced UNIX programming' and spend an hour with Chapter 10 on signals. There is a big chart on signals and default actions.

signal() is messy. It puts integers AND function pointers through the same argument and return value. 0 means SIG_DFL, 1 means SIG_IGN, anything else is presumed to be a function pointer. Your array of function pointers is going to be full of SIG_DFL's, which amounts to NULLs.