From: Hugh McMaster Subject: hostname: Remove redundant codepath and replace with FormatMessage Message-Id: Date: Tue, 3 Mar 2015 15:55:06 +1100 Removes the redundant vsprintfW codepath and replaces it with FormatMessage. Precedent for these changes: attrib:      6aacb090f6bad8f3109cea710cc3aab462eb97d4 ipconfig:  66b06a499d97338dc5ef0b5c4c78b107d577ced8 net:         f3f24fb5d4b017e026333c5d00066d727741a4b5 xcopy:     0cdabdd64873439052b3efa84850bc08cacf5395 From 49585438cd1c21a0ca3e25be988e585570b2e2f6 Mon Sep 17 00:00:00 2001 From: Hugh McMaster Date: Tue, 3 Mar 2015 14:55:01 +1100 Subject: hostname: Remove redundant codepath and replace with FormatMessage Removes the redundant vsprintfW codepath and replaces it with FormatMessage. Precedent for these changes: attrib: 6aacb090f6bad8f3109cea710cc3aab462eb97d4 ipconfig: 66b06a499d97338dc5ef0b5c4c78b107d577ced8 net: f3f24fb5d4b017e026333c5d00066d727741a4b5 xcopy: 0cdabdd64873439052b3efa84850bc08cacf5395 --- programs/hostname/hostname.c | 19 ++++++++++++------- programs/hostname/hostname.rc | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/programs/hostname/hostname.c b/programs/hostname/hostname.c index 8bf8676..c553041 100644 --- a/programs/hostname/hostname.c +++ b/programs/hostname/hostname.c @@ -39,13 +39,14 @@ #include "hostname.h" -static int hostname_vprintfW(const WCHAR *msg, va_list va_args) +static int hostname_writeconsole(const WCHAR *msg, va_list va_args) { int wlen; DWORD count, ret; - WCHAR msg_buffer[8192]; + WCHAR *msg_buffer; - wlen = vsprintfW(msg_buffer, msg, va_args); + wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, + msg, 0, 0, (WCHAR *)&msg_buffer, 0, &va_args); ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL); if (!ret) @@ -61,13 +62,17 @@ static int hostname_vprintfW(const WCHAR *msg, va_list va_args) NULL, 0, NULL, NULL); msgA = HeapAlloc(GetProcessHeap(), 0, len); if (!msgA) + { + LocalFree(msg_buffer); 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); } + LocalFree(msg_buffer); return count; } @@ -78,7 +83,7 @@ static int hostname_printfW(const WCHAR *msg, ...) int len; va_start(va_args, msg); - len = hostname_vprintfW(msg, va_args); + len = hostname_writeconsole(msg, va_args); va_end(va_args); return len; @@ -94,7 +99,7 @@ static int hostname_message_printfW(int msg, ...) sizeof(msg_buffer)/sizeof(WCHAR)); va_start(va_args, msg); - len = hostname_vprintfW(msg_buffer, va_args); + len = hostname_writeconsole(msg_buffer, va_args); va_end(va_args); return len; @@ -102,7 +107,7 @@ static int hostname_message_printfW(int msg, ...) static int hostname_message(int msg) { - static const WCHAR formatW[] = {'%','s',0}; + static const WCHAR formatW[] = {'%','1',0}; WCHAR msg_buffer[8192]; LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, @@ -113,7 +118,7 @@ static int hostname_message(int msg) static void display_computer_name(void) { - static const WCHAR fmtW[] = {'%','s','\r','\n',0}; + static const WCHAR fmtW[] = {'%','1','\r','\n',0}; char nameA[256]; WCHAR nameW[256]; diff --git a/programs/hostname/hostname.rc b/programs/hostname/hostname.rc index 182584f..f5e8e4e 100644 --- a/programs/hostname/hostname.rc +++ b/programs/hostname/hostname.rc @@ -27,6 +27,6 @@ LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT STRINGTABLE { STRING_USAGE, "Usage: hostname\n" - STRING_INVALID_OPTION, "Error: Invalid option '%c'.\n" + STRING_INVALID_OPTION, "Error: Invalid option '%1!c!'.\n" STRING_CANNOT_SET_HOSTNAME, "Error: Setting the system hostname is not possible with the hostname utility.\n" } -- 1.9.1