]> pd.if.org Git - pdclib/blob - platform/example/functions/signal/raise.c
Whitespace cleanups.
[pdclib] / platform / example / functions / signal / raise.c
1 /* raise( int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <signal.h>
8
9 #ifndef REGTEST
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
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 );
20
21 int raise( int sig )
22 {
23     void (*sighandler)( int );
24     char const * message;
25     switch ( sig )
26     {
27         case SIGABRT:
28             sighandler = _PDCLIB_sigabrt;
29             message = "Abnormal termination (SIGABRT)";
30             break;
31         case SIGFPE:
32             sighandler = _PDCLIB_sigfpe;
33             message = "Arithmetic exception (SIGFPE)";
34             break;
35         case SIGILL:
36             sighandler = _PDCLIB_sigill;
37             message = "Illegal instruction (SIGILL)";
38             break;
39         case SIGINT:
40             sighandler = _PDCLIB_sigint;
41             message = "Interactive attention signal (SIGINT)";
42             break;
43         case SIGSEGV:
44             sighandler = _PDCLIB_sigsegv;
45             message = "Invalid memory access (SIGSEGV)";
46             break;
47         case SIGTERM:
48             sighandler = _PDCLIB_sigterm;
49             message = "Termination request (SIGTERM)";
50             break;
51         default:
52             fprintf( stderr, "Unknown signal #%d\n", sig );
53             _Exit( EXIT_FAILURE );
54     }
55     if ( sighandler == SIG_DFL )
56     {
57         fputs( message, stderr );
58         _Exit( EXIT_FAILURE );
59     }
60     else if ( sighandler != SIG_IGN )
61     {
62         sighandler = signal( sig, SIG_DFL );
63         sighandler( sig );
64     }
65     return 0;
66 }
67
68 #endif
69
70 #ifdef TEST
71
72 #include "_PDCLIB_test.h"
73
74 #include <stdlib.h>
75
76 static volatile sig_atomic_t flag = 0;
77
78 static int expected_signal = 0;
79
80 static void test_handler( int sig )
81 {
82     TESTCASE( sig == expected_signal );
83     flag = 1;
84 }
85
86 int main( void )
87 {
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 */
102     flag = 0;
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 );
111     return TEST_RESULTS;
112 }
113
114 #endif