about summary refs log tree commit diff
path: root/src/libstddjb/buffer_getvall.c
blob: a58f914a636294d39cd58affe315a13bd81fb5f8 (plain) (blame)
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) ;
  {
    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 ;
}