ucrtbase: Implement %g format for strftime.
[wine.git] / programs / hostname / hostname.c
blob0738e718e1121efe5177d58c6db4dffea5397b53
1 /*
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
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <wincon.h>
27 #include <winnls.h>
28 #include <winuser.h>
30 #include "hostname.h"
32 static int hostname_vprintfW(const WCHAR *msg, __ms_va_list va_args)
34 int wlen;
35 DWORD count, ret;
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);
41 if (!ret)
43 DWORD len;
44 char *msgA;
46 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
47 * back to WriteFile(), assuming the console encoding is still the right
48 * one in that case.
50 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
51 NULL, 0, NULL, NULL);
52 msgA = HeapAlloc(GetProcessHeap(), 0, len);
53 if (!msgA)
54 return 0;
56 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
57 NULL, NULL);
58 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
59 HeapFree(GetProcessHeap(), 0, msgA);
62 return count;
65 static int WINAPIV hostname_printfW(const WCHAR *msg, ...)
67 __ms_va_list va_args;
68 int len;
70 __ms_va_start(va_args, msg);
71 len = hostname_vprintfW(msg, va_args);
72 __ms_va_end(va_args);
74 return len;
77 static int WINAPIV hostname_message_printfW(int msg, ...)
79 __ms_va_list va_args;
80 WCHAR msg_buffer[8192];
81 int len;
83 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
85 __ms_va_start(va_args, msg);
86 len = hostname_vprintfW(msg_buffer, va_args);
87 __ms_va_end(va_args);
89 return len;
92 static int hostname_message(int msg)
94 static const WCHAR formatW[] = {'%','s',0};
95 WCHAR msg_buffer[8192];
97 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
99 return hostname_printfW(formatW, msg_buffer);
102 static int display_computer_name(void)
104 static const WCHAR fmtW[] = {'%','s','\r','\n',0};
106 WCHAR name[MAX_COMPUTERNAME_LENGTH + 1];
107 DWORD size = ARRAY_SIZE(name);
108 BOOL ret;
110 ret = GetComputerNameW(name, &size);
111 if (!ret)
113 hostname_message_printfW(STRING_CANNOT_GET_HOSTNAME, GetLastError());
114 return 1;
117 hostname_printfW(fmtW, name);
118 return 0;
121 int __cdecl wmain(int argc, WCHAR *argv[])
123 if (argc > 1)
125 static const WCHAR slashHelpW[] = {'/','?',0};
127 unsigned int i;
129 if (!wcsncmp(argv[1], slashHelpW, ARRAY_SIZE(slashHelpW) - 1))
131 hostname_message(STRING_USAGE);
132 return 1;
135 for (i = 1; i < argc; i++)
137 if (argv[i][0] == '-')
139 switch (argv[i][1])
141 case 's':
142 /* Ignore the option and continue processing. */
143 break;
144 case '?':
145 hostname_message(STRING_USAGE);
146 return 1;
147 default:
148 hostname_message_printfW(STRING_INVALID_OPTION, argv[i][1]);
149 hostname_message(STRING_USAGE);
150 return 1;
153 else
155 hostname_message(STRING_CANNOT_SET_HOSTNAME);
156 hostname_message(STRING_USAGE);
157 return 1;
162 return display_computer_name();