1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2007, 2009-2016 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 3 of the License, or
8 (at your option) any later version.
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
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
31 #if !_LIBC && ENABLE_NLS
33 # define _(msgid) gettext (msgid)
41 # define mbsrtowcs __mbsrtowcs
42 # define USE_UNLOCKED_IO 0
43 # define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
44 # define _GL_ARG_NONNULL(a)
46 # include "getprogname.h"
50 # include "unlocked-io.h"
54 # define _(String) String
57 /* If NULL, error will flush stdout, then print on stderr the program
58 name, a colon and a space. Otherwise, error will call this
59 function without parameters instead. */
60 void (*error_print_progname) (void);
62 /* This variable is incremented each time 'error' is called. */
63 unsigned int error_message_count;
66 /* In the GNU C library, there is a predefined variable for this. */
68 # define program_name program_invocation_name
71 # include <libio/libioP.h>
73 /* In GNU libc we want do not want to use the common name 'error' directly.
74 Instead make it a weak alias. */
75 extern void __error (int status, int errnum, const char *message, ...)
76 __attribute__ ((__format__ (__printf__, 3, 4)));
77 extern void __error_at_line (int status, int errnum, const char *file_name,
78 unsigned int line_number, const char *message,
80 __attribute__ ((__format__ (__printf__, 5, 6)));
81 # define error __error
82 # define error_at_line __error_at_line
84 # include <libio/iolibio.h>
85 # define fflush(s) _IO_fflush (s)
87 # define putc(c, fp) _IO_putc (c, fp)
89 # include <bits/libc-lock.h>
96 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
97 /* Get declarations of the native Windows API functions. */
98 # define WIN32_LEAN_AND_MEAN
100 /* Get _get_osfhandle. */
101 # include "msvc-nothrow.h"
104 /* The gnulib override of fcntl is not needed in this file. */
107 # if !HAVE_DECL_STRERROR_R
108 # ifndef HAVE_DECL_STRERROR_R
109 "this configure-time declaration test was not run"
111 # if STRERROR_R_CHAR_P
118 #define program_name getprogname ()
120 # if HAVE_STRERROR_R || defined strerror_r
121 # define __strerror_r strerror_r
122 # endif /* HAVE_STRERROR_R || defined strerror_r */
123 #endif /* not _LIBC */
126 /* Return non-zero if FD is open. */
130 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
131 /* On native Windows: The initial state of unassigned standard file
132 descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
133 There is no fcntl, and the gnulib replacement fcntl does not support
135 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
138 # error Please port fcntl to your platform
140 return 0 <= fcntl (fd, F_GETFL);
151 # if GNULIB_FREOPEN_SAFER
152 /* Use of gnulib's freopen-safer module normally ensures that
154 whenever stdout is open. */
155 stdout_fd = STDOUT_FILENO;
157 /* POSIX states that fileno (stdout) after fclose is unspecified. But in
158 practice it is not a problem, because stdout is statically allocated and
159 the fd of a FILE stream is stored as a field in its allocated memory. */
160 stdout_fd = fileno (stdout);
162 /* POSIX states that fflush (stdout) after fclose is unspecified; it
163 is safe in glibc, but not on all other platforms. fflush (NULL)
164 is always defined, but too draconian. */
165 if (0 <= stdout_fd && is_open (stdout_fd))
171 print_errno_message (int errnum)
175 #if defined HAVE_STRERROR_R || _LIBC
177 # if _LIBC || STRERROR_R_CHAR_P
178 s = __strerror_r (errnum, errbuf, sizeof errbuf);
180 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
186 s = strerror (errnum);
191 s = _("Unknown system error");
195 __fxprintf (NULL, ": %s", s);
197 fprintf (stderr, ": %s", s);
201 static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
202 error_tail (int status, int errnum, const char *message, va_list args)
205 if (_IO_fwide (stderr, 0) > 0)
207 size_t len = strlen (message) + 1;
208 wchar_t *wmessage = NULL;
212 bool use_malloc = false;
216 if (__libc_use_alloca (len * sizeof (wchar_t)))
217 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
223 wchar_t *p = (wchar_t *) realloc (wmessage,
224 len * sizeof (wchar_t));
228 fputws_unlocked (L"out of memory\n", stderr);
235 memset (&st, '\0', sizeof (st));
238 res = mbsrtowcs (wmessage, &tmp, len, &st);
242 if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
244 /* This really should not happen if everything is fine. */
252 if (res == (size_t) -1)
254 /* The string cannot be converted. */
260 wmessage = (wchar_t *) L"???";
263 __vfwprintf (stderr, wmessage, args);
270 vfprintf (stderr, message, args);
273 ++error_message_count;
275 print_errno_message (errnum);
277 __fxprintf (NULL, "\n");
287 /* Print the program name and error message MESSAGE, which is a printf-style
288 format string with optional args.
289 If ERRNUM is nonzero, print its corresponding system error message.
290 Exit with status STATUS if it is nonzero. */
292 error (int status, int errnum, const char *message, ...)
296 #if defined _LIBC && defined __libc_ptf_call
297 /* We do not want this call to be cut short by a thread
298 cancellation. Therefore disable cancellation for now. */
299 int state = PTHREAD_CANCEL_ENABLE;
300 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
306 _IO_flockfile (stderr);
308 if (error_print_progname)
309 (*error_print_progname) ();
313 __fxprintf (NULL, "%s: ", program_name);
315 fprintf (stderr, "%s: ", program_name);
319 va_start (args, message);
320 error_tail (status, errnum, message, args);
323 _IO_funlockfile (stderr);
324 # ifdef __libc_ptf_call
325 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
330 /* Sometimes we want to have at most one error per line. This
331 variable controls whether this mode is selected or not. */
332 int error_one_per_line;
335 error_at_line (int status, int errnum, const char *file_name,
336 unsigned int line_number, const char *message, ...)
340 if (error_one_per_line)
342 static const char *old_file_name;
343 static unsigned int old_line_number;
345 if (old_line_number == line_number
346 && (file_name == old_file_name
347 || (old_file_name != NULL
349 && strcmp (old_file_name, file_name) == 0)))
351 /* Simply return and print nothing. */
354 old_file_name = file_name;
355 old_line_number = line_number;
358 #if defined _LIBC && defined __libc_ptf_call
359 /* We do not want this call to be cut short by a thread
360 cancellation. Therefore disable cancellation for now. */
361 int state = PTHREAD_CANCEL_ENABLE;
362 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
368 _IO_flockfile (stderr);
370 if (error_print_progname)
371 (*error_print_progname) ();
375 __fxprintf (NULL, "%s:", program_name);
377 fprintf (stderr, "%s:", program_name);
382 __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
383 file_name, line_number);
385 fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
386 file_name, line_number);
389 va_start (args, message);
390 error_tail (status, errnum, message, args);
393 _IO_funlockfile (stderr);
394 # ifdef __libc_ptf_call
395 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
401 /* Make the weak alias. */
403 # undef error_at_line
404 weak_alias (__error, error)
405 weak_alias (__error_at_line, error_at_line)