1
/* Error handler for noninteractive utilities
2
Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc.
5
NOTE: The canonical source of this file is maintained with the GNU C Library.
6
Bugs can be reported to bug-glibc@gnu.org.
8
This program is free software; you can redistribute it and/or modify it
9
under the terms of the GNU General Public License as published by the
10
Free Software Foundation; either version 2, or (at your option) any
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
31
#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
34
# define VA_START(args, lastarg) va_start(args, lastarg)
37
# define VA_START(args, lastarg) va_start(args)
40
# define va_alist a1, a2, a3, a4, a5, a6, a7, a8
41
# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
44
#if STDC_HEADERS || _LIBC
54
# if ENABLE_NLS || defined _LIBC
57
# define _(Str) gettext (Str)
64
/* If NULL, error will flush stdout, then print on stderr the program
65
name, a colon and a space. Otherwise, error will call this
66
function without parameters instead. */
67
void (*error_print_progname) (
73
/* This variable is incremented each time `error' is called. */
74
unsigned int error_message_count;
77
/* In the GNU C library, there is a predefined variable for this. */
79
# define program_name program_invocation_name
82
/* In GNU libc we want do not want to use the common name `error' directly.
83
Instead make it a weak alias. */
84
# define error __error
85
# define error_at_line __error_at_line
88
# include <libio/iolibio.h>
89
# define fflush(s) _IO_fflush (s)
94
/* The calling program should define program_name and set it to the
95
name of the executing program. */
96
extern char *program_name;
98
# ifdef HAVE_STRERROR_R
99
# define __strerror_r strerror_r
102
# ifndef strerror /* On some systems, strerror is a macro */
107
private_strerror (errnum)
110
extern char *sys_errlist[];
113
if (errnum > 0 && errnum <= sys_nerr)
114
return _(sys_errlist[errnum]);
115
return _("Unknown system error");
117
# define strerror private_strerror
118
# endif /* HAVE_STRERROR */
119
# endif /* HAVE_STRERROR_R */
120
#endif /* not _LIBC */
122
/* Print the program name and error message MESSAGE, which is a printf-style
123
format string with optional args.
124
If ERRNUM is nonzero, print its corresponding system error message.
125
Exit with status STATUS if it is nonzero. */
129
#if defined VA_START && __STDC__
130
error (int status, int errnum, const char *message, ...)
132
error (status, errnum, message, va_alist)
143
if (error_print_progname)
144
(*error_print_progname) ();
148
fprintf (stderr, "%s: ", program_name);
152
VA_START (args, message);
153
# if HAVE_VPRINTF || _LIBC
154
vfprintf (stderr, message, args);
156
_doprnt (message, args, stderr);
160
fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
163
++error_message_count;
166
#if defined HAVE_STRERROR_R || _LIBC
168
fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
170
fprintf (stderr, ": %s", strerror (errnum));
179
/* Sometimes we want to have at most one error per line. This
180
variable controls whether this mode is selected or not. */
181
int error_one_per_line;
184
#if defined VA_START && __STDC__
185
error_at_line (int status, int errnum, const char *file_name,
186
unsigned int line_number, const char *message, ...)
188
error_at_line (status, errnum, file_name, line_number, message, va_alist)
191
const char *file_name;
192
unsigned int line_number;
201
if (error_one_per_line)
203
static const char *old_file_name;
204
static unsigned int old_line_number;
206
if (old_line_number == line_number &&
207
(file_name == old_file_name || !strcmp (old_file_name, file_name)))
208
/* Simply return and print nothing. */
211
old_file_name = file_name;
212
old_line_number = line_number;
215
if (error_print_progname)
216
(*error_print_progname) ();
220
fprintf (stderr, "%s:", program_name);
223
if (file_name != NULL)
224
fprintf (stderr, "%s:%d: ", file_name, line_number);
227
VA_START (args, message);
228
# if HAVE_VPRINTF || _LIBC
229
vfprintf (stderr, message, args);
231
_doprnt (message, args, stderr);
235
fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
238
++error_message_count;
241
#if defined HAVE_STRERROR_R || _LIBC
243
fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
245
fprintf (stderr, ": %s", strerror (errnum));
255
/* Make the weak alias. */
257
# undef error_at_line
258
weak_alias (__error, error)
259
weak_alias (__error_at_line, error_at_line)