On Wed, Jan 23, 2019 at 02:34:49PM +0530, Isira Seneviratne wrote:
From 45a793368c781ea5b882938acf030f52b0242c7e Mon Sep 17 00:00:00 2001 From: Isira-Seneviratne isirasen96@gmail.com Date: Sat, 5 Jan 2019 10:46:15 +0530 Subject: [PATCH v2] ping: Add Ctrl+C event handling
Signed-off-by: Isira-Seneviratne isirasen96@gmail.com
programs/ping/ping_main.c | 66 ++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 12 deletions(-)
diff --git a/programs/ping/ping_main.c b/programs/ping/ping_main.c index 9c4f23959e..fddb1738e2 100644 --- a/programs/ping/ping_main.c +++ b/programs/ping/ping_main.c @@ -2,7 +2,7 @@
- ping program
- Copyright (C) 2010 Trey Hunner
- Copyright (C) 2018 Isira Seneviratne
- Copyright (C) 2018-2019 Isira Seneviratne
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
@@ -39,8 +39,42 @@ #include "wine/debug.h" #include "wine/heap.h"
+static unsigned int n, i, w, l; +static int rec, lost, min, max; +static float avg; +static char ip[25];
WINE_DEFAULT_DEBUG_CHANNEL(ping);
+static void display_ping_stats() +{
- if (rec != 0)
- {
avg /= rec;
printf("Approximate round trip times in milli-seconds:\n");
printf("\tMinimum = %dms, Maximum = %dms, Average = %.0fms\n",
min, max, avg);
- }
+}
+/* Displays ping statistics when the interrupt signal is sent to the program */ +BOOL WINAPI ping_interrupted(DWORD dwCtrlType) +{
- switch (dwCtrlType)
- {
case CTRL_C_EVENT:
printf("\nPing statistics for %s\n", ip);
printf("\tPackets: Sent = %d, Received = %d, Lost = %d (%.0f%% loss)\n",
i, rec, lost, (float) lost / i * 100);
display_ping_stats();
exit(0);
default:
return FALSE;
- }
+}
That moved two out of the four printf()s, so we're halfway there... I realise there's a difference wrt 'i' vs 'n', but using 'i' in both cases should work fine.
Btw, you don't need to move 'n', 'w' or 'l' to globals; they can stay local to main(). Also, let's rename the global 'i' to something like 'sent'.
Huw.