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:
+3
-2
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user