From d2a600590a2607d2bd729f3079aaec320efe7990 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 15 Mar 2008 19:09:53 +0100 Subject: [PATCH] Move write_all() from net.c to fd.c. This way it can be used also for programs that don't need networking. --- fd.c | 25 +++++++++++++++++++++++++ fd.h | 1 + net.c | 27 ++------------------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/fd.c b/fd.c index abad80a8..48e2faf9 100644 --- a/fd.c +++ b/fd.c @@ -15,6 +15,31 @@ #include "para.h" #include "error.h" +/* + * Write a buffer to a file descriptor, re-write on short writes. + * + * \param fd The file descriptor. + * \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. + */ +int write_all(int fd, const char *buf, size_t *len) +{ + size_t total = *len; + + assert(total); + *len = 0; + while (*len < total) { + int ret = write(fd, buf + *len, total - *len); + if (ret == -1) + return -ERRNO_TO_PARA_ERROR(errno); + *len += ret; + } + return 1; +} + /** * Check whether a file exists. * diff --git a/fd.h b/fd.h index 6472c137..22141be2 100644 --- a/fd.h +++ b/fd.h @@ -6,6 +6,7 @@ /** \file fd.h exported symbols from fd.c */ +int write_all(int fd, const char *buf, size_t *len); int file_exists(const char *); int para_select(int n, fd_set *readfds, fd_set *writefds, struct timeval *timeout_tv); diff --git a/net.c b/net.c index 427ab0f0..b2c9c43a 100644 --- a/net.c +++ b/net.c @@ -19,11 +19,13 @@ #define AI_ADDRCONFIG 0 #endif +#include #include "para.h" #include "error.h" #include "net.h" #include "string.h" +#include "fd.h" /** Information about one encrypted connection. */ @@ -345,31 +347,6 @@ struct in_addr extract_v4_addr(const struct sockaddr_storage *ss) return ia; } -/* - * Write a buffer to a file descriptor, re-write on short writes. - * - * \param fd The file descriptor. - * \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. - */ -static int write_all(int fd, const char *buf, size_t *len) -{ - size_t total = *len; - - assert(total); - *len = 0; - while (*len < total) { - int ret = write(fd, buf + *len, total - *len); - if (ret == -1) - return -ERRNO_TO_PARA_ERROR(errno); - *len += ret; - } - return 1; -} - /** * Encrypt and send a binary buffer. * -- 2.30.2