atexit.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. /*
  6. * Dec 2000 Manuel Novoa III
  7. *
  8. * Made atexit handling conform to standards... i.e. no args.
  9. * Removed on_exit since it did not match gnu libc definition.
  10. * Combined atexit and __do_exit into one object file.
  11. *
  12. * Feb 2000 Manuel Novoa III
  13. *
  14. * Reworked file after addition of __uClibc_main.
  15. * Changed name of __do_exit to atexit_handler.
  16. * Changed name of __cleanup to __uClibc_cleanup.
  17. * Moved declaration of __uClibc_cleanup to __uClibc_main
  18. * where it is initialized with (possibly weak alias)
  19. * __stdio_close_all.
  20. */
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. typedef void (*vfuncp) (void);
  25. extern vfuncp __uClibc_cleanup;
  26. #ifdef L_atexit
  27. extern void __stdio_close_all(void);
  28. static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
  29. static int __atexit_count = 0;
  30. static void atexit_handler(void)
  31. {
  32. int count;
  33. /*
  34. * Guard against more functions being added and againt being reinvoked.
  35. */
  36. __uClibc_cleanup = 0;
  37. /* In reverse order */
  38. for (count = __atexit_count ; count-- ; ) {
  39. (*__atexit_table[count])();
  40. }
  41. __stdio_close_all();
  42. }
  43. int atexit(vfuncp ptr)
  44. {
  45. if ((__uClibc_cleanup == 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
  46. __set_errno(ENOMEM);
  47. return -1;
  48. }
  49. if (ptr) {
  50. __uClibc_cleanup = atexit_handler;
  51. __atexit_table[__atexit_count++] = ptr;
  52. }
  53. return 0;
  54. }
  55. #endif
  56. #ifdef L_exit
  57. void exit(int rv)
  58. {
  59. if (__uClibc_cleanup) { /* Not already executing __uClibc_cleanup. */
  60. __uClibc_cleanup();
  61. }
  62. _exit(rv);
  63. }
  64. #endif