]> pd.if.org Git - pdclib/blob - functions/signal/raise.c
Cosmetic comment fixes.
[pdclib] / 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 #include "_PDCLIB_test.h"
72
73 #include <stdlib.h>
74
75 static volatile sig_atomic_t flag = 0;
76
77 static int expected_signal = 0;
78
79 static void test_handler( int sig )
80 {
81     TESTCASE( sig == expected_signal );
82     flag = 1;
83 }
84
85 int main( void )
86 {
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 */
101     flag = 0;
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 );
110     return TEST_RESULTS;
111 }
112 #endif