atexit.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <stdlib.h>
  22. #include <errno.h>
  23. typedef void (*vfuncp) (void);
  24. extern vfuncp __uClibc_cleanup;
  25. #ifdef L_atexit
  26. extern void __stdio_close_all(void);
  27. static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
  28. static int __atexit_count = 0;
  29. static void atexit_handler(void)
  30. {
  31. int count;
  32. /*
  33. * Guard against more functions being added and againt being reinvoked.
  34. */
  35. __uClibc_cleanup = 0;
  36. /* In reverse order */
  37. for (count = __atexit_count ; count-- ; ) {
  38. (*__atexit_table[count])();
  39. }
  40. __stdio_close_all();
  41. }
  42. int atexit(vfuncp ptr)
  43. {
  44. if ((__uClibc_cleanup == 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
  45. __set_errno(ENOMEM);
  46. return -1;
  47. }
  48. if (ptr) {
  49. __uClibc_cleanup = atexit_handler;
  50. __atexit_table[__atexit_count++] = ptr;
  51. }
  52. return 0;
  53. }
  54. #endif
  55. #ifdef L_exit
  56. void exit(int rv)
  57. {
  58. if (__uClibc_cleanup) { /* Not already executing __uClibc_cleanup. */
  59. __uClibc_cleanup();
  60. }
  61. _exit(rv);
  62. }
  63. #endif