0091-atexit.c (767B)
1 #include <assert.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 /* 6 output: 7 testing 8 fun1 9 fun1 10 fun1 11 fun1 12 fun1 13 fun1 14 fun1 15 fun1 16 fun1 17 fun1 18 fun1 19 fun1 20 fun1 21 fun1 22 fun1 23 fun1 24 fun1 25 fun1 26 fun1 27 fun1 28 fun1 29 fun1 30 fun1 31 fun1 32 fun1 33 fun1 34 fun1 35 fun1 36 fun1 37 fun2 38 fun3 39 done 40 end: 41 */ 42 43 static void 44 fun1(void) 45 { 46 puts("fun1"); 47 } 48 49 static void 50 fun2(void) 51 { 52 puts("fun2"); 53 } 54 55 static void 56 fun3(void) 57 { 58 puts("fun3"); 59 puts("done"); 60 fflush(stdout); 61 _Exit(0); 62 } 63 64 static void 65 fun4(void) 66 { 67 puts("fun4"); 68 } 69 70 int 71 main(void) 72 { 73 int i, r; 74 75 puts("testing"); 76 r = atexit(fun4); 77 assert(r == 0); 78 79 r = atexit(fun3); 80 assert(r == 0); 81 82 r = atexit(fun2); 83 assert(r == 0); 84 85 86 for (i = 0; i < 29; i++) { 87 r = atexit(fun1); 88 assert(r == 0); 89 } 90 exit(EXIT_SUCCESS); 91 92 printf("failed"); 93 exit(EXIT_FAILURE); 94 95 return 1; 96 }