00001 /* 00002 * Copyright (c) 1990 Regents of the University of California. 00003 * All rights reserved. 00004 * 00005 * %sccs.include.redist.c% 00006 */ 00007 00008 00009 /* 00010 00011 @deftypefun int xatexit (void (*@var{fn}) (void)) 00012 00013 Behaves as the standard @code{atexit} function, but with no limit on 00014 the number of registered functions. Returns 0 on success, or @minus{}1 on 00015 failure. If you use @code{xatexit} to register functions, you must use 00016 @code{xexit} to terminate your program. 00017 00018 @end deftypefun 00019 00020 */ 00021 00022 /* Adapted from newlib/libc/stdlib/{,at}exit.[ch]. 00023 If you use xatexit, you must call xexit instead of exit. */ 00024 00025 #include "ansidecl.h" 00026 #include "libiberty.h" 00027 00028 #include <stdio.h> 00029 00030 #ifdef ANSI_PROTOTYPES 00031 #include <stddef.h> 00032 #else 00033 #define size_t unsigned long 00034 #endif 00035 00036 #if VMS 00037 #include <stdlib.h> 00038 #include <unixlib.h> 00039 #else 00040 /* For systems with larger pointers than ints, this must be declared. */ 00041 PTR malloc PARAMS ((size_t)); 00042 #endif 00043 00044 static void xatexit_cleanup PARAMS ((void)); 00045 00046 /* Pointer to function run by xexit. */ 00047 extern void (*_xexit_cleanup) PARAMS ((void)); 00048 00049 #define XATEXIT_SIZE 32 00050 00051 struct xatexit { 00052 struct xatexit *next; /* next in list */ 00053 int ind; /* next index in this table */ 00054 void (*fns[XATEXIT_SIZE]) PARAMS ((void)); /* the table itself */ 00055 }; 00056 00057 /* Allocate one struct statically to guarantee that we can register 00058 at least a few handlers. */ 00059 static struct xatexit xatexit_first; 00060 00061 /* Points to head of LIFO stack. */ 00062 static struct xatexit *xatexit_head = &xatexit_first; 00063 00064 /* Register function FN to be run by xexit. 00065 Return 0 if successful, -1 if not. */ 00066 00067 int 00068 xatexit (fn) 00069 void (*fn) PARAMS ((void)); 00070 { 00071 register struct xatexit *p; 00072 00073 /* Tell xexit to call xatexit_cleanup. */ 00074 if (!_xexit_cleanup) 00075 _xexit_cleanup = xatexit_cleanup; 00076 00077 p = xatexit_head; 00078 if (p->ind >= XATEXIT_SIZE) 00079 { 00080 if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL) 00081 return -1; 00082 p->ind = 0; 00083 p->next = xatexit_head; 00084 xatexit_head = p; 00085 } 00086 p->fns[p->ind++] = fn; 00087 return 0; 00088 } 00089 00090 /* Call any cleanup functions. */ 00091 00092 static void 00093 xatexit_cleanup () 00094 { 00095 register struct xatexit *p; 00096 register int n; 00097 00098 for (p = xatexit_head; p; p = p->next) 00099 for (n = p->ind; --n >= 0;) 00100 (*p->fns[n]) (); 00101 }
1.5.6