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 );
71 #include <_PDCLIB_test.h>
75 static volatile sig_atomic_t flag = 0;
77 static int expected_signal = 0;
79 static void test_handler( int sig )
81 TESTCASE( sig == expected_signal );
87 /* Could be other than SIG_DFL if you changed the implementation. */
88 TESTCASE( signal( SIGABRT, SIG_IGN ) == SIG_DFL );
89 /* Should be ignored. */
90 TESTCASE( raise( SIGABRT ) == 0 );
91 /* Installing test handler, old handler should be returned */
92 TESTCASE( signal( SIGABRT, test_handler ) == SIG_IGN );
93 /* Raising and checking SIGABRT */
94 expected_signal = SIGABRT;
95 TESTCASE( raise( SIGABRT ) == 0 );
96 TESTCASE( flag == 1 );
97 /* Re-installing test handler, should have been reset to default */
98 /* Could be other than SIG_DFL if you changed the implementation. */
99 TESTCASE( signal( SIGABRT, test_handler ) == SIG_DFL );
100 /* Raising and checking SIGABRT */
102 TESTCASE( raise( SIGABRT ) == 0 );
103 TESTCASE( flag == 1 );
104 /* Installing test handler for different signal... */
105 TESTCASE( signal( SIGTERM, test_handler ) == SIG_DFL );
106 /* Raising and checking SIGTERM */
107 expected_signal = SIGTERM;
108 TESTCASE( raise( SIGTERM ) == 0 );
109 TESTCASE( flag == 1 );