From 1f2d5f95684e93f8523e369ef1fed7a75c444082 Mon Sep 17 00:00:00 2001
From: Laurent Bercot
+ int slurpn (stralloc *sa, int fd, size_t max)
+Same as slurp, but only grows the stralloc to a maximum
+of max bytes of total length. If there is still more to
+read on fd after sa→len has reached max,
+the function returns 0 with errno set to ENOBUFS.
+
int openslurpclose (stralloc *sa, char const *file)
Slurps the contents of file file into *sa.
diff --git a/src/libstddjb/slurpn.c b/src/libstddjb/slurpn.c
index ee5157f..317e300 100644
--- a/src/libstddjb/slurpn.c
+++ b/src/libstddjb/slurpn.c
@@ -11,25 +11,18 @@
int slurpn (int fd, stralloc *sa, size_t max)
{
- size_t sabase = sa->len ;
- int wasnull = !sa->s ;
for (;;)
{
ssize_t r ;
- size_t n = max && sa->len - sabase + N > max ? max - (sa->len - sabase) : N ;
- if (!n) { errno = ENOBUFS ; goto err ; }
+ size_t n = max && sa->len + N > max ? max - sa->len : N ;
+ if (!n) return (errno = ENOBUFS, 0) ;
if (!stralloc_readyplus(sa, n)) break ;
r = fd_read(fd, sa->s + sa->len, n) ;
switch (r)
{
- case -1 : goto err ;
+ case -1 : return 0 ;
case 0 : return 1 ;
default : sa->len += r ;
}
}
-
-err:
- if (wasnull) stralloc_free(sa) ;
- else sa->len = sabase ;
- return 0 ;
}
--
cgit v1.2.3