more
[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 static int
85 real_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   create_app_running_mutex();
125 #endif /* _WIN32 */
126
127   /*
128    * Get credential information for later use.
129    */
130   init_process_policies();
131
132   /*
133    * Attempt to get the pathname of the directory containing the
134    * executable file.
135    */
136   init_progfile_dir_error = init_progfile_dir(argv[0]);
137   if (init_progfile_dir_error != NULL) {
138     fprintf(stderr,
139             "captype: Can't get pathname of directory containing the captype program: %s.\n",
140             init_progfile_dir_error);
141     g_free(init_progfile_dir_error);
142   }
143
144   init_report_message(failure_warning_message, failure_warning_message,
145                       NULL, NULL, NULL);
146
147   wtap_init(TRUE);
148
149   /* Process the options */
150   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
151
152     switch (opt) {
153
154       case 'h':
155         printf("Captype (Wireshark) %s\n"
156                "Print the file types of capture files.\n"
157                "See https://www.wireshark.org for more information.\n",
158                get_ws_vcs_version_info());
159         print_usage(stdout);
160         exit(0);
161         break;
162
163       case 'v':
164         comp_info_str = get_compiled_version_info(NULL, NULL);
165         runtime_info_str = get_runtime_version_info(NULL);
166         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
167         g_string_free(comp_info_str, TRUE);
168         g_string_free(runtime_info_str, TRUE);
169         exit(0);
170         break;
171
172       case '?':              /* Bad flag - print usage message */
173         print_usage(stderr);
174         exit(1);
175         break;
176     }
177   }
178
179   if (argc < 2) {
180     print_usage(stderr);
181     return 1;
182   }
183
184   overall_error_status = 0;
185
186   for (i = 1; i < argc; i++) {
187     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
188
189     if(wth) {
190       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
191       wtap_close(wth);
192     } else {
193       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
194         printf("%s: unknown\n", argv[i]);
195       else {
196         cfile_open_failure_message("captype", argv[i], err, err_info);
197         overall_error_status = 2; /* remember that an error has occurred */
198       }
199     }
200
201   }
202
203   wtap_cleanup();
204   free_progdirs();
205   return overall_error_status;
206 }
207
208 #ifdef _WIN32
209 int
210 wmain(int argc, wchar_t *wc_argv[])
211 {
212   char **argv;
213
214   argv = arg_list_utf_16to8(argc, wc_argv);
215   return real_main(argc, argv);
216 }
217 #else
218 int
219 main(int argc, char *argv[])
220 {
221   return real_main(argc, argv);
222 }
223 #endif
224
225 /*
226  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
227  *
228  * Local variables:
229  * c-basic-offset: 2
230  * tab-width: 8
231  * indent-tabs-mode: nil
232  * End:
233  *
234  * vi: set shiftwidth=2 tabstop=8 expandtab:
235  * :indentSize=2:tabSize=8:noTabs=true:
236  */