summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Ledbetter <timledbetter@gmail.com>2023-05-16 06:13:32 +0100
committerAndreas Kling <kling@serenityos.org>2023-05-16 12:56:11 +0200
commit58e75be19944b1ce18faaf03a5532bb2450eb878 (patch)
tree0eff28a65eaa6ce2bd351242c63c59bf6c81b74e
parent755bd4d3c29802e4f8e59a4abe3289b2493ca932 (diff)
downloadserenity-58e75be19944b1ce18faaf03a5532bb2450eb878.zip
ping: Add -i option to specify the time to wait between packets
-rw-r--r--Userland/Utilities/ping.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/Userland/Utilities/ping.cpp b/Userland/Utilities/ping.cpp
index 0969b5b43d..2af9ca952a 100644
--- a/Userland/Utilities/ping.cpp
+++ b/Userland/Utilities/ping.cpp
@@ -33,6 +33,7 @@ static int max_ms;
static DeprecatedString host;
static int payload_size = -1;
static bool quiet = false;
+static u64 interval_in_microseconds = 1'000'000;
// variable part of header can be 0 to 40 bytes
// https://datatracker.ietf.org/doc/html/rfc791#section-3.1
static constexpr int max_optional_header_size_in_bytes = 40;
@@ -67,6 +68,23 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::ArgsParser args_parser;
args_parser.add_positional_argument(host, "Host to ping", "host");
args_parser.add_option(count, "Stop after sending specified number of ECHO_REQUEST packets.", "count", 'c', "count");
+ args_parser.add_option(Core::ArgsParser::Option {
+ .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
+ .help_string = "Wait `interval` seconds between sending each packet. Fractional seconds are allowed.",
+ .short_name = 'i',
+ .value_name = "interval",
+ .accept_value = [](StringView interval_in_seconds_string) {
+ if (interval_in_seconds_string.is_empty())
+ return false;
+
+ auto interval_in_seconds = interval_in_seconds_string.to_double();
+ if (!interval_in_seconds.has_value() || interval_in_seconds.value() <= 0)
+ return false;
+
+ interval_in_microseconds = static_cast<u64>(interval_in_seconds.value() * 1'000'000);
+ return true;
+ },
+ });
args_parser.add_option(payload_size, "Amount of bytes to send as payload in the ECHO_REQUEST packets.", "size", 's', "size");
args_parser.add_option(quiet, "Quiet mode. Only display summary when finished.", "quiet", 'q');
args_parser.parse(arguments);
@@ -216,6 +234,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
break;
}
- sleep(1);
+ usleep(interval_in_microseconds);
}
}