skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit 1f2d5f95684e93f8523e369ef1fed7a75c444082
parent 687b52b09b10a501d21f168b45a4e24fd8bdf65e
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Tue,  8 Aug 2023 09:39:15 +0000

 Fix back slurpn

Signed-off-by: Laurent Bercot <ska@appnovation.com>

Diffstat:
Mdoc/libstddjb/djbunix.html | 8++++++++
Msrc/libstddjb/slurpn.c | 13+++----------
2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/doc/libstddjb/djbunix.html b/doc/libstddjb/djbunix.html @@ -513,6 +513,14 @@ The function returns 1 if it succeeds, or 0 (and sets errno) if it fails. </p> <p> +<code> int slurpn (stralloc *sa, int fd, size_t max) </code> <br /> +Same as <tt>slurp</tt>, but only grows the stralloc to a maximum +of <em>max</em> bytes of total length. If there is still more to +read on <em>fd</em> after <em>sa&rarr;len</em> has reached <em>max</em>, +the function returns 0 with errno set to ENOBUFS. +</p> + +<p> <code> int openslurpclose (stralloc *sa, char const *file) </code> <br /> Slurps the contents of file <em>file</em> into *<em>sa</em>. Returns 1 if it succeeds, and 0 (and sets errno) if it fails. diff --git 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 ; }