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
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) INTUSE(_IO_fflush) (s)
93 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
98 /* The calling program should define program_name and set it to the
99 name of the executing program. */
100 extern char *program_name
;
102 # ifdef HAVE_STRERROR_R
103 # define __strerror_r strerror_r
106 # ifndef strerror /* On some systems, strerror is a macro */
111 private_strerror (errnum
)
114 extern char *sys_errlist
[];
117 if (errnum
> 0 && errnum
<= sys_nerr
)
118 return _(sys_errlist
[errnum
]);
119 return _("Unknown system error");
121 # define strerror private_strerror
122 # endif /* HAVE_STRERROR */
123 # endif /* HAVE_STRERROR_R */
124 #endif /* not _LIBC */
129 error_tail (int status
, int errnum
, const char *message
, va_list args
)
131 # if HAVE_VPRINTF || _LIBC
132 # if _LIBC && USE_IN_LIBIO
133 if (_IO_fwide (stderr
, 0) > 0)
135 # define ALLOCA_LIMIT 2000
136 size_t len
= strlen (message
) + 1;
137 wchar_t *wmessage
= NULL
;
144 if (len
< ALLOCA_LIMIT
)
145 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
148 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
151 wmessage
= (wchar_t *) realloc (wmessage
,
152 len
* sizeof (wchar_t));
154 if (wmessage
== NULL
)
156 fputws_unlocked (L
"out of memory\n", stderr
);
161 memset (&st
, '\0', sizeof (st
));
164 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
166 if (res
== (size_t) -1)
167 /* The string cannot be converted. */
168 wmessage
= (wchar_t *) L
"???";
170 __vfwprintf (stderr
, wmessage
, args
);
174 vfprintf (stderr
, message
, args
);
176 _doprnt (message
, args
, stderr
);
180 ++error_message_count
;
183 #if defined HAVE_STRERROR_R || _LIBC
185 char *s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
186 # if _LIBC && USE_IN_LIBIO
187 if (_IO_fwide (stderr
, 0) > 0)
188 __fwprintf (stderr
, L
": %s", s
);
191 fprintf (stderr
, ": %s", s
);
193 fprintf (stderr
, ": %s", strerror (errnum
));
196 #if _LIBC && USE_IN_LIBIO
197 if (_IO_fwide (stderr
, 0) > 0)
198 putwc (L
'\n', stderr
);
209 /* Print the program name and error message MESSAGE, which is a printf-style
210 format string with optional args.
211 If ERRNUM is nonzero, print its corresponding system error message.
212 Exit with status STATUS if it is nonzero. */
215 #if defined VA_START && __STDC__
216 error (int status
, int errnum
, const char *message
, ...)
218 error (status
, errnum
, message
, va_alist
)
232 _IO_flockfile (stderr
);
234 __flockfile (stderr
);
237 if (error_print_progname
)
238 (*error_print_progname
) ();
241 #if _LIBC && USE_IN_LIBIO
242 if (_IO_fwide (stderr
, 0) > 0)
243 __fwprintf (stderr
, L
"%s: ", program_name
);
246 fprintf (stderr
, "%s: ", program_name
);
250 VA_START (args
, message
);
251 error_tail (status
, errnum
, message
, args
);
253 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
255 ++error_message_count
;
257 fprintf (stderr
, ": %s", strerror (errnum
));
266 _IO_funlockfile (stderr
);
268 __funlockfile (stderr
);
273 /* Sometimes we want to have at most one error per line. This
274 variable controls whether this mode is selected or not. */
275 int error_one_per_line
;
278 #if defined VA_START && __STDC__
279 error_at_line (int status
, int errnum
, const char *file_name
,
280 unsigned int line_number
, const char *message
, ...)
282 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
285 const char *file_name
;
286 unsigned int line_number
;
295 if (error_one_per_line
)
297 static const char *old_file_name
;
298 static unsigned int old_line_number
;
300 if (old_line_number
== line_number
301 && (file_name
== old_file_name
302 || strcmp (old_file_name
, file_name
) == 0))
303 /* Simply return and print nothing. */
306 old_file_name
= file_name
;
307 old_line_number
= line_number
;
313 _IO_flockfile (stderr
);
315 __flockfile (stderr
);
318 if (error_print_progname
)
319 (*error_print_progname
) ();
322 #if _LIBC && USE_IN_LIBIO
323 if (_IO_fwide (stderr
, 0) > 0)
324 __fwprintf (stderr
, L
"%s: ", program_name
);
327 fprintf (stderr
, "%s:", program_name
);
330 if (file_name
!= NULL
)
332 #if _LIBC && USE_IN_LIBIO
333 if (_IO_fwide (stderr
, 0) > 0)
334 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
337 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
341 VA_START (args
, message
);
342 error_tail (status
, errnum
, message
, args
);
344 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
346 ++error_message_count
;
348 fprintf (stderr
, ": %s", strerror (errnum
));
357 _IO_funlockfile (stderr
);
359 __funlockfile (stderr
);
365 /* Make the weak alias. */
367 # undef error_at_line
368 weak_alias (__error
, error
)
369 weak_alias (__error_at_line
, error_at_line
)