From 7ce8e2d57bfa6c900a62a871288a89cc88d0bf6a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 1 Oct 2023 23:43:07 +0300 Subject: [PATCH] cat: Use write() instead of puts to print file contents This allows printing files that contain null bytes behave more like you would expect --- userspace/cat/main.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/userspace/cat/main.cpp b/userspace/cat/main.cpp index ec8fbf8f8c..28b43095c4 100644 --- a/userspace/cat/main.cpp +++ b/userspace/cat/main.cpp @@ -1,18 +1,18 @@ +#include #include -bool cat_file(FILE* fp) +bool cat_file(int fd) { - char buffer[1025]; + char buffer[1024]; size_t n_read; - while ((n_read = fread(buffer, 1, sizeof(buffer) - 1, fp)) > 0) + while (ssize_t n_read = read(fd, buffer, sizeof(buffer))) { - buffer[n_read] = '\0'; - fputs(buffer, stdout); - } - if (ferror(fp)) - { - perror("fread"); - return false; + if (n_read == -1) + { + perror("read"); + return false; + } + write(STDOUT_FILENO, buffer, n_read); } return true; } @@ -25,21 +25,21 @@ int main(int argc, char** argv) { for (int i = 1; i < argc; i++) { - FILE* fp = fopen(argv[i], "r"); - if (fp == nullptr) + int fd = open(argv[i], O_RDONLY); + if (fd == -1) { perror(argv[i]); ret = 1; continue; } - if (!cat_file(fp)) + if (!cat_file(fd)) ret = 1; - fclose(fp); + close(fd); } } else { - if (!cat_file(stdin)) + if (!cat_file(STDIN_FILENO)) ret = 1; }