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 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 # include <gnu/option-groups.h>
37 # define mbsrtowcs __mbsrtowcs
43 # define _(String) String
46 /* If NULL, error will flush stdout, then print on stderr the program
47 name, a colon and a space. Otherwise, error will call this
48 function without parameters instead. */
49 void (*error_print_progname) (void);
51 /* This variable is incremented each time `error' is called. */
52 unsigned int error_message_count;
55 /* In the GNU C library, there is a predefined variable for this. */
57 # define program_name program_invocation_name
60 # include <libio/libioP.h>
62 /* In GNU libc we want do not want to use the common name `error' directly.
63 Instead make it a weak alias. */
64 extern void __error (int status, int errnum, const char *message, ...)
65 __attribute__ ((__format__ (__printf__, 3, 4)));
66 extern void __error_at_line (int status, int errnum, const char *file_name,
67 unsigned int line_number, const char *message,
69 __attribute__ ((__format__ (__printf__, 5, 6)));;
70 # define error __error
71 # define error_at_line __error_at_line
73 # include <libio/iolibio.h>
74 # define fflush(s) INTUSE(_IO_fflush) (s)
76 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
78 # include <bits/libc-lock.h>
82 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
83 # ifndef HAVE_DECL_STRERROR_R
84 "this configure-time declaration test was not run"
89 /* The calling program should define program_name and set it to the
90 name of the executing program. */
91 extern char *program_name;
93 # if HAVE_STRERROR_R || defined strerror_r
94 # define __strerror_r strerror_r
95 # endif /* HAVE_STRERROR_R || defined strerror_r */
96 #endif /* not _LIBC */
99 print_errno_message (int errnum)
103 #if defined HAVE_STRERROR_R || _LIBC
105 # if STRERROR_R_CHAR_P || _LIBC
106 s = __strerror_r (errnum, errbuf, sizeof errbuf);
108 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
114 s = strerror (errnum);
119 s = _("Unknown system error");
123 __fxprintf (NULL, ": %s", s);
125 fprintf (stderr, ": %s", s);
130 error_tail (int status, int errnum, const char *message, va_list args)
133 if (_IO_fwide (stderr, 0) > 0)
135 #if __OPTION_POSIX_WIDE_CHAR_DEVICE_IO
136 # define ALLOCA_LIMIT 2000
137 size_t len = strlen (message) + 1;
138 wchar_t *wmessage = NULL;
142 bool use_malloc = false;
146 if (__libc_use_alloca (len * sizeof (wchar_t)))
147 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
153 wchar_t *p = (wchar_t *) realloc (wmessage,
154 len * sizeof (wchar_t));
158 fputws_unlocked (L"out of memory\n", stderr);
165 memset (&st, '\0', sizeof (st));
168 res = mbsrtowcs (wmessage, &tmp, len, &st);
172 if (__builtin_expect (len >= SIZE_MAX / 2, 0))
174 /* This really should not happen if everything is fine. */
182 if (res == (size_t) -1)
184 /* The string cannot be converted. */
190 wmessage = (wchar_t *) L"???";
193 __vfwprintf (stderr, wmessage, args);
203 vfprintf (stderr, message, args);
206 ++error_message_count;
208 print_errno_message (errnum);
210 __fxprintf (NULL, "\n");
220 /* Print the program name and error message MESSAGE, which is a printf-style
221 format string with optional args.
222 If ERRNUM is nonzero, print its corresponding system error message.
223 Exit with status STATUS if it is nonzero. */
225 error (int status, int errnum, const char *message, ...)
229 #if defined _LIBC && defined __libc_ptf_call
230 /* We do not want this call to be cut short by a thread
231 cancellation. Therefore disable cancellation for now. */
232 int state = PTHREAD_CANCEL_ENABLE;
233 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
239 _IO_flockfile (stderr);
241 if (error_print_progname)
242 (*error_print_progname) ();
246 __fxprintf (NULL, "%s: ", program_name);
248 fprintf (stderr, "%s: ", program_name);
252 va_start (args, message);
253 error_tail (status, errnum, message, args);
256 _IO_funlockfile (stderr);
257 # ifdef __libc_ptf_call
258 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
263 /* Sometimes we want to have at most one error per line. This
264 variable controls whether this mode is selected or not. */
265 int error_one_per_line;
268 error_at_line (int status, int errnum, const char *file_name,
269 unsigned int line_number, const char *message, ...)
273 if (error_one_per_line)
275 static const char *old_file_name;
276 static unsigned int old_line_number;
278 if (old_line_number == line_number
279 && (file_name == old_file_name
280 || strcmp (old_file_name, file_name) == 0))
281 /* Simply return and print nothing. */
284 old_file_name = file_name;
285 old_line_number = line_number;
288 #if defined _LIBC && defined __libc_ptf_call
289 /* We do not want this call to be cut short by a thread
290 cancellation. Therefore disable cancellation for now. */
291 int state = PTHREAD_CANCEL_ENABLE;
292 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
298 _IO_flockfile (stderr);
300 if (error_print_progname)
301 (*error_print_progname) ();
305 __fxprintf (NULL, "%s:", program_name);
307 fprintf (stderr, "%s:", program_name);
312 __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
313 file_name, line_number);
315 fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
316 file_name, line_number);
319 va_start (args, message);
320 error_tail (status, errnum, message, args);
323 _IO_funlockfile (stderr);
324 # ifdef __libc_ptf_call
325 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
331 /* Make the weak alias. */
333 # undef error_at_line
334 weak_alias (__error, error)
335 weak_alias (__error_at_line, error_at_line)