Menu

How to get exit status from ipstream

Help
2004-08-03
2013-01-20
  • Danny Aizer

    Danny Aizer - 2004-08-03

    Is there a way to get the exit status of a command
    which was executed via the ipstream class?

    E.g., I want to

    ipstream command("ls some_file 2>&1");
    while ( getline(ipstream ...) )
    {

    }

    if ( ipstream.status() != 0 ??? )
    {
      cout << "command failed with error << ipstream.status() << endl;
    }
    else
    {
      cout << "command successfully completed" << endl;
    }

     
    • Jonathan Wakely

      Jonathan Wakely - 2004-08-08

      There is a status member on the stream buffer class, see http://pstreams.sourceforge.net/doc/classredi_1_1basic__pstreambuf.html#a12

      e.g.

      if (iipstream.rdbuf()->status() != 0)
        // ...

      Before this will return the exit status you must have waited for the process to exit, using either pstreambuf::wait() or pstreambuf::close() (which calls wait())

       
    • Danny Aizer

      Danny Aizer - 2004-08-22

      I have been using the following:

      ipstream command("/bin/ls -l");
      if ( command.is_open() )
      {
        while (getline(command, str))
        {
          cout << str << endl;
        }

        if (true == command.rdbuf()->exited())
        {
          cout << "status="
                 << command.rdbuf()->status() << endl;
        }
        else
        {
          cerr << "process not exited" << endl;
        }

        command.close();
      }

      But this seems to make file handles not to be closed: when running the above in loop I finally get the "Too many files open" error (from rdbuf()->error()).

      Am I not using the class correctly, or is this a bug? In the code of exited() there's a TODO item about calling close_fd_array(), which seems to be related to the behaviour I'm experiencing.

      Thanks for any help,
      /Danny

       
      • Jonathan Wakely

        Jonathan Wakely - 2004-10-19

        Hi Danny,

        Sorry for not replying - I managed to forget to respond.

        There was a file descriptor leak which I fixed in June, but
        didn't make a new release until the new 0.5.0 release I
        made two days ago.

        Using the latest version I can run your example code in a
        loop with a resource limit of 6 open files, so nothing
        seems to be leaking now.

        Please let me know if you have any problems with the
        new release.

        thanks

        jon

         
        • Danny Aizer

          Danny Aizer - 2004-10-19

          Hi Jon,

          Thanks. I've been using the method from your previous post in the interim and it works.

          I have downloaded the new release (the first one to so according to SF.net), but I will only integrate with it next week or so, as I have other problems right now.

          /Danny

           

Log in to post a comment.