+static void time_normalize (struct timespec *ts) /* {{{ */
+{
+ while (ts->tv_nsec < 0)
+ {
+ if (ts->tv_sec == 0)
+ {
+ ts->tv_nsec = 0;
+ return;
+ }
+
+ ts->tv_sec -= 1;
+ ts->tv_nsec += 1000000000;
+ }
+
+ while (ts->tv_nsec >= 1000000000)
+ {
+ ts->tv_sec += 1;
+ ts->tv_nsec -= 1000000000;
+ }
+} /* }}} void time_normalize */
+
+static void time_calc (struct timespec *ts_dest, /* {{{ */
+ const struct timespec *ts_int,
+ const struct timeval *tv_begin,
+ const struct timeval *tv_end)
+{
+ ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
+ ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
+ time_normalize (ts_dest);
+
+ /* Assure that `(begin + interval) > end'.
+ * This may seem overly complicated, but `tv_sec' is of type `time_t'
+ * which may be `unsigned. *sigh* */
+ if ((tv_end->tv_sec > ts_dest->tv_sec)
+ || ((tv_end->tv_sec == ts_dest->tv_sec)
+ && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
+ {
+ ts_dest->tv_sec = 0;
+ ts_dest->tv_nsec = 0;
+ return;
+ }
+
+ ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
+ ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
+ time_normalize (ts_dest);
+} /* }}} void time_calc */
+
+#if USE_NCURSES
+static int context_window_repaint (ping_context_t *ctx, /* {{{ */
+ int index)
+{
+ if (ctx == NULL)
+ return (EINVAL);
+
+ if (ctx->window == NULL)
+ {
+ ctx->window = newwin (/* height = */ 4, /* width = */ 0,
+ /* start y = */ 4 * index, /* start x = */ 0);
+ }
+ else /* if (ctx->window != NULL) */
+ {
+ werase (ctx->window);
+ }
+
+ box (ctx->window, 0, 0);
+ mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
+ " %s ping statistics ",
+ ctx->host);
+ mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
+ "%i packets transmitted, %i received, %.2f%% packet "
+ "loss, time %.1fms",
+ ctx->req_sent, ctx->req_rcvd,
+ context_get_packet_loss (ctx),
+ ctx->latency_total);
+ if (ctx->req_rcvd != 0)
+ {
+ double average;
+ double deviation;
+
+ average = context_get_average (ctx);
+ deviation = context_get_stddev (ctx);
+
+ mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
+ "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
+ ctx->latency_min,
+ average,
+ ctx->latency_max,
+ deviation);
+ }
+
+ wrefresh (ctx->window);
+
+ return (0);
+} /* }}} int context_window_repaint */
+
+static int resize_windows (pingobj_t *ping) /* {{{ */
+{
+ int index;
+ pingobj_iter_t *iter;
+ int width = 0;
+ int height = 0;
+ int need_resize = 0;
+
+ while (42)
+ {
+ int key = wgetch (stdscr);
+ if (key == ERR)
+ break;
+ else if (key == KEY_RESIZE)
+ need_resize = 1;
+ }
+
+ if (!need_resize)
+ return (0);
+
+ getmaxyx (stdscr, height, width);
+ if ((height < 1) || (width < 1))
+ return (EINVAL);
+
+ index = 0;
+ for (iter = ping_iterator_get (ping);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ ping_context_t *ctx = ping_iterator_get_context (iter);
+
+ if (ctx->window == NULL)
+ {
+ index++;
+ continue;
+ }
+
+ wresize (ctx->window, 4, width);
+ context_window_repaint (ctx, index);
+
+ index++;
+ }
+
+ if (main_win != NULL)
+ {
+ wresize (main_win, height - (4 * index), width);
+ /* touchwin (main_win); */
+ /* wrefresh (main_win); */
+ clearok (main_win, TRUE);
+ }
+
+ return (0);
+} /* }}} int resize_windows */
+#endif
+
+static void print_host (pingobj_iter_t *iter, /* {{{ */
+ int index)