3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
14 extern void (*_PDCLIB_sigabrt)( int );
15 extern void (*_PDCLIB_sigfpe)( int );
16 extern void (*_PDCLIB_sigill)( int );
17 extern void (*_PDCLIB_sigint)( int );
18 extern void (*_PDCLIB_sigsegv)( int );
19 extern void (*_PDCLIB_sigterm)( int );
23 void (*sighandler)( int );
28 sighandler = _PDCLIB_sigabrt;
29 message = "Abnormal termination (SIGABRT)";
32 sighandler = _PDCLIB_sigfpe;
33 message = "Arithmetic exception (SIGFPE)";
36 sighandler = _PDCLIB_sigill;
37 message = "Illegal instruction (SIGILL)";
40 sighandler = _PDCLIB_sigint;
41 message = "Interactive attention signal (SIGINT)";
44 sighandler = _PDCLIB_sigsegv;
45 message = "Invalid memory access (SIGSEGV)";
48 sighandler = _PDCLIB_sigterm;
49 message = "Termination request (SIGTERM)";
52 fprintf( stderr, "Unknown signal #%d\n", sig );
53 _Exit( EXIT_FAILURE );
55 if ( sighandler == SIG_DFL )
57 fputs( message, stderr );
58 _Exit( EXIT_FAILURE );
60 else if ( sighandler != SIG_IGN )
62 sighandler = signal( sig, SIG_DFL );
72 #include "_PDCLIB_test.h"
76 static volatile sig_atomic_t flag = 0;
78 static int expected_signal = 0;
80 static void test_handler( int sig )
82 TESTCASE( sig == expected_signal );
88 /* Could be other than SIG_DFL if you changed the implementation. */
89 TESTCASE( signal( SIGABRT, SIG_IGN ) == SIG_DFL );
90 /* Should be ignored. */
91 TESTCASE( raise( SIGABRT ) == 0 );
92 /* Installing test handler, old handler should be returned */
93 TESTCASE( signal( SIGABRT, test_handler ) == SIG_IGN );
94 /* Raising and checking SIGABRT */
95 expected_signal = SIGABRT;
96 TESTCASE( raise( SIGABRT ) == 0 );
97 TESTCASE( flag == 1 );
98 /* Re-installing test handler, should have been reset to default */
99 /* Could be other than SIG_DFL if you changed the implementation. */
100 TESTCASE( signal( SIGABRT, test_handler ) == SIG_DFL );
101 /* Raising and checking SIGABRT */
103 TESTCASE( raise( SIGABRT ) == 0 );
104 TESTCASE( flag == 1 );
105 /* Installing test handler for different signal... */
106 TESTCASE( signal( SIGTERM, test_handler ) == SIG_DFL );
107 /* Raising and checking SIGTERM */
108 expected_signal = SIGTERM;
109 TESTCASE( raise( SIGTERM ) == 0 );
110 TESTCASE( flag == 1 );