1 /* atexit.c -- Do nothing, but to return a success
2 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2, or (at your option)
6     any later version.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software Foundation,
15     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16 
17 #ifdef HAVE_CONFIG_H
18 #  include <config.h>
19 #endif
20 
21 #ifdef HAVE_ON_EXIT
22 /* The function used by on_exit has two args:
23      The routine named is called as
24           (*procp)(status, arg);
25      where status is the argument with which exit()  was  called,
26      or  zero  if main returns.  Typically, arg is the address of
27      an argument vector to (*procp), but may be an integer value.
28 
29   Use this space to store the address to call the atexit'ed function,
30   so that there are no mismatches between arities of call procedures.
31 */
32 
33 static void
on_exit_wrapper(status,func)34 on_exit_wrapper (status, func)
35   int status;
36   void (* func) ();
37 {
38   func ();
39 }
40 
41 int
42 atexit (func)
43     void (*func) ();
44 {
45   return on_exit (on_exit_wrapper, func);
46 }
47 
48 #else /* !defined(HAVE_ON_EXIT) */
49 /* Define the registering function
50  * and a replacement exiting function */
51 
52 #define EXIT_STACK_SIZE 32
53 
54 typedef void (*exit_fn_t) ();
55 static exit_fn_t exit_stack[EXIT_STACK_SIZE];
56 static int exit_stack_len = 0;
57 
58 int
atexit(func)59 atexit (func)
60   exit_fn_t func;
61 {
62   if (exit_stack_len >= EXIT_STACK_SIZE)
63     return 1;
64 
65   exit_stack [exit_stack_len ++] = func;
66   return 0;
67 }
68 
69 void
exit(status)70 exit (status)
71    int status;
72 {
73   while (exit_stack_len > 0)
74     exit_stack [--exit_stack_len] ();
75 #undef exit
76   exit (status);
77 }
78 
79 #endif
80