Updated to fedora-glibc-20051116T0829
[glibc.git] / stdlib / exit.c
blobe5e25960b1ec9f074324dc1fdcfa547db570512c
1 /* Copyright (C) 1991,95,96,97,99,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "exit.h"
24 #include "set-hooks.h"
25 DEFINE_HOOK (__libc_atexit, (void))
28 /* Call all functions registered with `atexit' and `on_exit',
29 in the reverse of the order in which they were registered
30 perform stdio cleanup, and terminate program execution with STATUS. */
31 void
32 exit (int status)
34 /* We do it this way to handle recursive calls to exit () made by
35 the functions registered with `atexit' and `on_exit'. We call
36 everyone on the list and use the status value in the last
37 exit (). */
38 while (__exit_funcs != NULL)
40 struct exit_function_list *old;
42 while (__exit_funcs->idx > 0)
44 const struct exit_function *const f =
45 &__exit_funcs->fns[--__exit_funcs->idx];
46 switch (f->flavor)
48 case ef_free:
49 case ef_us:
50 break;
51 case ef_on:
52 (*f->func.on.fn) (status, f->func.on.arg);
53 break;
54 case ef_at:
55 (*f->func.at) ();
56 break;
57 case ef_cxa:
58 (*f->func.cxa.fn) (f->func.cxa.arg, status);
59 break;
63 old = __exit_funcs;
64 __exit_funcs = __exit_funcs->next;
65 if (__exit_funcs != NULL)
66 /* Don't free the last element in the chain, this is the statically
67 allocate element. */
68 free (old);
71 RUN_HOOK (__libc_atexit, ());
73 _exit (status);
75 libc_hidden_def (exit)