Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/chcp.com/main.c
4389 views
1
/*
2
* Copyright 2019 Erich E. Hoover
3
*
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
8
*
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
13
*
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17
*/
18
19
#include <windows.h>
20
#include <stdlib.h>
21
#include "resource.h"
22
23
#include "wine/debug.h"
24
25
WINE_DEFAULT_DEBUG_CHANNEL(chcp);
26
27
static void output_writeconsole(const WCHAR *str, DWORD wlen)
28
{
29
DWORD count;
30
31
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL))
32
{
33
DWORD len;
34
char *msgA;
35
36
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
37
* back to WriteFile(), assuming the console encoding is still the right
38
* one in that case.
39
*/
40
len = WideCharToMultiByte(GetOEMCP(), 0, str, wlen, NULL, 0, NULL, NULL);
41
msgA = malloc(len);
42
if (!msgA) return;
43
44
WideCharToMultiByte(GetOEMCP(), 0, str, wlen, msgA, len, NULL, NULL);
45
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
46
free(msgA);
47
}
48
}
49
50
static void output_formatstring(const WCHAR *fmt, va_list va_args)
51
{
52
WCHAR *str;
53
DWORD len;
54
55
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
56
fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
57
if (!len && GetLastError() != ERROR_NO_WORK_DONE)
58
{
59
WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
60
return;
61
}
62
output_writeconsole(str, len);
63
LocalFree(str);
64
}
65
66
static void WINAPIV output_message(unsigned int id, ...)
67
{
68
WCHAR *fmt = NULL;
69
int len;
70
va_list va_args;
71
72
if (!(len = LoadStringW(GetModuleHandleW(NULL), id, (WCHAR *)&fmt, 0)))
73
{
74
WINE_FIXME("LoadString failed with %ld\n", GetLastError());
75
return;
76
}
77
78
len++;
79
fmt = malloc(len * sizeof(WCHAR));
80
if (!fmt) return;
81
82
LoadStringW(GetModuleHandleW(NULL), id, fmt, len);
83
84
va_start(va_args, id);
85
output_formatstring(fmt, va_args);
86
va_end(va_args);
87
88
free(fmt);
89
}
90
91
int __cdecl wmain(int argc, WCHAR *argv[])
92
{
93
int i;
94
95
if (argc == 1)
96
{
97
output_message(STRING_ACTIVE_CODE_PAGE, GetConsoleCP());
98
return 0;
99
}
100
else if (argc == 2)
101
{
102
int codepage, success;
103
104
if (!lstrcmpW(argv[1], L"/?"))
105
{
106
output_message(STRING_USAGE);
107
return 0;
108
}
109
110
codepage = _wtoi(argv[1]);
111
success = SetConsoleCP(codepage) && SetConsoleOutputCP(codepage);
112
113
if (success)
114
output_message(STRING_ACTIVE_CODE_PAGE, codepage);
115
else
116
output_message(STRING_INVALID_CODE_PAGE);
117
118
return !success;
119
}
120
121
WINE_FIXME("unexpected arguments:");
122
for (i = 0; i < argc; i++)
123
WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
124
WINE_FIXME("\n");
125
126
return 0;
127
}
128
129