src/oping.c: Move "context_get_packet_loss" into a separate function.
[liboping.git] / src / oping.c
index bf494ba..6cede31 100644 (file)
@@ -102,13 +102,13 @@ static int     opt_send_ttl   = 64;
 static WINDOW *main_win = NULL;
 #endif
 
-static void sigint_handler (int signal)
+static void sigint_handler (int signal) /* {{{ */
 {
        /* Make compiler happy */
        signal = 0;
        /* Exit the loop */
        opt_count = 0;
-}
+} /* }}} void sigint_handler */
 
 static ping_context_t *context_create (void)
 {
@@ -147,6 +147,50 @@ static void context_destroy (ping_context_t *context)
        free (context);
 }
 
+static double context_get_average (ping_context_t *ctx) /* {{{ */
+{
+       double num_total;
+
+       if (ctx == NULL)
+               return (-1.0);
+
+       if (ctx->req_rcvd < 1)
+               return (-0.0);
+
+       num_total = (double) ctx->req_rcvd;
+       return (ctx->latency_total / num_total);
+} /* }}} double context_get_average */
+
+static double context_get_stddev (ping_context_t *ctx) /* {{{ */
+{
+       double num_total;
+
+       if (ctx == NULL)
+               return (-1.0);
+
+       if (ctx->req_rcvd < 1)
+               return (-0.0);
+       else if (ctx->req_rcvd < 2)
+               return (0.0);
+
+       num_total = (double) ctx->req_rcvd;
+       return (sqrt (((num_total * ctx->latency_total_square)
+                                       - (ctx->latency_total * ctx->latency_total))
+                               / (num_total * (num_total - 1.0))));
+} /* }}} double context_get_stddev */
+
+static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
+{
+       if (ctx == NULL)
+               return (-1.0);
+
+       if (ctx->req_sent < 1)
+               return (0.0);
+
+       return (100.0 * (ctx->req_sent - ctx->req_rcvd)
+                       / ((double) ctx->req_sent));
+} /* }}} double context_get_packet_loss */
+
 static void usage_exit (const char *name, int status)
 {
        int name_length;
@@ -172,7 +216,7 @@ static void usage_exit (const char *name, int status)
        exit (status);
 }
 
-static int read_options (int argc, char **argv)
+static int read_options (int argc, char **argv) /* {{{ */
 {
        int optchar;
 
@@ -254,7 +298,7 @@ static int read_options (int argc, char **argv)
        }
 
        return (optind);
-}
+} /* }}} read_options */
 
 static void print_host (pingobj_iter_t *iter)
 {
@@ -332,16 +376,12 @@ static void print_host (pingobj_iter_t *iter)
                        context->latency_total);
        if (context->req_rcvd != 0)
        {
-               double num_total;
                double average;
                double deviation;
 
-               num_total = (double) context->req_rcvd;
-
-               average = context->latency_total / num_total;
-               deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
-                               / (num_total * (num_total - 1.0)));
-
+               average = context_get_average (context);
+               deviation = context_get_stddev (context);
+                       
                mvwprintw (context->window, /* y = */ 2, /* x = */ 2,
                                "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
                                context->latency_min,
@@ -476,20 +516,16 @@ static int print_footer (pingobj_t *ping)
                printf ("\n--- %s ping statistics ---\n"
                                "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
                                context->host, context->req_sent, context->req_rcvd,
-                               100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
+                               context_get_packet_loss (context),
                                context->latency_total);
 
                if (context->req_rcvd != 0)
                {
-                       double num_total;
                        double average;
                        double deviation;
 
-                       num_total = (double) context->req_rcvd;
-
-                       average = context->latency_total / num_total;
-                       deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
-                                       / (num_total * (num_total - 1.0)));
+                       average = context_get_average (context);
+                       deviation = context_get_stddev (context);
 
                        printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
                                        context->latency_min,