[HOSTNAME] Retrieve the DNS *host name* of the computer, and not the computer name.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 15 Jun 2019 23:00:40 +0000 (01:00 +0200)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 15 Jun 2019 23:00:40 +0000 (01:00 +0200)
CORE-16095, ROSTESTS-326

base/applications/cmdutils/hostname/hostname.c

index 4e49c5d..00ded61 100644 (file)
@@ -1,28 +1,12 @@
 /*
 /*
- *  ReactOS Win32 Applications
- *  Copyright (C) 2005 ReactOS Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * COPYRIGHT : See COPYING in the top level directory
- * PROJECT   : ReactOS/Win32 get host name
- * FILE      : subsys/system/hostname/hostname.c
- * PROGRAMMER: Emanuele Aliberti (ea@reactos.com)
+ * PROJECT:     ReactOS Hostname Command
+ * LICENSE:     LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
+ * PURPOSE:     Retrieves the current DNS host name of the computer.
+ * COPYRIGHT:   Copyright 2005-2019 Emanuele Aliberti (ea@reactos.com)
+ *              Copyright 2019 Hermes Belusca-Maito
  */
 
  */
 
+#include <stdlib.h>
 #include <conio.h>
 
 #include <windef.h>
 #include <conio.h>
 
 #include <windef.h>
@@ -35,35 +19,54 @@ int wmain(int argc, WCHAR* argv[])
 {
     WCHAR Msg[100];
 
 {
     WCHAR Msg[100];
 
-    if (1 == argc)
+    if (argc == 1)
     {
     {
-        WCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1] = L"";
-        DWORD ComputerNameSize = sizeof(ComputerName) / sizeof(ComputerName[0]);
+        BOOL bSuccess;
+        WCHAR LocalHostName[256] = L""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
+        DWORD HostNameSize = _countof(LocalHostName);
+        PWSTR HostName = LocalHostName;
+
+        /* Try to retrieve the host name using the local buffer */
+        bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
+        if (!bSuccess && (GetLastError() == ERROR_MORE_DATA))
+        {
+            /* Retry with a larger buffer since the local buffer was too small */
+            HostName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HostNameSize * sizeof(WCHAR));
+            if (HostName)
+                bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
+        }
 
 
-        if (!GetComputerName(ComputerName, &ComputerNameSize))
+        if (bSuccess)
+        {
+            /* Print out the host name */
+            _cwprintf(L"%s\n", HostName);
+        }
+
+        /* If a larger buffer has been allocated, free it */
+        if (HostName && (HostName != LocalHostName))
+            HeapFree(GetProcessHeap(), 0, HostName);
+
+        if (!bSuccess)
         {
             /* Fail in case of error */
         {
             /* Fail in case of error */
-            LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, 100);
+            LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, _countof(Msg));
             _cwprintf(L"%s %lu.\n", Msg, GetLastError());
             return 1;
         }
             _cwprintf(L"%s %lu.\n", Msg, GetLastError());
             return 1;
         }
-
-        /* Print out the computer's name */
-        _cwprintf(L"%s\n", ComputerName);
     }
     else
     {
         if ((wcsicmp(argv[1], L"-s") == 0) || (wcsicmp(argv[1], L"/s") == 0))
         {
     }
     else
     {
         if ((wcsicmp(argv[1], L"-s") == 0) || (wcsicmp(argv[1], L"/s") == 0))
         {
-            /* The program doesn't allow the user to set the computer's name */
-            LoadStringW(GetModuleHandle(NULL), IDS_NOSET, Msg, 100);
+            /* The program doesn't allow the user to set the host name */
+            LoadStringW(GetModuleHandle(NULL), IDS_NOSET, Msg, _countof(Msg));
             _cwprintf(L"%s\n", Msg);
             return 1;
         }
         else
         {
             /* Let the user know what the program does */
             _cwprintf(L"%s\n", Msg);
             return 1;
         }
         else
         {
             /* Let the user know what the program does */
-            LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, 100);
+            LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, _countof(Msg));
             _cwprintf(L"\n%s\n\n", Msg);
         }
     }
             _cwprintf(L"\n%s\n\n", Msg);
         }
     }