1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2004, 2005 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 # define mbsrtowcs __mbsrtowcs
35 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
38 # define VA_START(args, lastarg) va_start(args, lastarg)
41 # define VA_START(args, lastarg) va_start(args)
44 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
45 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
48 #if STDC_HEADERS || _LIBC
58 # define _(String) String
61 /* If NULL, error will flush stdout, then print on stderr the program
62 name, a colon and a space. Otherwise, error will call this
63 function without parameters instead. */
64 void (*error_print_progname
) (
70 /* This variable is incremented each time `error' is called. */
71 unsigned int error_message_count
;
74 /* In the GNU C library, there is a predefined variable for this. */
76 # define program_name program_invocation_name
79 # include <libio/libioP.h>
81 /* In GNU libc we want do not want to use the common name `error' directly.
82 Instead make it a weak alias. */
83 extern void __error (int status
, int errnum
, const char *message
, ...)
84 __attribute__ ((__format__ (__printf__
, 3, 4)));
85 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
86 unsigned int line_number
, const char *message
,
88 __attribute__ ((__format__ (__printf__
, 5, 6)));;
89 # define error __error
90 # define error_at_line __error_at_line
92 # include <libio/iolibio.h>
93 # define fflush(s) INTUSE(_IO_fflush) (s)
95 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
97 # include <bits/libc-lock.h>
101 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
102 # ifndef HAVE_DECL_STRERROR_R
103 "this configure-time declaration test was not run"
108 /* The calling program should define program_name and set it to the
109 name of the executing program. */
110 extern char *program_name
;
112 # if HAVE_STRERROR_R || defined strerror_r
113 # define __strerror_r strerror_r
116 # ifndef HAVE_DECL_STRERROR
117 "this configure-time declaration test was not run"
119 # if !HAVE_DECL_STRERROR
124 private_strerror (int errnum
)
126 extern char *sys_errlist
[];
129 if (errnum
> 0 && errnum
<= sys_nerr
)
130 return _(sys_errlist
[errnum
]);
131 return _("Unknown system error");
133 # define strerror private_strerror
134 # endif /* HAVE_STRERROR */
135 # endif /* HAVE_STRERROR_R || defined strerror_r */
136 #endif /* not _LIBC */
139 print_errno_message (int errnum
)
143 #if defined HAVE_STRERROR_R || _LIBC
145 # if STRERROR_R_CHAR_P || _LIBC
146 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
148 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
154 s
= strerror (errnum
);
159 s
= _("Unknown system error");
163 __fxprintf (NULL
, ": %s", s
);
165 fprintf (stderr
, ": %s", s
);
171 error_tail (int status
, int errnum
, const char *message
, va_list args
)
173 # if HAVE_VPRINTF || _LIBC
175 if (_IO_fwide (stderr
, 0) > 0)
177 # define ALLOCA_LIMIT 2000
178 size_t len
= strlen (message
) + 1;
179 wchar_t *wmessage
= NULL
;
183 bool use_malloc
= false;
187 if (__libc_use_alloca (len
* sizeof (wchar_t)))
188 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
194 wchar_t *p
= (wchar_t *) realloc (wmessage
,
195 len
* sizeof (wchar_t));
199 fputws_unlocked (L
"out of memory\n", stderr
);
206 memset (&st
, '\0', sizeof (st
));
209 res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
);
213 if (__builtin_expect (len
>= SIZE_MAX
/ 2, 0))
215 /* This really should not happen if everything is fine. */
223 if (res
== (size_t) -1)
225 /* The string cannot be converted. */
228 wmessage
= (wchar_t *) L
"???";
231 __vfwprintf (stderr
, wmessage
, args
);
238 vfprintf (stderr
, message
, args
);
240 _doprnt (message
, args
, stderr
);
244 ++error_message_count
;
246 print_errno_message (errnum
);
248 __fxprintf (NULL
, "\n");
259 /* Print the program name and error message MESSAGE, which is a printf-style
260 format string with optional args.
261 If ERRNUM is nonzero, print its corresponding system error message.
262 Exit with status STATUS if it is nonzero. */
265 #if defined VA_START && __STDC__
266 error (int status
, int errnum
, const char *message
, ...)
268 error (status
, errnum
, message
, va_alist
)
279 #if defined _LIBC && defined __libc_ptf_call
280 /* We do not want this call to be cut short by a thread
281 cancellation. Therefore disable cancellation for now. */
282 int state
= PTHREAD_CANCEL_ENABLE
;
283 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
289 _IO_flockfile (stderr
);
291 if (error_print_progname
)
292 (*error_print_progname
) ();
296 __fxprintf (NULL
, "%s: ", program_name
);
298 fprintf (stderr
, "%s: ", program_name
);
303 VA_START (args
, message
);
304 error_tail (status
, errnum
, message
, args
);
306 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
308 ++error_message_count
;
310 print_errno_message (errnum
);
318 _IO_funlockfile (stderr
);
319 # ifdef __libc_ptf_call
320 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
325 /* Sometimes we want to have at most one error per line. This
326 variable controls whether this mode is selected or not. */
327 int error_one_per_line
;
330 #if defined VA_START && __STDC__
331 error_at_line (int status
, int errnum
, const char *file_name
,
332 unsigned int line_number
, const char *message
, ...)
334 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
337 const char *file_name
;
338 unsigned int line_number
;
347 if (error_one_per_line
)
349 static const char *old_file_name
;
350 static unsigned int old_line_number
;
352 if (old_line_number
== line_number
353 && (file_name
== old_file_name
354 || strcmp (old_file_name
, file_name
) == 0))
355 /* Simply return and print nothing. */
358 old_file_name
= file_name
;
359 old_line_number
= line_number
;
362 #if defined _LIBC && defined __libc_ptf_call
363 /* We do not want this call to be cut short by a thread
364 cancellation. Therefore disable cancellation for now. */
365 int state
= PTHREAD_CANCEL_ENABLE
;
366 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
372 _IO_flockfile (stderr
);
374 if (error_print_progname
)
375 (*error_print_progname
) ();
379 __fxprintf (NULL
, "%s:", program_name
);
381 fprintf (stderr
, "%s:", program_name
);
385 if (file_name
!= NULL
)
388 __fxprintf (NULL
, "%s:%d: ", file_name
, line_number
);
390 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
395 VA_START (args
, message
);
396 error_tail (status
, errnum
, message
, args
);
398 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
400 ++error_message_count
;
402 print_errno_message (errnum
);
410 _IO_funlockfile (stderr
);
411 # ifdef __libc_ptf_call
412 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
418 /* Make the weak alias. */
420 # undef error_at_line
421 weak_alias (__error
, error
)
422 weak_alias (__error_at_line
, error_at_line
)