summaryrefslogtreecommitdiff
path: root/src/libstddjb/slurp.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/slurp.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/slurp.c')
-rw-r--r--src/libstddjb/slurp.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libstddjb/slurp.c b/src/libstddjb/slurp.c
new file mode 100644
index 0000000..553b29e
--- /dev/null
+++ b/src/libstddjb/slurp.c
@@ -0,0 +1,31 @@
+/* ISC license. */
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+#define N 4096
+
+int slurp (stralloc *sa, int fd)
+{
+ register unsigned int salen = sa->len ;
+ for (;;)
+ {
+ int r ;
+ if (!stralloc_readyplus(sa, N)) break ;
+ r = fd_read(fd, sa->s + sa->len, N) ;
+ switch (r)
+ {
+ case -1: goto err ;
+ case 0:
+ stralloc_shrink(sa) ;
+ return 1 ;
+ default:
+ sa->len += r ;
+ }
+ }
+err:
+ sa->len = salen ;
+ stralloc_shrink(sa) ;
+ return 0 ;
+}