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
90 # include <libio/iolibio.h>
91 # define fflush(s) INTUSE(_IO_fflush) (s)
93 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
97 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
98 # ifndef HAVE_DECL_STRERROR_R
99 "this configure-time declaration test was not run"
104 /* The calling program should define program_name and set it to the
105 name of the executing program. */
106 extern char *program_name
;
108 # if HAVE_STRERROR_R || defined strerror_r
109 # define __strerror_r strerror_r
112 # ifndef HAVE_DECL_STRERROR
113 "this configure-time declaration test was not run"
115 # if !HAVE_DECL_STRERROR
120 private_strerror (int errnum
)
122 extern char *sys_errlist
[];
125 if (errnum
> 0 && errnum
<= sys_nerr
)
126 return _(sys_errlist
[errnum
]);
127 return _("Unknown system error");
129 # define strerror private_strerror
130 # endif /* HAVE_STRERROR */
131 # endif /* HAVE_STRERROR_R || defined strerror_r */
132 #endif /* not _LIBC */
135 print_errno_message (int errnum
)
139 #if defined HAVE_STRERROR_R || _LIBC
141 # if STRERROR_R_CHAR_P || _LIBC
142 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
144 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
150 s
= strerror (errnum
);
155 s
= _("Unknown system error");
159 if (_IO_fwide (stderr
, 0) > 0)
161 __fwprintf (stderr
, L
": %s", s
);
166 fprintf (stderr
, ": %s", s
);
171 error_tail (int status
, int errnum
, const char *message
, va_list args
)
173 # if HAVE_VPRINTF || _LIBC
175 if (_IO_fwide (stderr
, 0) > 0)
177 # define ALLOCA_LIMIT 2000
178 size_t len
= strlen (message
) + 1;
179 wchar_t *wmessage
= NULL
;
186 if (len
< ALLOCA_LIMIT
)
187 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
190 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
193 wmessage
= (wchar_t *) realloc (wmessage
,
194 len
* sizeof (wchar_t));
196 if (wmessage
== NULL
)
198 fputws_unlocked (L
"out of memory\n", stderr
);
203 memset (&st
, '\0', sizeof (st
));
206 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
208 if (res
== (size_t) -1)
209 /* The string cannot be converted. */
210 wmessage
= (wchar_t *) L
"???";
212 __vfwprintf (stderr
, wmessage
, args
);
216 vfprintf (stderr
, message
, args
);
218 _doprnt (message
, args
, stderr
);
222 ++error_message_count
;
224 print_errno_message (errnum
);
226 if (_IO_fwide (stderr
, 0) > 0)
227 putwc (L
'\n', stderr
);
238 /* Print the program name and error message MESSAGE, which is a printf-style
239 format string with optional args.
240 If ERRNUM is nonzero, print its corresponding system error message.
241 Exit with status STATUS if it is nonzero. */
244 #if defined VA_START && __STDC__
245 error (int status
, int errnum
, const char *message
, ...)
247 error (status
, errnum
, message
, va_alist
)
260 _IO_flockfile (stderr
);
262 if (error_print_progname
)
263 (*error_print_progname
) ();
267 if (_IO_fwide (stderr
, 0) > 0)
268 __fwprintf (stderr
, L
"%s: ", program_name
);
271 fprintf (stderr
, "%s: ", program_name
);
275 VA_START (args
, message
);
276 error_tail (status
, errnum
, message
, args
);
278 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
280 ++error_message_count
;
282 print_errno_message (errnum
);
290 _IO_funlockfile (stderr
);
294 /* Sometimes we want to have at most one error per line. This
295 variable controls whether this mode is selected or not. */
296 int error_one_per_line
;
299 #if defined VA_START && __STDC__
300 error_at_line (int status
, int errnum
, const char *file_name
,
301 unsigned int line_number
, const char *message
, ...)
303 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
306 const char *file_name
;
307 unsigned int line_number
;
316 if (error_one_per_line
)
318 static const char *old_file_name
;
319 static unsigned int old_line_number
;
321 if (old_line_number
== line_number
322 && (file_name
== old_file_name
323 || strcmp (old_file_name
, file_name
) == 0))
324 /* Simply return and print nothing. */
327 old_file_name
= file_name
;
328 old_line_number
= line_number
;
333 _IO_flockfile (stderr
);
335 if (error_print_progname
)
336 (*error_print_progname
) ();
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
)
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
;
365 print_errno_message (errnum
);
373 _IO_funlockfile (stderr
);
378 /* Make the weak alias. */
380 # undef error_at_line
381 weak_alias (__error
, error
)
382 weak_alias (__error_at_line
, error_at_line
)