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.
This commit is contained in:
Michael Forney 2017-01-01 17:00:32 -08:00 committed by Anselm R Garbe
parent 529e50a7ad
commit 5cb3a1eba1
3 changed files with 26 additions and 1 deletions

View File

@ -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)

21
libutil/writeall.c Normal file
View File

@ -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;
}

3
util.h
View File

@ -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);