atexit.c 1.5 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. * 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 <errno.h>
  14. /* ATEXIT.H */
  15. #define MAXONEXIT 20 /* AIUI Posix requires 10 */
  16. typedef void (*vfuncp) (void);
  17. extern vfuncp __cleanup;
  18. extern void __do_exit();
  19. extern void _exit __P((int __status)) __attribute__ ((__noreturn__));
  20. extern vfuncp __atexit_table[MAXONEXIT];
  21. extern int __atexit_count;
  22. /* End ATEXIT.H */
  23. #ifdef L_atexit
  24. int atexit(vfuncp ptr)
  25. {
  26. if ((__atexit_count < 0) || (__atexit_count >= MAXONEXIT)) {
  27. errno = ENOMEM;
  28. return -1;
  29. }
  30. if (ptr) {
  31. __cleanup = __do_exit;
  32. __atexit_table[__atexit_count++] = ptr;
  33. }
  34. return 0;
  35. }
  36. vfuncp __atexit_table[MAXONEXIT];
  37. int __atexit_count = 0;
  38. void __do_exit(int rv)
  39. {
  40. int count = __atexit_count - 1;
  41. __atexit_count = -1; /* ensure no more will be added */
  42. __cleanup = 0; /* Calling exit won't re-do this */
  43. /* In reverse order */
  44. for (; count >= 0; count--) {
  45. (*__atexit_table[count])();
  46. }
  47. }
  48. #endif
  49. #ifdef L_exit
  50. void __stdio_close_all(void); /* note: see _start.S - could be faked */
  51. vfuncp __cleanup = 0;
  52. void exit(int rv)
  53. {
  54. if (__cleanup)
  55. __cleanup();
  56. __stdio_close_all();
  57. _exit(rv);
  58. }
  59. #endif