aboutsummaryrefslogtreecommitdiff
path: root/butl/fdstream.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2016-11-11 11:33:21 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2016-11-14 19:48:22 +0300
commit834f0df3850c2b115e2febbf5b6bdafbe88651e2 (patch)
tree0304a9e25585b3e59847da0ee52ddcb83464b9b1 /butl/fdstream.cxx
parentb47ab3e06b8af07a79b58f23d18b5a06c2dc8108 (diff)
Add fdopen_pipe()
Diffstat (limited to 'butl/fdstream.cxx')
-rw-r--r--butl/fdstream.cxx32
1 files changed, 29 insertions, 3 deletions
diff --git a/butl/fdstream.cxx b/butl/fdstream.cxx
index 16801ed..6ebfaf2 100644
--- a/butl/fdstream.cxx
+++ b/butl/fdstream.cxx
@@ -6,14 +6,14 @@
#ifndef _WIN32
# include <fcntl.h> // open(), O_*, fcntl()
-# include <unistd.h> // close(), read(), write(), lseek(), dup(), ssize_t,
- // STD*_FILENO
+# include <unistd.h> // close(), read(), write(), lseek(), dup(), pipe(),
+ // ssize_t, STD*_FILENO
# include <sys/uio.h> // writev(), iovec
# include <sys/stat.h> // S_I*
# include <sys/types.h> // off_t
#else
# include <io.h> // _close(), _read(), _write(), _setmode(), _sopen(),
- // _lseek(), _dup()
+ // _lseek(), _dup(), _pipe()
# include <share.h> // _SH_DENYNO
# include <stdio.h> // _fileno(), stdin, stdout, stderr
# include <fcntl.h> // _O_*
@@ -816,6 +816,18 @@ namespace butl
return fdmode (STDERR_FILENO, m);
}
+ fdpipe
+ fdopen_pipe (fdopen_mode m)
+ {
+ assert (m == fdopen_mode::none || m == fdopen_mode::binary);
+
+ int pd[2];
+ if (pipe (pd) == -1)
+ throw_ios_failure (errno);
+
+ return {auto_fd (pd[0]), auto_fd (pd[1])};
+ }
+
#else
bool
@@ -914,5 +926,19 @@ namespace butl
return fdmode (fd, m);
}
+ fdpipe
+ fdopen_pipe (fdopen_mode m)
+ {
+ assert (m == fdopen_mode::none || m == fdopen_mode::binary);
+
+ int pd[2];
+ if (_pipe (
+ pd,
+ 64 * 1024, // Set buffer size to 64K.
+ _O_NOINHERIT | (m == fdopen_mode::none ? _O_TEXT : _O_BINARY)) == -1)
+ throw_ios_failure (errno);
+
+ return {auto_fd (pd[0]), auto_fd (pd[1])};
+ }
#endif
}