2 * Reports capture file type
5 * Copyright 2004 Ian Schorr
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
15 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
23 #include <wsutil/ws_getopt.h>
27 #include <wiretap/wtap.h>
29 #include <wsutil/cmdarg_err.h>
30 #include <wsutil/file_util.h>
31 #include <wsutil/filesystem.h>
32 #include <wsutil/privileges.h>
34 #include <wsutil/version_info.h>
37 #include <wsutil/plugins.h>
40 #include <wsutil/report_message.h>
41 #include <wsutil/str_util.h>
42 #include <wsutil/wslog.h>
44 #include "ui/failure_message.h"
47 print_usage(FILE *output
)
49 fprintf(output
, "\n");
50 fprintf(output
, "Usage: captype [options] <infile> ...\n");
51 fprintf(output
, "\n");
52 fprintf(output
, "Miscellaneous:\n");
53 fprintf(output
, " -h, --help display this help and exit\n");
54 fprintf(output
, " -v, --version display version info and exit\n");
58 * Report an error in command-line arguments.
61 captype_cmdarg_err(const char *msg_format
, va_list ap
)
63 fprintf(stderr
, "captype: ");
64 vfprintf(stderr
, msg_format
, ap
);
65 fprintf(stderr
, "\n");
69 * Report additional information for an error in command-line arguments.
72 captype_cmdarg_err_cont(const char *msg_format
, va_list ap
)
74 vfprintf(stderr
, msg_format
, ap
);
75 fprintf(stderr
, "\n");
79 main(int argc
, char *argv
[])
81 char *configuration_init_error
;
82 static const struct report_message_routines captype_report_routines
= {
87 write_failure_message
,
88 cfile_open_failure_message
,
89 cfile_dump_open_failure_message
,
90 cfile_read_failure_message
,
91 cfile_write_failure_message
,
92 cfile_close_failure_message
99 int overall_error_status
;
100 static const struct ws_option long_options
[] = {
101 {"help", ws_no_argument
, NULL
, 'h'},
102 {"version", ws_no_argument
, NULL
, 'v'},
107 * Set the C-language locale to the native environment and set the
108 * code page to UTF-8 on Windows.
111 setlocale(LC_ALL
, ".UTF-8");
113 setlocale(LC_ALL
, "");
116 cmdarg_err_init(captype_cmdarg_err
, captype_cmdarg_err_cont
);
118 /* Initialize log handler early so we can have proper logging during startup. */
119 ws_log_init("captype", vcmdarg_err
);
121 /* Early logging command-line initialization. */
122 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, 1);
124 ws_noisy("Finished log init and parsing command line log arguments");
126 /* Initialize the version information. */
127 ws_init_version_info("Captype", NULL
, NULL
);
130 create_app_running_mutex();
134 * Get credential information for later use.
136 init_process_policies();
139 * Attempt to get the pathname of the directory containing the
142 configuration_init_error
= configuration_init(argv
[0], NULL
);
143 if (configuration_init_error
!= NULL
) {
145 "captype: Can't get pathname of directory containing the captype program: %s.\n",
146 configuration_init_error
);
147 g_free(configuration_init_error
);
150 init_report_message("captype", &captype_report_routines
);
154 /* Process the options */
155 while ((opt
= ws_getopt_long(argc
, argv
, "hv", long_options
, NULL
)) !=-1) {
160 show_help_header("Print the file types of capture files.");
170 case '?': /* Bad flag - print usage message */
182 overall_error_status
= 0;
184 for (i
= 1; i
< argc
; i
++) {
185 wth
= wtap_open_offline(argv
[i
], WTAP_TYPE_AUTO
, &err
, &err_info
, FALSE
);
188 printf("%s: %s\n", argv
[i
], wtap_file_type_subtype_name(wtap_file_type_subtype(wth
)));
191 if (err
== WTAP_ERR_FILE_UNKNOWN_FORMAT
)
192 printf("%s: unknown\n", argv
[i
]);
194 cfile_open_failure_message(argv
[i
], err
, err_info
);
195 overall_error_status
= 2; /* remember that an error has occurred */
203 return overall_error_status
;