--- /dev/null
+#include <cerrno>
+#include <unistd.h>
+#include <fcntl.h>
+#include <msp/core/systemerror.h>
+#include "handle.h"
+#include "handle_private.h"
+
+namespace Msp {
+namespace IO {
+
+Handle::operator const void *() const
+{
+ return priv->handle!=-1 ? this : 0;
+}
+
+
+void sys_set_blocking(Handle &handle, bool b)
+{
+ int flags = fcntl(*handle, F_GETFD);
+ fcntl(*handle, F_SETFL, (flags&~O_NONBLOCK)|(b?0:O_NONBLOCK));
+}
+
+unsigned sys_read(Handle &handle, char *buf, unsigned size)
+{
+ int ret = read(*handle, buf, size);
+ if(ret==-1)
+ {
+ if(errno==EAGAIN)
+ return 0;
+ else
+ throw system_error("read");
+ }
+
+ return ret;
+}
+
+unsigned sys_write(Handle &handle, const char *buf, unsigned size)
+{
+ int ret = write(*handle, buf, size);
+ if(ret==-1)
+ {
+ if(errno==EAGAIN)
+ return 0;
+ else
+ throw system_error("write");
+ }
+
+ return ret;
+}
+
+void sys_close(Handle &handle)
+{
+ close(*handle);
+}
+
+} // namespace IO
+} // namespace Msp