summaryrefslogtreecommitdiff
path: root/src/libstddjb/siovec_trunc.c
blob: ca409224bde7a4437d75900cc7ac62519922f95c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* ISC license. */

#include <sys/types.h>
#include <sys/uio.h>
#include <skalibs/siovec.h>

unsigned int siovec_trunc (struct iovec *v, unsigned int n, size_t len)
{
  size_t w = siovec_len(v, n) ;
  unsigned int i = n ;
  if (w < len) return n ;
  len = w - len ;
  while (len && i--)
  {
    w = len > v[i].iov_len ? v[i].iov_len : len ;
    v[i].iov_len -= w ; len -= w ;
  }
  return i ;
}