xref: /openbsd/regress/lib/libc/longjmp/longjmp.c (revision 6e9bf348)
1 /*	$OpenBSD: longjmp.c,v 1.4 2002/02/18 11:27:45 art Exp $	*/
2 /*
3  *	Artur Grabowski <art@openbsd.org>, 2002 Public Domain.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <setjmp.h>
10 #include <err.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/resource.h>
14 
15 
16 jmp_buf buf;
17 
18 /*
19  * When longjmp is passed the incorrect arg (0), it should translate it into
20  * something better.
21  *
22  * The rlimit is here in case we start spinning.
23  */
24 int
main(int argc,char ** argv)25 main(int argc, char **argv)
26 {
27 	struct rlimit rl;
28 	volatile int i, expect;
29 	int (*sj)(jmp_buf);
30 	void (*lj)(jmp_buf, int);
31 	int ch;
32 	extern char *__progname;
33 
34 	sj = setjmp;
35 	lj = longjmp;
36 
37 	while ((ch = getopt(argc, argv, "_")) != -1) {
38 		switch (ch) {
39 		case '_':
40 			sj = _setjmp;
41 			lj = _longjmp;
42 			break;
43 		default:
44 			fprintf(stderr, "Usage: %s [-_]\n", __progname);
45 			exit(1);
46 		}
47 	}
48 
49 	rl.rlim_cur = 2;
50 	rl.rlim_max = 2;
51 	if (setrlimit(RLIMIT_CPU, &rl) < 0)
52 		err(1, "setrlimit");
53 
54 	expect = 0;
55 	i = (*sj)(buf);
56 	if (i == 0 && expect != 0)
57 		errx(1, "setjmp returns 0 on longjmp(.., 0)");
58 	if (expect == 0) {
59 		expect = -1;
60 		(*lj)(buf, 0);
61 	}
62 
63 	expect = 0;
64 	i = (*sj)(buf);
65 	if (i != expect)
66 		errx(1, "bad return from setjmp %d/%d", expect, i);
67 	if (expect < 1000)
68 		(*lj)(buf, expect += 2);
69 
70 	return 0;
71 }
72