regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / wsutil / failure_message_simple.c
blob005b486bff0a68f576618cc811fa625b86687e7b
1 /* failure_message_simple.c
2 * Routines to print various "standard" failure messages used in multiple
3 * places
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
22 #include "config.h"
24 #include <string.h>
25 #include <errno.h>
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.
37 void
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.
50 void
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
58 * a capture file.
59 * "filename" is the name of the file being read from; "err" is assumed
60 * to be a UNIX-style errno.
62 void
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
71 * a capture file.
72 * "filename" is the name of the file being written to; "err" is assumed
73 * to be a UNIX-style errno.
75 void
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
84 * a capture file.
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
87 * UNIX-style errno.
89 void
90 rename_failure_message_simple(const char *old_filename, const char *new_filename,
91 int err)
93 cmdarg_err("An error occurred while renaming the file \"%s\" to \"%s\": %s.",
94 old_filename, new_filename, g_strerror(err));
97 static char*
98 input_file_description(const char* fname)
100 char* fstring;
102 if (strcmp(fname, "-") == 0) {
103 /* We're reading from the standard input */
104 fstring = g_strdup("standard input");
105 } else {
106 /* We're reading from a file */
107 fstring = ws_strdup_printf("file \"%s\"", fname);
109 return fstring;
112 static char*
113 output_file_description(const char* fname)
115 char* fstring;
117 if (strcmp(fname, "-") == 0) {
118 /* We're writing to the standard output */
119 fstring = g_strdup("standard output");
120 } else {
121 /* We're writing to a file */
122 fstring = ws_strdup_printf("file \"%s\"", fname);
124 return fstring;
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.
133 void
134 cfile_open_failure_message_simple(const char* filename, int err, char* err_info)
136 if (err < 0) {
138 * Wiretap error.
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);
146 } else
147 cmdarg_err(file_open_error_message(err, false), filename);
148 cmdarg_err_cont("This should not happen.");
149 g_free(err_info);
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.
160 void
161 cfile_dump_open_failure_message_simple(const char* filename, int err, char* err_info,
162 int file_type_subtype _U_)
164 if (err < 0) {
166 * Wiretap error.
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);
174 } else
175 cmdarg_err(file_open_error_message(err, true), filename);
176 cmdarg_err_cont("This should not happen.");
177 g_free(err_info);
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.
186 void
187 cfile_read_failure_message_simple(const char* filename, int err, char* err_info)
189 char* file_string;
191 /* Get a string that describes what we're reading from */
192 file_string = input_file_description(filename);
194 if (err < 0) {
195 cmdarg_err("An error occurred while reading the %s: libwiretap error %i.",
196 file_string, err);
197 } else {
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.");
202 g_free(file_string);
203 g_free(err_info);
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.
217 void
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("");
229 } else {
230 in_file_string = input_file_description(in_filename);
231 in_frame_string = ws_strdup_printf(" %" PRIu64 " of %s", framenum,
232 in_file_string);
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);
239 if (err < 0) {
240 cmdarg_err("An error occurred while writing to the %s: libwiretap error %i.",
241 out_file_string, err);
242 } else {
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);
249 g_free(err_info);
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.
276 void
277 cfile_close_failure_message_simple(const char* filename, int err, char* err_info)
279 char* file_string;
281 /* Get a string that describes what we're writing to */
282 file_string = output_file_description(filename);
284 if (err < 0) {
285 cmdarg_err("An error occurred while closing the file %s: libwiretap error %i.",
286 file_string, err);
287 } else {
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.");
292 g_free(file_string);
293 g_free(err_info);
297 * Register these routines with the report_message mechanism.
299 void
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);