Update Travis to Trusty
[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  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <locale.h>
33 #include <errno.h>
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_GETOPT_H
40 #include <getopt.h>
41 #endif
42
43 #ifdef HAVE_LIBZ
44 #include <zlib.h>     /* to get the libz version number */
45 #endif
46
47 #include <glib.h>
48
49 #include <wsutil/crash_info.h>
50 #include <wsutil/file_util.h>
51 #include <wsutil/filesystem.h>
52 #include <wsutil/privileges.h>
53 #include <wsutil/ws_diag_control.h>
54 #include <wsutil/ws_version_info.h>
55
56 #ifdef HAVE_PLUGINS
57 #include <wsutil/plugins.h>
58 #endif
59
60 #include "wtap.h"
61 #include <wsutil/report_err.h>
62 #include <wsutil/str_util.h>
63
64 #ifdef _WIN32
65 #include <wsutil/unicode-utils.h>
66 #endif /* _WIN32 */
67
68 #ifndef HAVE_GETOPT_LONG
69 #include "wsutil/wsgetopt.h"
70 #endif
71
72 static void
73 print_usage(FILE *output)
74 {
75   fprintf(output, "\n");
76   fprintf(output, "Usage: captype <infile> ...\n");
77 }
78
79 #ifdef HAVE_PLUGINS
80 /*
81  *  Don't report failures to load plugins because most (non-wiretap) plugins
82  *  *should* fail to load (because we're not linked against libwireshark and
83  *  dissector plugins need libwireshark).
84  */
85 static void
86 failure_message(const char *msg_format _U_, va_list ap _U_)
87 {
88   return;
89 }
90 #endif
91
92 static void
93 get_captype_compiled_info(GString *str)
94 {
95   /* LIBZ */
96   g_string_append(str, ", ");
97 #ifdef HAVE_LIBZ
98   g_string_append(str, "with libz ");
99 #ifdef ZLIB_VERSION
100   g_string_append(str, ZLIB_VERSION);
101 #else /* ZLIB_VERSION */
102   g_string_append(str, "(version unknown)");
103 #endif /* ZLIB_VERSION */
104 #else /* HAVE_LIBZ */
105   g_string_append(str, "without libz");
106 #endif /* HAVE_LIBZ */
107 }
108
109 static void
110 get_captype_runtime_info(GString *str)
111 {
112   /* zlib */
113 #if defined(HAVE_LIBZ) && !defined(_WIN32)
114   g_string_append_printf(str, ", with libz %s", zlibVersion());
115 #endif
116 }
117
118 int
119 main(int argc, char *argv[])
120 {
121   GString *comp_info_str;
122   GString *runtime_info_str;
123   wtap  *wth;
124   int    err;
125   gchar *err_info;
126   int    i;
127   int    opt;
128   int    overall_error_status;
129 DIAG_OFF(cast-qual)
130   static const struct option long_options[] = {
131       {(char *)"help", no_argument, NULL, 'h'},
132       {(char *)"version", no_argument, NULL, 'v'},
133       {0, 0, 0, 0 }
134   };
135 DIAG_ON(cast-qual)
136
137 #ifdef HAVE_PLUGINS
138   char  *init_progfile_dir_error;
139 #endif
140
141   /* Set the C-language locale to the native environment. */
142   setlocale(LC_ALL, "");
143
144   /* Get the compile-time version information string */
145   comp_info_str = get_compiled_version_info(NULL, get_captype_compiled_info);
146
147   /* Get the run-time version information string */
148   runtime_info_str = get_runtime_version_info(get_captype_runtime_info);
149
150   /* Add it to the information to be reported on a crash. */
151   ws_add_crash_info("Captype (Wireshark) %s\n"
152          "\n"
153          "%s"
154          "\n"
155          "%s",
156       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
157
158 #ifdef _WIN32
159   arg_list_utf_16to8(argc, argv);
160   create_app_running_mutex();
161 #endif /* _WIN32 */
162
163   /*
164    * Get credential information for later use.
165    */
166   init_process_policies();
167   init_open_routines();
168
169 #ifdef HAVE_PLUGINS
170   if ((init_progfile_dir_error = init_progfile_dir(argv[0], (void *)main))) {
171     g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
172     g_free(init_progfile_dir_error);
173   } else {
174     /* Register all the plugin types we have. */
175     wtap_register_plugin_types(); /* Types known to libwiretap */
176
177     init_report_err(failure_message,NULL,NULL,NULL);
178
179     /* Scan for plugins.  This does *not* call their registration routines;
180        that's done later. */
181     scan_plugins();
182
183     /* Register all libwiretap plugin modules. */
184     register_all_wiretap_modules();
185   }
186 #endif
187
188   /* Process the options */
189   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
190
191     switch (opt) {
192
193       case 'h':
194         printf("Captype (Wireshark) %s\n"
195                "Print the file types of capture files.\n"
196                "See http://www.wireshark.org for more information.\n",
197                get_ws_vcs_version_info());
198         print_usage(stdout);
199         exit(0);
200         break;
201
202       case 'v':
203         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
204         g_string_free(comp_info_str, TRUE);
205         g_string_free(runtime_info_str, TRUE);
206         exit(0);
207         break;
208
209       case '?':              /* Bad flag - print usage message */
210         print_usage(stderr);
211         exit(1);
212         break;
213     }
214   }
215
216   if (argc < 2) {
217     print_usage(stderr);
218     return 1;
219   }
220
221   overall_error_status = 0;
222
223   for (i = 1; i < argc; i++) {
224     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
225
226     if(wth) {
227       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
228       wtap_close(wth);
229     } else {
230       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
231         printf("%s: unknown\n", argv[i]);
232       else {
233         fprintf(stderr, "captype: Can't open %s: %s\n", argv[i],
234                 wtap_strerror(err));
235         if (err_info != NULL) {
236           fprintf(stderr, "(%s)\n", err_info);
237           g_free(err_info);
238         }
239         overall_error_status = 1; /* remember that an error has occurred */
240       }
241     }
242
243   }
244
245   return overall_error_status;
246 }
247
248 /*
249  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
250  *
251  * Local variables:
252  * c-basic-offset: 2
253  * tab-width: 8
254  * indent-tabs-mode: nil
255  * End:
256  *
257  * vi: set shiftwidth=2 tabstop=8 expandtab:
258  * :indentSize=2:tabSize=8:noTabs=true:
259  */