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/str_util.h>
41 #include <wsutil/wslog.h>
43 #include "ui/failure_message.h"
46 print_usage(FILE *output
)
48 fprintf(output
, "\n");
49 fprintf(output
, "Usage: captype [options] <infile> ...\n");
50 fprintf(output
, "\n");
51 fprintf(output
, "Miscellaneous:\n");
52 fprintf(output
, " -h, --help display this help and exit\n");
53 fprintf(output
, " -v, --version display version info and exit\n");
57 * Report an error in command-line arguments.
60 captype_cmdarg_err(const char *msg_format
, va_list ap
)
62 fprintf(stderr
, "captype: ");
63 vfprintf(stderr
, msg_format
, ap
);
64 fprintf(stderr
, "\n");
68 * Report additional information for an error in command-line arguments.
71 captype_cmdarg_err_cont(const char *msg_format
, va_list ap
)
73 vfprintf(stderr
, msg_format
, ap
);
74 fprintf(stderr
, "\n");
78 main(int argc
, char *argv
[])
80 char *configuration_init_error
;
86 int overall_error_status
;
87 static const struct ws_option long_options
[] = {
88 {"help", ws_no_argument
, NULL
, 'h'},
89 {"version", ws_no_argument
, NULL
, 'v'},
94 * Set the C-language locale to the native environment and set the
95 * code page to UTF-8 on Windows.
98 setlocale(LC_ALL
, ".UTF-8");
100 setlocale(LC_ALL
, "");
103 cmdarg_err_init(captype_cmdarg_err
, captype_cmdarg_err_cont
);
105 /* Initialize log handler early so we can have proper logging during startup. */
106 ws_log_init("captype", vcmdarg_err
);
108 /* Early logging command-line initialization. */
109 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, 1);
111 ws_noisy("Finished log init and parsing command line log arguments");
113 /* Initialize the version information. */
114 ws_init_version_info("Captype", NULL
, NULL
);
117 create_app_running_mutex();
121 * Get credential information for later use.
123 init_process_policies();
126 * Attempt to get the pathname of the directory containing the
129 configuration_init_error
= configuration_init(argv
[0], NULL
);
130 if (configuration_init_error
!= NULL
) {
132 "captype: Can't get pathname of directory containing the captype program: %s.\n",
133 configuration_init_error
);
134 g_free(configuration_init_error
);
137 init_report_failure_message("captype");
141 /* Process the options */
142 while ((opt
= ws_getopt_long(argc
, argv
, "hv", long_options
, NULL
)) !=-1) {
147 show_help_header("Print the file types of capture files.");
157 case '?': /* Bad flag - print usage message */
169 overall_error_status
= 0;
171 for (i
= 1; i
< argc
; i
++) {
172 wth
= wtap_open_offline(argv
[i
], WTAP_TYPE_AUTO
, &err
, &err_info
, false);
175 printf("%s: %s\n", argv
[i
], wtap_file_type_subtype_name(wtap_file_type_subtype(wth
)));
178 if (err
== WTAP_ERR_FILE_UNKNOWN_FORMAT
)
179 printf("%s: unknown\n", argv
[i
]);
181 cfile_open_failure_message(argv
[i
], err
, err_info
);
182 overall_error_status
= 2; /* remember that an error has occurred */
190 return overall_error_status
;