atexit.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if (__stdio_close_all)
  42. __stdio_close_all();
  43. }
  44. int atexit(vfuncp ptr)
  45. {
  46. if ((__uClibc_cleanup == 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
  47. __set_errno(ENOMEM);
  48. return -1;
  49. }
  50. if (ptr) {
  51. __uClibc_cleanup = atexit_handler;
  52. __atexit_table[__atexit_count++] = ptr;
  53. }
  54. return 0;
  55. }
  56. #endif
  57. #ifdef L_exit
  58. void exit(int rv)
  59. {
  60. if (__uClibc_cleanup) { /* Not already executing __uClibc_cleanup. */
  61. __uClibc_cleanup();
  62. }
  63. _exit(rv);
  64. }
  65. #endif