2011-05-20 12:43:21 +00: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 08:47:53 +00:00
|
|
|
#include <stdio.h>
|
2011-05-20 12:43:21 +00:00
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <wincon.h>
|
|
|
|
#include <winnls.h>
|
|
|
|
#include <winuser.h>
|
|
|
|
|
|
|
|
#include "hostname.h"
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
static int hostname_vprintfW(const WCHAR *msg, __ms_va_list va_args)
|
2011-05-20 12:43:21 +00:00
|
|
|
{
|
|
|
|
int wlen;
|
|
|
|
DWORD count, ret;
|
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
wlen = vswprintf(msg_buffer, ARRAY_SIZE(msg_buffer), msg, va_args);
|
2011-05-20 12:43:21 +00:00
|
|
|
|
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
2011-08-24 12:29:31 +00: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.
|
|
|
|
*/
|
2011-05-20 12:43:21 +00:00
|
|
|
len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
|
|
|
|
NULL, 0, NULL, NULL);
|
|
|
|
msgA = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
if (!msgA)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
|
|
|
|
NULL, NULL);
|
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, msgA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
static int WINAPIV hostname_printfW(const WCHAR *msg, ...)
|
2011-05-20 12:43:21 +00:00
|
|
|
{
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_list va_args;
|
2011-05-20 12:43:21 +00:00
|
|
|
int len;
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_start(va_args, msg);
|
2011-05-20 12:43:21 +00:00
|
|
|
len = hostname_vprintfW(msg, va_args);
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_end(va_args);
|
2011-05-20 12:43:21 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
static int WINAPIV hostname_message_printfW(int msg, ...)
|
2011-05-20 12:43:21 +00:00
|
|
|
{
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_list va_args;
|
2011-05-20 12:43:21 +00:00
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
int len;
|
|
|
|
|
2018-07-23 22:01:25 +00:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2011-05-20 12:43:21 +00:00
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_start(va_args, msg);
|
2011-05-20 12:43:21 +00:00
|
|
|
len = hostname_vprintfW(msg_buffer, va_args);
|
2019-04-29 08:47:53 +00:00
|
|
|
__ms_va_end(va_args);
|
2011-05-20 12:43:21 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hostname_message(int msg)
|
|
|
|
{
|
|
|
|
static const WCHAR formatW[] = {'%','s',0};
|
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2018-07-23 22:01:25 +00:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2011-05-20 12:43:21 +00:00
|
|
|
|
|
|
|
return hostname_printfW(formatW, msg_buffer);
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:29:08 +00:00
|
|
|
static int display_computer_name(void)
|
2011-05-20 12:43:21 +00:00
|
|
|
{
|
2011-06-01 12:13:21 +00:00
|
|
|
static const WCHAR fmtW[] = {'%','s','\r','\n',0};
|
2011-05-20 12:43:21 +00:00
|
|
|
|
2018-10-05 16:29:08 +00:00
|
|
|
WCHAR name[MAX_COMPUTERNAME_LENGTH + 1];
|
|
|
|
DWORD size = ARRAY_SIZE(name);
|
|
|
|
BOOL ret;
|
2011-05-20 12:43:21 +00:00
|
|
|
|
2018-10-05 16:29:08 +00:00
|
|
|
ret = GetComputerNameW(name, &size);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
hostname_message_printfW(STRING_CANNOT_GET_HOSTNAME, GetLastError());
|
|
|
|
return 1;
|
|
|
|
}
|
2011-05-20 12:43:21 +00:00
|
|
|
|
2018-10-05 16:29:08 +00:00
|
|
|
hostname_printfW(fmtW, name);
|
|
|
|
return 0;
|
2011-05-20 12:43:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 15:05:47 +00:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argv[])
|
2011-05-20 12:43:21 +00:00
|
|
|
{
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
static const WCHAR slashHelpW[] = {'/','?',0};
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-04-29 08:47:53 +00:00
|
|
|
if (!wcsncmp(argv[1], slashHelpW, ARRAY_SIZE(slashHelpW) - 1))
|
2011-05-20 12:43:21 +00: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 16:29:08 +00:00
|
|
|
return display_computer_name();
|
2011-05-20 12:43:21 +00:00
|
|
|
}
|