1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 #if !_LIBC && ENABLE_NLS
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
62 # include <libio/libioP.h>
64 /* In GNU libc we want do not want to use the common name `error' directly.
65 Instead make it a weak alias. */
66 extern void __error (int status, int errnum, const char *message, ...)
67 __attribute__ ((__format__ (__printf__, 3, 4)));
68 extern void __error_at_line (int status, int errnum, const char *file_name,
69 unsigned int line_number, const char *message,
71 __attribute__ ((__format__ (__printf__, 5, 6)));;
72 # define error __error
73 # define error_at_line __error_at_line
75 # include <libio/iolibio.h>
76 # define fflush(s) INTUSE(_IO_fflush) (s)
78 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
80 # include <bits/libc-lock.h>
84 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
85 # ifndef HAVE_DECL_STRERROR_R
86 "this configure-time declaration test was not run"
92 # define SIZE_MAX ((size_t) -1)
95 /* The calling program should define program_name and set it to the
96 name of the executing program. */
97 extern char *program_name;
99 # if HAVE_STRERROR_R || defined strerror_r
100 # define __strerror_r strerror_r
102 #endif /* not _LIBC */
105 print_errno_message (int errnum)
107 char const *s = NULL;
109 #if defined HAVE_STRERROR_R || _LIBC
111 # if STRERROR_R_CHAR_P || _LIBC
112 s = __strerror_r (errnum, errbuf, sizeof errbuf);
114 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
120 if (! s && ! (s = strerror (errnum)))
121 s = _("Unknown system error");
125 if (_IO_fwide (stderr, 0) > 0)
127 __fwprintf (stderr, L": %s", s);
132 fprintf (stderr, ": %s", s);
136 error_tail (int status, int errnum, const char *message, va_list args)
139 if (_IO_fwide (stderr, 0) > 0)
141 # define ALLOCA_LIMIT 2000
142 size_t len = strlen (message) + 1;
143 const wchar_t *wmessage = L"out of memory";
144 wchar_t *wbuf = (len < ALLOCA_LIMIT
145 ? alloca (len * sizeof *wbuf)
146 : len <= SIZE_MAX / sizeof *wbuf
147 ? malloc (len * sizeof *wbuf)
154 const char *tmp = message;
155 memset (&st, '\0', sizeof (st));
156 res = mbsrtowcs (wbuf, &tmp, len, &st);
157 wmessage = res == (size_t) -1 ? L"???" : wbuf;
160 __vfwprintf (stderr, wmessage, args);
161 if (! (len < ALLOCA_LIMIT))
166 vfprintf (stderr, message, args);
169 ++error_message_count;
171 print_errno_message (errnum);
173 if (_IO_fwide (stderr, 0) > 0)
174 putwc (L'\n', stderr);
184 /* Print the program name and error message MESSAGE, which is a printf-style
185 format string with optional args.
186 If ERRNUM is nonzero, print its corresponding system error message.
187 Exit with status STATUS if it is nonzero. */
189 error (int status, int errnum, const char *message, ...)
193 #if defined _LIBC && defined __libc_ptf_call
194 /* We do not want this call to be cut short by a thread
195 cancellation. Therefore disable cancellation for now. */
196 int state = PTHREAD_CANCEL_ENABLE;
197 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
203 _IO_flockfile (stderr);
205 if (error_print_progname)
206 (*error_print_progname) ();
210 if (_IO_fwide (stderr, 0) > 0)
211 __fwprintf (stderr, L"%s: ", program_name);
214 fprintf (stderr, "%s: ", program_name);
217 va_start (args, message);
218 error_tail (status, errnum, message, args);
221 _IO_funlockfile (stderr);
222 # ifdef __libc_ptf_call
223 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
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;
253 #if defined _LIBC && defined __libc_ptf_call
254 /* We do not want this call to be cut short by a thread
255 cancellation. Therefore disable cancellation for now. */
256 int state = PTHREAD_CANCEL_ENABLE;
257 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
263 _IO_flockfile (stderr);
265 if (error_print_progname)
266 (*error_print_progname) ();
270 if (_IO_fwide (stderr, 0) > 0)
271 __fwprintf (stderr, L"%s: ", program_name);
274 fprintf (stderr, "%s:", program_name);
277 if (file_name != NULL)
280 if (_IO_fwide (stderr, 0) > 0)
281 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
284 fprintf (stderr, "%s:%d: ", file_name, line_number);
287 va_start (args, message);
288 error_tail (status, errnum, message, args);
291 _IO_funlockfile (stderr);
292 # ifdef __libc_ptf_call
293 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
299 /* Make the weak alias. */
301 # undef error_at_line
302 weak_alias (__error, error)
303 weak_alias (__error_at_line, error_at_line)