Author: marek Date: 2009-11-08 13:54:29 +0000 (Sun, 08 Nov 2009) New Revision: 1476
Modified: trunk/batctl/ping.c Log: [batctl] Prevent division by zero when no packets were send
We will try to print a statistic even when we receive a sigterm or sigint before we send any data or started to count the send data. This means that the we try to calculate the data loss by dividing through zero as it is the number of send packets. This is not possible and we must check for that corner case.
Signed-off-by: Sven Eckelmann sven.eckelmann@gmx.de
Modified: trunk/batctl/ping.c =================================================================== --- trunk/batctl/ping.c 2009-11-08 01:15:11 UTC (rev 1475) +++ trunk/batctl/ping.c 2009-11-08 13:54:29 UTC (rev 1476) @@ -72,7 +72,7 @@ fd_set read_socket; int ret = EXIT_FAILURE, ping_fd = 0, res, optchar, found_args = 1; int loop_count = -1, loop_interval = 1, timeout = 1; - unsigned int seq_counter = 0, packets_out = 0, packets_in = 0; + unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss; char *dst_string, *mac_string; double time_delta; float min = 0.0, max = 0.0, avg = 0.0; @@ -237,9 +237,14 @@
}
+ if (packets_out == 0) + packets_loss = 0; + else + packets_loss = ((packets_out - packets_in) * 100) / packets_out; + printf("--- %s ping statistics ---\n", dst_string); printf("%d packets transmitted, %d received, %d%% packet loss\n", - packets_out, packets_in, (((packets_out - packets_in) * 100) / packets_out)); + packets_out, packets_in, packets_loss); printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n", min, (packets_in ? (avg / packets_in) : 0.000), max, (max - min));