atexit.c 1.6 KB

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