From e2e608b073c54d84733508e2caa29071d8187d11 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 13 Feb 2024 12:31:09 +0100 Subject: [PATCH] cat: properly output the read data Using puts to output data retrieved through write is broken. Puts expects a null-terminated string, which is not returned by read. Use write to exactly output the data previously read. --- programs/cat.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/programs/cat.c b/programs/cat.c index a844e25..3b3eaee 100644 --- a/programs/cat.c +++ b/programs/cat.c @@ -18,9 +18,10 @@ static inline void print_content(const char *path, char *buffer, unsigned buflen // Open the file. int fd = open(path, O_RDONLY, 42); if (fd >= 0) { + ssize_t bytes_read = 0; // Put on the standard output the characters. - while (read(fd, buffer, buflen) > 0) { - puts(buffer); + while ((bytes_read = read(fd, buffer, buflen)) > 0) { + write(STDOUT_FILENO, buffer, bytes_read); } // Close the file descriptor. close(fd);