1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2004, 2005 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>. */
30 # define mbsrtowcs __mbsrtowcs
33 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
36 # define VA_START(args, lastarg) va_start(args, lastarg)
39 # define VA_START(args, lastarg) va_start(args)
42 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
43 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
46 #if STDC_HEADERS || _LIBC
56 # define _(String) String
59 /* If NULL, error will flush stdout, then print on stderr the program
60 name, a colon and a space. Otherwise, error will call this
61 function without parameters instead. */
62 void (*error_print_progname
) (
68 /* This variable is incremented each time `error' is called. */
69 unsigned int error_message_count
;
72 /* In the GNU C library, there is a predefined variable for this. */
74 # define program_name program_invocation_name
77 # include <libio/libioP.h>
79 /* In GNU libc we want do not want to use the common name `error' directly.
80 Instead make it a weak alias. */
81 extern void __error (int status
, int errnum
, const char *message
, ...)
82 __attribute__ ((__format__ (__printf__
, 3, 4)));
83 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
84 unsigned int line_number
, const char *message
,
86 __attribute__ ((__format__ (__printf__
, 5, 6)));;
87 # define error __error
88 # define error_at_line __error_at_line
90 # include <libio/iolibio.h>
91 # define fflush(s) INTUSE(_IO_fflush) (s)
93 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
95 # include <bits/libc-lock.h>
99 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
100 # ifndef HAVE_DECL_STRERROR_R
101 "this configure-time declaration test was not run"
106 /* The calling program should define program_name and set it to the
107 name of the executing program. */
108 extern char *program_name
;
110 # if HAVE_STRERROR_R || defined strerror_r
111 # define __strerror_r strerror_r
114 # ifndef HAVE_DECL_STRERROR
115 "this configure-time declaration test was not run"
117 # if !HAVE_DECL_STRERROR
122 private_strerror (int errnum
)
124 extern char *sys_errlist
[];
127 if (errnum
> 0 && errnum
<= sys_nerr
)
128 return _(sys_errlist
[errnum
]);
129 return _("Unknown system error");
131 # define strerror private_strerror
132 # endif /* HAVE_STRERROR */
133 # endif /* HAVE_STRERROR_R || defined strerror_r */
134 #endif /* not _LIBC */
137 print_errno_message (int errnum
)
141 #if defined HAVE_STRERROR_R || _LIBC
143 # if STRERROR_R_CHAR_P || _LIBC
144 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
146 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
152 s
= strerror (errnum
);
157 s
= _("Unknown system error");
161 __fxprintf (NULL
, ": %s", s
);
163 fprintf (stderr
, ": %s", s
);
169 error_tail (int status
, int errnum
, const char *message
, va_list args
)
171 # if HAVE_VPRINTF || _LIBC
173 if (_IO_fwide (stderr
, 0) > 0)
175 # define ALLOCA_LIMIT 2000
176 size_t len
= strlen (message
) + 1;
177 wchar_t *wmessage
= NULL
;
181 bool use_malloc
= false;
185 if (__libc_use_alloca (len
* sizeof (wchar_t)))
186 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
192 wchar_t *p
= (wchar_t *) realloc (wmessage
,
193 len
* sizeof (wchar_t));
197 fputws_unlocked (L
"out of memory\n", stderr
);
204 memset (&st
, '\0', sizeof (st
));
207 res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
);
211 if (__builtin_expect (len
>= SIZE_MAX
/ 2, 0))
213 /* This reallyy should not happen if everything is fine. */
221 if (res
== (size_t) -1)
223 /* The string cannot be converted. */
226 wmessage
= (wchar_t *) L
"???";
229 __vfwprintf (stderr
, wmessage
, args
);
236 vfprintf (stderr
, message
, args
);
238 _doprnt (message
, args
, stderr
);
242 ++error_message_count
;
244 print_errno_message (errnum
);
246 __fxprintf (NULL
, "\n");
257 /* Print the program name and error message MESSAGE, which is a printf-style
258 format string with optional args.
259 If ERRNUM is nonzero, print its corresponding system error message.
260 Exit with status STATUS if it is nonzero. */
263 #if defined VA_START && __STDC__
264 error (int status
, int errnum
, const char *message
, ...)
266 error (status
, errnum
, message
, va_alist
)
277 #if defined _LIBC && defined __libc_ptf_call
278 /* We do not want this call to be cut short by a thread
279 cancellation. Therefore disable cancellation for now. */
280 int state
= PTHREAD_CANCEL_ENABLE
;
281 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
287 _IO_flockfile (stderr
);
289 if (error_print_progname
)
290 (*error_print_progname
) ();
294 __fxprintf (NULL
, "%s: ", program_name
);
296 fprintf (stderr
, "%s: ", program_name
);
301 VA_START (args
, message
);
302 error_tail (status
, errnum
, message
, args
);
304 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
306 ++error_message_count
;
308 print_errno_message (errnum
);
316 _IO_funlockfile (stderr
);
317 # ifdef __libc_ptf_call
318 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
323 /* Sometimes we want to have at most one error per line. This
324 variable controls whether this mode is selected or not. */
325 int error_one_per_line
;
328 #if defined VA_START && __STDC__
329 error_at_line (int status
, int errnum
, const char *file_name
,
330 unsigned int line_number
, const char *message
, ...)
332 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
335 const char *file_name
;
336 unsigned int line_number
;
345 if (error_one_per_line
)
347 static const char *old_file_name
;
348 static unsigned int old_line_number
;
350 if (old_line_number
== line_number
351 && (file_name
== old_file_name
352 || strcmp (old_file_name
, file_name
) == 0))
353 /* Simply return and print nothing. */
356 old_file_name
= file_name
;
357 old_line_number
= line_number
;
360 #if defined _LIBC && defined __libc_ptf_call
361 /* We do not want this call to be cut short by a thread
362 cancellation. Therefore disable cancellation for now. */
363 int state
= PTHREAD_CANCEL_ENABLE
;
364 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
370 _IO_flockfile (stderr
);
372 if (error_print_progname
)
373 (*error_print_progname
) ();
377 __fxprintf (NULL
, "%s:", program_name
);
379 fprintf (stderr
, "%s:", program_name
);
383 if (file_name
!= NULL
)
386 __fxprintf (NULL
, "%s:%d: ", file_name
, line_number
);
388 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
393 VA_START (args
, message
);
394 error_tail (status
, errnum
, message
, args
);
396 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
398 ++error_message_count
;
400 print_errno_message (errnum
);
408 _IO_funlockfile (stderr
);
409 # ifdef __libc_ptf_call
410 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
416 /* Make the weak alias. */
418 # undef error_at_line
419 weak_alias (__error
, error
)
420 weak_alias (__error_at_line
, error_at_line
)