Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / failure_message.c
blob53589c9740c19505c213a7636afc573a8701d186
1 /* failure_message.c
2 * Routines to print various "standard" failure messages used in multiple
3 * places
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include <string.h>
15 #include <errno.h>
17 #include <wiretap/wtap.h>
18 #include <wsutil/filesystem.h>
19 #include <wsutil/report_message.h>
20 #include <wsutil/cmdarg_err.h>
22 #include "ui/failure_message.h"
25 * Generic error message.
27 void
28 failure_message(const char *msg_format, va_list ap)
30 vcmdarg_err(msg_format, ap);
34 * Error message for a failed attempt to open or create a file
35 * other than a capture file.
36 * "filename" is the name of the file being opened; "err" is assumed
37 * to be a UNIX-style errno; "for_writing" is true if we're opening
38 * the file for writing and false if we're opening it for reading.
40 void
41 open_failure_message(const char *filename, int err, bool for_writing)
43 cmdarg_err(file_open_error_message(err, for_writing), filename);
47 * Error message for a failed attempt to read from a file other than
48 * a capture file.
49 * "filename" is the name of the file being read from; "err" is assumed
50 * to be a UNIX-style errno.
52 void
53 read_failure_message(const char *filename, int err)
55 cmdarg_err("An error occurred while reading from the file \"%s\": %s.",
56 filename, g_strerror(err));
60 * Error message for a failed attempt to write to a file other than
61 * a capture file.
62 * "filename" is the name of the file being written to; "err" is assumed
63 * to be a UNIX-style errno.
65 void
66 write_failure_message(const char *filename, int err)
68 cmdarg_err("An error occurred while writing to the file \"%s\": %s.",
69 filename, g_strerror(err));
73 * Error message for a failed attempt to rename a file other than
74 * a capture file.
75 * "old_filename" is the name of the file being renamed; "new_filename"
76 * is the name to which it's being renamed; "err" is assumed to be a
77 * UNIX-style errno.
79 void
80 rename_failure_message(const char *old_filename, const char *new_filename,
81 int err)
83 cmdarg_err("An error occurred while renaming the file \"%s\" to \"%s\": %s.",
84 old_filename, new_filename, g_strerror(err));
87 static char *
88 input_file_description(const char *fname)
90 char *fstring;
92 if (strcmp(fname, "-") == 0) {
93 /* We're reading from the standard input */
94 fstring = g_strdup("standard input");
95 } else {
96 /* We're reading from a file */
97 fstring = ws_strdup_printf("file \"%s\"", fname);
99 return fstring;
102 static char *
103 output_file_description(const char *fname)
105 char *fstring;
107 if (strcmp(fname, "-") == 0) {
108 /* We're writing to the standard output */
109 fstring = g_strdup("standard output");
110 } else {
111 /* We're writing to a file */
112 fstring = ws_strdup_printf("file \"%s\"", fname);
114 return fstring;
118 * Error message for a failed attempt to open a capture file for reading.
119 * "filename" is the name of the file being opened; "err" is assumed
120 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
121 * to be a string giving further information for some WTAP_ERR_ values.
123 void
124 cfile_open_failure_message(const char *filename, int err, char *err_info)
126 if (err < 0) {
128 * Wiretap error.
129 * Get a string that describes what we're opening.
131 char *file_description = input_file_description(filename);
133 switch (err) {
135 case WTAP_ERR_NOT_REGULAR_FILE:
136 cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
137 file_description);
138 break;
140 case WTAP_ERR_RANDOM_OPEN_PIPE:
141 cmdarg_err("The %s is a pipe or FIFO; %s can't read pipe or FIFO files in two-pass mode.",
142 file_description, get_friendly_program_name());
143 break;
145 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
146 cmdarg_err("The %s isn't a capture file in a format %s understands.",
147 file_description, get_friendly_program_name());
148 break;
150 case WTAP_ERR_UNSUPPORTED:
151 cmdarg_err("The %s contains record data that %s doesn't support.\n"
152 "(%s)",
153 file_description, get_friendly_program_name(),
154 err_info != NULL ? err_info : "no information supplied");
155 g_free(err_info);
156 break;
158 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
159 cmdarg_err("The %s is a capture for a network type that %s doesn't support.",
160 file_description, get_friendly_program_name());
161 break;
163 case WTAP_ERR_BAD_FILE:
164 cmdarg_err("The %s appears to be damaged or corrupt.\n"
165 "(%s)",
166 file_description,
167 err_info != NULL ? err_info : "no information supplied");
168 g_free(err_info);
169 break;
171 case WTAP_ERR_CANT_OPEN:
172 cmdarg_err("The %s could not be opened for some unknown reason.",
173 file_description);
174 break;
176 case WTAP_ERR_SHORT_READ:
177 cmdarg_err("The %s appears to have been cut short in the middle of a packet or other data.",
178 file_description);
179 break;
181 case WTAP_ERR_DECOMPRESS:
182 cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt."
183 "(%s)",
184 file_description,
185 err_info != NULL ? err_info : "no information supplied");
186 g_free(err_info);
187 break;
189 case WTAP_ERR_INTERNAL:
190 cmdarg_err("An internal error occurred opening the %s.\n"
191 "(%s)",
192 file_description,
193 err_info != NULL ? err_info : "no information supplied");
194 g_free(err_info);
195 break;
197 case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
198 cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support."
199 "(%s)",
200 file_description,
201 err_info != NULL ? err_info : "no information supplied");
202 g_free(err_info);
203 break;
205 default:
206 cmdarg_err("The %s could not be opened: %s.",
207 file_description,
208 wtap_strerror(err));
209 break;
211 g_free(file_description);
212 } else
213 cmdarg_err(file_open_error_message(err, false), filename);
217 * Error message for a failed attempt to open a capture file for writing.
218 * "filename" is the name of the file being opened; "err" is assumed
219 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
220 * to be a string giving further information for some WTAP_ERR_ values;
221 * "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type
222 * and subtype of file being opened.
224 void
225 cfile_dump_open_failure_message(const char *filename, int err, char *err_info,
226 int file_type_subtype)
228 if (err < 0) {
230 * Wiretap error.
231 * Get a string that describes what we're opening.
233 char *file_description = output_file_description(filename);
235 switch (err) {
237 case WTAP_ERR_NOT_REGULAR_FILE:
238 cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
239 file_description);
240 break;
242 case WTAP_ERR_CANT_WRITE_TO_PIPE:
243 cmdarg_err("The %s is a pipe, and \"%s\" capture files can't be written to a pipe.",
244 file_description,
245 wtap_file_type_subtype_name(file_type_subtype));
246 break;
248 case WTAP_ERR_UNWRITABLE_FILE_TYPE:
249 cmdarg_err("%s doesn't support writing capture files in that format.",
250 get_friendly_program_name());
251 break;
253 case WTAP_ERR_UNWRITABLE_ENCAP:
254 cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
255 wtap_file_type_subtype_name(file_type_subtype));
256 break;
258 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
259 cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
260 wtap_file_type_subtype_name(file_type_subtype));
261 break;
263 case WTAP_ERR_CANT_OPEN:
264 cmdarg_err("The %s could not be created for some unknown reason.",
265 file_description);
266 break;
268 case WTAP_ERR_SHORT_WRITE:
269 cmdarg_err("A full header couldn't be written to the %s.",
270 file_description);
271 break;
273 case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
274 cmdarg_err("This file type cannot be written as a compressed file.");
275 break;
277 case WTAP_ERR_INTERNAL:
278 cmdarg_err("An internal error occurred creating the %s.\n"
279 "(%s)",
280 file_description,
281 err_info != NULL ? err_info : "no information supplied");
282 g_free(err_info);
283 break;
285 default:
286 cmdarg_err("The %s could not be created: %s.",
287 file_description,
288 wtap_strerror(err));
289 break;
291 g_free(file_description);
292 } else
293 cmdarg_err(file_open_error_message(err, true), filename);
297 * Error message for a failed attempt to read from a capture file.
298 * "filename" is the name of the file being opened; "err" is assumed
299 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
300 * to be a string giving further information for some WTAP_ERR_ values.
302 void
303 cfile_read_failure_message(const char *filename, int err, char *err_info)
305 char *file_string;
307 /* Get a string that describes what we're reading from */
308 file_string = input_file_description(filename);
310 switch (err) {
312 case WTAP_ERR_UNSUPPORTED:
313 cmdarg_err("The %s contains record data that %s doesn't support.\n"
314 "(%s)",
315 file_string, get_friendly_program_name(),
316 err_info != NULL ? err_info : "no information supplied");
317 g_free(err_info);
318 break;
320 case WTAP_ERR_SHORT_READ:
321 cmdarg_err("The %s appears to have been cut short in the middle of a packet.",
322 file_string);
323 break;
325 case WTAP_ERR_BAD_FILE:
326 cmdarg_err("The %s appears to be damaged or corrupt.\n"
327 "(%s)",
328 file_string,
329 err_info != NULL ? err_info : "no information supplied");
330 g_free(err_info);
331 break;
333 case WTAP_ERR_DECOMPRESS:
334 cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt.\n"
335 "(%s)",
336 file_string,
337 err_info != NULL ? err_info : "no information supplied");
338 g_free(err_info);
339 break;
341 case WTAP_ERR_INTERNAL:
342 cmdarg_err("An internal error occurred while reading the %s.\n(%s)",
343 file_string,
344 err_info != NULL ? err_info : "no information supplied");
345 g_free(err_info);
346 break;
348 case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
349 cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support.\n"
350 "(%s)",
351 file_string,
352 err_info != NULL ? err_info : "no information supplied");
353 g_free(err_info);
354 break;
356 default:
357 cmdarg_err("An error occurred while reading the %s: %s.",
358 file_string, wtap_strerror(err));
359 break;
361 g_free(file_string);
365 * Error message for a failed attempt to write to a capture file.
366 * "in_filename" is the name of the file from which the record
367 * being written came; "out_filename" is the name of the file to
368 * which we're writing; "err" is assumed "err" is assumed to be a
369 * UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed to be
370 * a string giving further information for some WTAP_ERR_ values;
371 * "framenum" is the frame number of the record on which the error
372 * occurred; "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value
373 * for the type and subtype of file being written.
375 void
376 cfile_write_failure_message(const char *in_filename, const char *out_filename,
377 int err, char *err_info,
378 uint64_t framenum, int file_type_subtype)
380 char *in_file_string;
381 char *in_frame_string;
382 char *out_file_string;
384 /* Get a string that describes what we're reading from */
385 if (in_filename == NULL) {
386 in_frame_string = g_strdup("");
387 } else {
388 in_file_string = input_file_description(in_filename);
389 in_frame_string = ws_strdup_printf(" %" PRIu64 " of %s", framenum,
390 in_file_string);
391 g_free(in_file_string);
394 /* Get a string that describes what we're writing to */
395 out_file_string = output_file_description(out_filename);
397 switch (err) {
399 case WTAP_ERR_UNWRITABLE_ENCAP:
401 * This is a problem with the particular frame we're writing
402 * and the file type and subtype we're writing; note that,
403 * and report the frame number and file type/subtype.
405 cmdarg_err("Frame%s has a network type that can't be saved in a \"%s\" file.",
406 in_frame_string,
407 wtap_file_type_subtype_name(file_type_subtype));
408 break;
410 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
412 * This is a problem with the particular frame we're writing and
413 * the file type and subtype we're writing; note that, and report
414 * the frame number and file type/subtype.
416 cmdarg_err("Frame%s has a network type that differs from the network type of earlier packets, which isn't supported in a \"%s\" file.",
417 in_frame_string,
418 wtap_file_type_subtype_description(file_type_subtype));
419 break;
421 case WTAP_ERR_PACKET_TOO_LARGE:
423 * This is a problem with the particular frame we're writing
424 * and the file type and subtype we're writing; note that,
425 * and report the frame number and file type/subtype.
427 cmdarg_err("Frame%s is larger than %s supports in a \"%s\" file.",
428 in_frame_string, get_friendly_program_name(),
429 wtap_file_type_subtype_name(file_type_subtype));
430 break;
432 case WTAP_ERR_UNWRITABLE_REC_TYPE:
434 * This is a problem with the particular record we're writing
435 * and the file type and subtype we're writing; note that,
436 * and report the record number and file type/subtype.
438 cmdarg_err("Record%s has a record type that can't be saved in a \"%s\" file.",
439 in_frame_string,
440 wtap_file_type_subtype_name(file_type_subtype));
441 break;
443 case WTAP_ERR_UNWRITABLE_REC_DATA:
445 * This is a problem with the particular record we're writing
446 * and the file type and subtype we're writing; note that,
447 * and report the record number and file type/subtype.
449 cmdarg_err("Record%s has data that can't be saved in a \"%s\" file.\n"
450 "(%s)",
451 in_frame_string,
452 wtap_file_type_subtype_name(file_type_subtype),
453 err_info != NULL ? err_info : "no information supplied");
454 g_free(err_info);
455 break;
457 case WTAP_ERR_SHORT_WRITE:
458 cmdarg_err("A full write couldn't be done to the %s.",
459 out_file_string);
460 break;
462 case WTAP_ERR_INTERNAL:
463 cmdarg_err("An internal error occurred while writing record%s to the %s.\n(%s)",
464 in_frame_string, out_file_string,
465 err_info != NULL ? err_info : "no information supplied");
466 g_free(err_info);
467 break;
469 case ENOSPC:
470 cmdarg_err("Not all the packets could be written to the %s because there is "
471 "no space left on the file system.",
472 out_file_string);
473 break;
475 #ifdef EDQUOT
476 case EDQUOT:
477 cmdarg_err("Not all the packets could be written to the %s because you are "
478 "too close to, or over your disk quota.",
479 out_file_string);
480 break;
481 #endif
483 default:
484 cmdarg_err("An error occurred while writing to the %s: %s.",
485 out_file_string, wtap_strerror(err));
486 break;
488 g_free(in_frame_string);
489 g_free(out_file_string);
493 * Error message for a failed attempt to close a capture file.
494 * "filename" is the name of the file being closed; "err" is assumed
495 * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
496 * to be a string giving further information for some WTAP_ERR_ values.
498 * When closing a capture file:
500 * some information in the file that can't be determined until
501 * all packets have been written might be written to the file
502 * (such as a table of the file offsets of all packets);
504 * data buffered in the low-level file writing code might be
505 * flushed to the file;
507 * for remote file systems, data written to the file but not
508 * yet sent to the server might be sent to the server or, if
509 * that data was sent asynchronously, "out of space", "disk
510 * quota exceeded", or "I/O error" indications might have
511 * been received but not yet delivered, and the close operation
512 * could deliver them;
514 * so we have to check for write errors here.
516 void
517 cfile_close_failure_message(const char *filename, int err, char *err_info)
519 char *file_string;
521 /* Get a string that describes what we're writing to */
522 file_string = output_file_description(filename);
524 switch (err) {
526 case WTAP_ERR_CANT_CLOSE:
527 cmdarg_err("The %s couldn't be closed for some unknown reason.",
528 file_string);
529 break;
531 case WTAP_ERR_SHORT_WRITE:
532 cmdarg_err("A full write couldn't be done to the %s.",
533 file_string);
534 break;
536 case WTAP_ERR_INTERNAL:
537 cmdarg_err("An internal error occurred closing the file \"%s\".\n"
538 "(%s)",
539 file_string,
540 err_info != NULL ? err_info : "no information supplied");
541 g_free(err_info);
542 break;
544 case ENOSPC:
545 cmdarg_err("Not all the packets could be written to the %s because there is "
546 "no space left on the file system.",
547 file_string);
548 break;
550 #ifdef EDQUOT
551 case EDQUOT:
552 cmdarg_err("Not all the packets could be written to the %s because you are "
553 "too close to, or over your disk quota.",
554 file_string);
555 break;
556 #endif
558 default:
559 cmdarg_err("An error occurred while closing the file %s: %s.",
560 file_string, wtap_strerror(err));
561 break;
563 g_free(file_string);
567 * Register these routines with the report_message mechanism.
569 void
570 init_report_failure_message(const char *friendly_program_name)
572 static const struct report_message_routines report_failure_routines = {
573 failure_message,
574 failure_message,
575 open_failure_message,
576 read_failure_message,
577 write_failure_message,
578 rename_failure_message,
579 cfile_open_failure_message,
580 cfile_dump_open_failure_message,
581 cfile_read_failure_message,
582 cfile_write_failure_message,
583 cfile_close_failure_message
586 init_report_message(friendly_program_name, &report_failure_routines);