xref: /plan9/sys/src/libc/port/atexit.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 
4 #define	NEXIT	33
5 
6 static struct
7 {
8 	void	(*f)(void);
9 	int	pid;
10 }onex[NEXIT];
11 
12 atexit(void (*f)(void))
13 {
14 	int i;
15 
16 	for(i=0; i<NEXIT; i++)
17 		if(onex[i].f == 0) {
18 			onex[i].f = f;
19 			onex[i].pid = getpid();
20 			return 1;
21 		}
22 	return 0;
23 }
24 
25 void
26 atexitdont(void (*f)(void))
27 {
28 	int i, pid;
29 
30 	pid = getpid();
31 	for(i=0; i<NEXIT; i++)
32 		if(onex[i].f == f && onex[i].pid == pid)
33 			onex[i].f = 0;
34 }
35 
36 void
37 exits(char *s)
38 {
39 	int i, pid;
40 	void (*f)(void);
41 
42 	pid = getpid();
43 
44 	for(i = NEXIT-1; i >= 0; i--)
45 		if((f = onex[i].f) && pid == onex[i].pid) {
46 			onex[i].f = 0;
47 			(*f)();
48 		}
49 	_exits(s);
50 }
51