skalibs

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

skagetlnmaxsep.c (807B)


      1 /* ISC license. */
      2 
      3 #include <sys/uio.h>
      4 #include <errno.h>
      5 #include <skalibs/buffer.h>
      6 #include <skalibs/siovec.h>
      7 #include <skalibs/stralloc.h>
      8 #include <skalibs/skamisc.h>
      9 
     10 int skagetlnmaxsep (buffer *b, stralloc *sa, size_t max, char const *sep, size_t seplen)
     11 {
     12   size_t start = sa->len ;
     13   for (;;)
     14   {
     15     struct iovec v[2] ;
     16     size_t pos ;
     17     int r ;
     18     buffer_rpeek(b, v) ;
     19     pos = siovec_bytein(v, 2, sep, seplen) ;
     20     r = pos < buffer_len(b) ; pos += r ;
     21     if (!stralloc_readyplus(sa, pos)) return -1 ;
     22     buffer_getnofill(b, sa->s + sa->len, pos) ; sa->len += pos ;
     23     if (r) return 1 ;
     24     if (sa->len - start >= max) return (errno = EMSGSIZE, -1) ;
     25     r = buffer_fill(b) ;
     26     if (r < 0) return r ;
     27     if (!r) return (sa->s && (sa->len > start)) ? (errno = EPIPE, -1) : 0 ;
     28   }
     29 }