summaryrefslogtreecommitdiff
path: root/src/libstddjb/buffer_getvall.c
blob: 3a90871b9ba9739e8fc2913bde29ae9ecca15188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* ISC license. */

#include <sys/uio.h>
#include <errno.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/buffer.h>
#include <skalibs/siovec.h>

int buffer_getvall (buffer *b, struct iovec const *v, unsigned int n, size_t *written)
{
  size_t len = siovec_len(v, n) ;
  struct iovec vv[n ? n : 1] ;
  if (*written > len) return (errno = EINVAL, -1) ;
  {
    register unsigned int i = n ;
    while (i--) vv[i] = v[i] ;
  }
  siovec_seek(vv, n, *written) ;
  for (;;)
  {
    ssize_t r ;
    size_t w = buffer_getvnofill(b, vv, n) ;
    *written += w ;
    if (*written >= len) break ;
    siovec_seek(vv, n, w) ;
    r = sanitize_read(buffer_fill(b)) ;
    if (r <= 0) return r ;
  }
  return 1 ;
}