From: Andre Noll Date: Tue, 6 Dec 2011 19:46:17 +0000 (+0100) Subject: fd: Make write_all() receive the length by value. X-Git-Tag: v0.4.10~11^2~12 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=ca059ef393a5ea00c3318314b3de5229f9fd7ea0 fd: Make write_all() receive the length by value. Not a single caller actually checked the value of the passed len pointer after the call, which is a sure sign for a bad API. Just return the number of bytes written. --- diff --git a/afh.c b/afh.c index f2745b61..8810a28b 100644 --- a/afh.c +++ b/afh.c @@ -152,7 +152,7 @@ static int cat_file(struct afh_info *afhi, int audio_format_id, if (!size) continue; PARA_INFO_LOG("writing chunk %lu\n", i); - ret = write_all(STDOUT_FILENO, buf, &size); + ret = write_all(STDOUT_FILENO, buf, size); if (ret < 0) return ret; } diff --git a/audioc.c b/audioc.c index 7435a869..84c2a394 100644 --- a/audioc.c +++ b/audioc.c @@ -339,7 +339,7 @@ int main(int argc, char *argv[]) size_t n = ret = recv_bin_buffer(fd, buf, bufsize); if (ret <= 0) break; - ret = write_all(STDOUT_FILENO, buf, &n); + ret = write_all(STDOUT_FILENO, buf, n); } while (ret >= 0); out: if (ret < 0) diff --git a/crypt.c b/crypt.c index 69e4a314..7161e626 100644 --- a/crypt.c +++ b/crypt.c @@ -298,7 +298,7 @@ int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf, memcpy(remainder, buf + l1, len - l1); RC4(&scc->send->key, len - l1, remainder, tmp + l1); } - ret = write_all(scc->fd, (char *)tmp, &len); + ret = write_all(scc->fd, (char *)tmp, len); free(tmp); return ret; } diff --git a/fd.c b/fd.c index 2e05313e..429960cc 100644 --- a/fd.c +++ b/fd.c @@ -25,22 +25,21 @@ * \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; } /** diff --git a/fd.h b/fd.h index c87c0ea6..b12067ec 100644 --- a/fd.h +++ b/fd.h @@ -6,7 +6,7 @@ /** \file fd.h exported symbols from fd.c */ -int write_all(int fd, const char *buf, size_t *len); +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/gcrypt.c b/gcrypt.c index 2f38f1a6..467e7399 100644 --- a/gcrypt.c +++ b/gcrypt.c @@ -951,7 +951,7 @@ int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf, gret = gcry_cipher_encrypt(scc->send->handle, tmp, size, (unsigned char *)buf, size); assert(gret == 0); - ret = write_all(scc->fd, (char *)tmp, &size); + ret = write_all(scc->fd, (char *)tmp, size); free(tmp); return ret; } diff --git a/net.c b/net.c index 3ce98ea4..d32cc7ee 100644 --- a/net.c +++ b/net.c @@ -705,7 +705,7 @@ int send_bin_buffer(int fd, const char *buf, size_t len) { if (!len) PARA_CRIT_LOG("len == 0\n"); - return write_all(fd, buf, &len); + return write_all(fd, buf, len); } /**