1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
34 #define _(msgid) gettext (msgid)
38 # define mbsrtowcs __mbsrtowcs
41 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
44 # define VA_START(args, lastarg) va_start(args, lastarg)
47 # define VA_START(args, lastarg) va_start(args)
50 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
51 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
54 #if STDC_HEADERS || _LIBC
62 #include "unlocked-io.h"
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 extern void __error (int status, int errnum, const char *message, ...)
85 __attribute__ ((__format__ (__printf__, 3, 4)));
86 extern void __error_at_line (int status, int errnum, const char *file_name,
87 unsigned int line_number, const char *message,
89 __attribute__ ((__format__ (__printf__, 5, 6)));;
90 # define error __error
91 # define error_at_line __error_at_line
94 # include <libio/iolibio.h>
95 # define fflush(s) _IO_fflush (s)
100 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
101 # ifndef HAVE_DECL_STRERROR_R
102 "this configure-time declaration test was not run"
107 /* The calling program should define program_name and set it to the
108 name of the executing program. */
109 extern char *program_name;
111 # if HAVE_STRERROR_R || defined strerror_r
112 # define __strerror_r strerror_r
115 # ifndef HAVE_DECL_STRERROR
116 "this configure-time declaration test was not run"
118 # if !HAVE_DECL_STRERROR
123 private_strerror (int errnum)
125 extern char *sys_errlist[];
128 if (errnum > 0 && errnum <= sys_nerr)
129 return _(sys_errlist[errnum]);
130 return _("Unknown system error");
132 # define strerror private_strerror
133 # endif /* HAVE_STRERROR */
134 # endif /* HAVE_STRERROR_R || defined strerror_r */
135 #endif /* not _LIBC */
138 print_errno_message (int errnum)
142 #if defined HAVE_STRERROR_R || _LIBC
144 # if STRERROR_R_CHAR_P || _LIBC
145 s = __strerror_r (errnum, errbuf, sizeof errbuf);
147 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
153 s = strerror (errnum);
158 s = _("Unknown system error");
161 #if _LIBC && USE_IN_LIBIO
162 if (_IO_fwide (stderr, 0) > 0)
164 __fwprintf (stderr, L": %s", s);
169 fprintf (stderr, ": %s", s);
174 error_tail (int status, int errnum, const char *message, va_list args)
176 # if HAVE_VPRINTF || _LIBC
177 # if _LIBC && USE_IN_LIBIO
178 if (_IO_fwide (stderr, 0) > 0)
180 # define ALLOCA_LIMIT 2000
181 size_t len = strlen (message) + 1;
182 wchar_t *wmessage = NULL;
189 if (len < ALLOCA_LIMIT)
190 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
193 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
196 wmessage = (wchar_t *) realloc (wmessage,
197 len * sizeof (wchar_t));
199 if (wmessage == NULL)
201 fputws_unlocked (L"out of memory\n", stderr);
206 memset (&st, '\0', sizeof (st));
209 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
211 if (res == (size_t) -1)
212 /* The string cannot be converted. */
213 wmessage = (wchar_t *) L"???";
215 __vfwprintf (stderr, wmessage, args);
219 vfprintf (stderr, message, args);
221 _doprnt (message, args, stderr);
225 ++error_message_count;
227 print_errno_message (errnum);
228 # if _LIBC && USE_IN_LIBIO
229 if (_IO_fwide (stderr, 0) > 0)
230 putwc (L'\n', stderr);
241 /* Print the program name and error message MESSAGE, which is a printf-style
242 format string with optional args.
243 If ERRNUM is nonzero, print its corresponding system error message.
244 Exit with status STATUS if it is nonzero. */
247 #if defined VA_START && __STDC__
248 error (int status, int errnum, const char *message, ...)
250 error (status, errnum, message, va_alist)
264 _IO_flockfile (stderr);
266 __flockfile (stderr);
269 if (error_print_progname)
270 (*error_print_progname) ();
273 #if _LIBC && USE_IN_LIBIO
274 if (_IO_fwide (stderr, 0) > 0)
275 __fwprintf (stderr, L"%s: ", program_name);
278 fprintf (stderr, "%s: ", program_name);
282 VA_START (args, message);
283 error_tail (status, errnum, message, args);
285 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
287 ++error_message_count;
289 print_errno_message (errnum);
298 _IO_funlockfile (stderr);
300 __funlockfile (stderr);
305 /* Sometimes we want to have at most one error per line. This
306 variable controls whether this mode is selected or not. */
307 int error_one_per_line;
310 #if defined VA_START && __STDC__
311 error_at_line (int status, int errnum, const char *file_name,
312 unsigned int line_number, const char *message, ...)
314 error_at_line (status, errnum, file_name, line_number, message, va_alist)
317 const char *file_name;
318 unsigned int line_number;
327 if (error_one_per_line)
329 static const char *old_file_name;
330 static unsigned int old_line_number;
332 if (old_line_number == line_number
333 && (file_name == old_file_name
334 || strcmp (old_file_name, file_name) == 0))
335 /* Simply return and print nothing. */
338 old_file_name = file_name;
339 old_line_number = line_number;
345 _IO_flockfile (stderr);
347 __flockfile (stderr);
350 if (error_print_progname)
351 (*error_print_progname) ();
354 #if _LIBC && USE_IN_LIBIO
355 if (_IO_fwide (stderr, 0) > 0)
356 __fwprintf (stderr, L"%s: ", program_name);
359 fprintf (stderr, "%s:", program_name);
362 if (file_name != NULL)
364 #if _LIBC && USE_IN_LIBIO
365 if (_IO_fwide (stderr, 0) > 0)
366 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
369 fprintf (stderr, "%s:%d: ", file_name, line_number);
373 VA_START (args, message);
374 error_tail (status, errnum, message, args);
376 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
378 ++error_message_count;
380 print_errno_message (errnum);
389 _IO_funlockfile (stderr);
391 __funlockfile (stderr);
397 /* Make the weak alias. */
399 # undef error_at_line
400 weak_alias (__error, error)
401 weak_alias (__error_at_line, error_at_line)