diff options
Diffstat (limited to 'misc/error.c')
-rw-r--r-- | misc/error.c | 117 |
1 files changed, 38 insertions, 79 deletions
diff --git a/misc/error.c b/misc/error.c index b4e8b6c938..c407b01e21 100644 --- a/misc/error.c +++ b/misc/error.c @@ -1,5 +1,5 @@ /* Error handler for noninteractive utilities - Copyright (C) 1990-2018 Free Software Foundation, Inc. + Copyright (C) 1990-2019 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -14,7 +14,7 @@ You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see - <http://www.gnu.org/licenses/>. */ + <https://www.gnu.org/licenses/>. */ /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ @@ -200,75 +200,18 @@ print_errno_message (int errnum) } static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3)) -error_tail (int status, int errnum, const char *message, va_list args) +error_tail (int status, int errnum, const char *message, va_list args, + unsigned int mode_flags) { #if _LIBC - if (_IO_fwide (stderr, 0) > 0) - { - size_t len = strlen (message) + 1; - wchar_t *wmessage = NULL; - mbstate_t st; - size_t res; - const char *tmp; - bool use_malloc = false; - - while (1) - { - if (__libc_use_alloca (len * sizeof (wchar_t))) - wmessage = (wchar_t *) alloca (len * sizeof (wchar_t)); - else - { - if (!use_malloc) - wmessage = NULL; - - wchar_t *p = (wchar_t *) realloc (wmessage, - len * sizeof (wchar_t)); - if (p == NULL) - { - free (wmessage); - fputws_unlocked (L"out of memory\n", stderr); - return; - } - wmessage = p; - use_malloc = true; - } - - memset (&st, '\0', sizeof (st)); - tmp = message; - - res = mbsrtowcs (wmessage, &tmp, len, &st); - if (res != len) - break; - - if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0)) - { - /* This really should not happen if everything is fine. */ - res = (size_t) -1; - break; - } - - len *= 2; - } - - if (res == (size_t) -1) - { - /* The string cannot be converted. */ - if (use_malloc) - { - free (wmessage); - use_malloc = false; - } - wmessage = (wchar_t *) L"???"; - } - - __vfwprintf (stderr, wmessage, args); - - if (use_malloc) - free (wmessage); - } - else + int ret = __vfxprintf (stderr, message, args, mode_flags); + if (ret < 0 && errno == ENOMEM && _IO_fwide (stderr, 0) > 0) + /* Leave a trace in case the heap allocation of the message string + failed. */ + fputws_unlocked (L"out of memory\n", stderr); +#else + vfprintf (stderr, message, args); #endif - vfprintf (stderr, message, args); va_end (args); ++error_message_count; @@ -290,10 +233,9 @@ error_tail (int status, int errnum, const char *message, va_list args) If ERRNUM is nonzero, print its corresponding system error message. Exit with status STATUS if it is nonzero. */ void -error (int status, int errnum, const char *message, ...) +__error_internal (int status, int errnum, const char *message, + va_list args, unsigned int mode_flags) { - va_list args; - #if defined _LIBC && defined __libc_ptf_call /* We do not want this call to be cut short by a thread cancellation. Therefore disable cancellation for now. */ @@ -317,8 +259,7 @@ error (int status, int errnum, const char *message, ...) #endif } - va_start (args, message); - error_tail (status, errnum, message, args); + error_tail (status, errnum, message, args, mode_flags); #ifdef _LIBC _IO_funlockfile (stderr); @@ -327,17 +268,25 @@ error (int status, int errnum, const char *message, ...) # endif #endif } + +void +error (int status, int errnum, const char *message, ...) +{ + va_list ap; + va_start (ap, message); + __error_internal (status, errnum, message, ap, 0); + va_end (ap); +} /* Sometimes we want to have at most one error per line. This variable controls whether this mode is selected or not. */ int error_one_per_line; void -error_at_line (int status, int errnum, const char *file_name, - unsigned int line_number, const char *message, ...) +__error_at_line_internal (int status, int errnum, const char *file_name, + unsigned int line_number, const char *message, + va_list args, unsigned int mode_flags) { - va_list args; - if (error_one_per_line) { static const char *old_file_name; @@ -388,8 +337,7 @@ error_at_line (int status, int errnum, const char *file_name, file_name, line_number); #endif - va_start (args, message); - error_tail (status, errnum, message, args); + error_tail (status, errnum, message, args, mode_flags); #ifdef _LIBC _IO_funlockfile (stderr); @@ -399,6 +347,17 @@ error_at_line (int status, int errnum, const char *file_name, #endif } +void +error_at_line (int status, int errnum, const char *file_name, + unsigned int line_number, const char *message, ...) +{ + va_list ap; + va_start (ap, message); + __error_at_line_internal (status, errnum, file_name, line_number, + message, ap, 0); + va_end (ap); +} + #ifdef _LIBC /* Make the weak alias. */ # undef error |