Move send_buffer() and send_va_buffer() from net.c to fd.c.
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index 6f9266f47a56cd8dee3e99b8651d12a89bb4069b..6f487c410020795a73476fa79ba552c2ad29e4fd 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
  * \param buf The buffer to be sent.
  * \param len The length of \a buf.
  *
- * \return Standard. In any case, the number of bytes that have been written is
- * stored in \a len.
+ * \return Standard.
  */
-int write_all(int fd, const char *buf, size_t *len)
+int write_all(int fd, const char *buf, size_t len)
 {
-       size_t total = *len;
+       size_t total = len;
 
        assert(total);
-       *len = 0;
-       while (*len < total) {
-               int ret = write(fd, buf + *len, total - *len);
+       len = 0;
+       while (len < total) {
+               int ret = write(fd, buf + len, total - len);
                if (ret == -1)
                        return -ERRNO_TO_PARA_ERROR(errno);
-               *len += ret;
+               len += ret;
        }
-       return 1;
+       return len;
+}
+
+/**
+ * Send a buffer given by a format string.
+ *
+ * \param fd The file descriptor.
+ * \param fmt A format string.
+ *
+ * \return Standard.
+ */
+__printf_2_3 int write_va_buffer(int fd, const char *fmt, ...)
+{
+       char *msg;
+       int ret;
+
+       PARA_VSPRINTF(fmt, msg);
+       ret = write_buffer(fd, msg);
+       free(msg);
+       return ret;
 }
 
 /**
@@ -563,6 +581,9 @@ out:
 int para_munmap(void *start, size_t length)
 {
        int err;
+
+       if (!start)
+               return 0;
        if (munmap(start, length) >= 0)
                return 1;
        err = errno;