From 8ed7087f300377140dbcd2fc22514e64cfff58fd Mon Sep 17 00:00:00 2001 From: leibniz Date: Tue, 3 Dec 2024 19:04:41 +0200 Subject: [PATCH] chapters/io: Fix `rread()` and `wwrite()` logic As per `read()` and `write()` syscall wrappers documentation, the return value of a successful operation is the count of bytes read/written that could be less than the length given as param. Signed-off-by: Andrei Adrian Ragman --- .../drills/tasks/my-cat/solution/src/my_cat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapters/io/file-descriptors/drills/tasks/my-cat/solution/src/my_cat.c b/chapters/io/file-descriptors/drills/tasks/my-cat/solution/src/my_cat.c index ea789b4a94..bf69469f8d 100644 --- a/chapters/io/file-descriptors/drills/tasks/my-cat/solution/src/my_cat.c +++ b/chapters/io/file-descriptors/drills/tasks/my-cat/solution/src/my_cat.c @@ -25,7 +25,7 @@ size_t rread(int fd, void *buf, size_t size) size_t total_bytes_read = 0; while (true) { - bytes_read = read(fd, buf, size); + bytes_read = read(fd, buf + total_bytes_read, size - total_bytes_read); DIE(bytes_read < 0, "read"); if (bytes_read == 0) @@ -57,7 +57,7 @@ size_t wwrite(int fd, const void *buf, size_t size) size_t total_bytes_written = 0; while (true) { - bytes_written = write(fd, buf, size); + bytes_written = write(fd, buf + total_bytes_written, size - total_bytes_written); DIE(bytes_written < 0, "write"); total_bytes_written += bytes_written;