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