Click to See Complete Forum and Search --> : Exit status of system command??
rmvinodh123
04-12-2007, 03:06 AM
I want to include some error checking in my code for the system command
system("ls -z"); didn't not print any error in the terminal.
system("ls") gave an exit status of 1 in my system,does this value vary on different systems??
please help.
tecknophreak
04-12-2007, 08:04 AM
While some might caution against using the system command, according to man (3) system, you need to call WEXITSTATUS(retval) to get the returned value.
I.E.
int main() {
int ret = system("ls -z");
std::cout << "Status: " << WEXITSTATUS(ret) << std::endl;
}
bwkaz
04-12-2007, 06:34 PM
according to man (3) system, you need to call WEXITSTATUS(retval) to get the returned value. Yes, however the man page should also note that you can't use WEXITSTATUS unless WIFEXITED returns true. (If you do, you'll get the wrong result.) So your code should go like this:
int main() {
int ret = system("ls -z");
if(WIFEXITED(ret)) {
std::cout << "Program exited, status: " << WEXITSTATUS(ret) << std::endl;
}
else if(WIFSIGNALED(ret)) {
std::cerr << "Program was killed by a signal: " << WTERMSIG(ret) << std::endl;
}
else {
std::cerr << "Program exited for some unknown reason." << std::endl;
}
} I should note that I just looked at my machine's system(3) manpage, and it doesn't say anything about these other macros. That's really wrong; it really should talk about the entire setup if it talks about any of these macros. See the wait(2) manpage (man 2 wait) for the full details.
rmvinodh123
04-15-2007, 10:56 PM
Thanks for the reply.
rmvinodh123
04-15-2007, 10:59 PM
very informative thx.