From ea7b73aac55177d1d556d0c9dba04b0870d3aaf6 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 16 Sep 2025 20:49:49 -0400 Subject: Allow short reads from read_func --- cpp/examples/all_functions.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'cpp/examples') diff --git a/cpp/examples/all_functions.cpp b/cpp/examples/all_functions.cpp index 326ae39..5081e83 100644 --- a/cpp/examples/all_functions.cpp +++ b/cpp/examples/all_functions.cpp @@ -15,24 +15,13 @@ public: } ~FdReader() { close(m_fd); } size_t read(char *buf, size_t size) { - size_t total_read = 0; - while (true) { - // must call read in a loop to fill buf up as much as possible! - // (read isn't guaranteed to read len bytes even if it could) - ssize_t ret = ::read(m_fd, buf, size); - if (ret < 0) { - // read error - throw "read error"; - } else if (ret == 0) { - // end-of-file - break; - } else { - total_read += ret; - buf += ret; - size -= ret; - } + ssize_t ret = ::read(m_fd, buf, size); + if (ret < 0) { + // read error + throw "read error"; + } else { + return ret; } - return total_read; } private: int m_fd; -- cgit v1.2.3