2 * Hostname display utility
4 * Copyright 2008 Andrew Riedi
5 * Copyright 2010-2011 Andrew Nguyen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 static int hostname_vprintfW(const WCHAR
*msg
, va_list va_args
)
36 WCHAR msg_buffer
[8192];
38 wlen
= vswprintf(msg_buffer
, ARRAY_SIZE(msg_buffer
), msg
, va_args
);
40 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
46 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
47 * back to WriteFile(), assuming the console encoding is still the right
50 len
= WideCharToMultiByte(CP_ACP
, 0, msg_buffer
, wlen
,
52 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
56 WideCharToMultiByte(CP_ACP
, 0, msg_buffer
, wlen
, msgA
, len
, NULL
, NULL
);
57 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
58 HeapFree(GetProcessHeap(), 0, msgA
);
64 static int WINAPIV
hostname_printfW(const WCHAR
*msg
, ...)
69 va_start(va_args
, msg
);
70 len
= hostname_vprintfW(msg
, va_args
);
76 static int WINAPIV
hostname_message_printfW(int msg
, ...)
79 WCHAR msg_buffer
[8192];
82 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
84 va_start(va_args
, msg
);
85 len
= hostname_vprintfW(msg_buffer
, va_args
);
91 static int hostname_message(int msg
)
93 WCHAR msg_buffer
[8192];
95 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
97 return hostname_printfW(L
"%s", msg_buffer
);
100 static int display_computer_name(void)
102 WCHAR name
[MAX_COMPUTERNAME_LENGTH
+ 1];
103 DWORD size
= ARRAY_SIZE(name
);
106 ret
= GetComputerNameW(name
, &size
);
109 hostname_message_printfW(STRING_CANNOT_GET_HOSTNAME
, GetLastError());
113 hostname_printfW(L
"%s\r\n", name
);
117 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
123 if (!wcsncmp(argv
[1], L
"/?", ARRAY_SIZE(L
"/?") - 1))
125 hostname_message(STRING_USAGE
);
129 for (i
= 1; i
< argc
; i
++)
131 if (argv
[i
][0] == '-')
136 /* Ignore the option and continue processing. */
139 hostname_message(STRING_USAGE
);
142 hostname_message_printfW(STRING_INVALID_OPTION
, argv
[i
][1]);
143 hostname_message(STRING_USAGE
);
149 hostname_message(STRING_CANNOT_SET_HOSTNAME
);
150 hostname_message(STRING_USAGE
);
156 return display_computer_name();