diff options
author | pommicket <pommicket@gmail.com> | 2025-09-16 20:49:49 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-16 20:52:22 -0400 |
commit | ea7b73aac55177d1d556d0c9dba04b0870d3aaf6 (patch) | |
tree | 98862ab518680573c4d1d77542bc88b5ec9ceb2a /examples | |
parent | 62bb1ffdee060819657161e260e75e3e1df017ac (diff) |
Allow short reads from read_func
Diffstat (limited to 'examples')
-rw-r--r-- | examples/all_functions.c | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/examples/all_functions.c b/examples/all_functions.c index 413d114..ee960b3 100644 --- a/examples/all_functions.c +++ b/examples/all_functions.c @@ -10,21 +10,16 @@ static size_t custom_read(void *udata, char *buf, size_t size) { int fd = (int)(intptr_t)udata; - 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(fd, buf, size); - if (ret <= 0) { - // read error/end-of-file - break; - } else { - total_read += ret; - buf += ret; - size -= ret; - } + // only read up to 4 bytes at a time. why not! + // it's much slower, but it is allowed. + ssize_t ret = read(fd, buf, size < 4 ? size : 4); + if (ret < 0) { + // read error occured. + // we could store an error away somewhere if we wanted to + // (read errors are unusual anyways.) + ret = 0; } - return total_read; + return ret; } int main(void) { |