No empty .Rs/.Re
[netbsd-mini2440.git] / regress / lib / libc / atexit / atexit.c
blob44758b04dab31bbe36bbe07a79d5b8e052c4e330
1 /* $NetBSD: atexit.c,v 1.1 2003/03/01 04:20:54 thorpej Exp $ */
3 /*
4 * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize().
6 * Written by Jason R. Thorpe, February 2003.
7 * Public domain.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <string.h>
14 #include <unistd.h>
16 extern int __cxa_atexit(void (*func)(void *), void *, void *);
17 extern void __cxa_finalize(void *);
19 static int dso_handle_1;
20 static int dso_handle_2;
21 static int dso_handle_3;
23 static int arg_1;
24 static int arg_2;
25 static int arg_3;
27 static int exiting_state;
29 #define ASSERT(expr) \
30 do { \
31 if ((expr) == 0) { \
32 write(STDERR_FILENO, __func__, strlen(__func__)); \
33 write(STDERR_FILENO, ": ", 2); \
34 write(STDERR_FILENO, __STRING(expr), \
35 strlen(__STRING(expr))); \
36 write(STDERR_FILENO, "\n", 1); \
37 my_abort(); \
38 } \
39 } while (/*CONSTCOND*/0)
41 #define SUCCESS() \
42 do { \
43 write(STDOUT_FILENO, __func__, strlen(__func__)); \
44 write(STDOUT_FILENO, "\n", 1); \
45 } while (/*CONSTCOND*/0)
47 static void
48 my_abort(void)
51 (void)raise(SIGABRT);
52 /* NOTREACHED */
55 static void
56 cxa_handler_5(void *arg)
58 static int cxa_handler_5_called;
60 ASSERT(arg == (void *)&arg_1);
61 ASSERT(cxa_handler_5_called == 0);
62 ASSERT(exiting_state == 5);
64 exiting_state--;
65 cxa_handler_5_called = 1;
66 SUCCESS();
69 static void
70 cxa_handler_4(void *arg)
72 static int cxa_handler_4_called;
74 ASSERT(arg == (void *)&arg_1);
75 ASSERT(cxa_handler_4_called == 0);
76 ASSERT(exiting_state == 4);
78 exiting_state--;
79 cxa_handler_4_called = 1;
80 SUCCESS();
83 static void
84 cxa_handler_3(void *arg)
86 static int cxa_handler_3_called;
88 ASSERT(arg == (void *)&arg_2);
89 ASSERT(cxa_handler_3_called == 0);
90 ASSERT(exiting_state == 3);
92 exiting_state--;
93 cxa_handler_3_called = 1;
94 SUCCESS();
97 static void
98 cxa_handler_2(void *arg)
100 static int cxa_handler_2_called;
102 ASSERT(arg == (void *)&arg_3);
103 ASSERT(cxa_handler_2_called == 0);
104 ASSERT(exiting_state == 2);
106 exiting_state--;
107 cxa_handler_2_called = 1;
108 SUCCESS();
111 static void
112 normal_handler_1(void)
114 static int normal_handler_1_called;
116 ASSERT(normal_handler_1_called == 0);
117 ASSERT(exiting_state == 1);
119 exiting_state--;
120 normal_handler_1_called = 1;
121 SUCCESS();
124 static void
125 normal_handler_0(void)
127 static int normal_handler_0_called;
129 ASSERT(normal_handler_0_called == 0);
130 ASSERT(exiting_state == 0);
132 normal_handler_0_called = 1;
133 SUCCESS();
137 main(int argc, char *argv[])
140 exiting_state = 5;
142 ASSERT(0 == atexit(normal_handler_0));
143 ASSERT(0 == atexit(normal_handler_1));
144 ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1));
145 ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1));
146 ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2));
147 ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3));
149 __cxa_finalize(&dso_handle_1);
150 __cxa_finalize(&dso_handle_2);
151 exit(0);