xref: /openbsd-src/regress/lib/libm/msun/atf-c.c (revision c36e572ec6e4c7a6aca6564610c2c45de467ead4)
1*c36e572eSmbuhl /*	$OpenBSD: atf-c.c,v 1.1 2021/10/22 18:00:22 mbuhl Exp $	*/
2*c36e572eSmbuhl /*
3*c36e572eSmbuhl  * Copyright (c) 2019-2021 Moritz Buhl <mbuhl@openbsd.org>
4*c36e572eSmbuhl  *
5*c36e572eSmbuhl  * Permission to use, copy, modify, and distribute this software for any
6*c36e572eSmbuhl  * purpose with or without fee is hereby granted, provided that the above
7*c36e572eSmbuhl  * copyright notice and this permission notice appear in all copies.
8*c36e572eSmbuhl  *
9*c36e572eSmbuhl  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*c36e572eSmbuhl  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*c36e572eSmbuhl  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*c36e572eSmbuhl  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*c36e572eSmbuhl  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*c36e572eSmbuhl  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*c36e572eSmbuhl  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*c36e572eSmbuhl  */
17*c36e572eSmbuhl 
18*c36e572eSmbuhl #include <sys/wait.h>
19*c36e572eSmbuhl 
20*c36e572eSmbuhl #include <err.h>
21*c36e572eSmbuhl #include <errno.h>
22*c36e572eSmbuhl #include <limits.h>
23*c36e572eSmbuhl #include <stdarg.h>
24*c36e572eSmbuhl #include <stdio.h>
25*c36e572eSmbuhl #include <stdlib.h>
26*c36e572eSmbuhl #include <pwd.h>
27*c36e572eSmbuhl #include <unistd.h>
28*c36e572eSmbuhl 
29*c36e572eSmbuhl #include "atf-c.h"
30*c36e572eSmbuhl 
31*c36e572eSmbuhl void usage(void);
32*c36e572eSmbuhl 
33*c36e572eSmbuhl int cleanup;
34*c36e572eSmbuhl int count;
35*c36e572eSmbuhl int inspect;
36*c36e572eSmbuhl int run;
37*c36e572eSmbuhl int test;
38*c36e572eSmbuhl 
39*c36e572eSmbuhl int
main(int argc,char * argv[])40*c36e572eSmbuhl main(int argc, char *argv[])
41*c36e572eSmbuhl {
42*c36e572eSmbuhl 	int ch, test;
43*c36e572eSmbuhl 	const char *errstr, *num;
44*c36e572eSmbuhl 
45*c36e572eSmbuhl 	while ((ch = getopt(argc, argv, "c:i:nr:")) != -1) {
46*c36e572eSmbuhl 		switch(ch) {
47*c36e572eSmbuhl 		case 'c':
48*c36e572eSmbuhl 			cleanup = 1;
49*c36e572eSmbuhl 			num = optarg;
50*c36e572eSmbuhl 			break;
51*c36e572eSmbuhl 		case 'i':
52*c36e572eSmbuhl 			inspect = 1;
53*c36e572eSmbuhl 			num = optarg;
54*c36e572eSmbuhl 			break;
55*c36e572eSmbuhl 		case 'n':
56*c36e572eSmbuhl 			count = 1;
57*c36e572eSmbuhl 			break;
58*c36e572eSmbuhl 		case 'r':
59*c36e572eSmbuhl 			run = 1;
60*c36e572eSmbuhl 			num = optarg;
61*c36e572eSmbuhl 			break;
62*c36e572eSmbuhl 		default:
63*c36e572eSmbuhl 			usage();
64*c36e572eSmbuhl 		}
65*c36e572eSmbuhl 	}
66*c36e572eSmbuhl 	argc -= optind;
67*c36e572eSmbuhl 	argv += optind;
68*c36e572eSmbuhl 
69*c36e572eSmbuhl 	if (cleanup + count + inspect + run > 1)
70*c36e572eSmbuhl 		usage();
71*c36e572eSmbuhl 
72*c36e572eSmbuhl 	if (cleanup || inspect || run) {
73*c36e572eSmbuhl 		test = strtonum(num, 1, INT_MAX, &errstr);
74*c36e572eSmbuhl 		if (errstr != NULL)
75*c36e572eSmbuhl 			errx(1, "test # is %s: %s", errstr, argv[1]);
76*c36e572eSmbuhl 	}
77*c36e572eSmbuhl 	if (count)
78*c36e572eSmbuhl 		printf("%d\n", atf_test(0, 0));
79*c36e572eSmbuhl 	else if (cleanup)
80*c36e572eSmbuhl 		ATF_CLEANUP(test);
81*c36e572eSmbuhl 	else if (run)
82*c36e572eSmbuhl 		ATF_RUN(test);
83*c36e572eSmbuhl 	else if (inspect)
84*c36e572eSmbuhl 		ATF_INSPECT(test);
85*c36e572eSmbuhl 	else
86*c36e572eSmbuhl 		usage();
87*c36e572eSmbuhl 
88*c36e572eSmbuhl 	return 0;
89*c36e572eSmbuhl }
90*c36e572eSmbuhl 
91*c36e572eSmbuhl void
usage(void)92*c36e572eSmbuhl usage(void)
93*c36e572eSmbuhl {
94*c36e572eSmbuhl 	fprintf(stderr, "usage: %s [-n] [-c|i|r test#]\n", getprogname());
95*c36e572eSmbuhl 	exit(1);
96*c36e572eSmbuhl }
97*c36e572eSmbuhl 
98*c36e572eSmbuhl void
atf_require(int exp,int expected_errno,const char * expstr,const char * src,const int lineno,char * fmt,...)99*c36e572eSmbuhl atf_require(int exp, int expected_errno, const char *expstr, const char *src,
100*c36e572eSmbuhl     const int lineno, char *fmt, ...)
101*c36e572eSmbuhl {
102*c36e572eSmbuhl 	va_list args;
103*c36e572eSmbuhl 	if (!(exp)) {
104*c36e572eSmbuhl 		fprintf(stderr, "\n%s:%d: ", src, lineno);
105*c36e572eSmbuhl 		if (fmt != NULL) {
106*c36e572eSmbuhl 			va_start(args, fmt);
107*c36e572eSmbuhl 			vfprintf(stderr, fmt, args);
108*c36e572eSmbuhl 			va_end(args);
109*c36e572eSmbuhl 		} else {
110*c36e572eSmbuhl 			fprintf(stderr, "'%s' evaluated to false\n", expstr);
111*c36e572eSmbuhl 		}
112*c36e572eSmbuhl 		exit(1);
113*c36e572eSmbuhl 	} else if (expected_errno >= 0 && errno != expected_errno) {
114*c36e572eSmbuhl 		fprintf(stderr, "\n%s:%d: ", src, lineno);
115*c36e572eSmbuhl 		fprintf(stderr, "expected errno %d but got %d instead\n",
116*c36e572eSmbuhl 		    expected_errno, errno);
117*c36e572eSmbuhl 		exit(1);
118*c36e572eSmbuhl 	}
119*c36e572eSmbuhl 	return;
120*c36e572eSmbuhl }
121*c36e572eSmbuhl 
122*c36e572eSmbuhl void
atf_tc_fail(char * fmt,...)123*c36e572eSmbuhl atf_tc_fail(char *fmt, ...)
124*c36e572eSmbuhl {
125*c36e572eSmbuhl 	va_list args;
126*c36e572eSmbuhl 	va_start(args, fmt);
127*c36e572eSmbuhl 	verrx(1, fmt, args);
128*c36e572eSmbuhl }
129