adjtime: Port to LibMain

This commit is contained in:
mjz19910 2022-01-16 23:22:35 -07:00 committed by Andreas Kling
parent 14a267347c
commit 2cf02c6f38
Notes: sideshowbarker 2024-07-17 20:33:23 +09:00
3 changed files with 13 additions and 22 deletions

View File

@ -474,7 +474,7 @@ if (BUILD_LAGOM)
# Lagom Utilities
add_executable(adjtime_lagom ../../Userland/Utilities/adjtime.cpp)
set_target_properties(adjtime_lagom PROPERTIES OUTPUT_NAME adjtime)
target_link_libraries(adjtime_lagom LagomCore)
target_link_libraries(adjtime_lagom LagomCore LagomMain)
add_executable(disasm_lagom ../../Userland/Utilities/disasm.cpp)
set_target_properties(disasm_lagom PROPERTIES OUTPUT_NAME disasm)

View File

@ -52,6 +52,7 @@ foreach(CMD_SRC ${CMD_SOURCES})
endforeach()
target_link_libraries(abench LibAudio LibMain LibCore)
target_link_libraries(adjtime LibMain)
target_link_libraries(allocate LibMain)
target_link_libraries(aplay LibAudio LibMain)
target_link_libraries(arp LibMain)

View File

@ -1,30 +1,29 @@
/*
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
* Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
#ifdef __serenity__
if (pledge("stdio settime", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio settime"));
#endif
Core::ArgsParser args_parser;
double delta = __builtin_nan("");
Optional<double> delta;
args_parser.add_option(delta, "Adjust system time by this many seconds", "set", 's', "delta_seconds");
args_parser.parse(argc, argv);
args_parser.parse(arguments);
if (!__builtin_isnan(delta)) {
long delta_us = static_cast<long>(round(delta * 1'000'000));
if (delta.has_value()) {
long delta_us = static_cast<long>(round(*delta * 1'000'000));
timeval delta_timeval;
delta_timeval.tv_sec = delta_us / 1'000'000;
delta_timeval.tv_usec = delta_us % 1'000'000;
@ -32,24 +31,15 @@ int main(int argc, char** argv)
delta_timeval.tv_sec--;
delta_timeval.tv_usec += 1'000'000;
}
if (adjtime(&delta_timeval, nullptr) < 0) {
perror("adjtime set");
return 1;
}
TRY(Core::System::adjtime(&delta_timeval, nullptr));
}
#ifdef __serenity__
if (pledge("stdio", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio"));
#endif
timeval remaining_delta_timeval;
if (adjtime(nullptr, &remaining_delta_timeval) < 0) {
perror("adjtime get");
return 1;
}
TRY(Core::System::adjtime(nullptr, &remaining_delta_timeval));
double remaining_delta = remaining_delta_timeval.tv_sec + remaining_delta_timeval.tv_usec / 1'000'000.0;
outln("{}", remaining_delta);