getlnmax.c (713B)
1 /* ISC license. */ 2 3 #include <sys/uio.h> 4 #include <errno.h> 5 6 #include <skalibs/buffer.h> 7 #include <skalibs/siovec.h> 8 #include <skalibs/skamisc.h> 9 10 int getlnmax (buffer *b, char *d, size_t max, size_t *w, char sep) 11 { 12 if (*w > max) return (errno = EINVAL, -1) ; 13 for (;;) 14 { 15 size_t len = buffer_len(b) ; 16 struct iovec v[2] ; 17 size_t pos ; 18 ssize_t r ; 19 buffer_rpeek(b, v) ; 20 if (len > max - *w) len = max - *w ; 21 pos = siovec_bytechr(v, 2, sep) ; 22 if (pos > len) pos = len ; 23 r = pos < len ; pos += r ; 24 buffer_getnofill(b, d + *w, pos) ; *w += pos ; 25 if (r) return 1 ; 26 if (*w >= max) return (errno = ERANGE, -1) ; 27 r = buffer_fill(b) ; 28 if (r <= 0) return r ; 29 } 30 }