2011-05-20 14:43:21 +02:00
|
|
|
/*
|
|
|
|
* Hostname display utility
|
|
|
|
*
|
|
|
|
* Copyright 2008 Andrew Riedi
|
|
|
|
* Copyright 2010-2011 Andrew Nguyen
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2019-04-29 10:47:53 +02:00
|
|
|
#include <stdio.h>
|
2011-05-20 14:43:21 +02:00
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <wincon.h>
|
|
|
|
#include <winnls.h>
|
|
|
|
#include <winuser.h>
|
|
|
|
|
|
|
|
#include "hostname.h"
|
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
static int hostname_vprintfW(const WCHAR *msg, __ms_va_list va_args)
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
|
|
|
int wlen;
|
|
|
|
DWORD count, ret;
|
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
wlen = vswprintf(msg_buffer, ARRAY_SIZE(msg_buffer), msg, va_args);
|
2011-05-20 14:43:21 +02:00
|
|
|
|
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
2011-08-24 14:29:31 +02:00
|
|
|
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
|
|
|
|
* back to WriteFile(), assuming the console encoding is still the right
|
|
|
|
* one in that case.
|
|
|
|
*/
|
2020-11-12 20:38:08 +01:00
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, msg_buffer, wlen,
|
2011-05-20 14:43:21 +02:00
|
|
|
NULL, 0, NULL, NULL);
|
|
|
|
msgA = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
if (!msgA)
|
|
|
|
return 0;
|
|
|
|
|
2020-11-12 20:38:08 +01:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, msg_buffer, wlen, msgA, len, NULL, NULL);
|
2011-05-20 14:43:21 +02:00
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, msgA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
static int WINAPIV hostname_printfW(const WCHAR *msg, ...)
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_list va_args;
|
2011-05-20 14:43:21 +02:00
|
|
|
int len;
|
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_start(va_args, msg);
|
2011-05-20 14:43:21 +02:00
|
|
|
len = hostname_vprintfW(msg, va_args);
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_end(va_args);
|
2011-05-20 14:43:21 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
static int WINAPIV hostname_message_printfW(int msg, ...)
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_list va_args;
|
2011-05-20 14:43:21 +02:00
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
int len;
|
|
|
|
|
2018-07-24 00:01:25 +02:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2011-05-20 14:43:21 +02:00
|
|
|
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_start(va_args, msg);
|
2011-05-20 14:43:21 +02:00
|
|
|
len = hostname_vprintfW(msg_buffer, va_args);
|
2019-04-29 10:47:53 +02:00
|
|
|
__ms_va_end(va_args);
|
2011-05-20 14:43:21 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hostname_message(int msg)
|
|
|
|
{
|
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2018-07-24 00:01:25 +02:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2011-05-20 14:43:21 +02:00
|
|
|
|
2020-10-05 23:22:52 +02:00
|
|
|
return hostname_printfW(L"%s", msg_buffer);
|
2011-05-20 14:43:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 18:29:08 +02:00
|
|
|
static int display_computer_name(void)
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
2018-10-05 18:29:08 +02:00
|
|
|
WCHAR name[MAX_COMPUTERNAME_LENGTH + 1];
|
|
|
|
DWORD size = ARRAY_SIZE(name);
|
|
|
|
BOOL ret;
|
2011-05-20 14:43:21 +02:00
|
|
|
|
2018-10-05 18:29:08 +02:00
|
|
|
ret = GetComputerNameW(name, &size);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
hostname_message_printfW(STRING_CANNOT_GET_HOSTNAME, GetLastError());
|
|
|
|
return 1;
|
|
|
|
}
|
2011-05-20 14:43:21 +02:00
|
|
|
|
2020-10-05 23:22:52 +02:00
|
|
|
hostname_printfW(L"%s\r\n", name);
|
2018-10-05 18:29:08 +02:00
|
|
|
return 0;
|
2011-05-20 14:43:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argv[])
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2020-10-05 23:22:52 +02:00
|
|
|
if (!wcsncmp(argv[1], L"/?", ARRAY_SIZE(L"/?") - 1))
|
2011-05-20 14:43:21 +02:00
|
|
|
{
|
|
|
|
hostname_message(STRING_USAGE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (argv[i][0] == '-')
|
|
|
|
{
|
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
/* Ignore the option and continue processing. */
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
hostname_message(STRING_USAGE);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
hostname_message_printfW(STRING_INVALID_OPTION, argv[i][1]);
|
|
|
|
hostname_message(STRING_USAGE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hostname_message(STRING_CANNOT_SET_HOSTNAME);
|
|
|
|
hostname_message(STRING_USAGE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:29:08 +02:00
|
|
|
return display_computer_name();
|
2011-05-20 14:43:21 +02:00
|
|
|
}
|