ju**********@yahoo.co.in writes:
The main() function of my C program is doing some system calls.
If any of my system call fails, I want to terminate the program
and propagate the exact cause of exit (the error number) to the parent
process.
What is the correct way of doing this ?
Should I do,
exit(errno);
Or
return(errno);
You can do either. (BTW, the parentheses on "return(errno);" are
superfluous but harmless; "return errno;" is slightly preferred.) The
main difference is that exit() works from anywhere; return terminates
the program only if it's executed within main().
However, there's no guarantee that the information will be correctly
passed back to the caller. The only strictly portable values you can
return from the main program are 0, EXIT_SUCCESS, and EXIT_FAILURE.
I'm guessing that you're using a Unix-like system. You should
probably ask about this in comp.unix.programmer. (Some things to
consider: how much information is actually passed to the environment,
and whether the system has particular conventions for a program's exit
status.)
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.