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>. */
38 # define mbsrtowcs __mbsrtowcs
42 # include "unlocked-io.h"
46 # define _(String) String
49 /* If NULL, error will flush stdout, then print on stderr the program
50 name, a colon and a space. Otherwise, error will call this
51 function without parameters instead. */
52 void (*error_print_progname) (void);
54 /* This variable is incremented each time `error' is called. */
55 unsigned int error_message_count;
58 /* In the GNU C library, there is a predefined variable for this. */
60 # define program_name program_invocation_name
61 # include <libio/libioP.h>
63 /* In GNU libc we want do not want to use the common name `error' directly.
64 Instead make it a weak alias. */
65 extern void __error (int status, int errnum, const char *message, ...)
66 __attribute__ ((__format__ (__printf__, 3, 4)));
67 extern void __error_at_line (int status, int errnum, const char *file_name,
68 unsigned int line_number, const char *message,
70 __attribute__ ((__format__ (__printf__, 5, 6)));;
71 # define error __error
72 # define error_at_line __error_at_line
74 # include <libio/iolibio.h>
75 # define fflush(s) INTUSE(_IO_fflush) (s)
77 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
81 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
82 # ifndef HAVE_DECL_STRERROR_R
83 "this configure-time declaration test was not run"
88 /* The calling program should define program_name and set it to the
89 name of the executing program. */
90 extern char *program_name;
92 # if HAVE_STRERROR_R || defined strerror_r
93 # define __strerror_r strerror_r
95 #endif /* not _LIBC */
98 print_errno_message (int errnum)
102 #if defined HAVE_STRERROR_R || _LIBC
104 # if STRERROR_R_CHAR_P || _LIBC
105 s = __strerror_r (errnum, errbuf, sizeof errbuf);
107 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
113 s = strerror (errnum);
118 s = _("Unknown system error");
122 if (_IO_fwide (stderr, 0) > 0)
124 __fwprintf (stderr, L": %s", s);
129 fprintf (stderr, ": %s", s);
133 error_tail (int status, int errnum, const char *message, va_list args)
135 #if HAVE_VPRINTF || _LIBC
137 if (_IO_fwide (stderr, 0) > 0)
139 # define ALLOCA_LIMIT 2000
140 size_t len = strlen (message) + 1;
141 wchar_t *wmessage = NULL;
148 if (len < ALLOCA_LIMIT)
149 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
152 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
155 wmessage = (wchar_t *) realloc (wmessage,
156 len * sizeof (wchar_t));
158 if (wmessage == NULL)
160 fputws_unlocked (L"out of memory\n", stderr);
165 memset (&st, '\0', sizeof (st));
168 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
170 if (res == (size_t) -1)
171 /* The string cannot be converted. */
172 wmessage = (wchar_t *) L"???";
174 __vfwprintf (stderr, wmessage, args);
178 vfprintf (stderr, message, args);
180 _doprnt (message, args, stderr);
184 ++error_message_count;
186 print_errno_message (errnum);
188 if (_IO_fwide (stderr, 0) > 0)
189 putwc (L'\n', stderr);
199 /* Print the program name and error message MESSAGE, which is a printf-style
200 format string with optional args.
201 If ERRNUM is nonzero, print its corresponding system error message.
202 Exit with status STATUS if it is nonzero. */
205 error (int status, int errnum, const char *message, ...)
211 _IO_flockfile (stderr);
213 if (error_print_progname)
214 (*error_print_progname) ();
218 if (_IO_fwide (stderr, 0) > 0)
219 __fwprintf (stderr, L"%s: ", program_name);
222 fprintf (stderr, "%s: ", program_name);
225 va_start (args, message);
226 error_tail (status, errnum, message, args);
229 _IO_funlockfile (stderr);
233 /* Sometimes we want to have at most one error per line. This
234 variable controls whether this mode is selected or not. */
235 int error_one_per_line;
238 error_at_line (int status, int errnum, const char *file_name,
239 unsigned int line_number, const char *message, ...)
243 if (error_one_per_line)
245 static const char *old_file_name;
246 static unsigned int old_line_number;
248 if (old_line_number == line_number
249 && (file_name == old_file_name
250 || strcmp (old_file_name, file_name) == 0))
251 /* Simply return and print nothing. */
254 old_file_name = file_name;
255 old_line_number = line_number;
260 _IO_flockfile (stderr);
262 if (error_print_progname)
263 (*error_print_progname) ();
267 if (_IO_fwide (stderr, 0) > 0)
268 __fwprintf (stderr, L"%s: ", program_name);
271 fprintf (stderr, "%s:", program_name);
274 if (file_name != NULL)
277 if (_IO_fwide (stderr, 0) > 0)
278 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
281 fprintf (stderr, "%s:%d: ", file_name, line_number);
284 va_start (args, message);
285 error_tail (status, errnum, message, args);
288 _IO_funlockfile (stderr);
293 /* Make the weak alias. */
295 # undef error_at_line
296 weak_alias (__error, error)
297 weak_alias (__error_at_line, error_at_line)