1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
35 # define mbsrtowcs __mbsrtowcs
41 # define _(String) String
44 /* If NULL, error will flush stdout, then print on stderr the program
45 name, a colon and a space. Otherwise, error will call this
46 function without parameters instead. */
47 void (*error_print_progname
) (void);
49 /* This variable is incremented each time `error' is called. */
50 unsigned int error_message_count
;
53 /* In the GNU C library, there is a predefined variable for this. */
55 # define program_name program_invocation_name
58 # include <libio/libioP.h>
60 /* In GNU libc we want do not want to use the common name `error' directly.
61 Instead make it a weak alias. */
62 extern void __error (int status
, int errnum
, const char *message
, ...)
63 __attribute__ ((__format__ (__printf__
, 3, 4)));
64 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
65 unsigned int line_number
, const char *message
,
67 __attribute__ ((__format__ (__printf__
, 5, 6)));;
68 # define error __error
69 # define error_at_line __error_at_line
71 # include <libio/iolibio.h>
72 # define fflush(s) _IO_fflush (s)
74 # define putc(c, fp) _IO_putc (c, fp)
76 # include <bits/libc-lock.h>
80 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
81 # ifndef HAVE_DECL_STRERROR_R
82 "this configure-time declaration test was not run"
87 /* The calling program should define program_name and set it to the
88 name of the executing program. */
89 extern char *program_name
;
91 # if HAVE_STRERROR_R || defined strerror_r
92 # define __strerror_r strerror_r
93 # endif /* HAVE_STRERROR_R || defined strerror_r */
94 #endif /* not _LIBC */
97 print_errno_message (int errnum
)
101 #if defined HAVE_STRERROR_R || _LIBC
103 # if STRERROR_R_CHAR_P || _LIBC
104 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
106 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
112 s
= strerror (errnum
);
117 s
= _("Unknown system error");
121 __fxprintf (NULL
, ": %s", s
);
123 fprintf (stderr
, ": %s", s
);
128 error_tail (int status
, int errnum
, const char *message
, va_list args
)
131 if (_IO_fwide (stderr
, 0) > 0)
133 # define ALLOCA_LIMIT 2000
134 size_t len
= strlen (message
) + 1;
135 wchar_t *wmessage
= NULL
;
139 bool use_malloc
= false;
143 if (__libc_use_alloca (len
* sizeof (wchar_t)))
144 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
150 wchar_t *p
= (wchar_t *) realloc (wmessage
,
151 len
* sizeof (wchar_t));
155 fputws_unlocked (L
"out of memory\n", stderr
);
162 memset (&st
, '\0', sizeof (st
));
165 res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
);
169 if (__builtin_expect (len
>= SIZE_MAX
/ 2, 0))
171 /* This really should not happen if everything is fine. */
179 if (res
== (size_t) -1)
181 /* The string cannot be converted. */
187 wmessage
= (wchar_t *) L
"???";
190 __vfwprintf (stderr
, wmessage
, args
);
197 vfprintf (stderr
, message
, args
);
200 ++error_message_count
;
202 print_errno_message (errnum
);
204 __fxprintf (NULL
, "\n");
214 /* Print the program name and error message MESSAGE, which is a printf-style
215 format string with optional args.
216 If ERRNUM is nonzero, print its corresponding system error message.
217 Exit with status STATUS if it is nonzero. */
219 error (int status
, int errnum
, const char *message
, ...)
223 #if defined _LIBC && defined __libc_ptf_call
224 /* We do not want this call to be cut short by a thread
225 cancellation. Therefore disable cancellation for now. */
226 int state
= PTHREAD_CANCEL_ENABLE
;
227 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
233 _IO_flockfile (stderr
);
235 if (error_print_progname
)
236 (*error_print_progname
) ();
240 __fxprintf (NULL
, "%s: ", program_name
);
242 fprintf (stderr
, "%s: ", program_name
);
246 va_start (args
, message
);
247 error_tail (status
, errnum
, message
, args
);
250 _IO_funlockfile (stderr
);
251 # ifdef __libc_ptf_call
252 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
257 /* Sometimes we want to have at most one error per line. This
258 variable controls whether this mode is selected or not. */
259 int error_one_per_line
;
262 error_at_line (int status
, int errnum
, const char *file_name
,
263 unsigned int line_number
, const char *message
, ...)
267 if (error_one_per_line
)
269 static const char *old_file_name
;
270 static unsigned int old_line_number
;
272 if (old_line_number
== line_number
273 && (file_name
== old_file_name
274 || (old_file_name
!= NULL
276 && strcmp (old_file_name
, file_name
) == 0)))
277 /* Simply return and print nothing. */
280 old_file_name
= file_name
;
281 old_line_number
= line_number
;
284 #if defined _LIBC && defined __libc_ptf_call
285 /* We do not want this call to be cut short by a thread
286 cancellation. Therefore disable cancellation for now. */
287 int state
= PTHREAD_CANCEL_ENABLE
;
288 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
294 _IO_flockfile (stderr
);
296 if (error_print_progname
)
297 (*error_print_progname
) ();
301 __fxprintf (NULL
, "%s:", program_name
);
303 fprintf (stderr
, "%s:", program_name
);
308 __fxprintf (NULL
, file_name
!= NULL
? "%s:%d: " : " ",
309 file_name
, line_number
);
311 fprintf (stderr
, file_name
!= NULL
? "%s:%d: " : " ",
312 file_name
, line_number
);
315 va_start (args
, message
);
316 error_tail (status
, errnum
, message
, args
);
319 _IO_funlockfile (stderr
);
320 # ifdef __libc_ptf_call
321 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
327 /* Make the weak alias. */
329 # undef error_at_line
330 weak_alias (__error
, error
)
331 weak_alias (__error_at_line
, error_at_line
)