TODO decrypt_krb5_data => proto_tree_add_expert_format cryptotvb TODO: decrypted_tvb
[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  * SPDX-License-Identifier: GPL-2.0-or-later
12  */
13
14 #include <config.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <locale.h>
21 #include <errno.h>
22
23 #ifdef HAVE_GETOPT_H
24 #include <getopt.h>
25 #endif
26
27 #include <glib.h>
28
29 #include <wiretap/wtap.h>
30
31 #include <ui/cmdarg_err.h>
32 #include <wsutil/file_util.h>
33 #include <wsutil/filesystem.h>
34 #include <wsutil/privileges.h>
35 #include <cli_main.h>
36 #include <version_info.h>
37
38 #ifdef HAVE_PLUGINS
39 #include <wsutil/plugins.h>
40 #endif
41
42 #include <wsutil/report_message.h>
43 #include <wsutil/str_util.h>
44
45 #ifndef HAVE_GETOPT_LONG
46 #include "wsutil/wsgetopt.h"
47 #endif
48
49 #include "ui/failure_message.h"
50
51 static void
52 print_usage(FILE *output)
53 {
54   fprintf(output, "\n");
55   fprintf(output, "Usage: captype <infile> ...\n");
56 }
57
58 /*
59  * General errors and warnings are reported with an console message
60  * in captype.
61  */
62 static void
63 failure_warning_message(const char *msg_format, va_list ap)
64 {
65   fprintf(stderr, "captype: ");
66   vfprintf(stderr, msg_format, ap);
67   fprintf(stderr, "\n");
68 }
69
70 /*
71  * Report additional information for an error in command-line arguments.
72  */
73 static void
74 failure_message_cont(const char *msg_format, va_list ap)
75 {
76   vfprintf(stderr, msg_format, ap);
77   fprintf(stderr, "\n");
78 }
79
80 int
81 main(int argc, char *argv[])
82 {
83   char  *init_progfile_dir_error;
84   wtap  *wth;
85   int    err;
86   gchar *err_info;
87   int    i;
88   int    opt;
89   int    overall_error_status;
90   static const struct option long_options[] = {
91       {"help", no_argument, NULL, 'h'},
92       {"version", no_argument, NULL, 'v'},
93       {0, 0, 0, 0 }
94   };
95
96   /* Set the C-language locale to the native environment. */
97   setlocale(LC_ALL, "");
98
99   cmdarg_err_init(failure_warning_message, failure_message_cont);
100
101   /* Initialize the version information. */
102   ws_init_version_info("Captype (Wireshark)", NULL, NULL, NULL);
103
104 #ifdef _WIN32
105   create_app_running_mutex();
106 #endif /* _WIN32 */
107
108   /*
109    * Get credential information for later use.
110    */
111   init_process_policies();
112
113   /*
114    * Attempt to get the pathname of the directory containing the
115    * executable file.
116    */
117   init_progfile_dir_error = init_progfile_dir(argv[0]);
118   if (init_progfile_dir_error != NULL) {
119     fprintf(stderr,
120             "captype: Can't get pathname of directory containing the captype program: %s.\n",
121             init_progfile_dir_error);
122     g_free(init_progfile_dir_error);
123   }
124
125   init_report_message(failure_warning_message, failure_warning_message,
126                       NULL, NULL, NULL);
127
128   wtap_init(TRUE);
129
130   /* Process the options */
131   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
132
133     switch (opt) {
134
135       case 'h':
136         show_help_header("Print the file types of capture files.");
137         print_usage(stdout);
138         exit(0);
139         break;
140
141       case 'v':
142         show_version();
143         exit(0);
144         break;
145
146       case '?':              /* Bad flag - print usage message */
147         print_usage(stderr);
148         exit(1);
149         break;
150     }
151   }
152
153   if (argc < 2) {
154     print_usage(stderr);
155     return 1;
156   }
157
158   overall_error_status = 0;
159
160   for (i = 1; i < argc; i++) {
161     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
162
163     if(wth) {
164       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
165       wtap_close(wth);
166     } else {
167       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
168         printf("%s: unknown\n", argv[i]);
169       else {
170         cfile_open_failure_message("captype", argv[i], err, err_info);
171         overall_error_status = 2; /* remember that an error has occurred */
172       }
173     }
174
175   }
176
177   wtap_cleanup();
178   free_progdirs();
179   return overall_error_status;
180 }
181
182 /*
183  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
184  *
185  * Local variables:
186  * c-basic-offset: 2
187  * tab-width: 8
188  * indent-tabs-mode: nil
189  * End:
190  *
191  * vi: set shiftwidth=2 tabstop=8 expandtab:
192  * :indentSize=2:tabSize=8:noTabs=true:
193  */