1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2007 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
34 # define _(msgid) gettext (msgid)
42 # define mbsrtowcs __mbsrtowcs
46 # include "unlocked-io.h"
50 # define _(String) String
53 /* If NULL, error will flush stdout, then print on stderr the program
54 name, a colon and a space. Otherwise, error will call this
55 function without parameters instead. */
56 void (*error_print_progname
) (void);
58 /* This variable is incremented each time `error' is called. */
59 unsigned int error_message_count
;
62 /* In the GNU C library, there is a predefined variable for this. */
64 # define program_name program_invocation_name
67 # include <libio/libioP.h>
69 /* In GNU libc we want do not want to use the common name `error' directly.
70 Instead make it a weak alias. */
71 extern void __error (int status
, int errnum
, const char *message
, ...)
72 __attribute__ ((__format__ (__printf__
, 3, 4)));
73 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
74 unsigned int line_number
, const char *message
,
76 __attribute__ ((__format__ (__printf__
, 5, 6)));;
77 # define error __error
78 # define error_at_line __error_at_line
80 # include <libio/iolibio.h>
81 # define fflush(s) INTUSE(_IO_fflush) (s)
83 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
85 # include <bits/libc-lock.h>
89 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
90 # ifndef HAVE_DECL_STRERROR_R
91 "this configure-time declaration test was not run"
96 /* The calling program should define program_name and set it to the
97 name of the executing program. */
98 extern char *program_name
;
100 # if HAVE_STRERROR_R || defined strerror_r
101 # define __strerror_r strerror_r
102 # endif /* HAVE_STRERROR_R || defined strerror_r */
103 #endif /* not _LIBC */
106 print_errno_message (int errnum
)
110 #if defined HAVE_STRERROR_R || _LIBC
112 # if STRERROR_R_CHAR_P || _LIBC
113 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
115 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
121 s
= strerror (errnum
);
126 s
= _("Unknown system error");
130 __fxprintf (NULL
, ": %s", s
);
132 fprintf (stderr
, ": %s", s
);
137 error_tail (int status
, int errnum
, const char *message
, va_list args
)
140 if (_IO_fwide (stderr
, 0) > 0)
142 # define ALLOCA_LIMIT 2000
143 size_t len
= strlen (message
) + 1;
144 wchar_t *wmessage
= NULL
;
148 bool use_malloc
= false;
152 if (__libc_use_alloca (len
* sizeof (wchar_t)))
153 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
159 wchar_t *p
= (wchar_t *) realloc (wmessage
,
160 len
* sizeof (wchar_t));
164 fputws_unlocked (L
"out of memory\n", stderr
);
171 memset (&st
, '\0', sizeof (st
));
174 res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
);
178 if (__builtin_expect (len
>= SIZE_MAX
/ 2, 0))
180 /* This really should not happen if everything is fine. */
188 if (res
== (size_t) -1)
190 /* The string cannot be converted. */
196 wmessage
= (wchar_t *) L
"???";
199 __vfwprintf (stderr
, wmessage
, args
);
206 vfprintf (stderr
, message
, args
);
209 ++error_message_count
;
211 print_errno_message (errnum
);
213 __fxprintf (NULL
, "\n");
223 /* Print the program name and error message MESSAGE, which is a printf-style
224 format string with optional args.
225 If ERRNUM is nonzero, print its corresponding system error message.
226 Exit with status STATUS if it is nonzero. */
228 error (int status
, int errnum
, const char *message
, ...)
232 #if defined _LIBC && defined __libc_ptf_call
233 /* We do not want this call to be cut short by a thread
234 cancellation. Therefore disable cancellation for now. */
235 int state
= PTHREAD_CANCEL_ENABLE
;
236 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
242 _IO_flockfile (stderr
);
244 if (error_print_progname
)
245 (*error_print_progname
) ();
249 __fxprintf (NULL
, "%s: ", program_name
);
251 fprintf (stderr
, "%s: ", program_name
);
255 va_start (args
, message
);
256 error_tail (status
, errnum
, message
, args
);
259 _IO_funlockfile (stderr
);
260 # ifdef __libc_ptf_call
261 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
266 /* Sometimes we want to have at most one error per line. This
267 variable controls whether this mode is selected or not. */
268 int error_one_per_line
;
271 error_at_line (int status
, int errnum
, const char *file_name
,
272 unsigned int line_number
, const char *message
, ...)
276 if (error_one_per_line
)
278 static const char *old_file_name
;
279 static unsigned int old_line_number
;
281 if (old_line_number
== line_number
282 && (file_name
== old_file_name
283 || strcmp (old_file_name
, file_name
) == 0))
284 /* Simply return and print nothing. */
287 old_file_name
= file_name
;
288 old_line_number
= line_number
;
291 #if defined _LIBC && defined __libc_ptf_call
292 /* We do not want this call to be cut short by a thread
293 cancellation. Therefore disable cancellation for now. */
294 int state
= PTHREAD_CANCEL_ENABLE
;
295 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
301 _IO_flockfile (stderr
);
303 if (error_print_progname
)
304 (*error_print_progname
) ();
308 __fxprintf (NULL
, "%s:", program_name
);
310 fprintf (stderr
, "%s:", program_name
);
315 __fxprintf (NULL
, file_name
!= NULL
? "%s:%d: " : " ",
316 file_name
, line_number
);
318 fprintf (stderr
, file_name
!= NULL
? "%s:%d: " : " ",
319 file_name
, line_number
);
322 va_start (args
, message
);
323 error_tail (status
, errnum
, message
, args
);
326 _IO_funlockfile (stderr
);
327 # ifdef __libc_ptf_call
328 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
334 /* Make the weak alias. */
336 # undef error_at_line
337 weak_alias (__error
, error
)
338 weak_alias (__error_at_line
, error_at_line
)