diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 847ced9698..f8c02ba6a8 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -147,7 +147,7 @@ target_link_libraries(ntpquery LibMain) target_link_libraries(open LibDesktop LibMain) target_link_libraries(pape LibGUI LibMain) target_link_libraries(passwd LibCrypt LibMain) -target_link_libraries(paste LibGUI) +target_link_libraries(paste LibGUI LibCore LibMain) target_link_libraries(pgrep LibRegex LibMain) target_link_libraries(pidof LibMain) target_link_libraries(ping LibMain) diff --git a/Userland/Utilities/paste.cpp b/Userland/Utilities/paste.cpp index e21b3858f3..28e03453a2 100644 --- a/Userland/Utilities/paste.cpp +++ b/Userland/Utilities/paste.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2019-2021, Sergey Bugaev + * Copyright (c) 2022, Zachary Penn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,8 +8,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -17,21 +20,14 @@ static void spawn_command(const Vector& command, const ByteBuffer& data, const char* state) { - int pipefd[2]; - if (pipe(pipefd) < 0) { - perror("pipe"); - return; - } + auto pipefd = MUST(Core::System::pipe2(0)); + pid_t pid = MUST(Core::System::fork()); - pid_t pid = fork(); - if (pid < 0) { - perror("fork"); - return; - } else if (pid == 0) { + if (pid == 0) { // We're the child. - dup2(pipefd[0], 0); - close(pipefd[0]); - close(pipefd[1]); + MUST(Core::System::dup2(pipefd[0], 0)); + MUST(Core::System::close(pipefd[0])); + MUST(Core::System::close(pipefd[1])); setenv("CLIPBOARD_STATE", state, true); execvp(command[0], const_cast(command.data())); perror("exec"); @@ -39,18 +35,20 @@ static void spawn_command(const Vector& command, const ByteBuffer& } // We're the parent. - close(pipefd[0]); + MUST(Core::System::close(pipefd[0])); FILE* f = fdopen(pipefd[1], "w"); fwrite(data.data(), data.size(), 1, f); + if (ferror(f)) warnln("failed to write data to the pipe: {}", strerror(ferror(f))); + fclose(f); if (wait(nullptr) < 0) perror("wait"); } -int main(int argc, char* argv[]) +ErrorOr serenity_main(Main::Arguments arguments) { bool print_type = false; bool no_newline = false; @@ -63,9 +61,9 @@ int main(int argc, char* argv[]) args_parser.add_option(no_newline, "Do not append a newline", "no-newline", 'n'); args_parser.add_option(watch, "Run a command when clipboard data changes", "watch", 'w'); args_parser.add_positional_argument(watch_command, "Command to run in watch mode", "command", Core::ArgsParser::Required::No); - args_parser.parse(argc, argv); + args_parser.parse(arguments); - auto app = GUI::Application::construct(argc, argv); + auto app = GUI::Application::construct(arguments); auto& clipboard = GUI::Clipboard::the();