atexit()

S

Steve Lambert

Hi,

Is it possible for the functions registered with atexit() to have access to
parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?

I'd like to register a set of routines to be executed on exit(EXIT_FAILURE)
and a different set of routines on exit(EXIT_SUCCESS).

Any way of doing this?

Cheers

Steve
 
M

Mike Wahler

Steve Lambert said:
Hi,

Is it possible for the functions registered with atexit() to have access to
parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?

I'd like to register a set of routines to be executed on exit(EXIT_FAILURE)
and a different set of routines on exit(EXIT_SUCCESS).

Any way of doing this?

My first thought was to pass this info ('failure' or 'success')
via an argument to the registered function. BUT:

==========================================
From the C standard:

7.20.4.2 The atexit function

Synopsis

1 #include <stdlib.h>
int atexit(void (*func)(void));

Description

2 The atexit function registers the function pointed to by func,
to be called without arguments at normal program termination.

Environmental limits

3 The implementation shall support the registration of at least
32 functions.

Returns

4 The atexit function returns zero if the registration succeeds,
nonzero if it fails.

==========================================


So, as much as I dislike using globals, it seems that would
be the way to pass this info: set a global to EXIT_SUCCESS
or EXIT_FAILURE (or whatever other value you find useful),
and have a 'controlling' registered function inspect that
value, and invoke one of a set of other functions which
performs the desired actions.

-Mike
 
P

Peter Shaggy Haywood

Groovy hepcat Mike Wahler was jivin' on Thu, 30 Sep 2004 23:21:34 GMT
in comp.lang.c.
Re: atexit()'s a cool scene! Dig it!

[Snip.]
So, as much as I dislike using globals, it seems that would
be the way to pass this info: set a global to EXIT_SUCCESS
or EXIT_FAILURE (or whatever other value you find useful),
and have a 'controlling' registered function inspect that
value, and invoke one of a set of other functions which
performs the desired actions.

That's what I would have suggested if you hadn't beaten me to it. :)
But I would add this.
Make the so called global variable static and put it in its own
translation unit. Also put in this translation unit an exit() wrapper
that sets this variable and a function to read its value. When exiting
the program, call the wrapper instead of exit() directly, and call
the other function from the function registered with atexit().

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top