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