From 122778b9ac19a8e887be2a4f1aaa756bac5d709d Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 24 Mar 2022 20:39:49 +0100 Subject: [PATCH] tee: Port to LibMain and move to SerenityOS code patterns This patch ports the utility 'tee' to LibMain and converts a larger part of its code to our SerenityOS patterns. --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/tee.cpp | 97 +++++++++++++------------------ 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 650f22e047..9890dd8f78 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -193,6 +193,7 @@ target_link_libraries(sysctl LibMain) target_link_libraries(tac LibMain) target_link_libraries(tail LibMain) target_link_libraries(tar LibMain LibArchive LibCompress) +target_link_libraries(tee LibMain) target_link_libraries(telws LibProtocol LibLine) target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell) target_link_libraries(test-imap LibIMAP LibMain) diff --git a/Userland/Utilities/tee.cpp b/Userland/Utilities/tee.cpp index fcf37d7e39..9f73c1db2b 100644 --- a/Userland/Utilities/tee.cpp +++ b/Userland/Utilities/tee.cpp @@ -1,19 +1,20 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, Kenneth Myhra * * SPDX-License-Identifier: BSD-2-Clause */ #include #include -#include +#include +#include #include #include -#include #include #include -static Vector collect_fds(Vector paths, bool append, bool* err) +static ErrorOr> collect_fds(Vector paths, bool append) { int oflag; mode_t mode; @@ -26,53 +27,40 @@ static Vector collect_fds(Vector paths, bool append, bool* err } Vector fds; - for (const char* path : paths) { - int fd = open(path, oflag, mode); - if (fd < 0) { - perror("failed to open file for writing"); - *err = true; - } else { - fds.append(fd); - } + TRY(fds.try_ensure_capacity(paths.size() + 1)); + for (auto path : paths) { + int fd = TRY(Core::System::open(path, oflag, mode)); + fds.unchecked_append(fd); } - fds.append(STDOUT_FILENO); + fds.unchecked_append(STDOUT_FILENO); return fds; } -static void copy_stdin(Vector& fds, bool* err) +static ErrorOr copy_stdin(Vector& fds, bool* err) { for (;;) { - char buf[4096]; - ssize_t nread = read(STDIN_FILENO, buf, sizeof(buf)); + Array buffer; + auto buffer_span = buffer.span(); + auto nread = TRY(Core::System::read(STDIN_FILENO, buffer_span)); + buffer_span = buffer_span.trim(nread); if (nread == 0) break; - if (nread < 0) { - perror("read() error"); - *err = true; - // a failure to read from stdin should lead to an early exit - return; - } Vector broken_fds; for (size_t i = 0; i < fds.size(); ++i) { auto fd = fds.at(i); - int twrite = 0; - while (twrite != nread) { - ssize_t nwrite = write(fd, buf + twrite, nread - twrite); - if (nwrite < 0) { - if (errno == EINTR) { - continue; - } else { - perror("write() failed"); - *err = true; - broken_fds.append(fd); - // write failures to a successfully opened fd shall - // prevent further writes, but shall not block writes - // to the other open fds - break; - } + auto nwrite_or_error = Core::System::write(fd, buffer_span); + if (nwrite_or_error.is_error()) { + if (nwrite_or_error.error().code() == EINTR) { + continue; } else { - twrite += nwrite; + warnln("{}", nwrite_or_error.release_error()); + *err = true; + broken_fds.append(fd); + // write failures to a successfully opened fd shall + // prevent further writes, but shall not block writes + // to the other open fds + break; } } } @@ -81,16 +69,16 @@ static void copy_stdin(Vector& fds, bool* err) for (auto to_remove : broken_fds) fds.remove_first_matching([&](int fd) { return to_remove == fd; }); } + + return {}; } -static void close_fds(Vector& fds) +static ErrorOr close_fds(Vector& fds) { - for (int fd : fds) { - int closed = close(fd); - if (closed < 0) { - perror("failed to close output file"); - } - } + for (int fd : fds) + TRY(Core::System::close(fd)); + + return {}; } static void int_handler(int) @@ -98,28 +86,25 @@ static void int_handler(int) // pass } -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { bool append = false; bool ignore_interrupts = false; - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.add_option(append, "Append, don't overwrite", "append", 'a'); args_parser.add_option(ignore_interrupts, "Ignore SIGINT", "ignore-interrupts", 'i'); args_parser.add_positional_argument(paths, "Files to copy stdin to", "file", Core::ArgsParser::Required::No); - args_parser.parse(argc, argv); + args_parser.parse(arguments); - if (ignore_interrupts) { - if (signal(SIGINT, int_handler) == SIG_ERR) - perror("failed to install SIGINT handler"); - } + if (ignore_interrupts) + TRY(Core::System::signal(SIGINT, int_handler)); - bool err_open = false; + auto fds = TRY(collect_fds(paths, append)); bool err_write = false; - auto fds = collect_fds(paths, append, &err_open); - copy_stdin(fds, &err_write); - close_fds(fds); + TRY(copy_stdin(fds, &err_write)); + TRY(close_fds(fds)); - return (err_open || err_write) ? 1 : 0; + return err_write ? 1 : 0; }