ReactOS 0.4.16-dev-2534-gdcd45a5
hostname.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Hostname Command
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Retrieves the current DNS host name of the computer.
5 * COPYRIGHT: Copyright 2005 Emanuele Aliberti <ea@reactos.com>
6 * Copyright 2019-2026 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
7 */
8
9#include <stdlib.h>
10#include <stdio.h>
11
12#include <windef.h>
13#include <winbase.h>
14
15#include <conutils.h>
16
17#include "resource.h"
18
19static VOID
22 _In_ DWORD dwError)
23{
24 INT Len;
25
26 if (dwError == ERROR_SUCCESS)
27 return;
28
31 else // if (Message)
34 NULL, dwError, LANG_USER_DEFAULT);
35 if (Len <= 0) /* Fall back in case the error is not defined */
36 ConPrintf(StdErr, L"%lu\n", dwError);
37}
38
39int wmain(int argc, WCHAR* argv[])
40{
41 /* Initialize the Console Standard Streams */
43
44 if (argc == 1)
45 {
46 BOOL bSuccess;
47 WCHAR LocalHostName[256] = L""; // MAX_COMPUTERNAME_LENGTH + 1 for NetBIOS name.
48 DWORD HostNameSize = _countof(LocalHostName);
49 PWSTR HostName = LocalHostName;
50
51 /* Try to retrieve the host name using the local buffer */
52 bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
53 if (!bSuccess && (GetLastError() == ERROR_MORE_DATA))
54 {
55 /* Retry with a larger buffer since the local buffer was too small */
56 HostName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HostNameSize * sizeof(WCHAR));
57 if (HostName)
58 bSuccess = GetComputerNameExW(ComputerNameDnsHostname, HostName, &HostNameSize);
59 }
60
61 /* Print out the host name */
62 if (bSuccess)
63 ConPrintf(StdOut, L"%s\n", HostName);
64
65 /* If a larger buffer has been allocated, free it */
66 if (HostName && (HostName != LocalHostName))
67 HeapFree(GetProcessHeap(), 0, HostName);
68
69 if (!bSuccess)
70 {
71 /* Fail in case of error */
73 return 1;
74 }
75 }
76 else
77 {
78 if ((_wcsicmp(argv[1], L"-s") == 0) || (_wcsicmp(argv[1], L"/s") == 0))
79 {
80 /* The program doesn't allow the user to set the host name */
82 return 1;
83 }
84 else
85 {
86 /* Let the user know what the program does */
88 }
89 }
90
91 return 0;
92}
93
94/* EOF */
#define IDS_USAGE
Definition: resource.h:3
#define IDS_ERROR
Definition: resource.h:18
#define IDS_NOSET
Definition: resource.h:4
BOOL WINAPI GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPWSTR lpBuffer, LPDWORD nSize)
Definition: compname.c:216
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: conutils_noros.h:8
#define ConInitStdStreams()
Definition: conutils_noros.h:5
void ConPrintf(FILE *fp, LPCWSTR psz,...)
#define StdOut
Definition: conutils_noros.h:6
#define StdErr
Definition: conutils_noros.h:7
void ConResPuts(FILE *fp, UINT nID)
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define Len
Definition: deflate.h:82
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define IS_INTRESOURCE(x)
Definition: loader.c:613
MonoAssembly int argc
Definition: metahost.c:107
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
static const WCHAR Message[]
Definition: register.c:74
#define L(x)
Definition: resources.c:13
#define PtrToUlong(u)
Definition: config.h:107
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
static VOID PrintError(_In_opt_ PCWSTR Message, _In_ DWORD dwError)
Definition: hostname.c:20
#define argv
Definition: mplay32.c:18
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
INT ConMsgPuts(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId)
Definition: outstream.c:835
int wmain()
#define _countof(array)
Definition: sndvol32.h:70
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
int32_t INT
Definition: typedefs.h:58
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:400
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
__wchar_t WCHAR
Definition: xmlstorage.h:180