Implemented `-h' option.
[liboping.git] / src / oping.c
index cc115f2..37f64b2 100644 (file)
@@ -67,6 +67,7 @@ typedef struct ping_context
        double latency_min;
        double latency_max;
        double latency_total;
+       double latency_total_square;
 } ping_context_t;
 
 static double opt_interval   = 1.0;
@@ -91,6 +92,7 @@ ping_context_t *context_create (void)
        ret->latency_min   = -1.0;
        ret->latency_max   = -1.0;
        ret->latency_total = 0.0;
+       ret->latency_total_square = 0.0;
 
        return (ret);
 }
@@ -113,7 +115,7 @@ int read_options (int argc, char **argv)
 
        while (1)
        {
-               optchar = getopt (argc, argv, "46c:i:");
+               optchar = getopt (argc, argv, "46c:hi:");
 
                if (optchar == -1)
                        break;
@@ -143,6 +145,7 @@ int read_options (int argc, char **argv)
                                }
                                break;
 
+                       case 'h':
                        default:
                                usage_exit (argv[0]);
                }
@@ -173,13 +176,14 @@ void print_host (pingobj_iter_t *iter)
        {
                context->req_rcvd++;
                context->latency_total += latency;
+               context->latency_total_square += (latency * latency);
 
                if ((context->latency_max < 0.0) || (context->latency_max < latency))
                        context->latency_max = latency;
                if ((context->latency_min < 0.0) || (context->latency_min > latency))
                        context->latency_min = latency;
 
-               printf ("echo reply from %s (%s): icmp_seq=%u time=%.1f ms\n",
+               printf ("echo reply from %s (%s): icmp_seq=%u time=%.2f ms\n",
                                context->host, context->addr,
                                (unsigned int) sequence, latency);
        }
@@ -253,17 +257,17 @@ int main (int argc, char **argv)
        int optind;
        int i;
 
+       optind = read_options (argc, argv);
+
+       if (optind >= argc)
+               usage_exit (argv[0]);
+
        if (geteuid () != 0)
        {
                fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
                return (1);
        }
 
-       optind = read_options (argc, argv);
-
-       if (optind >= argc)
-               usage_exit (argv[0]);
-
        if ((ping = ping_construct ()) == NULL)
        {
                fprintf (stderr, "ping_construct failed\n");
@@ -383,15 +387,28 @@ int main (int argc, char **argv)
                context = ping_iterator_get_context (iter);
 
                printf ("\n--- %s ping statistics ---\n"
-                               "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n"
-                               "rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\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->latency_total,
-                               context->latency_min,
-                               context->latency_total / ((double) context->req_rcvd),
-                               context->latency_max,
-                               0.00);
+                               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 (context->latency_total_square - (num_total * average * average));
+
+                       printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
+                                       context->latency_min,
+                                       average,
+                                       context->latency_max,
+                                       deviation);
+               }
 
                ping_iterator_set_context (iter, NULL);
                free (context);