1 /* failure_message_simple.c
2 * Routines to print various "standard" failure messages used in multiple
5 * This is a "simple" version that does not link with libwiretap and interpret
6 * the WTAP_ERR_ or WTAP_FILE_TYPE_SUBTYPE_ values that are parameters to the
7 * capture file routines (cfile_*). It is for use in dumpcap, which does not
8 * link with libwiretap or libui. The libwiretap-related routines should not
9 * be called from dumpcap, but a rudimentary implementation is provided since
10 * wsutil/report_message expects them.
12 * Console programs that do link against libwiretap should include
13 * ui/failure_message.h instead.
15 * Wireshark - Network traffic analyzer
16 * By Gerald Combs <gerald@wireshark.org>
17 * Copyright 1998 Gerald Combs
19 * SPDX-License-Identifier: GPL-2.0-or-later
27 #include <wiretap/wtap.h>
28 #include <wsutil/filesystem.h>
29 #include <wsutil/report_message.h>
30 #include <wsutil/cmdarg_err.h>
32 #include "wsutil/failure_message_simple.h"
35 * Generic error message.
38 failure_message_simple(const char *msg_format
, va_list ap
)
40 vcmdarg_err(msg_format
, ap
);
44 * Error message for a failed attempt to open or create a file
45 * other than a capture file.
46 * "filename" is the name of the file being opened; "err" is assumed
47 * to be a UNIX-style errno; "for_writing" is true if we're opening
48 * the file for writing and false if we're opening it for reading.
51 open_failure_message_simple(const char *filename
, int err
, bool for_writing
)
53 cmdarg_err(file_open_error_message(err
, for_writing
), filename
);
57 * Error message for a failed attempt to read from a file other than
59 * "filename" is the name of the file being read from; "err" is assumed
60 * to be a UNIX-style errno.
63 read_failure_message_simple(const char *filename
, int err
)
65 cmdarg_err("An error occurred while reading from the file \"%s\": %s.",
66 filename
, g_strerror(err
));
70 * Error message for a failed attempt to write to a file other than
72 * "filename" is the name of the file being written to; "err" is assumed
73 * to be a UNIX-style errno.
76 write_failure_message_simple(const char *filename
, int err
)
78 cmdarg_err("An error occurred while writing to the file \"%s\": %s.",
79 filename
, g_strerror(err
));
83 * Error message for a failed attempt to rename a file other than
85 * "old_filename" is the name of the file being renamed; "new_filename"
86 * is the name to which it's being renamed; "err" is assumed to be a
90 rename_failure_message_simple(const char *old_filename
, const char *new_filename
,
93 cmdarg_err("An error occurred while renaming the file \"%s\" to \"%s\": %s.",
94 old_filename
, new_filename
, g_strerror(err
));
98 input_file_description(const char* fname
)
102 if (strcmp(fname
, "-") == 0) {
103 /* We're reading from the standard input */
104 fstring
= g_strdup("standard input");
106 /* We're reading from a file */
107 fstring
= ws_strdup_printf("file \"%s\"", fname
);
113 output_file_description(const char* fname
)
117 if (strcmp(fname
, "-") == 0) {
118 /* We're writing to the standard output */
119 fstring
= g_strdup("standard output");
121 /* We're writing to a file */
122 fstring
= ws_strdup_printf("file \"%s\"", fname
);
128 * Error message for a failed attempt to open a capture file for reading.
129 * "filename" is the name of the file being opened; "err" is assumed
130 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
131 * to be a string giving further information for some WTAP_ERR_ values.
134 cfile_open_failure_message_simple(const char* filename
, int err
, char* err_info
)
139 * Get a string that describes what we're opening.
141 char* file_description
= input_file_description(filename
);
143 cmdarg_err("The %s could not be opened: libwiretap error %i.",
144 file_description
, err
);
145 g_free(file_description
);
147 cmdarg_err(file_open_error_message(err
, false), filename
);
148 cmdarg_err_cont("This should not happen.");
153 * Error message for a failed attempt to open a capture file for writing.
154 * "filename" is the name of the file being opened; "err" is assumed
155 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
156 * to be a string giving further information for some WTAP_ERR_ values;
157 * "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type
158 * and subtype of file being opened.
161 cfile_dump_open_failure_message_simple(const char* filename
, int err
, char* err_info
,
162 int file_type_subtype _U_
)
167 * Get a string that describes what we're opening.
169 char* file_description
= output_file_description(filename
);
171 cmdarg_err("The %s could not be created: libwiretap error %i.",
172 file_description
, err
);
173 g_free(file_description
);
175 cmdarg_err(file_open_error_message(err
, true), filename
);
176 cmdarg_err_cont("This should not happen.");
181 * Error message for a failed attempt to read from a capture file.
182 * "filename" is the name of the file being opened; "err" is assumed
183 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
184 * to be a string giving further information for some WTAP_ERR_ values.
187 cfile_read_failure_message_simple(const char* filename
, int err
, char* err_info
)
191 /* Get a string that describes what we're reading from */
192 file_string
= input_file_description(filename
);
195 cmdarg_err("An error occurred while reading the %s: libwiretap error %i.",
198 cmdarg_err("An error occurred while reading the %s: %s.",
199 file_string
, g_strerror(err
));
201 cmdarg_err_cont("This should not happen.");
207 * Error message for a failed attempt to write to a capture file.
208 * "in_filename" is the name of the file from which the record
209 * being written came; "out_filename" is the name of the file to
210 * which we're writing; "err" is assumed "err" is assumed to be a
211 * UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed to be
212 * a string giving further information for some WTAP_ERR_ values;
213 * "framenum" is the frame number of the record on which the error
214 * occurred; "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value
215 * for the type and subtype of file being written.
218 cfile_write_failure_message_simple(const char* in_filename
, const char* out_filename
,
219 int err
, char* err_info
,
220 uint64_t framenum
, int file_type_subtype _U_
)
222 char* in_file_string
;
223 char* in_frame_string
;
224 char* out_file_string
;
226 /* Get a string that describes what we're reading from */
227 if (in_filename
== NULL
) {
228 in_frame_string
= g_strdup("");
230 in_file_string
= input_file_description(in_filename
);
231 in_frame_string
= ws_strdup_printf(" %" PRIu64
" of %s", framenum
,
233 g_free(in_file_string
);
236 /* Get a string that describes what we're writing to */
237 out_file_string
= output_file_description(out_filename
);
240 cmdarg_err("An error occurred while writing to the %s: libwiretap error %i.",
241 out_file_string
, err
);
243 cmdarg_err("An error occurred while writing to the %s: %s.",
244 out_file_string
, g_strerror(err
));
246 cmdarg_err_cont("This should not happen.");
247 g_free(in_frame_string
);
248 g_free(out_file_string
);
253 * Error message for a failed attempt to close a capture file.
254 * "filename" is the name of the file being closed; "err" is assumed
255 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
256 * to be a string giving further information for some WTAP_ERR_ values.
258 * When closing a capture file:
260 * some information in the file that can't be determined until
261 * all packets have been written might be written to the file
262 * (such as a table of the file offsets of all packets);
264 * data buffered in the low-level file writing code might be
265 * flushed to the file;
267 * for remote file systems, data written to the file but not
268 * yet sent to the server might be sent to the server or, if
269 * that data was sent asynchronously, "out of space", "disk
270 * quota exceeded", or "I/O error" indications might have
271 * been received but not yet delivered, and the close operation
272 * could deliver them;
274 * so we have to check for write errors here.
277 cfile_close_failure_message_simple(const char* filename
, int err
, char* err_info
)
281 /* Get a string that describes what we're writing to */
282 file_string
= output_file_description(filename
);
285 cmdarg_err("An error occurred while closing the file %s: libwiretap error %i.",
288 cmdarg_err("An error occurred while closing the file %s: %s.",
289 file_string
, g_strerror(err
));
291 cmdarg_err_cont("This should not happen.");
297 * Register these routines with the report_message mechanism.
300 init_report_failure_message_simple(const char *friendly_program_name
)
302 static const struct report_message_routines report_failure_routines
= {
303 failure_message_simple
,
304 failure_message_simple
,
305 open_failure_message_simple
,
306 read_failure_message_simple
,
307 write_failure_message_simple
,
308 rename_failure_message_simple
,
309 cfile_open_failure_message_simple
,
310 cfile_dump_open_failure_message_simple
,
311 cfile_read_failure_message_simple
,
312 cfile_write_failure_message_simple
,
313 cfile_close_failure_message_simple
316 init_report_message(friendly_program_name
, &report_failure_routines
);