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[])
30 argv = (char **)malloc((argc + 1) * sizeof(char *));
32 fprintf(stderr, "Out of memory for converted argument list\n");
35 for (i = 0; i < argc; i++) {
37 * XXX = use WC_ERR_INVALID_CHARS rather than 0, and fail if
38 * the argument isn't valid UTF-16?
43 width = WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, NULL, 0,
46 fprintf(stderr, "WideCharToMultiByte failed: %d\n",
50 utf8_string = malloc(width);
51 if (utf8_string == NULL) {
53 "Out of memory for converted argument list\n");
56 if (WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, utf8_string,
57 width, NULL, NULL) == 0) {
58 fprintf(stderr, "WideCharToMultiByte failed: %d\n",
62 argv[i] = utf8_string;
66 * The original "main" routine was renamed to "real_main" via a macro in
67 * the cli_main.h header file since either "main" or "wmain" can be
68 * defined on Windows, but not both.
70 return real_main(argc, argv);