Qt: Fix leak on CaptureFileDialog preview of file with errors
[wireshark.git] / captype.c
blob080a543307a9146c3b8b16a955402bb287a728a0
1 /* captype.c
2 * Reports capture file type
4 * Based on capinfos.c
5 * Copyright 2004 Ian Schorr
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <config.h>
15 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include <locale.h>
23 #include <wsutil/ws_getopt.h>
25 #include <glib.h>
27 #include <wiretap/wtap.h>
29 #include <wsutil/cmdarg_err.h>
30 #include <wsutil/file_util.h>
31 #include <wsutil/filesystem.h>
32 #include <wsutil/privileges.h>
33 #include <cli_main.h>
34 #include <wsutil/version_info.h>
36 #ifdef HAVE_PLUGINS
37 #include <wsutil/plugins.h>
38 #endif
40 #include <wsutil/report_message.h>
41 #include <wsutil/str_util.h>
42 #include <wsutil/wslog.h>
44 #include "ui/failure_message.h"
46 static void
47 print_usage(FILE *output)
49 fprintf(output, "\n");
50 fprintf(output, "Usage: captype [options] <infile> ...\n");
51 fprintf(output, "\n");
52 fprintf(output, "Miscellaneous:\n");
53 fprintf(output, " -h, --help display this help and exit\n");
54 fprintf(output, " -v, --version display version info and exit\n");
58 * Report an error in command-line arguments.
60 static void
61 captype_cmdarg_err(const char *msg_format, va_list ap)
63 fprintf(stderr, "captype: ");
64 vfprintf(stderr, msg_format, ap);
65 fprintf(stderr, "\n");
69 * Report additional information for an error in command-line arguments.
71 static void
72 captype_cmdarg_err_cont(const char *msg_format, va_list ap)
74 vfprintf(stderr, msg_format, ap);
75 fprintf(stderr, "\n");
78 int
79 main(int argc, char *argv[])
81 char *configuration_init_error;
82 static const struct report_message_routines captype_report_routines = {
83 failure_message,
84 failure_message,
85 open_failure_message,
86 read_failure_message,
87 write_failure_message,
88 cfile_open_failure_message,
89 cfile_dump_open_failure_message,
90 cfile_read_failure_message,
91 cfile_write_failure_message,
92 cfile_close_failure_message
94 wtap *wth;
95 int err;
96 gchar *err_info;
97 int i;
98 int opt;
99 int overall_error_status;
100 static const struct ws_option long_options[] = {
101 {"help", ws_no_argument, NULL, 'h'},
102 {"version", ws_no_argument, NULL, 'v'},
103 {0, 0, 0, 0 }
107 * Set the C-language locale to the native environment and set the
108 * code page to UTF-8 on Windows.
110 #ifdef _WIN32
111 setlocale(LC_ALL, ".UTF-8");
112 #else
113 setlocale(LC_ALL, "");
114 #endif
116 cmdarg_err_init(captype_cmdarg_err, captype_cmdarg_err_cont);
118 /* Initialize log handler early so we can have proper logging during startup. */
119 ws_log_init("captype", vcmdarg_err);
121 /* Early logging command-line initialization. */
122 ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
124 ws_noisy("Finished log init and parsing command line log arguments");
126 /* Initialize the version information. */
127 ws_init_version_info("Captype", NULL, NULL);
129 #ifdef _WIN32
130 create_app_running_mutex();
131 #endif /* _WIN32 */
134 * Get credential information for later use.
136 init_process_policies();
139 * Attempt to get the pathname of the directory containing the
140 * executable file.
142 configuration_init_error = configuration_init(argv[0], NULL);
143 if (configuration_init_error != NULL) {
144 fprintf(stderr,
145 "captype: Can't get pathname of directory containing the captype program: %s.\n",
146 configuration_init_error);
147 g_free(configuration_init_error);
150 init_report_message("captype", &captype_report_routines);
152 wtap_init(TRUE);
154 /* Process the options */
155 while ((opt = ws_getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
157 switch (opt) {
159 case 'h':
160 show_help_header("Print the file types of capture files.");
161 print_usage(stdout);
162 exit(0);
163 break;
165 case 'v':
166 show_version();
167 exit(0);
168 break;
170 case '?': /* Bad flag - print usage message */
171 print_usage(stderr);
172 exit(1);
173 break;
177 if (argc < 2) {
178 print_usage(stderr);
179 return 1;
182 overall_error_status = 0;
184 for (i = 1; i < argc; i++) {
185 wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
187 if(wth) {
188 printf("%s: %s\n", argv[i], wtap_file_type_subtype_name(wtap_file_type_subtype(wth)));
189 wtap_close(wth);
190 } else {
191 if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
192 printf("%s: unknown\n", argv[i]);
193 else {
194 cfile_open_failure_message(argv[i], err, err_info);
195 overall_error_status = 2; /* remember that an error has occurred */
201 wtap_cleanup();
202 free_progdirs();
203 return overall_error_status;