Line | Count | Source |
1 | #include <u.h> | |
2 | #include <libc.h> | |
3 | ||
4 | 0 | #define NEXIT 33 |
5 | ||
6 | static Lock onexlock; | |
7 | static struct | |
8 | { | |
9 | void (*f)(void); | |
10 | int pid; | |
11 | }onex[NEXIT]; | |
12 | ||
13 | int | |
14 | atexit(void (*f)(void)) | |
15 | 0 | { |
16 | 0 | int i; |
17 | ||
18 | 0 | lock(&onexlock); |
19 | 0 | for(i=0; i<NEXIT; i++) |
20 | 0 | if(onex[i].f == 0) { |
21 | 0 | onex[i].pid = getpid(); |
22 | 0 | onex[i].f = f; |
23 | 0 | unlock(&onexlock); |
24 | 0 | return 1; |
25 | 0 | } |
26 | 0 | unlock(&onexlock); |
27 | 0 | return 0; |
28 | 0 | } |
29 | ||
30 | void | |
31 | atexitdont(void (*f)(void)) | |
32 | 0 | { |
33 | 0 | int i, pid; |
34 | ||
35 | 0 | pid = getpid(); |
36 | 0 | for(i=0; i<NEXIT; i++) |
37 | 0 | if(onex[i].f == f && onex[i].pid == pid) |
38 | 0 | onex[i].f = 0; |
39 | 0 | } |
40 | ||
41 | void | |
42 | exits(char *s) | |
43 | 0 | { |
44 | 0 | int i, pid; |
45 | 0 | void (*f)(void); |
46 | ||
47 | 0 | pid = getpid(); |
48 | 0 | for(i = NEXIT-1; i >= 0; i--) |
49 | 0 | if((f = onex[i].f) && pid == onex[i].pid) { |
50 | 0 | onex[i].f = 0; |
51 | 0 | (*f)(); |
52 | 0 | } |
53 | 0 | if(s == 0 || *s == 0) |
54 | 0 | exit(0); |
55 | 0 | exit(exitcode(s)); |
56 | 0 | } |