home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 519b.lha / Casio_FZ-1 / cleanup.c < prev    next >
C/C++ Source or Header  |  1991-06-09  |  1KB  |  87 lines

  1. /* cleanup - Peter and Karl's standard cleanup management
  2.  *
  3.  * "cleaned up" the documentation  3/1/88
  4.  *
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <functions.h>
  9. #include <exec/memory.h>
  10. #include <stdio.h>
  11.  
  12. struct _clean 
  13. {
  14.     int (*function)();
  15.     struct _clean *next;
  16. } *cleanlist = NULL;
  17.  
  18. /* add_cleanup
  19.  * given a function, add it to a list of functions to call when cleanup
  20.  * is executed
  21.  */
  22. add_cleanup(function)
  23. int (*function)();
  24. {
  25.     struct _clean *ptr;
  26.  
  27.     ptr = AllocMem(sizeof(struct _clean), MEMF_PUBLIC);
  28.     if(!ptr)
  29.         return 0;
  30.     ptr->function = function;
  31.     ptr->next = cleanlist;
  32.     cleanlist = ptr;
  33. }
  34.  
  35. /* cleanup
  36.  * call all the functions that were passed as arguments to add_cleanup
  37.  * this run
  38.  */
  39. cleanup()
  40. {
  41.     struct _clean *ptr;
  42.     int (*f)();
  43.  
  44.     fflush(stdout);
  45.     fflush(stderr);
  46.  
  47.     while(cleanlist) 
  48.     {
  49.         /* locate the next cleanup function and get the function pointer */
  50.         ptr = cleanlist;
  51.         cleanlist = cleanlist->next;
  52.         f = ptr->function;
  53.  
  54.         /* cleanup must clean up after itself */
  55.         FreeMem(ptr, sizeof(struct _clean));
  56.  
  57.         /* execute the function */
  58.         (*f)();
  59.  
  60.         fflush(stdout);
  61.         fflush(stderr);
  62.     }
  63. }
  64.  
  65. /* panic - abort with an error message */
  66.  
  67. short panic_in_progress = 0;
  68.  
  69. panic(s)
  70. char *s;
  71. {
  72.     fflush(stdout);
  73.     fprintf(stderr,"panic: %s\n",s);
  74.     fflush(stderr);
  75.     if (!panic_in_progress)
  76.     {
  77.         cleanup();
  78.         exit(10);
  79.     }
  80.     fprintf(stderr,"double panic!\n");
  81. }
  82.  
  83. _abort()
  84. {
  85.     panic("^C or other C library abort");
  86. }
  87.