summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/longjmp/longjmp.c
blob: 7dea5bd97c30a916b0471c9274ba1d481cbe5419 (plain)
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
/*	$OpenBSD: longjmp.c,v 1.4 2002/02/18 11:27:45 art Exp $	*/
/*
 *	Artur Grabowski <art@openbsd.org>, 2002 Public Domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <setjmp.h>
#include <err.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>


jmp_buf buf;

/*
 * When longjmp is passed the incorrect arg (0), it should translate it into
 * something better.
 *
 * The rlimit is here in case we start spinning.
 */
int
main(int argc, char **argv)
{
	struct rlimit rl;
	volatile int i, expect;
	int (*sj)(jmp_buf);
	void (*lj)(jmp_buf, int);
	int ch;
	extern char *__progname;

	sj = setjmp;
	lj = longjmp;

	while ((ch = getopt(argc, argv, "_")) != -1) {
		switch (ch) {
		case '_':
			sj = _setjmp;
			lj = _longjmp;
			break;
		default:
			fprintf(stderr, "Usage: %s [-_]\n", __progname);
			exit(1);
		}
	}

	rl.rlim_cur = 2;
	rl.rlim_max = 2;
	if (setrlimit(RLIMIT_CPU, &rl) < 0)
		err(1, "setrlimit");

	expect = 0;
	i = (*sj)(buf);
	if (i == 0 && expect != 0)
		errx(1, "setjmp returns 0 on longjmp(.., 0)");
	if (expect == 0) {
		expect = -1;
		(*lj)(buf, 0);
	}

	expect = 0;
	i = (*sj)(buf);
	if (i != expect)
		errx(1, "bad return from setjmp %d/%d", expect, i);
	if (expect < 1000)
		(*lj)(buf, expect += 2);

	return 0;
}