File: setjmp-1.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (90 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Test of setjmp()/longjmp() functions: return value.
   $Id: setjmp-1.c,v 1.1.2.2 2008/03/24 11:29:54 dmix Exp $	*/

#include <stdlib.h>
#include <setjmp.h>

jmp_buf env;

int main ()
{
    int (* volatile v_setjmp) (jmp_buf);
    void (* volatile v_longjmp) (jmp_buf, int);

    /* Return 0 from setjmp().	*/
    if (setjmp (env))
	exit (__LINE__);

    /* Pass value throw longjmp().	*/
    switch (setjmp (env)) {
	case 0:
	    longjmp (env, 12345);
	    exit (__LINE__);
	case 12345:
	    break;
	default:
	    exit (__LINE__);
    }

    /* Replace 0 arg of longjmp().	*/
    switch (setjmp (env)) {
	case 0:
	    longjmp (env, 0);
	    exit (__LINE__);
	case 1:
	    break;
	default:
	    exit (__LINE__);
    }

    /* Check -1 value.	*/
    switch (setjmp (env)) {
	case 0:
	    longjmp (env, -1);
	    exit (__LINE__);
	case -1:
	    break;
	default:
	    exit (__LINE__);
    }

    /* Repeat above with volatile function pointers: exclude
       posible compiler optimization.	*/
    v_setjmp = setjmp;
    v_longjmp = longjmp;

    if (v_setjmp (env))
	exit (__LINE__);

    switch (v_setjmp (env)) {
	case 0:
	    v_longjmp (env, 12345);
	    exit (__LINE__);
	case 12345:
	    break;
	default:
	    exit (__LINE__);
    }

    switch (v_setjmp (env)) {
	case 0:
	    v_longjmp (env, 0);
	    exit (__LINE__);
	case 1:
	    break;
	default:
	    exit (__LINE__);
    }
    
    switch (v_setjmp (env)) {
	case 0:
	    v_longjmp (env, -1);
	    exit (__LINE__);
	case -1:
	    break;
	default:
	    exit (__LINE__);
    }

    return 0;
}