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)
136 if (_IO_fwide (stderr, 0) > 0)
138 # define ALLOCA_LIMIT 2000
139 size_t len = strlen (message) + 1;
140 wchar_t *wmessage = NULL;
147 if (len < ALLOCA_LIMIT)
148 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
151 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
154 wmessage = (wchar_t *) realloc (wmessage,
155 len * sizeof (wchar_t));
157 if (wmessage == NULL)
159 fputws_unlocked (L"out of memory\n", stderr);
164 memset (&st, '\0', sizeof (st));
167 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
169 if (res == (size_t) -1)
170 /* The string cannot be converted. */
171 wmessage = (wchar_t *) L"???";
173 __vfwprintf (stderr, wmessage, args);
177 vfprintf (stderr, message, args);
180 ++error_message_count;
182 print_errno_message (errnum);
184 if (_IO_fwide (stderr, 0) > 0)
185 putwc (L'\n', stderr);
195 /* Print the program name and error message MESSAGE, which is a printf-style
196 format string with optional args.
197 If ERRNUM is nonzero, print its corresponding system error message.
198 Exit with status STATUS if it is nonzero. */
200 error (int status, int errnum, const char *message, ...)
206 _IO_flockfile (stderr);
208 if (error_print_progname)
209 (*error_print_progname) ();
213 if (_IO_fwide (stderr, 0) > 0)
214 __fwprintf (stderr, L"%s: ", program_name);
217 fprintf (stderr, "%s: ", program_name);
220 va_start (args, message);
221 error_tail (status, errnum, message, args);
224 _IO_funlockfile (stderr);
228 /* Sometimes we want to have at most one error per line. This
229 variable controls whether this mode is selected or not. */
230 int error_one_per_line;
233 error_at_line (int status, int errnum, const char *file_name,
234 unsigned int line_number, const char *message, ...)
238 if (error_one_per_line)
240 static const char *old_file_name;
241 static unsigned int old_line_number;
243 if (old_line_number == line_number
244 && (file_name == old_file_name
245 || strcmp (old_file_name, file_name) == 0))
246 /* Simply return and print nothing. */
249 old_file_name = file_name;
250 old_line_number = line_number;
255 _IO_flockfile (stderr);
257 if (error_print_progname)
258 (*error_print_progname) ();
262 if (_IO_fwide (stderr, 0) > 0)
263 __fwprintf (stderr, L"%s: ", program_name);
266 fprintf (stderr, "%s:", program_name);
269 if (file_name != NULL)
272 if (_IO_fwide (stderr, 0) > 0)
273 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
276 fprintf (stderr, "%s:%d: ", file_name, line_number);
279 va_start (args, message);
280 error_tail (status, errnum, message, args);
283 _IO_funlockfile (stderr);
288 /* Make the weak alias. */
290 # undef error_at_line
291 weak_alias (__error, error)
292 weak_alias (__error_at_line, error_at_line)