2 * Compile and link this with all CLI programs where the main routine
3 * should get UTF-8 arguments on Windows. In those programs, include the
4 * cli_main.h header to rename main to real_main on Windows.
6 * This is used in software licensed under the GPLv2, and its license MUST
7 * be compatible with that license.
9 * This is used in software licensed under the Apache 2.0 license, and its
10 * license MUST be compatible with that license.
12 * For that purpose, we use the MIT (X11) license.
14 * SPDX-License-Identifier: MIT
25 wmain(int argc
, wchar_t *wc_argv
[])
31 argv
= (char **)malloc((argc
+ 1) * sizeof(char *));
33 fprintf(stderr
, "Out of memory for converted argument list\n");
36 for (i
= 0; i
< argc
; i
++) {
38 * XXX = use WC_ERR_INVALID_CHARS rather than 0, and fail if
39 * the argument isn't valid UTF-16?
44 width
= WideCharToMultiByte(CP_UTF8
, 0, wc_argv
[i
], -1, NULL
, 0,
47 fprintf(stderr
, "WideCharToMultiByte failed: %d\n",
51 utf8_string
= malloc(width
);
52 if (utf8_string
== NULL
) {
54 "Out of memory for converted argument list\n");
57 if (WideCharToMultiByte(CP_UTF8
, 0, wc_argv
[i
], -1, utf8_string
,
58 width
, NULL
, NULL
) == 0) {
59 fprintf(stderr
, "WideCharToMultiByte failed: %d\n",
63 argv
[i
] = utf8_string
;
67 * The original "main" routine was renamed to "real_main" via a macro in
68 * the cli_main.h header file since either "main" or "wmain" can be
69 * defined on Windows, but not both.
71 return_code
= real_main(argc
, argv
);
72 for (i
= 0; i
< argc
; i
++) {