-
Notifications
You must be signed in to change notification settings - Fork 160
/
setjmp_h.c
54 lines (37 loc) · 1.1 KB
/
setjmp_h.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
# setjmp.h
Jumps without scope restrictions of goto labels.
# setjmp
Saves register states, including instruction and pointer registers.
Return value:
- 0 if did not return from longjmp
- != 0 if just returned from a lonjmp
Application: error handling a la exception. Return val encodes the exception type.
# longjmp
Restores register states.
# longjmp and signals
If you longjmp from a signal handler you can recatch the signal afterwards.
*/
#include "common.h"
int setjmp_func(int jmp, jmp_buf env_buf) {
if (jmp)
longjmp(env_buf, 1);
else
return 1;
}
int main(void) {
jmp_buf env_buffer;
int val;
val = setjmp(env_buffer);
printf("setjmp = %i\n", val);
if (val != 0) {
/* Returned from longjmp. */
/* Val encodes the error code. */
} else {
/* This is evaulated normally. */
assert(setjmp_func(0, env_buffer) == 1);
/* This assert is never evaluated. */
assert(setjmp_func(1, env_buffer) * 0 == 1);
}
return EXIT_SUCCESS;
}