1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-2014 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>. */
32 #if !_LIBC && ENABLE_NLS
34 # define _(msgid) gettext (msgid)
42 # define mbsrtowcs __mbsrtowcs
43 # define USE_UNLOCKED_IO 0
44 # define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
45 # define _GL_ARG_NONNULL(a)
49 # include "unlocked-io.h"
53 # define _(String) String
56 /* If NULL, error will flush stdout, then print on stderr the program
57 name, a colon and a space. Otherwise, error will call this
58 function without parameters instead. */
59 void (*error_print_progname
) (void);
61 /* This variable is incremented each time 'error' is called. */
62 unsigned int error_message_count
;
65 /* In the GNU C library, there is a predefined variable for this. */
67 # define program_name program_invocation_name
70 # include <libio/libioP.h>
72 /* In GNU libc we want do not want to use the common name 'error' directly.
73 Instead make it a weak alias. */
74 extern void __error (int status
, int errnum
, const char *message
, ...)
75 __attribute__ ((__format__ (__printf__
, 3, 4)));
76 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
77 unsigned int line_number
, const char *message
,
79 __attribute__ ((__format__ (__printf__
, 5, 6)));;
80 # define error __error
81 # define error_at_line __error_at_line
83 # include <libio/iolibio.h>
84 # define fflush(s) _IO_fflush (s)
86 # define putc(c, fp) _IO_putc (c, fp)
88 # include <bits/libc-lock.h>
95 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
96 /* Get declarations of the native Windows API functions. */
97 # define WIN32_LEAN_AND_MEAN
99 /* Get _get_osfhandle. */
100 # include "msvc-nothrow.h"
103 /* The gnulib override of fcntl is not needed in this file. */
106 # if !HAVE_DECL_STRERROR_R
107 # ifndef HAVE_DECL_STRERROR_R
108 "this configure-time declaration test was not run"
110 # if STRERROR_R_CHAR_P
117 /* The calling program should define program_name and set it to the
118 name of the executing program. */
119 extern char *program_name
;
121 # if HAVE_STRERROR_R || defined strerror_r
122 # define __strerror_r strerror_r
123 # endif /* HAVE_STRERROR_R || defined strerror_r */
124 #endif /* not _LIBC */
127 /* Return non-zero if FD is open. */
131 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
132 /* On native Windows: The initial state of unassigned standard file
133 descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
134 There is no fcntl, and the gnulib replacement fcntl does not support
136 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
139 # error Please port fcntl to your platform
141 return 0 <= fcntl (fd
, F_GETFL
);
152 # if GNULIB_FREOPEN_SAFER
153 /* Use of gnulib's freopen-safer module normally ensures that
155 whenever stdout is open. */
156 stdout_fd
= STDOUT_FILENO
;
158 /* POSIX states that fileno (stdout) after fclose is unspecified. But in
159 practice it is not a problem, because stdout is statically allocated and
160 the fd of a FILE stream is stored as a field in its allocated memory. */
161 stdout_fd
= fileno (stdout
);
163 /* POSIX states that fflush (stdout) after fclose is unspecified; it
164 is safe in glibc, but not on all other platforms. fflush (NULL)
165 is always defined, but too draconian. */
166 if (0 <= stdout_fd
&& is_open (stdout_fd
))
172 print_errno_message (int errnum
)
176 #if defined HAVE_STRERROR_R || _LIBC
178 # if _LIBC || STRERROR_R_CHAR_P
179 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
181 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
187 s
= strerror (errnum
);
192 s
= _("Unknown system error");
196 __fxprintf (NULL
, ": %s", s
);
198 fprintf (stderr
, ": %s", s
);
202 static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
203 error_tail (int status
, int errnum
, const char *message
, va_list args
)
206 if (_IO_fwide (stderr
, 0) > 0)
208 size_t len
= strlen (message
) + 1;
209 wchar_t *wmessage
= NULL
;
213 bool use_malloc
= false;
217 if (__libc_use_alloca (len
* sizeof (wchar_t)))
218 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
224 wchar_t *p
= (wchar_t *) realloc (wmessage
,
225 len
* sizeof (wchar_t));
229 fputws_unlocked (L
"out of memory\n", stderr
);
236 memset (&st
, '\0', sizeof (st
));
239 res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
);
243 if (__builtin_expect (len
>= SIZE_MAX
/ sizeof (wchar_t) / 2, 0))
245 /* This really should not happen if everything is fine. */
253 if (res
== (size_t) -1)
255 /* The string cannot be converted. */
261 wmessage
= (wchar_t *) L
"???";
264 __vfwprintf (stderr
, wmessage
, args
);
271 vfprintf (stderr
, message
, args
);
274 ++error_message_count
;
276 print_errno_message (errnum
);
278 __fxprintf (NULL
, "\n");
288 /* Print the program name and error message MESSAGE, which is a printf-style
289 format string with optional args.
290 If ERRNUM is nonzero, print its corresponding system error message.
291 Exit with status STATUS if it is nonzero. */
293 error (int status
, int errnum
, const char *message
, ...)
297 #if defined _LIBC && defined __libc_ptf_call
298 /* We do not want this call to be cut short by a thread
299 cancellation. Therefore disable cancellation for now. */
300 int state
= PTHREAD_CANCEL_ENABLE
;
301 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
307 _IO_flockfile (stderr
);
309 if (error_print_progname
)
310 (*error_print_progname
) ();
314 __fxprintf (NULL
, "%s: ", program_name
);
316 fprintf (stderr
, "%s: ", program_name
);
320 va_start (args
, message
);
321 error_tail (status
, errnum
, message
, args
);
324 _IO_funlockfile (stderr
);
325 # ifdef __libc_ptf_call
326 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
331 /* Sometimes we want to have at most one error per line. This
332 variable controls whether this mode is selected or not. */
333 int error_one_per_line
;
336 error_at_line (int status
, int errnum
, const char *file_name
,
337 unsigned int line_number
, const char *message
, ...)
341 if (error_one_per_line
)
343 static const char *old_file_name
;
344 static unsigned int old_line_number
;
346 if (old_line_number
== line_number
347 && (file_name
== old_file_name
348 || (old_file_name
!= NULL
350 && strcmp (old_file_name
, file_name
) == 0)))
352 /* Simply return and print nothing. */
355 old_file_name
= file_name
;
356 old_line_number
= line_number
;
359 #if defined _LIBC && defined __libc_ptf_call
360 /* We do not want this call to be cut short by a thread
361 cancellation. Therefore disable cancellation for now. */
362 int state
= PTHREAD_CANCEL_ENABLE
;
363 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
369 _IO_flockfile (stderr
);
371 if (error_print_progname
)
372 (*error_print_progname
) ();
376 __fxprintf (NULL
, "%s:", program_name
);
378 fprintf (stderr
, "%s:", program_name
);
383 __fxprintf (NULL
, file_name
!= NULL
? "%s:%d: " : " ",
384 file_name
, line_number
);
386 fprintf (stderr
, file_name
!= NULL
? "%s:%d: " : " ",
387 file_name
, line_number
);
390 va_start (args
, message
);
391 error_tail (status
, errnum
, message
, args
);
394 _IO_funlockfile (stderr
);
395 # ifdef __libc_ptf_call
396 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
402 /* Make the weak alias. */
404 # undef error_at_line
405 weak_alias (__error
, error
)
406 weak_alias (__error_at_line
, error_at_line
)