atexit.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. * Manuel Novoa III Dec 2000
  7. *
  8. * Modifications:
  9. * Made atexit handling conform to standards... i.e. no args.
  10. * Removed on_exit since it did not match gnu libc definition.
  11. * Combined atexit and __do_exit into one object file.
  12. */
  13. #include <stdlib.h>
  14. #include <errno.h>
  15. typedef void (*vfuncp) (void);
  16. extern vfuncp __cleanup;
  17. #ifdef L_atexit
  18. static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
  19. static int __atexit_count = 0;
  20. static void __do_exit(void)
  21. {
  22. int count = __atexit_count - 1;
  23. __atexit_count = -1; /* ensure no more will be added */
  24. __cleanup = 0; /* Calling exit won't re-do this */
  25. /* In reverse order */
  26. for (; count >= 0; count--) {
  27. (*__atexit_table[count])();
  28. }
  29. }
  30. int atexit(vfuncp ptr)
  31. {
  32. if ((__atexit_count < 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
  33. errno = ENOMEM;
  34. return -1;
  35. }
  36. if (ptr) {
  37. __cleanup = __do_exit;
  38. __atexit_table[__atexit_count++] = ptr;
  39. }
  40. return 0;
  41. }
  42. #endif
  43. #ifdef L_exit
  44. vfuncp __cleanup = 0;
  45. void exit(int rv)
  46. {
  47. if (__cleanup)
  48. __cleanup();
  49. _exit(rv);
  50. }
  51. #endif