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