src/oping.c: Exit early if no host could be resolved.
[liboping.git] / src / oping.c
index 6496331..ca92778 100644 (file)
@@ -95,6 +95,9 @@
 # define OPING_YELLOW_HIST 5
 # define OPING_RED_HIST 6
 
+double const threshold_green = 0.8;
+double const threshold_yellow = 0.95;
+
 static char const * const hist_symbols_utf8[] = {
        "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
 static size_t const hist_symbols_utf8_num = sizeof (hist_symbols_utf8)
@@ -278,6 +281,7 @@ static double context_get_percentile (ping_context_t *ctx, /* {{{ */
        double threshold = percentile / 100.0;
        double index_to_ms_factor;
        size_t i;
+       double ret;
 
        if (ctx->histogram_ratio == NULL)
                return (NAN);
@@ -295,7 +299,13 @@ static double context_get_percentile (ping_context_t *ctx, /* {{{ */
 
        /* Multiply with i+1, because we're interested in the _upper_ bound of
         * each bucket. */
-       return (index_to_ms_factor * ((double) (i + 1)));
+       ret = (index_to_ms_factor * ((double) (i + 1)));
+
+       /* Avoid reporting a higher latency than latency_max. */
+       if (ret > ctx->latency_max)
+               ret = ctx->latency_max;
+
+       return (ret);
 } /* }}} double context_get_percentile */
 
 static double context_get_stddev (ping_context_t *ctx) /* {{{ */
@@ -1020,9 +1030,13 @@ static int update_graph_histogram (ping_context_t *ctx) /* {{{ */
 
                if (has_colors () == TRUE)
                {
-                       if ((ratio_this <= 0.5) || ((ratio_prev < 0.5) && (ratio_this > 0.5)))
+                       if ((ratio_this <= threshold_green)
+                                       || ((ratio_prev < threshold_green)
+                                               && (ratio_this > threshold_green)))
                                color = OPING_GREEN;
-                       else if ((ratio_this <= 0.95) || ((ratio_prev < 0.95) && (ratio_this > 0.95)))
+                       else if ((ratio_this <= threshold_yellow)
+                                       || ((ratio_prev < threshold_yellow)
+                                               && (ratio_this > threshold_yellow)))
                                color = OPING_YELLOW;
                        else
                                color = OPING_RED;
@@ -1406,11 +1420,13 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
                        else
                                ratio_prev = 0.0;
 
-                       if ((ratio_this <= 0.5) ||
-                                       ((ratio_prev < 0.5) && (ratio_this > 0.5)))
+                       if ((ratio_this <= threshold_green)
+                                       || ((ratio_prev < threshold_green)
+                                               && (ratio_this > threshold_green)))
                                color = OPING_GREEN;
-                       else if ((ratio_this <= 0.95) ||
-                                       ((ratio_prev < 0.95) && (ratio_this > 0.95)))
+                       else if ((ratio_this <= threshold_yellow)
+                                       || ((ratio_prev < threshold_yellow)
+                                               && (ratio_this > threshold_yellow)))
                                color = OPING_YELLOW;
                        else
                                color = OPING_RED;
@@ -1743,6 +1759,9 @@ int main (int argc, char **argv) /* {{{ */
                exit (EXIT_FAILURE);
        }
 
+       if (host_num == 0)
+               exit (EXIT_FAILURE);
+
 #if _POSIX_SAVED_IDS
        saved_set_uid = (uid_t) -1;
 #endif