Setting return value after calling "system" function

Hi,

Does anyone know how to set user-defined return value after "system" command is executed?

1
2
3
4
5
        std::stringstream stream;
        stream <<"hello /start "<< name.c_str();
        int rv=system(stream.str().c_str());
        if(rv!=0)
            Log("system command failed\n");


1) In above code, system("some command") is passed on to the command processor. rv is the return value, that says if "some command" actually passed or failed. I want to access the actual return value from "some command" and take decisions based on that. How would I do that?

2) If it cannot be done using "system" command, any other way to do that?

Thanks!
Last edited on
The value of rv is the return value of the command (of its main(), to be more accurate). Do you mean you want its text output?
Better go thru the man pages of system call.

It clearly says "The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise.".
@helios and (anyone else who knows): The value of rv indicates if "some command" within system(), passed or failed and that's about it. I am looking for a way to detect return value of that "some command".

Let's say "some command" has 5 different return values, and calling function needs to take decision based on that (and not based on whether that command was either executed or not), how would I do that?
The man page spells it out (n4nature just didn't include all of the text):

The value returned is -1 on error (e.g. fork() failed), and the return status of
the command otherwise. This latter return status is in the format specified in
wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case
/bin/sh could not be executed, the exit status will be that of a command that does
exit(127).
Topic archived. No new replies allowed.