home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 497a.lha / ComSMUS_v2.2 / voicebank.prog / cleanup.c < prev    next >
C/C++ Source or Header  |  1991-04-07  |  2KB  |  109 lines

  1. /* cleanup - Hackercorp Hackercore, standard cleanup management routines, v1.1
  2.   
  3.    This code is released to the public domain, 4/25/88, by Peter da Silva
  4.    and Karl Lehenbauer
  5.   
  6.    Usage:
  7.   
  8.       add_cleanup(cleanup_routine);
  9.       void cleanup_routine();
  10.  
  11.           Add a routine to the cleanup list.  When cleanup  is called, routines 
  12.         passed as arguments to add_cleanup will be executed  in the reverse 
  13.         order in which they were received.  See the source to  the IFF CAT 
  14.         archiver, iffar, for an example of how to use this.
  15.  
  16.     void cleanup()
  17.  
  18.         Execute all the routines passed as arguments to add_cleanup
  19.       
  20.     panic(s)
  21.     char *s;
  22.  
  23.         Abort the program by printing a panic message including string "s"
  24.         on stderr, flushing stdout and stderr, calling cleanup and exiting.
  25.  
  26.     _abort()
  27.         By defining _abort to call panic, entering control-C while using
  28.         stdio will cause the program to abort, executing your cleanup
  29.         routines.  Also note that Manx sdb calls _abort when the user
  30.         requests an exit, so your cleanup routines will be executed then
  31.         as well.
  32. */
  33.  
  34. #include <exec/types.h>
  35. #include <exec/memory.h>
  36. #include <functions.h>
  37. #include <stdio.h>
  38.  
  39. struct _clean 
  40. {
  41.     int (*function)();
  42.     struct _clean *next;
  43. } *cleanlist = NULL;
  44.  
  45. /* add_cleanup
  46.  * given a function, add it to a list of functions to call when cleanup
  47.  * is executed
  48.  */
  49. add_cleanup(function)
  50. int (*function)();
  51. {
  52.     struct _clean *ptr;
  53.  
  54.     ptr = AllocMem((long)sizeof(struct _clean), MEMF_PUBLIC);
  55.     if(!ptr)
  56.         return 0;
  57.     ptr->function = function;
  58.     ptr->next = cleanlist;
  59.     cleanlist = ptr;
  60. }
  61.  
  62. /* cleanup
  63.  * call all the functions that were passed as arguments to add_cleanup
  64.  * this run
  65.  */
  66. cleanup()
  67. {
  68.     struct _clean *ptr;
  69.     int (*f)();
  70.  
  71.     while(cleanlist) 
  72.     {
  73.         /* locate the next cleanup function and get the function pointer */
  74.         ptr = cleanlist;
  75.         cleanlist = cleanlist->next;
  76.         f = ptr->function;
  77.  
  78.         /* cleanup must clean up after itself */
  79.         FreeMem(ptr, sizeof(struct _clean));
  80.  
  81.         /* execute the function */
  82.         (*f)();
  83.     }
  84. }
  85.  
  86. /* panic - abort with an error message */
  87.  
  88. short panic_in_progress = 0;
  89.  
  90. panic(s)
  91. char *s;
  92. {
  93.     fflush(stdout);
  94.     fprintf(stderr,"panic: %s\n",s);
  95.     fflush(stderr);
  96.     if (!panic_in_progress)
  97.     {
  98.         cleanup();
  99.         exit(10);
  100.     }
  101.     fprintf(stderr,"double panic!\n");
  102.     exit(11);
  103. }
  104.  
  105. _abort()
  106. {
  107.     panic("^C or other C library abort");
  108. }
  109.