[hackers] [sbase] libutil: Add writeall utility function || Michael Forney

From: <git_AT_suckless.org>
Date: Mon, 3 Jul 2017 21:06:20 +0200 (CEST)

commit 5cb3a1eba1b7568b3c6c32c1e6808de24b38a757
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Sun Jan 1 17:00:32 2017 -0800
Commit: Anselm R Garbe <anselm_AT_garbe.us>
CommitDate: Mon Jul 3 21:04:12 2017 +0200

    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.

diff --git a/Makefile b/Makefile
index 9ec9990..1c39fef 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -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
new file mode 100644
index 0000000..4725ced
--- /dev/null
+++ b/libutil/writeall.c
_AT_@ -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
index 29325d2..b39d918 100644
--- a/util.h
+++ b/util.h
_AT_@ -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);
Received on Mon Jul 03 2017 - 21:06:20 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 03 2017 - 21:13:14 CEST