-
Notifications
You must be signed in to change notification settings - Fork 0
/
longjmp.c
45 lines (38 loc) · 927 Bytes
/
longjmp.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
/* longjmp.c
Demonstrate the use of setjmp() and longjmp() to perform a nonlocal goto.
Usage: longjmp [x]
The presence or absence of a command-line argument determines which of two
functions (f1() or f2()) we will longjmp() from.
*/
#include <setjmp.h>
#include "tlpi_hdr.h"
static jmp_buf env;
static void
f2(void)
{
longjmp(env, 2);
}
static void
f1(int argc)
{
if (argc == 1)
longjmp(env, 1);
f2();
}
int
main(int argc, char *argv[])
{
switch (setjmp(env)) {
case 0: /* This is the return after the initial setjmp() */
printf("Calling f1() after initial setjmp()\n");
f1(argc); /* Never returns... */
break; /* ... but this is good form */
case 1:
printf("We jumped back from f1()\n");
break;
case 2:
printf("We jumped back from f2()\n");
break;
}
exit(EXIT_SUCCESS);
}