mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
adjtime: Port to LibMain
This commit is contained in:
parent
14a267347c
commit
2cf02c6f38
3 changed files with 13 additions and 22 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue