posix_spawn_file_actions_* tests: Fix module dependencies.
[gnulib.git] / lib / verror.c
blob5217029704ddb25e68818637a93316dec7210bc3
1 /* va_list error handler for noninteractive utilities
2 Copyright (C) 2006-2007, 2009-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake. */
19 #include <config.h>
21 /* Specification. */
22 #include "verror.h"
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
28 #include "error.h"
29 #include "xvasprintf.h"
31 #if ENABLE_NLS
32 # include "gettext.h"
33 # define _(msgid) gettext (msgid)
34 #endif
36 #ifndef _
37 # define _(String) String
38 #endif
40 /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
41 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
42 If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
43 Use the globals error_print_progname and error_message_count similarly
44 to error(). */
45 void
46 verror (int status, int errnum, const char *format, va_list args)
48 verror_at_line (status, errnum, NULL, 0, format, args);
51 /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
52 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
53 If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
54 If FNAME is not NULL, prepend the message with "FNAME:LINENO:".
55 Use the globals error_print_progname, error_message_count, and
56 error_one_per_line similarly to error_at_line(). */
57 void
58 verror_at_line (int status, int errnum, const char *file,
59 unsigned int line_number, const char *format, va_list args)
61 char *message = xvasprintf (format, args);
62 if (message)
64 /* Until https://sourceware.org/bugzilla/show_bug.cgi?id=2997 is fixed,
65 glibc violates GNU Coding Standards when the file argument to
66 error_at_line is NULL. */
67 if (file)
68 error_at_line (status, errnum, file, line_number, "%s", message);
69 else
70 error (status, errnum, "%s", message);
72 else
74 /* EOVERFLOW, EINVAL, and EILSEQ from xvasprintf are signs of
75 serious programmer errors. */
76 error (0, errno, _("unable to display error message"));
77 abort ();
79 free (message);