/* Error handler for noninteractive utilities
Copyright (C) 1990-1998, 2000-2007, 2009-2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
#if !_LIBC
# include <config.h>
#endif
#include "error.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !_LIBC && ENABLE_NLS
# include "gettext.h"
#endif
#ifdef _LIBC
# include <libintl.h>
# include <stdbool.h>
# include <stdint.h>
# include <wchar.h>
#endif
#if USE_UNLOCKED_IO
# include "unlocked-io.h"
#endif
#ifndef _
#endif
/* If NULL, error will flush stdout, then print on stderr the program
name, a colon and a space. Otherwise, error will call this
function without parameters instead. */
void (*error_print_progname) (void);
/* This variable is incremented each time `error' is called. */
unsigned int error_message_count;
#ifdef _LIBC
/* In the GNU C library, there is a predefined variable for this. */
# include <errno.h>
# include <limits.h>
/* In GNU libc we want do not want to use the common name `error' directly.
Instead make it a weak alias. */
unsigned int line_number, const char *message,
...)
# include <bits/libc-lock.h>
#else /* not _LIBC */
# include <fcntl.h>
# include <unistd.h>
/* Get declarations of the Win32 API functions. */
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# endif
/* The gnulib override of fcntl is not needed in this file. */
# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
# ifndef HAVE_DECL_STRERROR_R
"this configure-time declaration test was not run"
# endif
char *strerror_r ();
# endif
/* The calling program should define program_name and set it to the
name of the executing program. */
extern char *program_name;
# if HAVE_STRERROR_R || defined strerror_r
# endif /* HAVE_STRERROR_R || defined strerror_r */
#endif /* not _LIBC */
#if !_LIBC
/* Return non-zero if FD is open. */
static inline int
{
/* On Win32: The initial state of unassigned standard file descriptors is
that they are open but point to an INVALID_HANDLE_VALUE. There is no
fcntl, and the gnulib replacement fcntl does not support F_GETFL. */
# else
# ifndef F_GETFL
# endif
# endif
}
#endif
static inline void
flush_stdout (void)
{
#if !_LIBC
int stdout_fd;
# if GNULIB_FREOPEN_SAFER
/* Use of gnulib's freopen-safer module normally ensures that
fileno (stdout) == 1
whenever stdout is open. */
# else
/* POSIX states that fileno (stdout) after fclose is unspecified. But in
practice it is not a problem, because stdout is statically allocated and
the fd of a FILE stream is stored as a field in its allocated memory. */
# endif
/* POSIX states that fflush (stdout) after fclose is unspecified; it
is safe in glibc, but not on all other platforms. fflush (NULL)
is always defined, but too draconian. */
#endif
}
static void
{
char const *s;
#if defined HAVE_STRERROR_R || _LIBC
# if STRERROR_R_CHAR_P || _LIBC
# else
s = errbuf;
else
s = 0;
# endif
#else
#endif
#if !_LIBC
if (! s)
s = _("Unknown system error");
#endif
#if _LIBC
#else
#endif
}
static void
{
#if _LIBC
{
const char *tmp;
bool use_malloc = false;
while (1)
{
else
{
if (!use_malloc)
if (p == NULL)
{
return;
}
wmessage = p;
use_malloc = true;
}
break;
{
/* This really should not happen if everything is fine. */
break;
}
len *= 2;
}
{
/* The string cannot be converted. */
if (use_malloc)
{
use_malloc = false;
}
}
if (use_malloc)
}
else
#endif
if (errnum)
#if _LIBC
#else
#endif
if (status)
}
/* Print the program name and error message MESSAGE, which is a printf-style
format string with optional args.
If ERRNUM is nonzero, print its corresponding system error message.
Exit with status STATUS if it is nonzero. */
void
{
#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. */
0);
#endif
flush_stdout ();
#ifdef _LIBC
#endif
if (error_print_progname)
(*error_print_progname) ();
else
{
#if _LIBC
#else
#endif
}
#ifdef _LIBC
# ifdef __libc_ptf_call
# endif
#endif
}
/* 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
unsigned int line_number, const char *message, ...)
{
if (error_one_per_line)
{
static const char *old_file_name;
static unsigned int old_line_number;
if (old_line_number == line_number
&& (file_name == old_file_name
/* Simply return and print nothing. */
return;
}
#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. */
0);
#endif
flush_stdout ();
#ifdef _LIBC
#endif
if (error_print_progname)
(*error_print_progname) ();
else
{
#if _LIBC
#else
#endif
}
#if _LIBC
#else
#endif
#ifdef _LIBC
# ifdef __libc_ptf_call
# endif
#endif
}
#ifdef _LIBC
/* Make the weak alias. */
#endif