add new tests from libc-testsuit (fcntl, f[w]scanf, setjmp, socket, stat)
[libc-test] / src / functional / setjmp.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
6 #include <setjmp.h>
7 #include "test.h"
8
9 #define TEST(c, ...) ((c) ? 1 : (error(#c" failed: " __VA_ARGS__),0))
10 #define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
11
12 int main(void)
13 {
14         volatile int x = 0, r;
15         jmp_buf jb;
16         sigjmp_buf sjb;
17         volatile sigset_t oldset;
18         sigset_t set;
19
20         if (!setjmp(jb)) {
21                 x = 1;
22                 longjmp(jb, 1);
23         }
24         TEST(x==1, "setjmp/longjmp seems to have been bypassed\n");
25
26         x = 0;
27         r = setjmp(jb);
28         if (!x) {
29                 x = 1;
30                 longjmp(jb, 0);
31         }
32         TEST(r==1, "longjmp(jb, 0) caused setjmp to return %d\n", r);
33
34         sigemptyset(&set);
35         sigaddset(&set, SIGUSR1);
36         sigprocmask(SIG_UNBLOCK, &set, &set);
37         oldset = set;
38
39         /* Improve the chances of catching failure of sigsetjmp to
40          * properly save the signal mask in the sigjmb_buf. */
41         memset(&sjb, -1, sizeof sjb);
42
43         if (!sigsetjmp(sjb, 1)) {
44                 sigemptyset(&set);
45                 sigaddset(&set, SIGUSR1);
46                 sigprocmask(SIG_BLOCK, &set, 0);
47                 siglongjmp(sjb, 1);
48         }
49         set = oldset;
50         sigprocmask(SIG_SETMASK, &set, &set);
51         TEST(sigismember(&set, SIGUSR1)==0, "siglongjmp failed to restore mask\n");
52
53         return test_status;
54 }