sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 5cb3a1eba1b7568b3c6c32c1e6808de24b38a757
parent 529e50a7adfba854f4db3caf11758dbc6d89effa
Author: Michael Forney <mforney@mforney.org>
Date:   Sun,  1 Jan 2017 17:00:32 -0800

libutil: Add writeall utility function

writeall makes successive write calls to write an entire buffer to the
output file descriptor. It returns the number of bytes written, or -1 on
the first error.

Diffstat:
MMakefile | 3++-
Alibutil/writeall.c | 21+++++++++++++++++++++
Mutil.h | 3+++
3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -79,7 +79,8 @@ LIBUTILSRC =\ libutil/strlcpy.c\ libutil/strsep.c\ libutil/strtonum.c\ - libutil/unescape.c + libutil/unescape.c\ + libutil/writeall.c LIB = $(LIBUTF) $(LIBUTIL) diff --git a/libutil/writeall.c b/libutil/writeall.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include <unistd.h> + +#include "../util.h" + +ssize_t +writeall(int fd, const void *buf, size_t len) +{ + const char *p = buf; + ssize_t n; + + while (len) { + n = write(fd, p, len); + if (n <= 0) + return n; + p += n; + len -= n; + } + + return p - (const char *)buf; +} diff --git a/util.h b/util.h @@ -62,6 +62,9 @@ char *strsep(char **, const char *); int enregcomp(int, regex_t *, const char *, int); int eregcomp(regex_t *, const char *, int); +/* io */ +ssize_t writeall(int, const void *, size_t); + /* misc */ void enmasse(int, char **, int (*)(const char *, const char *, int)); void fnck(const char *, const char *, int (*)(const char *, const char *, int), int);