testatexit.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This test program will register the maximum number of exit functions
  3. * with atexit(). When this program exits, each exit function should get
  4. * called in the reverse order in which it was registered. (If the system
  5. * supports more than 25 exit functions, the function names will loop, but
  6. * the effect will be the same. Feel free to add more functions if desired)
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. typedef void (*vfuncp) (void);
  11. /* All functions call exit(), in order to test that exit functions can call
  12. * exit() without screwing everything up. :)
  13. */
  14. static void exitfunc0(void) { printf("Executing exitfunc0.\n"); exit(0);}
  15. static void exitfunc1(void) { printf("Executing exitfunc1.\n"); exit(0);}
  16. static void exitfunc2(void) { printf("Executing exitfunc2.\n"); exit(0);}
  17. static void exitfunc3(void) { printf("Executing exitfunc3.\n"); exit(0);}
  18. static void exitfunc4(void) { printf("Executing exitfunc4.\n"); exit(0);}
  19. static void exitfunc5(void) { printf("Executing exitfunc5.\n"); exit(0);}
  20. static void exitfunc6(void) { printf("Executing exitfunc6.\n"); exit(0);}
  21. static void exitfunc7(void) { printf("Executing exitfunc7.\n"); exit(0);}
  22. static void exitfunc8(void) { printf("Executing exitfunc8.\n"); exit(0);}
  23. static void exitfunc9(void) { printf("Executing exitfunc9.\n"); exit(0);}
  24. static void exitfunc10(void) { printf("Executing exitfunc10.\n"); exit(0);}
  25. static void exitfunc11(void) { printf("Executing exitfunc11.\n"); exit(0);}
  26. static void exitfunc12(void) { printf("Executing exitfunc12.\n"); exit(0);}
  27. static void exitfunc13(void) { printf("Executing exitfunc13.\n"); exit(0);}
  28. static void exitfunc14(void) { printf("Executing exitfunc14.\n"); exit(0);}
  29. static void exitfunc15(void) { printf("Executing exitfunc15.\n"); exit(0);}
  30. static void exitfunc16(void) { printf("Executing exitfunc16.\n"); exit(0);}
  31. static void exitfunc17(void) { printf("Executing exitfunc17.\n"); exit(0);}
  32. static void exitfunc18(void) { printf("Executing exitfunc18.\n"); exit(0);}
  33. static void exitfunc19(void) { printf("Executing exitfunc19.\n"); exit(0);}
  34. static void exitfunc20(void) { printf("Executing exitfunc20.\n"); exit(0);}
  35. static void exitfunc21(void) { printf("Executing exitfunc21.\n"); exit(0);}
  36. static void exitfunc22(void) { printf("Executing exitfunc22.\n"); exit(0);}
  37. static void exitfunc23(void) { printf("Executing exitfunc23.\n"); exit(0);}
  38. static void exitfunc24(void) { printf("Executing exitfunc24.\n"); exit(0);}
  39. static vfuncp func_table[] =
  40. {
  41. exitfunc0, exitfunc1, exitfunc2, exitfunc3, exitfunc4,
  42. exitfunc5, exitfunc6, exitfunc7, exitfunc8, exitfunc9,
  43. exitfunc10, exitfunc11, exitfunc12, exitfunc13, exitfunc14,
  44. exitfunc15, exitfunc16, exitfunc17, exitfunc18, exitfunc19,
  45. exitfunc20, exitfunc21, exitfunc22, exitfunc23, exitfunc24
  46. };
  47. /* glibc dynamically adds exit functions, so it will keep adding until
  48. * it runs out of memory! So this will limit the number of exit functions
  49. * we add in the loop below. uClibc has a set limit (currently 20), so the
  50. * loop will go until it can't add any more (so it should not hit this limit).
  51. */
  52. #define ATEXIT_LIMIT 20
  53. int
  54. main ( void )
  55. {
  56. int i = 0;
  57. int count = 0;
  58. int numfuncs = sizeof(func_table)/sizeof(vfuncp);
  59. /* loop until no more can be added */
  60. while(count < ATEXIT_LIMIT && atexit(func_table[i]) >= 0) {
  61. printf("Registered exitfunc%d with atexit()\n", i);
  62. count++;
  63. i = (i+1) % numfuncs;
  64. }
  65. printf("%d functions registered with atexit.\n", count);
  66. /* implicit exit */
  67. }