summaryrefslogtreecommitdiff
path: root/src/libstddjb/iobufferk_flush.c
blob: 3f8d657ba8ea767321ad6a6381de30b7313a71cc (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASSPLICE

#include <skalibs/nonposix.h>
#include <fcntl.h>
#include <errno.h>
#include <skalibs/iobuffer.h>

static int iobufferk_flush_0 (iobufferk *k)
{
  while (k->n)
  {
    ssize_t r = splice(k->fd[0], 0, k->p[1], 0, k->n, 0) ;
    if (r < 0) return 0 ;
    else if (!r) break ;
    k->n -= r ;
  }
  return 1 ;
}

static int fakeflush (iobufferk *k)
{
  k->n = 0 ;
  return 1 ;
}

static int iobufferk_flush_3 (iobufferk *k)
{
  while (k->n)
  {
    ssize_t r = splice(k->p[0], 0, k->fd[1], 0, k->n, SPLICE_F_MORE | (k->nb & 2 ? SPLICE_F_NONBLOCK : 0)) ;
    if (r < 0)
    {
      if (errno == EINVAL) errno = ENOSYS ;
      return 0 ;
    }
    else if (!r) return (errno = EPIPE, 0) ;
    k->n -= r ;
  }
  return 1 ;
}

iobufferk_output_func_ref const iobufferk_flush_f[4] =
{
  &iobufferk_flush_0, &fakeflush, &fakeflush, &iobufferk_flush_3
} ;

#else

#include <errno.h>
#include <skalibs/iobuffer.h>

static int iobufferk_onosys (iobufferk *k)
{
  (void)k ;
  return (errno = ENOSYS, -1) ;
}

iobufferk_output_func_ref const iobufferk_flush_f[4] =
{
  &iobufferk_onosys, &iobufferk_onosys, &iobufferk_onosys, &iobufferk_onosys
} ;

#endif