1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

watch: Replace LibC function calls with their LibCore equivalents

This commit is contained in:
Tim Ledbetter 2023-07-12 23:23:34 +01:00 committed by Andreas Kling
parent 958c79da64
commit 2f3c41e033

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2020, Sahan Fernando <sahan.h.fernando@gmail.com> * Copyright (c) 2020, Sahan Fernando <sahan.h.fernando@gmail.com>
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -15,9 +16,7 @@
#include <LibFileSystem/FileSystem.h> #include <LibFileSystem/FileSystem.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <errno.h> #include <errno.h>
#include <spawn.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -70,30 +69,35 @@ static int run_command(Vector<DeprecatedString> const& command)
for (auto& arg : command) for (auto& arg : command)
argv.unchecked_append(arg.characters()); argv.unchecked_append(arg.characters());
argv.unchecked_append(nullptr); argv.unchecked_append(nullptr);
auto child_pid_or_error = Core::System::posix_spawnp(command[0], nullptr, nullptr, const_cast<char**>(argv.data()), environ);
if ((errno = posix_spawnp(const_cast<pid_t*>(&child_pid), argv[0], nullptr, nullptr, const_cast<char**>(argv.data()), environ))) { if (child_pid_or_error.is_error()) {
exit_code = 1; exit_code = 1;
perror("posix_spawn"); warnln("posix_spawn: {}", strerror(child_pid_or_error.error().code()));
return errno; return child_pid_or_error.error().code();
} }
child_pid = child_pid_or_error.release_value();
// Wait for the child to terminate, then return its exit code. // Wait for the child to terminate, then return its exit code.
int status; Core::System::WaitPidResult waitpid_result;
pid_t exited_pid; int error_code = 0;
do { do {
exited_pid = waitpid(child_pid, &status, 0); auto result_or_error = Core::System::waitpid(child_pid, 0);
} while (exited_pid < 0 && errno == EINTR); if (result_or_error.is_error())
VERIFY(exited_pid == child_pid); error_code = result_or_error.error().code();
else
waitpid_result = result_or_error.release_value();
} while (waitpid_result.pid < 0 && error_code == EINTR);
VERIFY(waitpid_result.pid == child_pid);
child_pid = -1; child_pid = -1;
if (exited_pid < 0) { if (error_code > 0) {
perror("waitpid"); warnln("waitpid: {}", strerror(error_code));
return 1; return 1;
} }
if (WIFEXITED(status)) { if (WIFEXITED(waitpid_result.status)) {
return WEXITSTATUS(status); return WEXITSTATUS(waitpid_result.status);
} else {
return 1;
} }
return 1;
} }
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)