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>. */
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
78 /* In GNU libc we want do not want to use the common name `error' directly.
79 Instead make it a weak alias. */
80 extern void __error (int status
, int errnum
, const char *message
, ...)
81 __attribute__ ((__format__ (__printf__
, 3, 4)));
82 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
83 unsigned int line_number
, const char *message
,
85 __attribute__ ((__format__ (__printf__
, 5, 6)));;
86 # define error __error
87 # define error_at_line __error_at_line
90 # include <libio/iolibio.h>
91 # define fflush(s) _IO_fflush (s)
96 /* The calling program should define program_name and set it to the
97 name of the executing program. */
98 extern char *program_name
;
100 # ifdef HAVE_STRERROR_R
101 # define __strerror_r strerror_r
104 # ifndef strerror /* On some systems, strerror is a macro */
109 private_strerror (errnum
)
112 extern char *sys_errlist
[];
115 if (errnum
> 0 && errnum
<= sys_nerr
)
116 return _(sys_errlist
[errnum
]);
117 return _("Unknown system error");
119 # define strerror private_strerror
120 # endif /* HAVE_STRERROR */
121 # endif /* HAVE_STRERROR_R */
122 #endif /* not _LIBC */
127 error_tail (int status
, int errnum
, const char *message
, va_list args
)
129 # if HAVE_VPRINTF || _LIBC
130 # if _LIBC && USE_IN_LIBIO
131 if (_IO_fwide (stderr
, 0) > 0)
133 # define ALLOCA_LIMIT 2000
134 size_t len
= strlen (message
) + 1;
135 wchar_t *wmessage
= NULL
;
142 if (len
< ALLOCA_LIMIT
)
143 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
146 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
149 wmessage
= (wchar_t *) realloc (wmessage
,
150 len
* sizeof (wchar_t));
152 if (wmessage
== NULL
)
154 fputws_unlocked (L
"out of memory\n", stderr
);
159 memset (&st
, '\0', sizeof (st
));
162 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
164 if (res
== (size_t) -1)
165 /* The string cannot be converted. */
166 wmessage
= (wchar_t *) L
"???";
168 __vfwprintf (stderr
, wmessage
, args
);
172 vfprintf (stderr
, message
, args
);
174 _doprnt (message
, args
, stderr
);
178 ++error_message_count
;
181 #if defined HAVE_STRERROR_R || _LIBC
183 char *s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
184 # if _LIBC && USE_IN_LIBIO
185 if (_IO_fwide (stderr
, 0) > 0)
186 __fwprintf (stderr
, L
": %s", s
);
189 fprintf (stderr
, ": %s", s
);
191 fprintf (stderr
, ": %s", strerror (errnum
));
194 #if _LIBC && USE_IN_LIBIO
195 if (_IO_fwide (stderr
, 0) > 0)
196 putwc (L
'\n', stderr
);
207 /* Print the program name and error message MESSAGE, which is a printf-style
208 format string with optional args.
209 If ERRNUM is nonzero, print its corresponding system error message.
210 Exit with status STATUS if it is nonzero. */
213 #if defined VA_START && __STDC__
214 error (int status
, int errnum
, const char *message
, ...)
216 error (status
, errnum
, message
, va_alist
)
230 _IO_flockfile (stderr
);
232 __flockfile (stderr
);
235 if (error_print_progname
)
236 (*error_print_progname
) ();
239 #if _LIBC && USE_IN_LIBIO
240 if (_IO_fwide (stderr
, 0) > 0)
241 __fwprintf (stderr
, L
"%s: ", program_name
);
244 fprintf (stderr
, "%s: ", program_name
);
248 VA_START (args
, message
);
249 error_tail (status
, errnum
, message
, args
);
251 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
253 ++error_message_count
;
255 fprintf (stderr
, ": %s", strerror (errnum
));
264 _IO_funlockfile (stderr
);
266 __funlockfile (stderr
);
271 /* Sometimes we want to have at most one error per line. This
272 variable controls whether this mode is selected or not. */
273 int error_one_per_line
;
276 #if defined VA_START && __STDC__
277 error_at_line (int status
, int errnum
, const char *file_name
,
278 unsigned int line_number
, const char *message
, ...)
280 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
283 const char *file_name
;
284 unsigned int line_number
;
293 if (error_one_per_line
)
295 static const char *old_file_name
;
296 static unsigned int old_line_number
;
298 if (old_line_number
== line_number
299 && (file_name
== old_file_name
300 || strcmp (old_file_name
, file_name
) == 0))
301 /* Simply return and print nothing. */
304 old_file_name
= file_name
;
305 old_line_number
= line_number
;
311 _IO_flockfile (stderr
);
313 __flockfile (stderr
);
316 if (error_print_progname
)
317 (*error_print_progname
) ();
320 #if _LIBC && USE_IN_LIBIO
321 if (_IO_fwide (stderr
, 0) > 0)
322 __fwprintf (stderr
, L
"%s: ", program_name
);
325 fprintf (stderr
, "%s:", program_name
);
328 if (file_name
!= NULL
)
330 #if _LIBC && USE_IN_LIBIO
331 if (_IO_fwide (stderr
, 0) > 0)
332 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
335 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
339 VA_START (args
, message
);
340 error_tail (status
, errnum
, message
, args
);
342 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
344 ++error_message_count
;
346 fprintf (stderr
, ": %s", strerror (errnum
));
355 _IO_funlockfile (stderr
);
357 __funlockfile (stderr
);
363 /* Make the weak alias. */
365 # undef error_at_line
366 weak_alias (__error
, error
)
367 weak_alias (__error_at_line
, error_at_line
)