Qt: fix memleaks on opening a context menu
[metze/wireshark/wip.git] / captype.c
1 /* captype.c
2  * Reports capture file type
3  *
4  * Based on capinfos.c
5  * Copyright 2004 Ian Schorr
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * SPDX-License-Identifier: GPL-2.0-or-later
12  */
13
14 #include <config.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <locale.h>
21 #include <errno.h>
22
23 #ifdef HAVE_GETOPT_H
24 #include <getopt.h>
25 #endif
26
27 #include <glib.h>
28
29 #include <wiretap/wtap.h>
30
31 #include <wsutil/cmdarg_err.h>
32 #include <wsutil/crash_info.h>
33 #include <wsutil/file_util.h>
34 #include <wsutil/filesystem.h>
35 #include <wsutil/privileges.h>
36 #include <version_info.h>
37
38 #ifdef HAVE_PLUGINS
39 #include <wsutil/plugins.h>
40 #endif
41
42 #include <wsutil/report_message.h>
43 #include <wsutil/str_util.h>
44
45 #ifdef _WIN32
46 #include <wsutil/unicode-utils.h>
47 #endif /* _WIN32 */
48
49 #ifndef HAVE_GETOPT_LONG
50 #include "wsutil/wsgetopt.h"
51 #endif
52
53 #include "ui/failure_message.h"
54
55 static void
56 print_usage(FILE *output)
57 {
58   fprintf(output, "\n");
59   fprintf(output, "Usage: captype <infile> ...\n");
60 }
61
62 /*
63  * General errors and warnings are reported with an console message
64  * in captype.
65  */
66 static void
67 failure_warning_message(const char *msg_format, va_list ap)
68 {
69   fprintf(stderr, "captype: ");
70   vfprintf(stderr, msg_format, ap);
71   fprintf(stderr, "\n");
72 }
73
74 /*
75  * Report additional information for an error in command-line arguments.
76  */
77 static void
78 failure_message_cont(const char *msg_format, va_list ap)
79 {
80   vfprintf(stderr, msg_format, ap);
81   fprintf(stderr, "\n");
82 }
83
84 int
85 main(int argc, char *argv[])
86 {
87   GString *comp_info_str;
88   GString *runtime_info_str;
89   char  *init_progfile_dir_error;
90   wtap  *wth;
91   int    err;
92   gchar *err_info;
93   int    i;
94   int    opt;
95   int    overall_error_status;
96   static const struct option long_options[] = {
97       {"help", no_argument, NULL, 'h'},
98       {"version", no_argument, NULL, 'v'},
99       {0, 0, 0, 0 }
100   };
101
102   /* Set the C-language locale to the native environment. */
103   setlocale(LC_ALL, "");
104
105   cmdarg_err_init(failure_warning_message, failure_message_cont);
106
107   /* Get the compile-time version information string */
108   comp_info_str = get_compiled_version_info(NULL, NULL);
109
110   /* Get the run-time version information string */
111   runtime_info_str = get_runtime_version_info(NULL);
112
113   /* Add it to the information to be reported on a crash. */
114   ws_add_crash_info("Captype (Wireshark) %s\n"
115          "\n"
116          "%s"
117          "\n"
118          "%s",
119       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
120   g_string_free(comp_info_str, TRUE);
121   g_string_free(runtime_info_str, TRUE);
122
123 #ifdef _WIN32
124   arg_list_utf_16to8(argc, argv);
125   create_app_running_mutex();
126 #endif /* _WIN32 */
127
128   /*
129    * Get credential information for later use.
130    */
131   init_process_policies();
132
133   /*
134    * Attempt to get the pathname of the directory containing the
135    * executable file.
136    */
137   init_progfile_dir_error = init_progfile_dir(argv[0]);
138   if (init_progfile_dir_error != NULL) {
139     fprintf(stderr,
140             "captype: Can't get pathname of directory containing the captype program: %s.\n",
141             init_progfile_dir_error);
142     g_free(init_progfile_dir_error);
143   }
144
145   init_report_message(failure_warning_message, failure_warning_message,
146                       NULL, NULL, NULL);
147
148   wtap_init(TRUE);
149
150   /* Process the options */
151   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
152
153     switch (opt) {
154
155       case 'h':
156         printf("Captype (Wireshark) %s\n"
157                "Print the file types of capture files.\n"
158                "See https://www.wireshark.org for more information.\n",
159                get_ws_vcs_version_info());
160         print_usage(stdout);
161         exit(0);
162         break;
163
164       case 'v':
165         comp_info_str = get_compiled_version_info(NULL, NULL);
166         runtime_info_str = get_runtime_version_info(NULL);
167         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
168         g_string_free(comp_info_str, TRUE);
169         g_string_free(runtime_info_str, TRUE);
170         exit(0);
171         break;
172
173       case '?':              /* Bad flag - print usage message */
174         print_usage(stderr);
175         exit(1);
176         break;
177     }
178   }
179
180   if (argc < 2) {
181     print_usage(stderr);
182     return 1;
183   }
184
185   overall_error_status = 0;
186
187   for (i = 1; i < argc; i++) {
188     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
189
190     if(wth) {
191       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
192       wtap_close(wth);
193     } else {
194       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
195         printf("%s: unknown\n", argv[i]);
196       else {
197         cfile_open_failure_message("captype", argv[i], err, err_info);
198         overall_error_status = 2; /* remember that an error has occurred */
199       }
200     }
201
202   }
203
204   wtap_cleanup();
205   free_progdirs();
206   return overall_error_status;
207 }
208
209 /*
210  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
211  *
212  * Local variables:
213  * c-basic-offset: 2
214  * tab-width: 8
215  * indent-tabs-mode: nil
216  * End:
217  *
218  * vi: set shiftwidth=2 tabstop=8 expandtab:
219  * :indentSize=2:tabSize=8:noTabs=true:
220  */