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

#include <unistd.h>

#include <skalibs/allreadwrite.h>
#include <skalibs/djbunix.h>

#define BSIZE 65536

off_t fd_cat (int from, int to)
{
  off_t n = 0 ;
  char buf[BSIZE] ;
  for (;;)
  {
    ssize_t r = fd_read(from, buf, BSIZE) ;
    if (r == -1) return -1 ;
    if (!r) break ;
    if (allwrite(to, buf, r) < r) return -1 ;
    n += r ;
  }
  return n ;
}