diff options
Diffstat (limited to 'src/libstddjb/slurp.c')
-rw-r--r-- | src/libstddjb/slurp.c | 31 |
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 ; +} |