Revert "Refactor Wiretap"
[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_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42
43 #include <glib.h>
44
45 #include <wsutil/privileges.h>
46 #include <wsutil/filesystem.h>
47 #include <wsutil/file_util.h>
48
49 #ifdef HAVE_PLUGINS
50 #include <wsutil/plugins.h>
51 #endif
52
53 #include "wtap.h"
54 #include <wsutil/report_err.h>
55 #include <wsutil/privileges.h>
56 #include <wsutil/str_util.h>
57
58 #ifdef _WIN32
59 #include <wsutil/unicode-utils.h>
60 #endif /* _WIN32 */
61
62 #include "version.h"
63
64 static void
65 usage(void)
66 {
67   fprintf(stderr, "Captype %s"
68 #ifdef GITVERSION
69       " (" GITVERSION " from " GITBRANCH ")"
70 #endif
71       "\n", VERSION);
72   fprintf(stderr, "Prints the file types of capture files.\n");
73   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
74   fprintf(stderr, "\n");
75   fprintf(stderr, "Usage: captype <infile> ...\n");
76 }
77
78 #ifdef HAVE_PLUGINS
79 /*
80  *  Don't report failures to load plugins because most (non-wiretap) plugins
81  *  *should* fail to load (because we're not linked against libwireshark and
82  *  dissector plugins need libwireshark).
83  */
84 static void
85 failure_message(const char *msg_format _U_, va_list ap _U_)
86 {
87   return;
88 }
89 #endif
90
91 int
92 main(int argc, char *argv[])
93 {
94   wtap  *wth;
95   int    err;
96   gchar *err_info;
97   int    i;
98   int    overall_error_status;
99
100 #ifdef HAVE_PLUGINS
101   char  *init_progfile_dir_error;
102 #endif
103
104 #ifdef _WIN32
105   arg_list_utf_16to8(argc, argv);
106   create_app_running_mutex();
107 #endif /* _WIN32 */
108
109   /*
110    * Get credential information for later use.
111    */
112   init_process_policies();
113   init_open_routines();
114
115 #ifdef HAVE_PLUGINS
116   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
117     g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
118     g_free(init_progfile_dir_error);
119   } else {
120     /* Register all the plugin types we have. */
121     wtap_register_plugin_types(); /* Types known to libwiretap */
122
123     init_report_err(failure_message,NULL,NULL,NULL);
124
125     /* Scan for plugins.  This does *not* call their registration routines;
126        that's done later. */
127     scan_plugins();
128
129     /* Register all libwiretap plugin modules. */
130     register_all_wiretap_modules();
131   }
132 #endif
133
134   /* Set the C-language locale to the native environment. */
135   setlocale(LC_ALL, "");
136
137   if (argc < 2) {
138     usage();
139     return 1;
140   }
141
142   overall_error_status = 0;
143
144   for (i = 1; i < argc; i++) {
145     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
146
147     if(wth) {
148       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
149       wtap_close(wth);
150     } else {
151       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
152         printf("%s: unknown\n", argv[i]);
153       else {
154         fprintf(stderr, "captype: Can't open %s: %s\n", argv[i],
155                 wtap_strerror(err));
156         switch (err) {
157
158         case WTAP_ERR_UNSUPPORTED:
159         case WTAP_ERR_UNSUPPORTED_ENCAP:
160         case WTAP_ERR_BAD_FILE:
161           fprintf(stderr, "(%s)\n", err_info);
162           g_free(err_info);
163           break;
164         }
165         overall_error_status = 1; /* remember that an error has occurred */
166       }
167     }
168
169   }
170
171   return overall_error_status;
172 }
173
174 /*
175  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
176  *
177  * Local variables:
178  * c-basic-offset: 2
179  * tab-width: 8
180  * indent-tabs-mode: nil
181  * End:
182  *
183  * vi: set shiftwidth=2 tabstop=8 expandtab:
184  * :indentSize=2:tabSize=8:noTabs=true:
185  */