[LWIP] Fix src/core/init.c a bit (#1620)
[reactos.git] / sdk / lib / crt / signal / signal.c
1 #include <precomp.h>
2 #include "include/internal/wine/msvcrt.h"
3
4 static sig_element signal_list[] =
5 {
6 { SIGINT, "CTRL+C",SIG_DFL },
7 { SIGILL, "Illegal instruction",SIG_DFL },
8 { SIGFPE, "Floating-point exception",SIG_DFL },
9 { SIGSEGV, "Illegal storage access",SIG_DFL },
10 { SIGTERM, "Termination request",SIG_DFL },
11 { SIGBREAK, "CTRL+BREAK",SIG_DFL },
12 { SIGABRT, "Abnormal termination",SIG_DFL }
13 };
14
15 //int nsignal = 21;
16
17 /*
18 * @implemented
19 */
20 //void ( *signal( int sig, void (__cdecl *func) ( int sig [, int subcode ] )) ) ( int sig );
21
22
23 __p_sig_fn_t signal(int sig, __p_sig_fn_t func)
24 {
25 __p_sig_fn_t temp;
26 unsigned int i;
27
28 switch (sig)
29 {
30 case SIGINT:
31 case SIGILL:
32 case SIGFPE:
33 case SIGSEGV:
34 case SIGTERM:
35 case SIGBREAK:
36 case SIGABRT:
37 break;
38
39 default:
40 _set_errno(EINVAL);
41 return SIG_ERR;
42 }
43
44 // check with IsBadCodePtr
45 if ( (uintptr_t)func < 4096 && func != SIG_DFL && func != SIG_IGN)
46 {
47 _set_errno(EINVAL);
48 return SIG_ERR;
49 }
50
51 for(i=0; i < sizeof(signal_list)/sizeof(signal_list[0]); i++)
52 {
53 if ( signal_list[i].signal == sig )
54 {
55 temp = signal_list[i].handler;
56 signal_list[i].handler = func;
57 return temp;
58 }
59 }
60
61 /* should be impossible to get here */
62 _set_errno(EINVAL);
63 return SIG_ERR;
64 }
65
66
67 /*
68 * @implemented
69 */
70 int
71 raise(int sig)
72 {
73 __p_sig_fn_t temp = 0;
74 unsigned int i;
75
76 switch (sig)
77 {
78 case SIGINT:
79 case SIGILL:
80 case SIGFPE:
81 case SIGSEGV:
82 case SIGTERM:
83 case SIGBREAK:
84 case SIGABRT:
85 break;
86
87 default:
88 //FIXME: set last err?
89 return -1;
90 }
91
92
93 // if(sig <= 0)
94 // return -1;
95 // if(sig > SIGMAX)
96 // return -1;
97
98 for(i=0;i<sizeof(signal_list)/sizeof(signal_list[0]);i++)
99 {
100 if ( signal_list[i].signal == sig )
101 {
102 temp = signal_list[i].handler;
103 break;
104 }
105 }
106
107 if(temp == SIG_IGN)// || (sig == SIGQUIT && temp == (_p_sig_fn_t)SIG_DFL))
108 return 0; /* Ignore it */
109
110 if(temp == SIG_DFL)
111 _default_handler(sig); /* this does not return */
112 else
113 temp(sig);
114
115 return 0;
116 }
117
118
119
120 void _default_handler(int sig)
121 {
122 _exit(3);
123 }
124
125
126
127
128