1 | /* |
---|
2 | * Copyright (c) 1990 Regents of the University of California. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * %sccs.include.redist.c% |
---|
6 | * |
---|
7 | * This function is a modified version of atexit.c |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | FUNCTION |
---|
12 | <<on_exit>>---request execution of function with argument at program exit |
---|
13 | |
---|
14 | INDEX |
---|
15 | on_exit |
---|
16 | |
---|
17 | SYNOPSIS |
---|
18 | #include <stdlib.h> |
---|
19 | int on_exit (void (*<[function]>)(int, void *), void *<[arg]>); |
---|
20 | |
---|
21 | DESCRIPTION |
---|
22 | You can use <<on_exit>> to enroll functions in a list of functions that |
---|
23 | will be called when your program terminates normally. The argument is |
---|
24 | a pointer to a user-defined function which takes two arguments. The |
---|
25 | first is the status code passed to exit and the second argument is of type |
---|
26 | pointer to void. The function must not return a result. The value |
---|
27 | of <[arg]> is registered and passed as the argument to <[function]>. |
---|
28 | |
---|
29 | The functions are kept in a LIFO stack; that is, the last function |
---|
30 | enrolled by <<atexit>> or <<on_exit>> will be the first to execute when |
---|
31 | your program exits. You can intermix functions using <<atexit>> and |
---|
32 | <<on_exit>>. |
---|
33 | |
---|
34 | There is no built-in limit to the number of functions you can enroll |
---|
35 | in this list; however, after every group of 32 functions is enrolled, |
---|
36 | <<atexit>>/<<on_exit>> will call <<malloc>> to get space for the next part |
---|
37 | of the list. The initial list of 32 functions is statically allocated, so |
---|
38 | you can always count on at least that many slots available. |
---|
39 | |
---|
40 | RETURNS |
---|
41 | <<on_exit>> returns <<0>> if it succeeds in enrolling your function, |
---|
42 | <<-1>> if it fails (possible only if no space was available for |
---|
43 | <<malloc>> to extend the list of functions). |
---|
44 | |
---|
45 | PORTABILITY |
---|
46 | <<on_exit>> is a non-standard glibc extension |
---|
47 | |
---|
48 | Supporting OS subroutines required: None |
---|
49 | */ |
---|
50 | |
---|
51 | #include <stddef.h> |
---|
52 | #include <stdlib.h> |
---|
53 | #include "atexit.h" |
---|
54 | |
---|
55 | #ifdef _REENT_SMALL |
---|
56 | |
---|
57 | #include "on_exit_args.h" |
---|
58 | |
---|
59 | /* force linking of static instance of _on_exit_args */ |
---|
60 | const void * const __on_exit_dummy = &__on_exit_args; |
---|
61 | |
---|
62 | #endif /* def _REENT_SMALL */ |
---|
63 | |
---|
64 | /* |
---|
65 | * Register a function to be performed at exit. |
---|
66 | */ |
---|
67 | |
---|
68 | int |
---|
69 | on_exit (void (*fn) (int, void *), |
---|
70 | void *arg) |
---|
71 | { |
---|
72 | return __register_exitproc (__et_onexit, (void (*)(void)) fn, arg, NULL); |
---|
73 | } |
---|