Revert "oleaut32: Dereference VT_RECORD|VT_BYREF in place.".
[wine.git] / programs / chcp.com / main.c
blobb4309ff086db6a4553091f7cc1484ac366f0b48c
1 /*
2 * Copyright 2019 Erich E. Hoover
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.
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.
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
19 #include <windows.h>
20 #include <stdlib.h>
21 #include "resource.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(chcp);
27 static void output_writeconsole(const WCHAR *str, DWORD wlen)
29 DWORD count;
31 if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL))
33 DWORD len;
34 char *msgA;
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.
40 len = WideCharToMultiByte(GetOEMCP(), 0, str, wlen, NULL, 0, NULL, NULL);
41 msgA = malloc(len);
42 if (!msgA) return;
44 WideCharToMultiByte(GetOEMCP(), 0, str, wlen, msgA, len, NULL, NULL);
45 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
46 free(msgA);
50 static void output_formatstring(const WCHAR *fmt, va_list va_args)
52 WCHAR *str;
53 DWORD len;
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)
59 WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
60 return;
62 output_writeconsole(str, len);
63 LocalFree(str);
66 static void WINAPIV output_message(unsigned int id, ...)
68 WCHAR *fmt = NULL;
69 int len;
70 va_list va_args;
72 if (!(len = LoadStringW(GetModuleHandleW(NULL), id, (WCHAR *)&fmt, 0)))
74 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
75 return;
78 len++;
79 fmt = malloc(len * sizeof(WCHAR));
80 if (!fmt) return;
82 LoadStringW(GetModuleHandleW(NULL), id, fmt, len);
84 va_start(va_args, id);
85 output_formatstring(fmt, va_args);
86 va_end(va_args);
88 free(fmt);
91 int __cdecl wmain(int argc, WCHAR *argv[])
93 int i;
95 if (argc == 1)
97 output_message(STRING_ACTIVE_CODE_PAGE, GetConsoleCP());
98 return 0;
100 else if (argc == 2)
102 int codepage, success;
104 if (!lstrcmpW(argv[1], L"/?"))
106 output_message(STRING_USAGE);
107 return 0;
110 codepage = _wtoi(argv[1]);
111 success = SetConsoleCP(codepage) && SetConsoleOutputCP(codepage);
113 if (success)
114 output_message(STRING_ACTIVE_CODE_PAGE, codepage);
115 else
116 output_message(STRING_INVALID_CODE_PAGE);
118 return !success;
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");
126 return 0;