Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: exit(main(argc,argv,env)); Message-ID: <494@cresswell.quintus.UUCP> Date: 24 Dec 87 00:05:59 GMT References: <1286@laidbak.UUCP> <174@goofy.megatest.UUCP> Organization: Quintus Computer Systems, Mountain View, CA Lines: 51 Summary: Sun-3s do have on_exit In article <174@goofy.megatest.UUCP>, djones@megatest.UUCP (Dave Jones) writes: > Hum. "onexit"... Just what's needed. (See my previous note, where I > rambled on about "Lexit()". > > But on my Sun-3, I find no manual-page for onexit, and > > nm /lib/libc.a | grep onexit > > returns silently. So it looks as though not ALL unixes have onexit(). % man exit % man 2 exit % man 3 exit % man 3 on_exit SYNOPSIS int on_exit(procp, arg) void (*procp)(); caddr_t arg; NOTES This call is specific to Sun Unix and should not be used if portability is a concern. Basically, there is a stack of up to 20 termination functions, and on_exit() pushes the pair procp,arg on the stack. On exit, these pairs are popped and procp(arg) called in reverse order. The problem with this feature (and with the version in dpANS) is that it is too global. What I would like to be able to do is { FILE *f; EXIT_HANDLER *e; FCHECK(f = fopen(...)); e = push_exit_handler(fclose, f); .... pop_run_one_handler(e); ... } so that I can put cleanup actions onto a stack and REMOVE them when they have been done, but if an exit() happens in between the actions will be done. Common Lisp's "unwind-protect" is the kind of thing we're after. Some other C implementations have a similar thing called "atexit()" or "at_exit()". While onexit() or whatever doesn't quite do what I want, I can at least register my own handler manager with it, so it's ok as a primitive.