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