+#include "string.h"
+#include "fd.h"
+
+/**
+ * Write an array of buffers to a file descriptor.
+ *
+ * \param fd The file descriptor.
+ * \param iov Pointer to one or more buffers.
+ * \param iovcnt The number of buffers.
+ *
+ * EAGAIN/EWOULDBLOCK is not considered a fatal error condition. For example
+ * DCCP CCID3 has a sending wait queue which fills up and is emptied
+ * asynchronously. The EAGAIN case means that there is currently no space in
+ * the wait queue, but this can change at any moment.
+ *
+ * \return Negative on fatal errors, number of bytes written else.
+ *
+ * For blocking file descriptors, this function returns either the sum of all
+ * buffer sizes, or the error code of the fatal error that caused the last
+ * write call to fail.
+ *
+ * For nonblocking file descriptors there is a third possibility: Any positive
+ * return value less than the sum of the buffer sizes indicates that some bytes
+ * have been written but the next write would block.
+ *
+ * \sa writev(2), \ref xwrite().
+ */
+int xwritev(int fd, struct iovec *iov, int iovcnt)
+{
+ size_t written = 0;
+ int i;
+ struct iovec saved_iov, *curiov;
+
+ i = 0;
+ curiov = iov;
+ saved_iov = *curiov;
+ while (i < iovcnt && curiov->iov_len > 0) {
+ ssize_t ret = writev(fd, curiov, iovcnt - i);
+ if (ret >= 0) {
+ written += ret;
+ while (ret > 0) {
+ if (ret < curiov->iov_len) {
+ curiov->iov_base += ret;
+ curiov->iov_len -= ret;
+ break;
+ }
+ ret -= curiov->iov_len;
+ *curiov = saved_iov;
+ i++;
+ if (i >= iovcnt)
+ return written;
+ curiov++;
+ saved_iov = *curiov;
+ }
+ continue;
+ }
+ if (errno == EINTR)
+ /*
+ * The write() call was interrupted by a signal before
+ * any data was written. Try again.
+ */
+ continue;
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
+ /*
+ * We don't consider this an error. Note that POSIX
+ * allows either error to be returned, and does not
+ * require these constants to have the same value.
+ */
+ return written;
+ /* fatal error */
+ return -ERRNO_TO_PARA_ERROR(errno);
+ }
+ return written;
+}
+
+/**
+ * Write a buffer to a file descriptor, re-writing on short writes.
+ *
+ * \param fd The file descriptor.
+ * \param buf The buffer to write.
+ * \param len The number of bytes to write.
+ *
+ * This is a simple wrapper for \ref xwritev().
+ *
+ * \return The return value of the underlying call to \ref xwritev().
+ */
+int xwrite(int fd, const char *buf, size_t len)
+{
+ struct iovec iov = {.iov_base = (void *)buf, .iov_len = len};
+ return xwritev(fd, &iov, 1);
+}
+
+/**
+ * Write all data to a file descriptor.
+ *
+ * \param fd The file descriptor.
+ * \param buf The buffer to be sent.
+ * \param len The length of \a buf.
+ *
+ * This is like \ref xwrite() but returns \p -E_SHORT_WRITE if not
+ * all data could be written.
+ *
+ * \return Number of bytes written on success, negative error code else.
+ */
+int write_all(int fd, const char *buf, size_t len)
+{
+ int ret = xwrite(fd, buf, len);
+
+ if (ret < 0)
+ return ret;
+ if (ret != len)
+ return -E_SHORT_WRITE;
+ return ret;
+}
+
+/**
+ * Write a buffer given by a format string.
+ *
+ * \param fd The file descriptor.
+ * \param fmt A format string.
+ *
+ * \return The return value of the underlying call to \ref write_all().
+ */
+__printf_2_3 int write_va_buffer(int fd, const char *fmt, ...)
+{
+ char *msg;
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = xvasprintf(&msg, fmt, ap);
+ ret = write_all(fd, msg, ret);
+ free(msg);
+ return ret;
+}
+
+/**
+ * Read from a non-blocking file descriptor into multiple buffers.
+ *
+ * \param fd The file descriptor to read from.
+ * \param iov Scatter/gather array used in readv().
+ * \param iovcnt Number of elements in \a iov.
+ * \param rfds An optional fd set pointer.
+ * \param num_bytes Result pointer. Contains the number of bytes read from \a fd.
+ *
+ * If \a rfds is not \p NULL and the (non-blocking) file descriptor \a fd is
+ * not set in \a rfds, this function returns early without doing anything.
+ * Otherwise The function tries to read up to \a sz bytes from \a fd, where \a
+ * sz is the sum of the lengths of all vectors in \a iov. As for xwrite(),
+ * \p EAGAIN is not considered an error condition. However, \p EOF is.
+ *
+ * \return Zero or a negative error code. If the underlying call to readv(2)
+ * returned zero (indicating an end of file condition) or failed for some
+ * reason other than \p EAGAIN, a negative error code is returned.
+ *
+ * In any case, \a num_bytes contains the number of bytes that have been
+ * successfully read from \a fd (zero if the first readv() call failed with
+ * EAGAIN). Note that even if the function returns negative, some data might
+ * have been read before the error occurred. In this case \a num_bytes is
+ * positive.
+ *
+ * \sa \ref xwrite(), read(2), readv(2).
+ */
+int readv_nonblock(int fd, struct iovec *iov, int iovcnt, fd_set *rfds,
+ size_t *num_bytes)
+{
+ int ret, i, j;
+
+ *num_bytes = 0;
+ /*
+ * Avoid a shortcoming of select(): Reads from a non-blocking fd might
+ * return EAGAIN even if FD_ISSET() returns true. However, FD_ISSET()
+ * returning false definitely means that no data can currently be read.
+ * This is the common case, so it is worth to avoid the overhead of the
+ * read() system call in this case.
+ */
+ if (rfds && !FD_ISSET(fd, rfds))
+ return 0;
+
+ for (i = 0, j = 0; i < iovcnt;) {
+
+ /* fix up the first iov */
+ assert(j < iov[i].iov_len);
+ iov[i].iov_base += j;
+ iov[i].iov_len -= j;
+ ret = readv(fd, iov + i, iovcnt - i);
+ iov[i].iov_base -= j;
+ iov[i].iov_len += j;
+
+ if (ret == 0)
+ return -E_EOF;
+ if (ret < 0) {
+ if (errno == EAGAIN)
+ return 0;
+ return -ERRNO_TO_PARA_ERROR(errno);
+ }
+ *num_bytes += ret;
+ while (ret > 0) {
+ if (ret < iov[i].iov_len - j) {
+ j += ret;
+ break;
+ }
+ ret -= iov[i].iov_len - j;
+ j = 0;
+ if (++i >= iovcnt)
+ break;
+ }
+ }
+ return 0;
+}
+
+/**
+ * Read from a non-blocking file descriptor into a single buffer.
+ *
+ * \param fd The file descriptor to read from.
+ * \param buf The buffer to read data to.
+ * \param sz The size of \a buf.
+ * \param rfds \see \ref readv_nonblock().
+ * \param num_bytes \see \ref readv_nonblock().
+ *
+ * This is a simple wrapper for readv_nonblock() which uses an iovec with a single
+ * buffer.
+ *
+ * \return The return value of the underlying call to readv_nonblock().
+ */
+int read_nonblock(int fd, void *buf, size_t sz, fd_set *rfds, size_t *num_bytes)
+{
+ struct iovec iov = {.iov_base = buf, .iov_len = sz};
+ return readv_nonblock(fd, &iov, 1, rfds, num_bytes);
+}
+