Operating System - HP-UX
1834882 Members
2409 Online
110071 Solutions
New Discussion

atexit, getting the exit code

 
James Myers
Occasional Contributor

atexit, getting the exit code

Is there a way for an atexit handler to get
the exit code that was in the exit call ?

like exit(5), in my atexit registered
function, can I determine it was 5 ?

I am using the atexit to register an exit
handler for a C program, using the aCC compiler, hpux 11.0
3 REPLIES 3
Jeff Schussele
Honored Contributor

Re: atexit, getting the exit code

Hi James,

echo $?

will give you exit code.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor

Re: atexit, getting the exit code

Because the handlers pushed by atexit expect no arguments there is no direct, portable method to accomplish this. However, you should be able to
create a wrapper function for exit(), e.g myexit() that first takes the argument and assigns it to a global variable which your handlers could then access.

e.g.

int Exit_Status = 1;

/* sample silly function to feed atexit() */
void myexithandler()
{
(void) fprintf(stderr,"Exit Status = %d\n",Exit_Status);
(void) fflush(stderr);
return;
} /* myexithandler */

Now instead of using exit() in your code, use myexit();

void myexit(int status)
{
Exit_Status = status;
exit(status);
/* NOTREACHED */
return;
} /* myexit */

If it ain't broke, I can fix that.
ranganath ramachandra
Esteemed Contributor

Re: atexit, getting the exit code

(there seems to be no nice API for this)

defining something with a name other than exit will not let you catch exit's that are not called form your code.

you can define your own 'void exit(int status) which will override exit() of libc. put your atexit handler code in this exit() routine and at the end a call to '_exit(status)'. the difference between calling exit() and _exit() is in the exit() man page.

if you run into problems because of other (crt0/dld.sl) atexit handlers not being called, you may need to implement atexit() as well ...

 
--
ranga
hp-ux 11i v3[i work for hpe]

Accept or Kudo