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