home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / Iff_Archiver / cleanup.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  2KB  |  107 lines

  1. /* cleanup - Hackercorp Hackercore, standard cleanup management routines, v1.2
  2.   
  3.    This code is released to the public domain, 5/9/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/memory.h>
  35. #include <stdio.h>
  36.  
  37. struct _clean 
  38. {
  39.     int (*function)();
  40.     struct _clean *next;
  41. } *cleanlist = NULL;
  42.  
  43. /* add_cleanup
  44.  * given a function, add it to a list of functions to call when cleanup
  45.  * is executed
  46.  */
  47. add_cleanup(function)
  48. int (*function)();
  49. {
  50.     struct _clean *ptr;
  51.  
  52.     ptr = AllocMem(sizeof(struct _clean), MEMF_PUBLIC);
  53.     if(!ptr)
  54.         return 0;
  55.     ptr->function = function;
  56.     ptr->next = cleanlist;
  57.     cleanlist = ptr;
  58. }
  59.  
  60. /* cleanup
  61.  * call all the functions that were passed as arguments to add_cleanup
  62.  * this run
  63.  */
  64. cleanup()
  65. {
  66.     struct _clean *ptr;
  67.     int (*f)();
  68.  
  69.     while(cleanlist) 
  70.     {
  71.         /* locate the next cleanup function and get the function pointer */
  72.         ptr = cleanlist;
  73.         cleanlist = cleanlist->next;
  74.         f = ptr->function;
  75.  
  76.         /* cleanup must clean up after itself */
  77.         FreeMem(ptr, sizeof(struct _clean));
  78.  
  79.         /* execute the function */
  80.         (*f)();
  81.     }
  82. }
  83.  
  84. /* panic - abort with an error message */
  85.  
  86. short panic_in_progress = 0;
  87.  
  88. panic(s)
  89. char *s;
  90. {
  91.     fflush(stdout);
  92.     fprintf(stderr,"panic: %s\n",s);
  93.     fflush(stderr);
  94.     if (!panic_in_progress)
  95.     {
  96.         cleanup();
  97.         exit(10);
  98.     }
  99.     fprintf(stderr,"double panic!\n");
  100.     exit(11);
  101. }
  102.  
  103. _abort()
  104. {
  105.     panic("^C or other C library abort");
  106. }
  107.