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.
This commit is contained in:
Florian Fischer
2024-02-13 12:31:09 +01:00
parent 1832c77fdb
commit e2e608b073
+3 -2
View File
@@ -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);