support for connect_cin

pipe is now non blocking by default
master
Marc Wäckerlin 20 years ago
parent 0cac265f4d
commit a37c9c2164
  1. 29
      mrw/unistd.hpp

@ -9,6 +9,10 @@
@license LGPL, see file <a href="license.html">COPYING</a>
$Log$
Revision 1.6 2004/12/14 20:32:10 marc
support for connect_cin
pipe is now non blocking by default
Revision 1.5 2004/08/28 16:21:25 marc
mrw-c++-0.92 (mrw)
- new file: version.cpp
@ -33,6 +37,7 @@
#define __MRW_UNISTD_HPP__
#include <unistd.h> // pipe, close
#include <fcntl.h> // fcntl (O_NONBLOCK)
#include <errno.h> // errno
namespace mrw {
@ -51,13 +56,22 @@ namespace mrw {
int _lastError;
public:
/// creates a unix pipe
Pipe() throw(std::bad_exception): _lastError(-1) {
/** @param blocking Flag whether the pipe is blocking (default: no) */
Pipe(bool blocking=false) throw(std::bad_exception): _lastError(-1) {
_fd[0] = -1;
_fd[1] = -1;
if (::pipe(_fd)==-1)
{
if (::pipe(_fd)==-1) {
_lastError=errno;
return;
}
if (!blocking) {
int val(fcntl(_fd[0], F_GETFL, 0));
if (fcntl(_fd[0], F_SETFL, (val!=-1?val:0)|O_NONBLOCK) == -1)
_lastError=errno;
}
val = fcntl(_fd[1], F_GETFL, 0);
if (fcntl(_fd[1], F_SETFL, (val!=-1?val:0)|O_NONBLOCK) == -1)
_lastError=errno;
}
}
/// destructor closes pipe if still open
~Pipe() throw(std::bad_exception) {
@ -92,6 +106,13 @@ namespace mrw {
int error() throw() {
return _lastError;
}
/// connect input stream to @c stdin
void connect_cin() throw(std::bad_exception) {
while (::dup2(_fd[0], 0)==-1) if (errno!=EINTR) {
_lastError = errno;
return;
}
}
/// connect output stream to @c stdout
void connect_cout() throw(std::bad_exception) {
while (::dup2(_fd[1], 1)==-1) if (errno!=EINTR) {

Loading…
Cancel
Save