On May 8, 9:05=A0pm, titanandrews <titanandr...@[EMAIL PROTECTED]
> wrote:
> Hi All,
>
> Is there any way to catch the exit code from someone calling
> exit(status) and check the value before the program terminates?
>
> Just for an idea, the code I'm thinking of is something like this:
>
> void exithandler()
> {
> =A0 =A0 DWORD exitCode =3D 0;
> =A0 =A0 ::GetExitCodeProcess(GetCurrentProcess(),&exitCode);
> =A0 =A0 std::cout << exitCode << std::endl;
> =A0 =A0 =A0 =A0 // prints 259, NOT 55
>
> }
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> =A0 =A0 atexit(exithandler);
> =A0 =A0 exit(55);
> =A0 =A0 return 0;
>
> }
>
> This is for Windows, but I am looking for a solution for UNIX/Linux.
> BTW... this solution does NOT work because the process has not yet
> terminated when my exit handler is called, thus resulting in exitCode
> of 259 instead of 55. 259 on Windows means the process is still
> active.
>
> Does anyone know if this can be done?
> Use case: Imagine you have a library and you want to check if someone
> is calling exit with a particular code.
Your post looks like OS/platform specific and bit OT here. Still i
would like to share my ideas.
As others have suggested, you may have a wrapper script and use $? to
find the exit code. But if you would like to do it using a program,
then the way i think is you need to use fork() and have control of the
child process. Whenever the child exits, you need to use waitpid to
know about the child exit status. The child details are stored in /
proc(may differ on platforms) as long as the child is alive.
If you like to know more, i would suggest you reading
Advance programming in the Unix environment - Richard W Stevens.
Of course, based on your interest, you may go thru all books by
Richard W Stevens - my guru. Alternately, you may even refer
comp.unix.programmer.
Thanks,
Balaji.


|