From 5f7f8f20b5556aa4ad569e33166b30781fa83bfb Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Thu, 2 Nov 2006 19:57:18 +0000 Subject: [PATCH] ui, libi2ncommon: (tomj) proper MAC address error handling, ability to get exit status from pipe streams (#898) ATTENTION: i18n translation --- src/pipestream.hxx | 53 +++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/pipestream.hxx b/src/pipestream.hxx index de59760..991e65c 100644 --- a/src/pipestream.hxx +++ b/src/pipestream.hxx @@ -25,9 +25,16 @@ class inpipebuf : public std::streambuf char buffer; FILE *pipe; + // "callback" variables for destructor to store exit status + bool *status_set; + int *exit_status; + public: inpipebuf(const std::string& command) { + status_set = NULL; + exit_status = NULL; + pipe = popen (command.c_str(), "r"); if (pipe == NULL) throw EXCEPTION (pipestream_error, "can't open program or permission denied"); @@ -38,12 +45,21 @@ class inpipebuf : public std::streambuf ~inpipebuf() { - if (pipe != NULL) - pclose (pipe); - - pipe = NULL; + if (pipe != NULL) { + int pclose_exit = pclose (pipe); + + if (exit_status && pclose_exit != -1) { + *status_set = true; + *exit_status = pclose_exit; + } + + pipe = NULL; + } } + void store_exit_status(bool *_status_set, int *_exit_status) + { status_set = _status_set; exit_status = _exit_status; } + protected: virtual int_type underflow() { @@ -67,10 +83,14 @@ class inpipestream : public std::istream { protected: inpipebuf buf; + public: inpipestream(const std::string& command) : buf(command), std::istream(&buf) {} + + void store_exit_status(bool *_status_set, int *_exit_status) + { buf.store_exit_status(_status_set, _exit_status); } }; class outpipebuf : public std::streambuf @@ -78,9 +98,16 @@ class outpipebuf : public std::streambuf protected: FILE *pipe; + // "callback" variables for destructor to store exit status + bool *status_set; + int *exit_status; + public: outpipebuf(const std::string& command) { + status_set = NULL; + exit_status = NULL; + pipe = popen (command.c_str(), "w"); if (pipe == NULL) throw EXCEPTION (pipestream_error, "can't open program or permission denied"); @@ -88,12 +115,21 @@ class outpipebuf : public std::streambuf ~outpipebuf() { - if (pipe != NULL) - pclose (pipe); + if (pipe != NULL) { + int pclose_exit = pclose (pipe); + + if (exit_status && pclose_exit != -1) { + *status_set = true; + *exit_status = pclose_exit; + } - pipe = NULL; + pipe = NULL; + } } + void store_exit_status(bool *_status_set, int *_exit_status) + { status_set = _status_set; exit_status = _exit_status; } + protected: virtual int_type overflow(int_type c) { @@ -119,6 +155,9 @@ class outpipestream : public std::ostream outpipestream(const std::string& command) : buf(command), std::ostream(&buf) {} + + void store_exit_status(bool *_status_set, int *_exit_status) + { buf.store_exit_status(_status_set, _exit_status); } }; #endif -- 1.7.1