proper way to return errors to parent process

J

junky_fellow

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);
 
R

Randy Howard

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);

Neither. At least not if you want it to be portable. The values
known to work for sure are 0, EXIT_SUCCESS and EXIT_FAILURE.

exit(errno) may do what you want, and it may not. On platforms where
it does work, then return will in all likelihood also work and the
shell or some other calling entity won't be able to tell the
difference.

Note that return is not a system or library call, so no parens are
required.
 
K

Keith Thompson

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.)
 

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

Forum statistics

Threads
474,159
Messages
2,570,888
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top