Introduce xwritev().
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index a73325ba5af60adb712213aa789243dc02f74d90..d2f93611de1e66ad9bbcd8dcd5aada40546f560f 100644 (file)
--- a/fd.c
+++ b/fd.c
 #include "fd.h"
 
 /**
- * Write a buffer to a file descriptor, re-writing on short writes.
+ * Write an array of buffers to a file descriptor.
  *
  * \param fd The file descriptor.
- * \param buf The buffer to write.
- * \param len The number of bytes to write.
+ * \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 \a len 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: A positive return value < \a
- * len indicates that some bytes have been written but the next write would
- * block.
+ * \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 xwrite(int fd, const char *buf, size_t len)
+int xwritev(int fd, struct iovec *iov, int iovcnt)
 {
        size_t written = 0;
-
-       while (written < len) {
-               ssize_t ret = write(fd, buf + written, len - written);
+       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)
@@ -66,6 +90,23 @@ int xwrite(int fd, const char *buf, size_t len)
        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.
  *