Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/hostname/hostname.c
4388 views
1
/*
2
* Hostname display utility
3
*
4
* Copyright 2008 Andrew Riedi
5
* Copyright 2010-2011 Andrew Nguyen
6
*
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.
11
*
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.
16
*
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
20
*/
21
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>
29
30
#include "hostname.h"
31
32
static int hostname_vprintfW(const WCHAR *msg, va_list va_args)
33
{
34
int wlen;
35
DWORD count;
36
WCHAR msg_buffer[8192];
37
38
wlen = vswprintf(msg_buffer, ARRAY_SIZE(msg_buffer), msg, va_args);
39
40
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL))
41
{
42
DWORD len;
43
char *msgA;
44
45
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
46
* back to WriteFile() with OEM code page.
47
*/
48
len = WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen,
49
NULL, 0, NULL, NULL);
50
msgA = HeapAlloc(GetProcessHeap(), 0, len);
51
if (!msgA)
52
return 0;
53
54
WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL);
55
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
56
HeapFree(GetProcessHeap(), 0, msgA);
57
}
58
59
return count;
60
}
61
62
static int WINAPIV hostname_printfW(const WCHAR *msg, ...)
63
{
64
va_list va_args;
65
int len;
66
67
va_start(va_args, msg);
68
len = hostname_vprintfW(msg, va_args);
69
va_end(va_args);
70
71
return len;
72
}
73
74
static int WINAPIV hostname_message_printfW(int msg, ...)
75
{
76
va_list va_args;
77
WCHAR msg_buffer[8192];
78
int len;
79
80
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
81
82
va_start(va_args, msg);
83
len = hostname_vprintfW(msg_buffer, va_args);
84
va_end(va_args);
85
86
return len;
87
}
88
89
static int hostname_message(int msg)
90
{
91
WCHAR msg_buffer[8192];
92
93
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
94
95
return hostname_printfW(L"%s", msg_buffer);
96
}
97
98
static int display_computer_name(void)
99
{
100
WCHAR name[MAX_COMPUTERNAME_LENGTH + 1];
101
DWORD size = ARRAY_SIZE(name);
102
BOOL ret;
103
104
ret = GetComputerNameW(name, &size);
105
if (!ret)
106
{
107
hostname_message_printfW(STRING_CANNOT_GET_HOSTNAME, GetLastError());
108
return 1;
109
}
110
111
hostname_printfW(L"%s\r\n", name);
112
return 0;
113
}
114
115
int __cdecl wmain(int argc, WCHAR *argv[])
116
{
117
if (argc > 1)
118
{
119
unsigned int i;
120
121
if (!wcsncmp(argv[1], L"/?", ARRAY_SIZE(L"/?") - 1))
122
{
123
hostname_message(STRING_USAGE);
124
return 1;
125
}
126
127
for (i = 1; i < argc; i++)
128
{
129
if (argv[i][0] == '-')
130
{
131
switch (argv[i][1])
132
{
133
case 's':
134
/* Ignore the option and continue processing. */
135
break;
136
case '?':
137
hostname_message(STRING_USAGE);
138
return 1;
139
default:
140
hostname_message_printfW(STRING_INVALID_OPTION, argv[i][1]);
141
hostname_message(STRING_USAGE);
142
return 1;
143
}
144
}
145
else
146
{
147
hostname_message(STRING_CANNOT_SET_HOSTNAME);
148
hostname_message(STRING_USAGE);
149
return 1;
150
}
151
}
152
}
153
154
return display_computer_name();
155
}
156
157