should rdbuf()->status() match exit() value?
Brought to you by:
redi
I wrote a custom program in C++ that looks liket this:
#include <cstdlib>
int main(){exit(1);}
It works in my shell, as I expect:
$ ./main
$ echo $?
1
$
However, the pstream library gices me -1
redi::pstream proc("./main");
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints -1
If I close the process, it gives me 36096
redi::pstream proc("./main");
proc.close();
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints 36096
If I read all the process streams, it gives me back 256:
redi::pstream proc("./main",redi::pstreams::psterr|redi::pstreams::pstdout);
while(std::Getline(proc.err(),temp)){}
proc.clear();
while(std::Getline(proc.out(),temp)){}
proc.close();
auto return_code = proc.rdbuf()->status();
std::cout << return_code << '\n'; //prints 256
Am I doing something wrong? Is a return value from 0 - 255 guaranteed to match the status()?
like*
getline*
gives*
I can't edit the ticket.
Last edit: Trevor Hickey 2016-06-07
Ignore the -1 then
Last edit: Trevor Hickey 2016-06-07
Am I suppose to use WEXITSTATUS on wait()?
I figured it out. I have to call
WEXITSTATUS()on the value returned back fromstatus();Last edit: Trevor Hickey 2016-06-07
/close
Right, it returns the same value as
waitorwaitpidso you need to use WIFEXITED, WEXITSTATUS etc.Last edit: Jonathan Wakely 2016-06-07