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