1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
33 # define mbsrtowcs __mbsrtowcs
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
39 # define VA_START(args, lastarg) va_start(args, lastarg)
42 # define VA_START(args, lastarg) va_start(args)
45 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
49 #if STDC_HEADERS || _LIBC
59 # include "unlocked-io.h"
63 # define _(String) String
66 /* If NULL, error will flush stdout, then print on stderr the program
67 name, a colon and a space. Otherwise, error will call this
68 function without parameters instead. */
69 void (*error_print_progname
) (
75 /* This variable is incremented each time `error' is called. */
76 unsigned int error_message_count
;
79 /* In the GNU C library, there is a predefined variable for this. */
81 # define program_name program_invocation_name
83 # include <libio/libioP.h>
85 /* In GNU libc we want do not want to use the common name `error' directly.
86 Instead make it a weak alias. */
87 extern void __error (int status
, int errnum
, const char *message
, ...)
88 __attribute__ ((__format__ (__printf__
, 3, 4)));
89 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
90 unsigned int line_number
, const char *message
,
92 __attribute__ ((__format__ (__printf__
, 5, 6)));;
93 # define error __error
94 # define error_at_line __error_at_line
96 # include <libio/iolibio.h>
97 # define fflush(s) INTUSE(_IO_fflush) (s)
99 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
101 #else /* not _LIBC */
103 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
104 # ifndef HAVE_DECL_STRERROR_R
105 "this configure-time declaration test was not run"
110 /* The calling program should define program_name and set it to the
111 name of the executing program. */
112 extern char *program_name
;
114 # if HAVE_STRERROR_R || defined strerror_r
115 # define __strerror_r strerror_r
118 # ifndef HAVE_DECL_STRERROR
119 "this configure-time declaration test was not run"
121 # if !HAVE_DECL_STRERROR
126 private_strerror (int errnum
)
128 extern char *sys_errlist
[];
131 if (errnum
> 0 && errnum
<= sys_nerr
)
132 return _(sys_errlist
[errnum
]);
133 return _("Unknown system error");
135 # define strerror private_strerror
136 # endif /* HAVE_STRERROR */
137 # endif /* HAVE_STRERROR_R || defined strerror_r */
138 #endif /* not _LIBC */
141 print_errno_message (int errnum
)
145 #if defined HAVE_STRERROR_R || _LIBC
147 # if STRERROR_R_CHAR_P || _LIBC
148 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
150 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
156 s
= strerror (errnum
);
161 s
= _("Unknown system error");
165 if (_IO_fwide (stderr
, 0) > 0)
167 __fwprintf (stderr
, L
": %s", s
);
172 fprintf (stderr
, ": %s", s
);
177 error_tail (int status
, int errnum
, const char *message
, va_list args
)
179 # if HAVE_VPRINTF || _LIBC
181 if (_IO_fwide (stderr
, 0) > 0)
183 # define ALLOCA_LIMIT 2000
184 size_t len
= strlen (message
) + 1;
185 wchar_t *wmessage
= NULL
;
192 if (len
< ALLOCA_LIMIT
)
193 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
196 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
199 wmessage
= (wchar_t *) realloc (wmessage
,
200 len
* sizeof (wchar_t));
202 if (wmessage
== NULL
)
204 fputws_unlocked (L
"out of memory\n", stderr
);
209 memset (&st
, '\0', sizeof (st
));
212 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
214 if (res
== (size_t) -1)
215 /* The string cannot be converted. */
216 wmessage
= (wchar_t *) L
"???";
218 __vfwprintf (stderr
, wmessage
, args
);
222 vfprintf (stderr
, message
, args
);
224 _doprnt (message
, args
, stderr
);
228 ++error_message_count
;
230 print_errno_message (errnum
);
232 if (_IO_fwide (stderr
, 0) > 0)
233 putwc (L
'\n', stderr
);
244 /* Print the program name and error message MESSAGE, which is a printf-style
245 format string with optional args.
246 If ERRNUM is nonzero, print its corresponding system error message.
247 Exit with status STATUS if it is nonzero. */
250 #if defined VA_START && __STDC__
251 error (int status
, int errnum
, const char *message
, ...)
253 error (status
, errnum
, message
, va_alist
)
266 _IO_flockfile (stderr
);
268 if (error_print_progname
)
269 (*error_print_progname
) ();
273 if (_IO_fwide (stderr
, 0) > 0)
274 __fwprintf (stderr
, L
"%s: ", program_name
);
277 fprintf (stderr
, "%s: ", program_name
);
281 VA_START (args
, message
);
282 error_tail (status
, errnum
, message
, args
);
284 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
286 ++error_message_count
;
288 print_errno_message (errnum
);
296 _IO_funlockfile (stderr
);
300 /* Sometimes we want to have at most one error per line. This
301 variable controls whether this mode is selected or not. */
302 int error_one_per_line
;
305 #if defined VA_START && __STDC__
306 error_at_line (int status
, int errnum
, const char *file_name
,
307 unsigned int line_number
, const char *message
, ...)
309 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
312 const char *file_name
;
313 unsigned int line_number
;
322 if (error_one_per_line
)
324 static const char *old_file_name
;
325 static unsigned int old_line_number
;
327 if (old_line_number
== line_number
328 && (file_name
== old_file_name
329 || strcmp (old_file_name
, file_name
) == 0))
330 /* Simply return and print nothing. */
333 old_file_name
= file_name
;
334 old_line_number
= line_number
;
339 _IO_flockfile (stderr
);
341 if (error_print_progname
)
342 (*error_print_progname
) ();
346 if (_IO_fwide (stderr
, 0) > 0)
347 __fwprintf (stderr
, L
"%s: ", program_name
);
350 fprintf (stderr
, "%s:", program_name
);
353 if (file_name
!= NULL
)
356 if (_IO_fwide (stderr
, 0) > 0)
357 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
360 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
364 VA_START (args
, message
);
365 error_tail (status
, errnum
, message
, args
);
367 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
369 ++error_message_count
;
371 print_errno_message (errnum
);
379 _IO_funlockfile (stderr
);
384 /* Make the weak alias. */
386 # undef error_at_line
387 weak_alias (__error
, error
)
388 weak_alias (__error_at_line
, error_at_line
)