File: setjmp-4.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 (41 lines) | stat: -rw-r--r-- 884 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
/* Test of setjmp()/longjmp() functions: frame pointer restoring.
   $Id: setjmp-4.c,v 1.1.2.2 2008/03/24 11:29:55 dmix Exp $	*/

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

jmp_buf env;

/* foo_jmp() use stack.	longjmp must restore frame pointer from env.	*/
void foo_jmp (const char *p)
{
    char s[16];
    strcpy (s, p);
    s[0] += 1;
    longjmp (env, strtol (s, 0, 0));
}

/* foo() use stack. If frame pointer would be scratch, return value
   (or all program) would be another.	*/
int foo (char *p)
{
    volatile char s[16];
    strcpy ((char *)s, p);
    switch (setjmp (env)) {
	case 0:     break;
	case 2:     return strtol ((const char *)s, 0, 0);
	default:    exit (__LINE__);
    }
    foo_jmp ("1");
    exit (__LINE__);
}

int main ()
{
    if (foo ("1234") != 1234)
	exit (__LINE__);
    if (foo ("4321") != 4321)
	exit (__LINE__);
    return 0;
}