From 33848f8a8ebef8e030b20fb35a6761005781b92d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 18 Sep 2021 10:10:53 +0900 Subject: [PATCH] ping: fancier sub-ms printing (I think %Ng is supposed to do this, but mine might be broken?) --- apps/ping.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/ping.c b/apps/ping.c index 0cba61d4..6454f053 100644 --- a/apps/ping.c +++ b/apps/ping.c @@ -141,10 +141,18 @@ int main(int argc, char * argv[]) { size_t len = ntohs(ipv4->length) - sizeof(struct IPV4_Header); /* Get the address */ char * from = inet_ntoa(*(struct in_addr*)&ipv4->source); - printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%zd ms\n", + int time_taken = (rcvd_at - sent_at); + printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%d", len, from, ntohs(icmp->sequence_number), ipv4->ttl, - (rcvd_at - sent_at) / 1000); - + time_taken / 1000); + if (time_taken < 1000) { + printf(".%03d", time_taken % 1000); + } else if (time_taken < 10000) { + printf(".%02d", (time_taken / 10) % 100); + } else if (time_taken < 100000) { + printf(".%01d", (time_taken / 100) % 10); + } + printf(" ms\n"); responses_received++; }