1
/* Error handler for noninteractive utilities
2
Copyright (C) 1990-1998, 2000-2005, 2006 Free Software Foundation, Inc.
3
This file is part of the GNU C Library.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2, or (at your option)
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License along
16
with this program; if not, write to the Free Software Foundation,
17
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32
#if !_LIBC && ENABLE_NLS
41
# define mbsrtowcs __mbsrtowcs
45
# include "unlocked-io.h"
49
# define _(String) String
52
/* If NULL, error will flush stdout, then print on stderr the program
53
name, a colon and a space. Otherwise, error will call this
54
function without parameters instead. */
55
void (*error_print_progname) (void);
57
/* This variable is incremented each time `error' is called. */
58
unsigned int error_message_count;
61
/* In the GNU C library, there is a predefined variable for this. */
63
# define program_name program_invocation_name
66
# include <libio/libioP.h>
68
/* In GNU libc we want do not want to use the common name `error' directly.
69
Instead make it a weak alias. */
70
extern void __error (int status, int errnum, const char *message, ...)
71
__attribute__ ((__format__ (__printf__, 3, 4)));
72
extern void __error_at_line (int status, int errnum, const char *file_name,
73
unsigned int line_number, const char *message,
75
__attribute__ ((__format__ (__printf__, 5, 6)));;
76
# define error __error
77
# define error_at_line __error_at_line
79
# include <libio/iolibio.h>
80
# define fflush(s) INTUSE(_IO_fflush) (s)
82
# define putc(c, fp) INTUSE(_IO_putc) (c, fp)
84
# include <bits/libc-lock.h>
88
# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
89
# ifndef HAVE_DECL_STRERROR_R
90
"this configure-time declaration test was not run"
95
/* The calling program should define program_name and set it to the
96
name of the executing program. */
97
extern char *program_name;
99
# if HAVE_STRERROR_R || defined strerror_r
100
# define __strerror_r strerror_r
101
# endif /* HAVE_STRERROR_R || defined strerror_r */
102
#endif /* not _LIBC */
105
print_errno_message (int errnum)
109
#if defined HAVE_STRERROR_R || _LIBC
111
# if STRERROR_R_CHAR_P || _LIBC
112
s = __strerror_r (errnum, errbuf, sizeof errbuf);
114
if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
120
s = strerror (errnum);
125
s = _("Unknown system error");
129
__fxprintf (NULL, ": %s", s);
131
fprintf (stderr, ": %s", s);
136
error_tail (int status, int errnum, const char *message, va_list args)
139
if (_IO_fwide (stderr, 0) > 0)
141
# define ALLOCA_LIMIT 2000
142
size_t len = strlen (message) + 1;
143
wchar_t *wmessage = NULL;
147
bool use_malloc = false;
151
if (__libc_use_alloca (len * sizeof (wchar_t)))
152
wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
158
wchar_t *p = (wchar_t *) realloc (wmessage,
159
len * sizeof (wchar_t));
163
fputws_unlocked (L"out of memory\n", stderr);
170
memset (&st, '\0', sizeof (st));
173
res = mbsrtowcs (wmessage, &tmp, len, &st);
177
if (__builtin_expect (len >= SIZE_MAX / 2, 0))
179
/* This really should not happen if everything is fine. */
187
if (res == (size_t) -1)
189
/* The string cannot be converted. */
195
wmessage = (wchar_t *) L"???";
198
__vfwprintf (stderr, wmessage, args);
205
vfprintf (stderr, message, args);
208
++error_message_count;
210
print_errno_message (errnum);
212
__fxprintf (NULL, "\n");
222
/* Print the program name and error message MESSAGE, which is a printf-style
223
format string with optional args.
224
If ERRNUM is nonzero, print its corresponding system error message.
225
Exit with status STATUS if it is nonzero. */
227
error (int status, int errnum, const char *message, ...)
231
#if defined _LIBC && defined __libc_ptf_call
232
/* We do not want this call to be cut short by a thread
233
cancellation. Therefore disable cancellation for now. */
234
int state = PTHREAD_CANCEL_ENABLE;
235
__libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
241
_IO_flockfile (stderr);
243
if (error_print_progname)
244
(*error_print_progname) ();
248
__fxprintf (NULL, "%s: ", program_name);
250
fprintf (stderr, "%s: ", program_name);
254
va_start (args, message);
255
error_tail (status, errnum, message, args);
258
_IO_funlockfile (stderr);
259
# ifdef __libc_ptf_call
260
__libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
265
/* Sometimes we want to have at most one error per line. This
266
variable controls whether this mode is selected or not. */
267
int error_one_per_line;
270
error_at_line (int status, int errnum, const char *file_name,
271
unsigned int line_number, const char *message, ...)
275
if (error_one_per_line)
277
static const char *old_file_name;
278
static unsigned int old_line_number;
280
if (old_line_number == line_number
281
&& (file_name == old_file_name
282
|| strcmp (old_file_name, file_name) == 0))
283
/* Simply return and print nothing. */
286
old_file_name = file_name;
287
old_line_number = line_number;
290
#if defined _LIBC && defined __libc_ptf_call
291
/* We do not want this call to be cut short by a thread
292
cancellation. Therefore disable cancellation for now. */
293
int state = PTHREAD_CANCEL_ENABLE;
294
__libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
300
_IO_flockfile (stderr);
302
if (error_print_progname)
303
(*error_print_progname) ();
307
__fxprintf (NULL, "%s:", program_name);
309
fprintf (stderr, "%s:", program_name);
314
__fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
315
file_name, line_number);
317
fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
318
file_name, line_number);
321
va_start (args, message);
322
error_tail (status, errnum, message, args);
325
_IO_funlockfile (stderr);
326
# ifdef __libc_ptf_call
327
__libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
333
/* Make the weak alias. */
335
# undef error_at_line
336
weak_alias (__error, error)
337
weak_alias (__error_at_line, error_at_line)