[PING]: Convert ping to use ConStreams. Also fix file redirection.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 29 Jan 2017 20:29:28 +0000 (20:29 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 29 Jan 2017 20:29:28 +0000 (20:29 +0000)
CORE-12122 #resolve

svn path=/trunk/; revision=73626

reactos/base/applications/network/ping/CMakeLists.txt
reactos/base/applications/network/ping/ping.c

index 8d2d5c6..e7f0b7b 100644 (file)
@@ -1,5 +1,8 @@
 
+include_directories(${REACTOS_SOURCE_DIR}/sdk/lib/conutils)
+
 add_executable(ping ping.c ping.rc)
 set_module_type(ping win32cui UNICODE)
-add_importlibs(ping ws2_32 iphlpapi user32 msvcrt kernel32 ntdll)
+target_link_libraries(ping conutils ${PSEH_LIB})
+add_importlibs(ping ws2_32 iphlpapi msvcrt kernel32 ntdll)
 add_cd_file(TARGET ping DESTINATION reactos/system32 FOR all)
index 6c31607..bbeafa6 100644 (file)
@@ -19,7 +19,6 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-
 /*
  * PROJECT:     ReactOS Ping Command
  * LICENSE:     MIT
  * PROGRAMMERS: Tim Crawford <crawfxrd@gmail.com>
  */
 
+#include <stdlib.h>
+
 #define WIN32_LEAN_AND_MEAN
 
-#include <stdio.h>
-#include <stdlib.h>
+#define WIN32_NO_STATUS
+#include <windef.h>
+#include <winbase.h>
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <iphlpapi.h>
 #include <icmpapi.h>
 
+#include <conutils.h>
+
 #include "resource.h"
 
 #define NDEBUG
@@ -52,9 +56,7 @@ static BOOL ResolveTarget(PCWSTR target);
 static void Ping(void);
 static void PrintStats(void);
 static BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType);
-static void PrintString(UINT id, ...);
 
-static HANDLE hStdOut;
 static HANDLE hIcmpFile = INVALID_HANDLE_VALUE;
 static ULONG Timeout = 4000;
 static int Family = AF_UNSPEC;
@@ -84,34 +86,30 @@ wmain(int argc, WCHAR *argv[])
     DWORD StrLen = 46;
     int Status;
 
-    IpOptions.Ttl = 128;
+    /* Initialize the Console Standard Streams */
+    ConInitStdStreams();
 
-    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+    IpOptions.Ttl = 128;
 
     if (!ParseCmdLine(argc, argv))
-    {
         return 1;
-    }
 
     if (!SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE))
     {
         DPRINT("Failed to set control handler: %lu\n", GetLastError());
-
         return 1;
     }
 
     Status = WSAStartup(MAKEWORD(2, 2), &wsaData);
     if (Status != 0)
     {
-        PrintString(IDS_WINSOCK_FAIL, Status);
-
+        ConResPrintf(StdErr, IDS_WINSOCK_FAIL, Status);
         return 1;
     }
 
     if (!ResolveTarget(TargetName))
     {
         WSACleanup();
-
         return 1;
     }
 
@@ -120,18 +118,13 @@ wmain(int argc, WCHAR *argv[])
         DPRINT("WSAAddressToStringW failed: %d\n", WSAGetLastError());
         FreeAddrInfoW(Target);
         WSACleanup();
-
         return 1;
     }
 
     if (Family == AF_INET6)
-    {
         hIcmpFile = Icmp6CreateFile();
-    }
     else
-    {
         hIcmpFile = IcmpCreateFile();
-    }
 
 
     if (hIcmpFile == INVALID_HANDLE_VALUE)
@@ -139,24 +132,19 @@ wmain(int argc, WCHAR *argv[])
         DPRINT("IcmpCreateFile failed: %lu\n", GetLastError());
         FreeAddrInfoW(Target);
         WSACleanup();
-
         return 1;
     }
 
     if (*CanonName)
-    {
-        PrintString(IDS_PINGING_HOSTNAME, CanonName, Address);
-    }
+        ConResPrintf(StdOut, IDS_PINGING_HOSTNAME, CanonName, Address);
     else
-    {
-        PrintString(IDS_PINGING_ADDRESS, Address);
-    }
+        ConResPrintf(StdOut, IDS_PINGING_ADDRESS, Address);
 
-    PrintString(IDS_PING_SIZE, RequestSize);
+    ConResPrintf(StdOut, IDS_PING_SIZE, RequestSize);
 
     Ping();
-    i = 1;
 
+    i = 1;
     while (i < PingCount)
     {
         Sleep(1000);
@@ -175,41 +163,6 @@ wmain(int argc, WCHAR *argv[])
     return 0;
 }
 
-static
-void
-PrintString(UINT id, ...)
-{
-#define RC_STRING_MAX_SIZE  4096
-
-    WCHAR Format[RC_STRING_MAX_SIZE];
-    LPWSTR lpMsgBuf = NULL;
-    DWORD Len;
-    va_list args;
-
-    if (!LoadStringW(GetModuleHandleW(NULL), id, Format, _countof(Format)))
-    {
-        DPRINT("LoadStringW failed: %lu\n", GetLastError());
-        return;
-    }
-
-    va_start(args, id);
-
-    Len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
-                         Format, 0, 0, (PWSTR)&lpMsgBuf, 0, &args);
-    if (lpMsgBuf /* && Len != 0 */)
-    {
-        // TODO: Handle writing to file. Well, use the ConUtils lib!
-        WriteConsoleW(hStdOut, lpMsgBuf, Len, &Len, NULL);
-        LocalFree(lpMsgBuf);
-    }
-    else
-    {
-        DPRINT("FormatMessageW failed: %lu\n", GetLastError());
-    }
-
-    va_end(args);
-}
-
 static
 BOOL
 ParseCmdLine(int argc, PWSTR argv[])
@@ -218,7 +171,7 @@ ParseCmdLine(int argc, PWSTR argv[])
 
     if (argc < 2)
     {
-        PrintString(IDS_USAGE);
+        ConResPrintf(StdOut, IDS_USAGE);
         return FALSE;
     }
 
@@ -237,69 +190,66 @@ ParseCmdLine(int argc, PWSTR argv[])
                 break;
 
             case L'n':
+            {
                 if (i + 1 < argc)
                 {
                     PingForever = FALSE;
                     PingCount = wcstoul(argv[++i], NULL, 0);
-
                     if (PingCount == 0)
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 1, UINT_MAX);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 1, UINT_MAX);
                         return FALSE;
                     }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'l':
+            {
                 if (i + 1 < argc)
                 {
                     RequestSize = wcstoul(argv[++i], NULL, 0);
-
                     if (RequestSize > MAX_SEND_SIZE)
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 0, MAX_SEND_SIZE);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 0, MAX_SEND_SIZE);
                         return FALSE;
                     }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'f':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
                 Family = AF_INET;
                 IpOptions.Flags |= IP_FLAG_DF;
                 break;
+            }
 
             case L'i':
+            {
                 if (i + 1 < argc)
                 {
                     ULONG Ttl = wcstoul(argv[++i], NULL, 0);
 
                     if ((Ttl == 0) || (Ttl > UCHAR_MAX))
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 1, UCHAR_MAX);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 1, UCHAR_MAX);
                         return FALSE;
                     }
 
@@ -307,18 +257,17 @@ ParseCmdLine(int argc, PWSTR argv[])
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'v':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
@@ -331,37 +280,34 @@ ParseCmdLine(int argc, PWSTR argv[])
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
 
                 break;
+            }
 
             case L'w':
+            {
                 if (i + 1 < argc)
                 {
                     Timeout = wcstoul(argv[++i], NULL, 0);
-
                     if (Timeout < DEFAULT_TIMEOUT)
-                    {
                         Timeout = DEFAULT_TIMEOUT;
-                    }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'R':
+            {
                 if (Family == AF_INET)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv6");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv6");
                     return FALSE;
                 }
 
@@ -369,36 +315,39 @@ ParseCmdLine(int argc, PWSTR argv[])
 
                 /* This option has been deprecated. Don't do anything. */
                 break;
+            }
 
             case L'4':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
                 Family = AF_INET;
                 break;
+            }
 
             case L'6':
+            {
                 if (Family == AF_INET)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv6");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv6");
                     return FALSE;
                 }
 
                 Family = AF_INET6;
                 break;
+            }
 
             case L'?':
-                PrintString(IDS_USAGE);
+                ConResPrintf(StdOut, IDS_USAGE);
                 return FALSE;
 
             default:
-                PrintString(IDS_BAD_OPTION, argv[i]);
-                PrintString(IDS_USAGE);
+                ConResPrintf(StdErr, IDS_BAD_OPTION, argv[i]);
+                ConResPrintf(StdErr, IDS_USAGE);
                 return FALSE;
             }
         }
@@ -406,8 +355,7 @@ ParseCmdLine(int argc, PWSTR argv[])
         {
             if (TargetName != NULL)
             {
-                PrintString(IDS_BAD_PARAMETER, argv[i]);
-
+                ConResPrintf(StdErr, IDS_BAD_PARAMETER, argv[i]);
                 return FALSE;
             }
 
@@ -417,8 +365,7 @@ ParseCmdLine(int argc, PWSTR argv[])
 
     if (TargetName == NULL)
     {
-        PrintString(IDS_MISSING_ADDRESS);
-
+        ConResPrintf(StdErr, IDS_MISSING_ADDRESS);
         return FALSE;
     }
 
@@ -444,8 +391,7 @@ ResolveTarget(PCWSTR target)
         Status = GetAddrInfoW(target, NULL, &hints, &Target);
         if (Status != 0)
         {
-            PrintString(IDS_UNKNOWN_HOST, target);
-
+            ConResPrintf(StdOut, IDS_UNKNOWN_HOST, target);
             return FALSE;
         }
 
@@ -453,12 +399,10 @@ ResolveTarget(PCWSTR target)
     }
     else if (ResolveAddress)
     {
-        Status = GetNameInfoW(
-            Target->ai_addr, Target->ai_addrlen,
-            CanonName, _countof(CanonName),
-            NULL, 0,
-            NI_NAMEREQD);
-
+        Status = GetNameInfoW(Target->ai_addr, Target->ai_addrlen,
+                              CanonName, _countof(CanonName),
+                              NULL, 0,
+                              NI_NAMEREQD);
         if (Status != 0)
         {
             DPRINT("GetNameInfoW failed: %d\n", WSAGetLastError());
@@ -482,30 +426,24 @@ Ping(void)
     SendBuffer = malloc(RequestSize);
     if (SendBuffer == NULL)
     {
-        PrintString(IDS_NO_RESOURCES);
-
+        ConResPrintf(StdErr, IDS_NO_RESOURCES);
         exit(1);
     }
 
     ZeroMemory(SendBuffer, RequestSize);
 
     if (Family == AF_INET6)
-    {
         ReplySize += sizeof(ICMPV6_ECHO_REPLY);
-    }
     else
-    {
         ReplySize += sizeof(ICMP_ECHO_REPLY);
-    }
 
     ReplySize += RequestSize + SIZEOF_ICMP_ERROR + SIZEOF_IO_STATUS_BLOCK;
 
     ReplyBuffer = malloc(ReplySize);
     if (ReplyBuffer == NULL)
     {
-        PrintString(IDS_NO_RESOURCES);
+        ConResPrintf(StdErr, IDS_NO_RESOURCES);
         free(SendBuffer);
-
         exit(1);
     }
 
@@ -520,20 +458,18 @@ Ping(void)
         ZeroMemory(&Source, sizeof(Source));
         Source.sin6_family = AF_INET6;
 
-        Status = Icmp6SendEcho2(
-            hIcmpFile, NULL, NULL, NULL,
-            &Source,
-            (struct sockaddr_in6 *)Target->ai_addr,
-            SendBuffer, (USHORT)RequestSize, &IpOptions,
-            ReplyBuffer, ReplySize, Timeout);
+        Status = Icmp6SendEcho2(hIcmpFile, NULL, NULL, NULL,
+                                &Source,
+                                (struct sockaddr_in6 *)Target->ai_addr,
+                                SendBuffer, (USHORT)RequestSize, &IpOptions,
+                                ReplyBuffer, ReplySize, Timeout);
     }
     else
     {
-        Status = IcmpSendEcho2(
-            hIcmpFile, NULL, NULL, NULL,
-            ((PSOCKADDR_IN)Target->ai_addr)->sin_addr.s_addr,
-            SendBuffer, (USHORT)RequestSize, &IpOptions,
-            ReplyBuffer, ReplySize, Timeout);
+        Status = IcmpSendEcho2(hIcmpFile, NULL, NULL, NULL,
+                               ((PSOCKADDR_IN)Target->ai_addr)->sin_addr.s_addr,
+                               SendBuffer, (USHORT)RequestSize, &IpOptions,
+                               ReplyBuffer, ReplySize, Timeout);
     }
 
     free(SendBuffer);
@@ -544,19 +480,19 @@ Ping(void)
         switch (Status)
         {
         case IP_DEST_HOST_UNREACHABLE:
-            PrintString(IDS_DEST_HOST_UNREACHABLE);
+            ConResPrintf(StdOut, IDS_DEST_HOST_UNREACHABLE);
             break;
 
         case IP_DEST_NET_UNREACHABLE:
-            PrintString(IDS_DEST_NET_UNREACHABLE);
+            ConResPrintf(StdOut, IDS_DEST_NET_UNREACHABLE);
             break;
 
         case IP_REQ_TIMED_OUT:
-            PrintString(IDS_REQUEST_TIMED_OUT);
+            ConResPrintf(StdOut, IDS_REQUEST_TIMED_OUT);
             break;
 
         default:
-            PrintString(IDS_TRANSMIT_FAILED, Status);
+            ConResPrintf(StdOut, IDS_TRANSMIT_FAILED, Status);
             break;
         }
     }
@@ -564,7 +500,7 @@ Ping(void)
     {
         EchosReceived++;
 
-        PrintString(IDS_REPLY_FROM, Address);
+        ConResPrintf(StdOut, IDS_REPLY_FROM, Address);
 
         if (Family == AF_INET6)
         {
@@ -575,38 +511,32 @@ Ping(void)
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
+            {
                 EchosSuccessful++;
 
                 if (pEchoReply->RoundTripTime == 0)
-                {
-                    PrintString(IDS_REPLY_TIME_0MS);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_0MS);
                 else
-                {
-                    PrintString(IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
 
                 if (pEchoReply->RoundTripTime < RTTMin || RTTMin == 0)
-                {
                     RTTMin = pEchoReply->RoundTripTime;
-                }
 
                 if (pEchoReply->RoundTripTime > RTTMax || RTTMax == 0)
-                {
                     RTTMax = pEchoReply->RoundTripTime;
-                }
 
-                wprintf(L"\n");
+                ConPuts(StdOut, L"\n");
 
                 RTTTotal += pEchoReply->RoundTripTime;
                 break;
+            }
 
             case IP_TTL_EXPIRED_TRANSIT:
-                PrintString(IDS_TTL_EXPIRED);
+                ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;
 
             default:
-                PrintString(IDS_REPLY_STATUS, pEchoReply->Status);
+                ConResPrintf(StdOut, IDS_REPLY_STATUS, pEchoReply->Status);
                 break;
             }
         }
@@ -619,40 +549,34 @@ Ping(void)
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
+            {
                 EchosSuccessful++;
 
-                PrintString(IDS_REPLY_BYTES, pEchoReply->DataSize);
+                ConResPrintf(StdOut, IDS_REPLY_BYTES, pEchoReply->DataSize);
 
                 if (pEchoReply->RoundTripTime == 0)
-                {
-                    PrintString(IDS_REPLY_TIME_0MS);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_0MS);
                 else
-                {
-                    PrintString(IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
 
-                PrintString(IDS_REPLY_TTL, pEchoReply->Options.Ttl);
+                ConResPrintf(StdOut, IDS_REPLY_TTL, pEchoReply->Options.Ttl);
 
                 if (pEchoReply->RoundTripTime < RTTMin || RTTMin == 0)
-                {
                     RTTMin = pEchoReply->RoundTripTime;
-                }
 
                 if (pEchoReply->RoundTripTime > RTTMax || RTTMax == 0)
-                {
                     RTTMax = pEchoReply->RoundTripTime;
-                }
 
                 RTTTotal += pEchoReply->RoundTripTime;
                 break;
+            }
 
             case IP_TTL_EXPIRED_TRANSIT:
-                PrintString(IDS_TTL_EXPIRED);
+                ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;
 
             default:
-                PrintString(IDS_REPLY_STATUS, pEchoReply->Status);
+                ConResPrintf(StdOut, IDS_REPLY_STATUS, pEchoReply->Status);
                 break;
             }
         }
@@ -668,13 +592,12 @@ PrintStats(void)
     ULONG EchosLost = EchosSent - EchosReceived;
     ULONG PercentLost = (ULONG)((EchosLost / (double)EchosSent) * 100.0);
 
-    PrintString(IDS_STATISTICS, Address, EchosSent, EchosReceived, EchosLost, PercentLost);
+    ConResPrintf(StdOut, IDS_STATISTICS, Address, EchosSent, EchosReceived, EchosLost, PercentLost);
 
     if (EchosSuccessful > 0)
     {
         ULONG RTTAverage = RTTTotal / EchosSuccessful;
-
-        PrintString(IDS_APPROXIMATE_RTT, RTTMin, RTTMax, RTTAverage);
+        ConResPrintf(StdOut, IDS_APPROXIMATE_RTT, RTTMin, RTTMax, RTTAverage);
     }
 }
 
@@ -687,12 +610,12 @@ ConsoleCtrlHandler(DWORD ControlType)
     {
     case CTRL_C_EVENT:
         PrintStats();
-        PrintString(IDS_CTRL_C);
+        ConResPrintf(StdOut, IDS_CTRL_C);
         return FALSE;
 
     case CTRL_BREAK_EVENT:
         PrintStats();
-        PrintString(IDS_CTRL_BREAK);
+        ConResPrintf(StdOut, IDS_CTRL_BREAK);
         return TRUE;
 
     case CTRL_CLOSE_EVENT: