home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 497a.lha / ComSMUS_v2.2 / src / cleanup.c next >
Encoding:
C/C++ Source or Header  |  1991-04-07  |  1.5 KB  |  91 lines

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