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