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 $ */
23 /* $DragonFly: src/contrib/tar/lib/Attic/error.c,v 1.2 2003/06/17 04:24:06 dillon Exp $ */
35 # define mbsrtowcs __mbsrtowcs
38 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
41 # define VA_START(args, lastarg) va_start(args, lastarg)
44 # define VA_START(args, lastarg) va_start(args)
47 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
48 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
51 #if STDC_HEADERS || _LIBC
60 #ifndef HAVE_DECL_STRERROR_R
61 "this configure-time declaration test was not run"
63 #if !HAVE_DECL_STRERROR_R
68 # define _(String) String
71 /* If NULL, error will flush stdout, then print on stderr the program
72 name, a colon and a space. Otherwise, error will call this
73 function without parameters instead. */
74 void (*error_print_progname) (
80 /* This variable is incremented each time `error' is called. */
81 unsigned int error_message_count;
84 /* In the GNU C library, there is a predefined variable for this. */
86 # define program_name program_invocation_name
89 /* In GNU libc we want do not want to use the common name `error' directly.
90 Instead make it a weak alias. */
91 extern void __error (int status, int errnum, const char *message, ...)
92 __attribute__ ((__format__ (__printf__, 3, 4)));
93 extern void __error_at_line (int status, int errnum, const char *file_name,
94 unsigned int line_number, const char *message,
96 __attribute__ ((__format__ (__printf__, 5, 6)));;
97 # define error __error
98 # define error_at_line __error_at_line
101 # include <libio/iolibio.h>
102 # define fflush(s) _IO_fflush (s)
105 #else /* not _LIBC */
107 /* The calling program should define program_name and set it to the
108 name of the executing program. */
109 extern char *program_name;
111 # ifdef HAVE_STRERROR_R
112 # define __strerror_r strerror_r
115 # ifndef strerror /* On some systems, strerror is a macro */
120 private_strerror (errnum)
123 extern char *sys_errlist[];
126 if (errnum > 0 && errnum <= sys_nerr)
127 return _(sys_errlist[errnum]);
128 return _("Unknown system error");
130 # define strerror private_strerror
131 # endif /* HAVE_STRERROR */
132 # endif /* HAVE_STRERROR_R */
133 #endif /* not _LIBC */
138 error_tail (int status, int errnum, const char *message, va_list args)
140 # if HAVE_VPRINTF || _LIBC
141 # if _LIBC && USE_IN_LIBIO
142 if (_IO_fwide (stderr, 0) > 0)
144 # define ALLOCA_LIMIT 2000
145 size_t len = strlen (message) + 1;
146 wchar_t *wmessage = NULL;
153 if (len < ALLOCA_LIMIT)
154 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
157 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
160 wmessage = (wchar_t *) realloc (wmessage,
161 len * sizeof (wchar_t));
163 if (wmessage == NULL)
165 fputws_unlocked (L"out of memory\n", stderr);
170 memset (&st, '\0', sizeof (st));
173 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
175 if (res == (size_t) -1)
176 /* The string cannot be converted. */
177 wmessage = (wchar_t *) L"???";
179 __vfwprintf (stderr, wmessage, args);
183 vfprintf (stderr, message, args);
185 _doprnt (message, args, stderr);
189 ++error_message_count;
192 # if defined HAVE_STRERROR_R || _LIBC
195 /* Don't use __strerror_r's return value because on some systems
196 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
197 (void)__strerror_r (errnum, errbuf, sizeof errbuf);
199 # if _LIBC && USE_IN_LIBIO
200 if (_IO_fwide (stderr, 0) > 0)
201 __fwprintf (stderr, L": %s", s);
204 fprintf (stderr, ": %s", s);
206 fprintf (stderr, ": %s", strerror (errnum));
209 # if _LIBC && USE_IN_LIBIO
210 if (_IO_fwide (stderr, 0) > 0)
211 putwc (L'\n', stderr);
222 /* Print the program name and error message MESSAGE, which is a printf-style
223 format string with optional args.
224 If ERRNUM is nonzero, print its corresponding system error message.
225 Exit with status STATUS if it is nonzero. */
228 #if defined VA_START && __STDC__
229 error (int status, int errnum, const char *message, ...)
231 error (status, errnum, message, va_alist)
245 _IO_flockfile (stderr);
247 __flockfile (stderr);
250 if (error_print_progname)
251 (*error_print_progname) ();
254 #if _LIBC && USE_IN_LIBIO
255 if (_IO_fwide (stderr, 0) > 0)
256 __fwprintf (stderr, L"%s: ", program_name);
259 fprintf (stderr, "%s: ", program_name);
263 VA_START (args, message);
264 error_tail (status, errnum, message, args);
266 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
268 ++error_message_count;
271 # if defined HAVE_STRERROR_R || _LIBC
273 /* Don't use __strerror_r's return value because on some systems
274 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
275 __strerror_r (errnum, errbuf, sizeof errbuf);
276 fprintf (stderr, ": %s", errbuf);
278 fprintf (stderr, ": %s", strerror (errnum));
289 _IO_funlockfile (stderr);
291 __funlockfile (stderr);
296 /* Sometimes we want to have at most one error per line. This
297 variable controls whether this mode is selected or not. */
298 int error_one_per_line;
301 #if defined VA_START && __STDC__
302 error_at_line (int status, int errnum, const char *file_name,
303 unsigned int line_number, const char *message, ...)
305 error_at_line (status, errnum, file_name, line_number, message, va_alist)
308 const char *file_name;
309 unsigned int line_number;
318 if (error_one_per_line)
320 static const char *old_file_name;
321 static unsigned int old_line_number;
323 if (old_line_number == line_number
324 && (file_name == old_file_name
325 || strcmp (old_file_name, file_name) == 0))
326 /* Simply return and print nothing. */
329 old_file_name = file_name;
330 old_line_number = line_number;
336 _IO_flockfile (stderr);
338 __flockfile (stderr);
341 if (error_print_progname)
342 (*error_print_progname) ();
345 #if _LIBC && USE_IN_LIBIO
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)
355 #if _LIBC && USE_IN_LIBIO
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;
372 # if defined HAVE_STRERROR_R || _LIBC
374 /* Don't use __strerror_r's return value because on some systems
375 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
376 __strerror_r (errnum, errbuf, sizeof errbuf);
377 fprintf (stderr, ": %s", errbuf);
379 fprintf (stderr, ": %s", strerror (errnum));
390 _IO_funlockfile (stderr);
392 __funlockfile (stderr);
398 /* Make the weak alias. */
400 # undef error_at_line
401 weak_alias (__error, error)
402 weak_alias (__error_at_line, error_at_line)