fd_catn.c (606B)
1 /* ISC license. */ 2 3 #include <unistd.h> 4 #include <errno.h> 5 6 #include <skalibs/allreadwrite.h> 7 #include <skalibs/djbunix.h> 8 9 #define BSIZE 65536 10 11 off_t fd_catn (int from, int to, off_t n) 12 { 13 off_t w = 0 ; 14 char buf[BSIZE] ; 15 while (n >= BSIZE) 16 { 17 size_t v ; 18 ssize_t r = fd_read(from, buf, BSIZE) ; 19 if (r == -1) return w ; 20 if (!r) return (errno = EPIPE, w) ; 21 v = allwrite(to, buf, r) ; 22 if (v < r) return w + v ; 23 n -= r ; w += r ; 24 } 25 26 if (n) 27 { 28 size_t r = allread(from, buf, n) ; 29 size_t v = 0 ; 30 if (r) v = allwrite(to, buf, r) ; 31 w += v ; 32 } 33 return w ; 34 }