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 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
22 /* $FreeBSD: src/contrib/tar/lib/error.c,v 1.2.2.1 2002/07/14 13:19:40 sobomax Exp $ */
34 # define mbsrtowcs __mbsrtowcs
37 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
40 # define VA_START(args, lastarg) va_start(args, lastarg)
43 # define VA_START(args, lastarg) va_start(args)
46 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
47 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
50 #if STDC_HEADERS || _LIBC
59 #ifndef HAVE_DECL_STRERROR_R
60 "this configure-time declaration test was not run"
62 #if !HAVE_DECL_STRERROR_R
67 # define _(String) String
70 /* If NULL, error will flush stdout, then print on stderr the program
71 name, a colon and a space. Otherwise, error will call this
72 function without parameters instead. */
73 void (*error_print_progname) (
79 /* This variable is incremented each time `error' is called. */
80 unsigned int error_message_count;
83 /* In the GNU C library, there is a predefined variable for this. */
85 # define program_name program_invocation_name
88 /* In GNU libc we want do not want to use the common name `error' directly.
89 Instead make it a weak alias. */
90 extern void __error (int status, int errnum, const char *message, ...)
91 __attribute__ ((__format__ (__printf__, 3, 4)));
92 extern void __error_at_line (int status, int errnum, const char *file_name,
93 unsigned int line_number, const char *message,
95 __attribute__ ((__format__ (__printf__, 5, 6)));;
96 # define error __error
97 # define error_at_line __error_at_line
100 # include <libio/iolibio.h>
101 # define fflush(s) _IO_fflush (s)
104 #else /* not _LIBC */
106 /* The calling program should define program_name and set it to the
107 name of the executing program. */
108 extern char *program_name;
110 # ifdef HAVE_STRERROR_R
111 # define __strerror_r strerror_r
114 # ifndef strerror /* On some systems, strerror is a macro */
119 private_strerror (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 */
132 #endif /* not _LIBC */
137 error_tail (int status, int errnum, const char *message, va_list args)
139 # if HAVE_VPRINTF || _LIBC
140 # if _LIBC && USE_IN_LIBIO
141 if (_IO_fwide (stderr, 0) > 0)
143 # define ALLOCA_LIMIT 2000
144 size_t len = strlen (message) + 1;
145 wchar_t *wmessage = NULL;
152 if (len < ALLOCA_LIMIT)
153 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
156 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
159 wmessage = (wchar_t *) realloc (wmessage,
160 len * sizeof (wchar_t));
162 if (wmessage == NULL)
164 fputws_unlocked (L"out of memory\n", stderr);
169 memset (&st, '\0', sizeof (st));
172 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
174 if (res == (size_t) -1)
175 /* The string cannot be converted. */
176 wmessage = (wchar_t *) L"???";
178 __vfwprintf (stderr, wmessage, args);
182 vfprintf (stderr, message, args);
184 _doprnt (message, args, stderr);
188 ++error_message_count;
191 # if defined HAVE_STRERROR_R || _LIBC
194 /* Don't use __strerror_r's return value because on some systems
195 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
196 (void)__strerror_r (errnum, errbuf, sizeof errbuf);
198 # if _LIBC && USE_IN_LIBIO
199 if (_IO_fwide (stderr, 0) > 0)
200 __fwprintf (stderr, L": %s", s);
203 fprintf (stderr, ": %s", s);
205 fprintf (stderr, ": %s", strerror (errnum));
208 # if _LIBC && USE_IN_LIBIO
209 if (_IO_fwide (stderr, 0) > 0)
210 putwc (L'\n', stderr);
221 /* Print the program name and error message MESSAGE, which is a printf-style
222 format string with optional args.
223 If ERRNUM is nonzero, print its corresponding system error message.
224 Exit with status STATUS if it is nonzero. */
227 #if defined VA_START && __STDC__
228 error (int status, int errnum, const char *message, ...)
230 error (status, errnum, message, va_alist)
244 _IO_flockfile (stderr);
246 __flockfile (stderr);
249 if (error_print_progname)
250 (*error_print_progname) ();
253 #if _LIBC && USE_IN_LIBIO
254 if (_IO_fwide (stderr, 0) > 0)
255 __fwprintf (stderr, L"%s: ", program_name);
258 fprintf (stderr, "%s: ", program_name);
262 VA_START (args, message);
263 error_tail (status, errnum, message, args);
265 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
267 ++error_message_count;
270 # if defined HAVE_STRERROR_R || _LIBC
272 /* Don't use __strerror_r's return value because on some systems
273 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
274 __strerror_r (errnum, errbuf, sizeof errbuf);
275 fprintf (stderr, ": %s", errbuf);
277 fprintf (stderr, ": %s", strerror (errnum));
288 _IO_funlockfile (stderr);
290 __funlockfile (stderr);
295 /* Sometimes we want to have at most one error per line. This
296 variable controls whether this mode is selected or not. */
297 int error_one_per_line;
300 #if defined VA_START && __STDC__
301 error_at_line (int status, int errnum, const char *file_name,
302 unsigned int line_number, const char *message, ...)
304 error_at_line (status, errnum, file_name, line_number, message, va_alist)
307 const char *file_name;
308 unsigned int line_number;
317 if (error_one_per_line)
319 static const char *old_file_name;
320 static unsigned int old_line_number;
322 if (old_line_number == line_number
323 && (file_name == old_file_name
324 || strcmp (old_file_name, file_name) == 0))
325 /* Simply return and print nothing. */
328 old_file_name = file_name;
329 old_line_number = line_number;
335 _IO_flockfile (stderr);
337 __flockfile (stderr);
340 if (error_print_progname)
341 (*error_print_progname) ();
344 #if _LIBC && USE_IN_LIBIO
345 if (_IO_fwide (stderr, 0) > 0)
346 __fwprintf (stderr, L"%s: ", program_name);
349 fprintf (stderr, "%s:", program_name);
352 if (file_name != NULL)
354 #if _LIBC && USE_IN_LIBIO
355 if (_IO_fwide (stderr, 0) > 0)
356 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
359 fprintf (stderr, "%s:%d: ", file_name, line_number);
363 VA_START (args, message);
364 error_tail (status, errnum, message, args);
366 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
368 ++error_message_count;
371 # if defined HAVE_STRERROR_R || _LIBC
373 /* Don't use __strerror_r's return value because on some systems
374 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
375 __strerror_r (errnum, errbuf, sizeof errbuf);
376 fprintf (stderr, ": %s", errbuf);
378 fprintf (stderr, ": %s", strerror (errnum));
389 _IO_funlockfile (stderr);
391 __funlockfile (stderr);
397 /* Make the weak alias. */
399 # undef error_at_line
400 weak_alias (__error, error)
401 weak_alias (__error_at_line, error_at_line)