1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001 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>. */
33 # define mbsrtowcs __mbsrtowcs
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
39 # define VA_START(args, lastarg) va_start(args, lastarg)
42 # define VA_START(args, lastarg) va_start(args)
45 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
49 #if STDC_HEADERS || _LIBC
58 #ifndef HAVE_DECL_STRERROR_R
59 "this configure-time declaration test was not run"
61 #if !HAVE_DECL_STRERROR_R
66 # define _(String) String
69 /* If NULL, error will flush stdout, then print on stderr the program
70 name, a colon and a space. Otherwise, error will call this
71 function without parameters instead. */
72 void (*error_print_progname) (
78 /* This variable is incremented each time `error' is called. */
79 unsigned int error_message_count;
82 /* In the GNU C library, there is a predefined variable for this. */
84 # define program_name program_invocation_name
87 /* In GNU libc we want do not want to use the common name `error' directly.
88 Instead make it a weak alias. */
89 extern void __error (int status, int errnum, const char *message, ...)
90 __attribute__ ((__format__ (__printf__, 3, 4)));
91 extern void __error_at_line (int status, int errnum, const char *file_name,
92 unsigned int line_number, const char *message,
94 __attribute__ ((__format__ (__printf__, 5, 6)));;
95 # define error __error
96 # define error_at_line __error_at_line
99 # include <libio/iolibio.h>
100 # define fflush(s) _IO_fflush (s)
103 #else /* not _LIBC */
105 /* The calling program should define program_name and set it to the
106 name of the executing program. */
107 extern char *program_name;
109 # ifdef HAVE_STRERROR_R
110 # define __strerror_r strerror_r
113 # ifndef strerror /* On some systems, strerror is a macro */
118 private_strerror (errnum)
121 extern char *sys_errlist[];
124 if (errnum > 0 && errnum <= sys_nerr)
125 return _(sys_errlist[errnum]);
126 return _("Unknown system error");
128 # define strerror private_strerror
129 # endif /* HAVE_STRERROR */
130 # endif /* HAVE_STRERROR_R */
131 #endif /* not _LIBC */
136 error_tail (int status, int errnum, const char *message, va_list args)
138 # if HAVE_VPRINTF || _LIBC
139 # if _LIBC && USE_IN_LIBIO
140 if (_IO_fwide (stderr, 0) > 0)
142 # define ALLOCA_LIMIT 2000
143 size_t len = strlen (message) + 1;
144 wchar_t *wmessage = NULL;
151 if (len < ALLOCA_LIMIT)
152 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
155 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
158 wmessage = (wchar_t *) realloc (wmessage,
159 len * sizeof (wchar_t));
161 if (wmessage == NULL)
163 fputws_unlocked (L"out of memory\n", stderr);
168 memset (&st, '\0', sizeof (st));
171 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
173 if (res == (size_t) -1)
174 /* The string cannot be converted. */
175 wmessage = (wchar_t *) L"???";
177 __vfwprintf (stderr, wmessage, args);
181 vfprintf (stderr, message, args);
183 _doprnt (message, args, stderr);
187 ++error_message_count;
190 # if defined HAVE_STRERROR_R || _LIBC
192 char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
193 # if _LIBC && USE_IN_LIBIO
194 if (_IO_fwide (stderr, 0) > 0)
195 __fwprintf (stderr, L": %s", s);
198 fprintf (stderr, ": %s", s);
200 fprintf (stderr, ": %s", strerror (errnum));
203 # if _LIBC && USE_IN_LIBIO
204 if (_IO_fwide (stderr, 0) > 0)
205 putwc (L'\n', stderr);
216 /* Print the program name and error message MESSAGE, which is a printf-style
217 format string with optional args.
218 If ERRNUM is nonzero, print its corresponding system error message.
219 Exit with status STATUS if it is nonzero. */
222 #if defined VA_START && __STDC__
223 error (int status, int errnum, const char *message, ...)
225 error (status, errnum, message, va_alist)
239 _IO_flockfile (stderr);
241 __flockfile (stderr);
244 if (error_print_progname)
245 (*error_print_progname) ();
248 #if _LIBC && USE_IN_LIBIO
249 if (_IO_fwide (stderr, 0) > 0)
250 __fwprintf (stderr, L"%s: ", program_name);
253 fprintf (stderr, "%s: ", program_name);
257 VA_START (args, message);
258 error_tail (status, errnum, message, args);
260 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
262 ++error_message_count;
265 # if defined HAVE_STRERROR_R || _LIBC
267 /* Don't use __strerror_r's return value because on some systems
268 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
269 __strerror_r (errnum, errbuf, sizeof errbuf);
270 fprintf (stderr, ": %s", errbuf);
272 fprintf (stderr, ": %s", strerror (errnum));
283 _IO_funlockfile (stderr);
285 __funlockfile (stderr);
290 /* Sometimes we want to have at most one error per line. This
291 variable controls whether this mode is selected or not. */
292 int error_one_per_line;
295 #if defined VA_START && __STDC__
296 error_at_line (int status, int errnum, const char *file_name,
297 unsigned int line_number, const char *message, ...)
299 error_at_line (status, errnum, file_name, line_number, message, va_alist)
302 const char *file_name;
303 unsigned int line_number;
312 if (error_one_per_line)
314 static const char *old_file_name;
315 static unsigned int old_line_number;
317 if (old_line_number == line_number
318 && (file_name == old_file_name
319 || strcmp (old_file_name, file_name) == 0))
320 /* Simply return and print nothing. */
323 old_file_name = file_name;
324 old_line_number = line_number;
330 _IO_flockfile (stderr);
332 __flockfile (stderr);
335 if (error_print_progname)
336 (*error_print_progname) ();
339 #if _LIBC && USE_IN_LIBIO
340 if (_IO_fwide (stderr, 0) > 0)
341 __fwprintf (stderr, L"%s: ", program_name);
344 fprintf (stderr, "%s:", program_name);
347 if (file_name != NULL)
349 #if _LIBC && USE_IN_LIBIO
350 if (_IO_fwide (stderr, 0) > 0)
351 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
354 fprintf (stderr, "%s:%d: ", file_name, line_number);
358 VA_START (args, message);
359 error_tail (status, errnum, message, args);
361 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
363 ++error_message_count;
366 # if defined HAVE_STRERROR_R || _LIBC
368 /* Don't use __strerror_r's return value because on some systems
369 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
370 __strerror_r (errnum, errbuf, sizeof errbuf);
371 fprintf (stderr, ": %s", errbuf);
373 fprintf (stderr, ": %s", strerror (errnum));
384 _IO_funlockfile (stderr);
386 __funlockfile (stderr);
392 /* Make the weak alias. */
394 # undef error_at_line
395 weak_alias (__error, error)
396 weak_alias (__error_at_line, error_at_line)