skalibs

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

getlnmaxsep.c (744B)


      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/skamisc.h>
      8 
      9 int getlnmaxsep (buffer *b, char *d, size_t max, size_t *w, char const *sep, size_t seplen)
     10 {
     11   if (max < *w) return (errno = EINVAL, -1) ;
     12   for (;;)
     13   {
     14     struct iovec v[2] ;
     15     size_t len = buffer_len(b) ;
     16     size_t pos ;
     17     ssize_t r ;
     18     buffer_rpeek(b, v) ;
     19     if (len > max - *w) len = max - *w ;
     20     pos = siovec_bytein(v, 2, sep, seplen) ;
     21     if (pos > len) pos = len ;
     22     r = pos < len ; pos += r ;
     23     buffer_getnofill(b, d + *w, pos) ; *w += pos ;
     24     if (*w >= max) return (errno = ERANGE, -1) ;
     25     if (r) return 1 ;
     26     r = buffer_fill(b) ;
     27     if (r <= 0) return r ;
     28   }
     29 }