io: add alternative to sendflie

This commit is contained in:
John Törnblom committed 2026-01-10 22:07:49 +01:00
1 parent 1917ea7d12
commit c82212bcf0
2 files changed
+39

No files matched your search

+33
View File
@@ -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 int
io_pread(int fd, void* buf, size_t n, off_t off) { io_pread(int fd, void* buf, size_t n, off_t off) {
int r; int r;
+6
View File
@@ -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); 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. * Copy exactly N bytes from a file descriptor to a socket.
**/ **/