https://git.reactos.org/?p=reactos.git;a=commitdiff;h=28ed2347ef8b382c0062e…
commit 28ed2347ef8b382c0062e7001765ba3a9b5b7a3e
Author: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
AuthorDate: Sun Jun 16 01:00:40 2019 +0200
Commit: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
CommitDate: Sun Jun 16 01:00:40 2019 +0200
[HOSTNAME] Retrieve the DNS *host name* of the computer, and not the computer name.
CORE-16095, ROSTESTS-326
---
base/applications/cmdutils/hostname/hostname.c | 69 ++++++++++++++------------
1 file changed, 36 insertions(+), 33 deletions(-)
diff --git a/base/applications/cmdutils/hostname/hostname.c
b/base/applications/cmdutils/hostname/hostname.c
index 4e49c5d4182..00ded61f1bf 100644
--- a/base/applications/cmdutils/hostname/hostname.c
+++ b/base/applications/cmdutils/hostname/hostname.c
@@ -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(a)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(a)reactos.com)
+ * Copyright 2019 Hermes Belusca-Maito
*/
+#include <stdlib.h>
#include <conio.h>
#include <windef.h>
@@ -35,35 +19,54 @@ int wmain(int argc, WCHAR* argv[])
{
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 */
- LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, 100);
+ LoadStringW(GetModuleHandle(NULL), IDS_ERROR, Msg, _countof(Msg));
_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))
{
- /* 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 */
- LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, 100);
+ LoadStringW(GetModuleHandle(NULL), IDS_USAGE, Msg, _countof(Msg));
_cwprintf(L"\n%s\n\n", Msg);
}
}