Qt: Fix leak on CaptureFileDialog preview of file with errors
[wireshark.git] / cli_main.c
bloba478e3d339310ce2f0e620ab81b6a0bcbeee40a6
1 /*
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
17 #include "cli_main.h"
19 #ifdef _WIN32
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <windows.h>
24 int
25 wmain(int argc, wchar_t *wc_argv[])
27 char **argv;
28 int i;
29 int return_code;
31 argv = (char **)malloc((argc + 1) * sizeof(char *));
32 if (argv == NULL) {
33 fprintf(stderr, "Out of memory for converted argument list\n");
34 return 2;
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?
41 int width;
42 char *utf8_string;
44 width = WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, NULL, 0,
45 NULL, NULL);
46 if (width == 0) {
47 fprintf(stderr, "WideCharToMultiByte failed: %d\n",
48 width);
49 return 2;
51 utf8_string = malloc(width);
52 if (utf8_string == NULL) {
53 fprintf(stderr,
54 "Out of memory for converted argument list\n");
55 return 2;
57 if (WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, utf8_string,
58 width, NULL, NULL) == 0) {
59 fprintf(stderr, "WideCharToMultiByte failed: %d\n",
60 width);
61 return 2;
63 argv[i] = utf8_string;
65 argv[i] = NULL;
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++) {
73 free(argv[i]);
75 free(argv);
76 return return_code;
78 #endif