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>. */
31 # define mbsrtowcs __mbsrtowcs
34 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
37 # define VA_START(args, lastarg) va_start(args, lastarg)
40 # define VA_START(args, lastarg) va_start(args)
43 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
44 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47 #if STDC_HEADERS || _LIBC
57 # define _(String) String
60 /* If NULL, error will flush stdout, then print on stderr the program
61 name, a colon and a space. Otherwise, error will call this
62 function without parameters instead. */
63 void (*error_print_progname
) (
69 /* This variable is incremented each time `error' is called. */
70 unsigned int error_message_count
;
73 /* In the GNU C library, there is a predefined variable for this. */
75 # 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
91 # include <libio/iolibio.h>
92 # define fflush(s) INTUSE(_IO_fflush) (s)
94 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
99 /* The calling program should define program_name and set it to the
100 name of the executing program. */
101 extern char *program_name
;
103 # ifdef HAVE_STRERROR_R
104 # define __strerror_r strerror_r
107 # ifndef strerror /* On some systems, strerror is a macro */
112 private_strerror (errnum
)
115 extern char *sys_errlist
[];
118 if (errnum
> 0 && errnum
<= sys_nerr
)
119 return _(sys_errlist
[errnum
]);
120 return _("Unknown system error");
122 # define strerror private_strerror
123 # endif /* HAVE_STRERROR */
124 # endif /* HAVE_STRERROR_R */
125 #endif /* not _LIBC */
130 error_tail (int status
, int errnum
, const char *message
, va_list args
)
132 # if HAVE_VPRINTF || _LIBC
133 # if _LIBC && USE_IN_LIBIO
134 if (_IO_fwide (stderr
, 0) > 0)
136 # define ALLOCA_LIMIT 2000
137 size_t len
= strlen (message
) + 1;
138 wchar_t *wmessage
= NULL
;
145 if (len
< ALLOCA_LIMIT
)
146 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
149 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
152 wmessage
= (wchar_t *) realloc (wmessage
,
153 len
* sizeof (wchar_t));
155 if (wmessage
== NULL
)
157 fputws_unlocked (L
"out of memory\n", stderr
);
162 memset (&st
, '\0', sizeof (st
));
165 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
167 if (res
== (size_t) -1)
168 /* The string cannot be converted. */
169 wmessage
= (wchar_t *) L
"???";
171 __vfwprintf (stderr
, wmessage
, args
);
175 vfprintf (stderr
, message
, args
);
177 _doprnt (message
, args
, stderr
);
181 ++error_message_count
;
184 #if defined HAVE_STRERROR_R || _LIBC
186 char *s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
187 # if _LIBC && USE_IN_LIBIO
188 if (_IO_fwide (stderr
, 0) > 0)
189 __fwprintf (stderr
, L
": %s", s
);
192 fprintf (stderr
, ": %s", s
);
194 fprintf (stderr
, ": %s", strerror (errnum
));
197 #if _LIBC && USE_IN_LIBIO
198 if (_IO_fwide (stderr
, 0) > 0)
199 putwc (L
'\n', stderr
);
210 /* Print the program name and error message MESSAGE, which is a printf-style
211 format string with optional args.
212 If ERRNUM is nonzero, print its corresponding system error message.
213 Exit with status STATUS if it is nonzero. */
216 #if defined VA_START && __STDC__
217 error (int status
, int errnum
, const char *message
, ...)
219 error (status
, errnum
, message
, va_alist
)
233 _IO_flockfile (stderr
);
235 __flockfile (stderr
);
238 if (error_print_progname
)
239 (*error_print_progname
) ();
242 #if _LIBC && USE_IN_LIBIO
243 if (_IO_fwide (stderr
, 0) > 0)
244 __fwprintf (stderr
, L
"%s: ", program_name
);
247 fprintf (stderr
, "%s: ", program_name
);
251 VA_START (args
, message
);
252 error_tail (status
, errnum
, message
, args
);
254 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
256 ++error_message_count
;
258 fprintf (stderr
, ": %s", strerror (errnum
));
267 _IO_funlockfile (stderr
);
269 __funlockfile (stderr
);
274 /* Sometimes we want to have at most one error per line. This
275 variable controls whether this mode is selected or not. */
276 int error_one_per_line
;
279 #if defined VA_START && __STDC__
280 error_at_line (int status
, int errnum
, const char *file_name
,
281 unsigned int line_number
, const char *message
, ...)
283 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
286 const char *file_name
;
287 unsigned int line_number
;
296 if (error_one_per_line
)
298 static const char *old_file_name
;
299 static unsigned int old_line_number
;
301 if (old_line_number
== line_number
302 && (file_name
== old_file_name
303 || strcmp (old_file_name
, file_name
) == 0))
304 /* Simply return and print nothing. */
307 old_file_name
= file_name
;
308 old_line_number
= line_number
;
314 _IO_flockfile (stderr
);
316 __flockfile (stderr
);
319 if (error_print_progname
)
320 (*error_print_progname
) ();
323 #if _LIBC && USE_IN_LIBIO
324 if (_IO_fwide (stderr
, 0) > 0)
325 __fwprintf (stderr
, L
"%s: ", program_name
);
328 fprintf (stderr
, "%s:", program_name
);
331 if (file_name
!= NULL
)
333 #if _LIBC && USE_IN_LIBIO
334 if (_IO_fwide (stderr
, 0) > 0)
335 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
338 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
342 VA_START (args
, message
);
343 error_tail (status
, errnum
, message
, args
);
345 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
347 ++error_message_count
;
349 fprintf (stderr
, ": %s", strerror (errnum
));
358 _IO_funlockfile (stderr
);
360 __funlockfile (stderr
);
366 /* Make the weak alias. */
368 # undef error_at_line
369 weak_alias (__error
, error
)
370 weak_alias (__error_at_line
, error_at_line
)