paste: Port to LibMain and LibCore

This commit is contained in:
Zack Penn 2022-02-18 23:26:39 -06:00 committed by Andreas Kling
parent 7880718fa8
commit 681b643093
2 changed files with 16 additions and 18 deletions

View file

@ -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)

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2019-2021, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2022, Zachary Penn <zack@sysdevs.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,8 +8,10 @@
#include <AK/Format.h>
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibMain/Main.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -17,21 +20,14 @@
static void spawn_command(const Vector<const char*>& 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<char**>(command.data()));
perror("exec");
@ -39,18 +35,20 @@ static void spawn_command(const Vector<const char*>& 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<int> 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();