Skip to content

Commit

Permalink
ping: fancier sub-ms printing (I think %Ng is supposed to do this, bu…
Browse files Browse the repository at this point in the history
…t mine might be broken?)
  • Loading branch information
klange committed Sep 18, 2021
1 parent 7d5bfe4 commit 33848f8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apps/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand Down

0 comments on commit 33848f8

Please sign in to comment.