hostname: Output a CRLF newline with the hostname string.
[wine.git] / programs / hostname / hostname.c
blob77ff23b42fd512c29b28dee8613fbeb40b924aeb
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 "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #if defined(__MINGW32__) || defined (_MSC_VER)
27 #include <winsock2.h>
28 #elif defined(HAVE_UNISTD_H)
29 #include <unistd.h>
30 #endif
32 #include <windef.h>
33 #include <winbase.h>
34 #include <wincon.h>
35 #include <winnls.h>
36 #include <winuser.h>
38 #include <wine/unicode.h>
40 #include "hostname.h"
42 static int hostname_vprintfW(const WCHAR *msg, va_list va_args)
44 int wlen;
45 DWORD count, ret;
46 WCHAR msg_buffer[8192];
48 wlen = vsprintfW(msg_buffer, msg, va_args);
50 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
51 if (!ret)
53 DWORD len;
54 char *msgA;
56 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
57 NULL, 0, NULL, NULL);
58 msgA = HeapAlloc(GetProcessHeap(), 0, len);
59 if (!msgA)
60 return 0;
62 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
63 NULL, NULL);
64 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
65 HeapFree(GetProcessHeap(), 0, msgA);
68 return count;
71 static int hostname_printfW(const WCHAR *msg, ...)
73 va_list va_args;
74 int len;
76 va_start(va_args, msg);
77 len = hostname_vprintfW(msg, va_args);
78 va_end(va_args);
80 return len;
83 static int hostname_message_printfW(int msg, ...)
85 va_list va_args;
86 WCHAR msg_buffer[8192];
87 int len;
89 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
90 sizeof(msg_buffer)/sizeof(WCHAR));
92 va_start(va_args, msg);
93 len = hostname_vprintfW(msg_buffer, va_args);
94 va_end(va_args);
96 return len;
99 static int hostname_message(int msg)
101 static const WCHAR formatW[] = {'%','s',0};
102 WCHAR msg_buffer[8192];
104 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
105 sizeof(msg_buffer)/sizeof(WCHAR));
107 return hostname_printfW(formatW, msg_buffer);
110 static void display_computer_name(void)
112 static const WCHAR fmtW[] = {'%','s','\r','\n',0};
114 char nameA[256];
115 WCHAR nameW[256];
117 gethostname(nameA, sizeof(nameA));
118 MultiByteToWideChar(CP_UNIXCP, 0, nameA, sizeof(nameA), nameW, sizeof(nameW)/sizeof(WCHAR));
120 hostname_printfW(fmtW, nameW);
123 int wmain(int argc, WCHAR *argv[])
125 if (argc > 1)
127 static const WCHAR slashHelpW[] = {'/','?',0};
129 unsigned int i;
131 if (!strncmpW(argv[1], slashHelpW, sizeof(slashHelpW)/sizeof(WCHAR) - 1))
133 hostname_message(STRING_USAGE);
134 return 1;
137 for (i = 1; i < argc; i++)
139 if (argv[i][0] == '-')
141 switch (argv[i][1])
143 case 's':
144 /* Ignore the option and continue processing. */
145 break;
146 case '?':
147 hostname_message(STRING_USAGE);
148 return 1;
149 default:
150 hostname_message_printfW(STRING_INVALID_OPTION, argv[i][1]);
151 hostname_message(STRING_USAGE);
152 return 1;
155 else
157 hostname_message(STRING_CANNOT_SET_HOSTNAME);
158 hostname_message(STRING_USAGE);
159 return 1;
164 display_computer_name();
166 return 0;