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 #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 /* In GNU libc we want do not want to use the common name `error' directly.
78 Instead make it a weak alias. */
79 extern void __error (int status, int errnum, const char *message, ...)
80 __attribute__ ((__format__ (__printf__, 3, 4)));
81 extern void __error_at_line (int status, int errnum, const char *file_name,
82 unsigned int line_number, const char *message,
84 __attribute__ ((__format__ (__printf__, 5, 6)));;
85 # define error __error
86 # define error_at_line __error_at_line
89 # include <libio/iolibio.h>
90 # define fflush(s) _IO_fflush (s)
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 # ifdef HAVE_STRERROR_R
100 # define __strerror_r strerror_r
103 # ifndef strerror /* On some systems, strerror is a macro */
108 private_strerror (errnum)
111 extern char *sys_errlist[];
114 if (errnum > 0 && errnum <= sys_nerr)
115 return _(sys_errlist[errnum]);
116 return _("Unknown system error");
118 # define strerror private_strerror
119 # endif /* HAVE_STRERROR */
120 # endif /* HAVE_STRERROR_R */
121 #endif /* not _LIBC */
126 error_tail (int status, int errnum, const char *message, va_list args)
128 # if HAVE_VPRINTF || _LIBC
130 if (_IO_fwide (stderr, 0) > 0)
132 # define ALLOCA_LIMIT 2000
133 size_t len = strlen (message) + 1;
134 wchar_t *wmessage = NULL;
141 if (len < ALLOCA_LIMIT)
142 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
145 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
148 wmessage = (wchar_t *) realloc (wmessage,
149 len * sizeof (wchar_t));
151 if (wmessage == NULL)
153 fputws (L"out of memory\n", stderr);
158 memset (&st, '\0', sizeof (st));
161 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
163 if (res == (size_t) -1)
164 /* The string cannot be converted. */
165 wmessage = (wchar_t *) L"???";
167 __vfwprintf (stderr, wmessage, args);
171 vfprintf (stderr, message, args);
173 _doprnt (message, args, stderr);
177 ++error_message_count;
180 #if defined HAVE_STRERROR_R || _LIBC
182 char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
184 if (_IO_fwide (stderr, 0) > 0)
185 __fwprintf (stderr, L": %s", s);
188 fprintf (stderr, ": %s", s);
190 fprintf (stderr, ": %s", strerror (errnum));
194 if (_IO_fwide (stderr, 0) > 0)
195 putwc (L'\n', stderr);
206 /* Print the program name and error message MESSAGE, which is a printf-style
207 format string with optional args.
208 If ERRNUM is nonzero, print its corresponding system error message.
209 Exit with status STATUS if it is nonzero. */
212 #if defined VA_START && __STDC__
213 error (int status, int errnum, const char *message, ...)
215 error (status, errnum, message, va_alist)
230 if (error_print_progname)
231 (*error_print_progname) ();
235 if (_IO_fwide (stderr, 0) > 0)
236 __fwprintf (stderr, L"%s: ", program_name);
239 fprintf (stderr, "%s: ", program_name);
243 VA_START (args, message);
244 error_tail (status, errnum, message, args);
246 funlockfile (stderr);
249 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
251 ++error_message_count;
253 fprintf (stderr, ": %s", strerror (errnum));
261 /* Sometimes we want to have at most one error per line. This
262 variable controls whether this mode is selected or not. */
263 int error_one_per_line;
266 #if defined VA_START && __STDC__
267 error_at_line (int status, int errnum, const char *file_name,
268 unsigned int line_number, const char *message, ...)
270 error_at_line (status, errnum, file_name, line_number, message, va_alist)
273 const char *file_name;
274 unsigned int line_number;
283 if (error_one_per_line)
285 static const char *old_file_name;
286 static unsigned int old_line_number;
288 if (old_line_number == line_number
289 && (file_name == old_file_name
290 || strcmp (old_file_name, file_name) == 0))
291 /* Simply return and print nothing. */
294 old_file_name = file_name;
295 old_line_number = line_number;
302 if (error_print_progname)
303 (*error_print_progname) ();
307 if (_IO_fwide (stderr, 0) > 0)
308 __fwprintf (stderr, L"%s: ", program_name);
311 fprintf (stderr, "%s:", program_name);
314 if (file_name != NULL)
317 if (_IO_fwide (stderr, 0) > 0)
318 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
321 fprintf (stderr, "%s:%d: ", file_name, line_number);
325 VA_START (args, message);
326 error_tail (status, errnum, message, args);
328 funlockfile (stderr);
331 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
333 ++error_message_count;
335 fprintf (stderr, ": %s", strerror (errnum));
344 /* Make the weak alias. */
346 # undef error_at_line
347 weak_alias (__error, error)
348 weak_alias (__error_at_line, error_at_line)