5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
16 extern void (*_PDCLIB_sigabrt)( int );
17 extern void (*_PDCLIB_sigfpe)( int );
18 extern void (*_PDCLIB_sigill)( int );
19 extern void (*_PDCLIB_sigint)( int );
20 extern void (*_PDCLIB_sigsegv)( int );
21 extern void (*_PDCLIB_sigterm)( int );
25 void (*sighandler)( int );
30 sighandler = _PDCLIB_sigabrt;
31 message = "Abnormal termination (SIGABRT)";
34 sighandler = _PDCLIB_sigfpe;
35 message = "Arithmetic exception (SIGFPE)";
38 sighandler = _PDCLIB_sigill;
39 message = "Illegal instruction (SIGILL)";
42 sighandler = _PDCLIB_sigint;
43 message = "Interactive attention signal (SIGINT)";
46 sighandler = _PDCLIB_sigsegv;
47 message = "Invalid memory access (SIGSEGV)";
50 sighandler = _PDCLIB_sigterm;
51 message = "Termination request (SIGTERM)";
54 fprintf( stderr, "Unknown signal #%d\n", sig );
55 _Exit( EXIT_FAILURE );
57 if ( sighandler == SIG_DFL )
59 fputs( message, stderr );
60 _Exit( EXIT_FAILURE );
62 else if ( sighandler != SIG_IGN )
64 sighandler = signal( sig, SIG_DFL );
73 #include <_PDCLIB_test.h>
77 static volatile sig_atomic_t flag = 0;
79 static int expected_signal = 0;
81 static void test_handler( int sig )
83 TESTCASE( sig == expected_signal );
89 /* Could be other than SIG_DFL if you changed the implementation. */
90 TESTCASE( signal( SIGABRT, SIG_IGN ) == SIG_DFL );
91 /* Should be ignored. */
92 TESTCASE( raise( SIGABRT ) == 0 );
93 /* Installing test handler, old handler should be returned */
94 TESTCASE( signal( SIGABRT, test_handler ) == SIG_IGN );
95 /* Raising and checking SIGABRT */
96 expected_signal = SIGABRT;
97 TESTCASE( raise( SIGABRT ) == 0 );
98 TESTCASE( flag == 1 );
99 /* Re-installing test handler, should have been reset to default */
100 /* Could be other than SIG_DFL if you changed the implementation. */
101 TESTCASE( signal( SIGABRT, test_handler ) == SIG_DFL );
102 /* Raising and checking SIGABRT */
104 TESTCASE( raise( SIGABRT ) == 0 );
105 TESTCASE( flag == 1 );
106 /* Installing test handler for different signal... */
107 TESTCASE( signal( SIGTERM, test_handler ) == SIG_DFL );
108 /* Raising and checking SIGTERM */
109 expected_signal = SIGTERM;
110 TESTCASE( raise( SIGTERM ) == 0 );
111 TESTCASE( flag == 1 );