From c82212bcf066080e6033b4e9c7732cf4cc8bf1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20T=C3=B6rnblom?= Date: Sat, 10 Jan 2026 22:07:49 +0100 Subject: [PATCH] io: add alternative to sendflie --- io.c | 33 +++++++++++++++++++++++++++++++++ io.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/io.c b/io.c index b0e1e28..f5289b1 100644 --- a/io.c +++ b/io.c @@ -57,6 +57,39 @@ io_nwrite(int fd, const void* buf, size_t n) { } +int +io_ncopy(int fd_in, int fd_out, size_t size) { + size_t copied = 0; + void* buf; + ssize_t n; + + if(!(buf=malloc(IO_COPY_BUFSIZE))) { + return -1; + } + + while(copied < size) { + n = size - copied; + if(n > IO_COPY_BUFSIZE) { + n = IO_COPY_BUFSIZE; + } + + if(io_nread(fd_in, buf, n)) { + free(buf); + return -1; + } + if(io_nwrite(fd_out, buf, n)) { + free(buf); + return -1; + } + + copied += n; + } + + free(buf); + return 0; +} + + int io_pread(int fd, void* buf, size_t n, off_t off) { int r; diff --git a/io.h b/io.h index b8ea27b..4224be0 100644 --- a/io.h +++ b/io.h @@ -37,6 +37,12 @@ int io_nread(int fd, void* buf, size_t n); int io_nwrite(int fd, const void* buf, size_t n); +/** + * Copy exactly N bytes from one file descriptor to another. + **/ +int io_ncopy(int fd_in, int fd_out, size_t size); + + /** * Copy exactly N bytes from a file descriptor to a socket. **/