Blame misc/error.c

Packit 6c4009
/* Error handler for noninteractive utilities
Packit 6c4009
   Copyright (C) 1990-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
Packit 6c4009
Packit 6c4009
#if !_LIBC
Packit 6c4009
# include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "error.h"
Packit 6c4009
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#if !_LIBC && ENABLE_NLS
Packit 6c4009
# include "gettext.h"
Packit 6c4009
# define _(msgid) gettext (msgid)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# include <libintl.h>
Packit 6c4009
# include <stdbool.h>
Packit 6c4009
# include <stdint.h>
Packit 6c4009
# include <wchar.h>
Packit 6c4009
# define mbsrtowcs __mbsrtowcs
Packit 6c4009
# define USE_UNLOCKED_IO 0
Packit 6c4009
# define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
Packit 6c4009
# define _GL_ARG_NONNULL(a)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if USE_UNLOCKED_IO
Packit 6c4009
# include "unlocked-io.h"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef _
Packit 6c4009
# define _(String) String
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* If NULL, error will flush stdout, then print on stderr the program
Packit 6c4009
   name, a colon and a space.  Otherwise, error will call this
Packit 6c4009
   function without parameters instead.  */
Packit 6c4009
void (*error_print_progname) (void);
Packit 6c4009
Packit 6c4009
/* This variable is incremented each time 'error' is called.  */
Packit 6c4009
unsigned int error_message_count;
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
/* In the GNU C library, there is a predefined variable for this.  */
Packit 6c4009
Packit 6c4009
# define program_name program_invocation_name
Packit 6c4009
# include <errno.h>
Packit 6c4009
# include <limits.h>
Packit 6c4009
# include <libio/libioP.h>
Packit 6c4009
Packit 6c4009
/* In GNU libc we want do not want to use the common name 'error' directly.
Packit 6c4009
   Instead make it a weak alias.  */
Packit 6c4009
extern void __error (int status, int errnum, const char *message, ...)
Packit 6c4009
     __attribute__ ((__format__ (__printf__, 3, 4)));
Packit 6c4009
extern void __error_at_line (int status, int errnum, const char *file_name,
Packit 6c4009
			     unsigned int line_number, const char *message,
Packit 6c4009
			     ...)
Packit 6c4009
     __attribute__ ((__format__ (__printf__, 5, 6)));;
Packit 6c4009
# define error __error
Packit 6c4009
# define error_at_line __error_at_line
Packit 6c4009
Packit 6c4009
# include <libio/iolibio.h>
Packit 6c4009
# define fflush(s) _IO_fflush (s)
Packit 6c4009
# undef putc
Packit 6c4009
# define putc(c, fp) _IO_putc (c, fp)
Packit 6c4009
Packit 6c4009
# include <libc-lock.h>
Packit 6c4009
Packit 6c4009
#else /* not _LIBC */
Packit 6c4009
Packit 6c4009
# include <fcntl.h>
Packit 6c4009
# include <unistd.h>
Packit 6c4009
Packit 6c4009
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 6c4009
/* Get declarations of the native Windows API functions.  */
Packit 6c4009
#  define WIN32_LEAN_AND_MEAN
Packit 6c4009
#  include <windows.h>
Packit 6c4009
/* Get _get_osfhandle.  */
Packit 6c4009
#  include "msvc-nothrow.h"
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* The gnulib override of fcntl is not needed in this file.  */
Packit 6c4009
# undef fcntl
Packit 6c4009
Packit 6c4009
# if !HAVE_DECL_STRERROR_R
Packit 6c4009
#  ifndef HAVE_DECL_STRERROR_R
Packit 6c4009
"this configure-time declaration test was not run"
Packit 6c4009
#  endif
Packit 6c4009
#  if STRERROR_R_CHAR_P
Packit 6c4009
char *strerror_r ();
Packit 6c4009
#  else
Packit 6c4009
int strerror_r ();
Packit 6c4009
#  endif
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* The calling program should define program_name and set it to the
Packit 6c4009
   name of the executing program.  */
Packit 6c4009
extern char *program_name;
Packit 6c4009
Packit 6c4009
# if HAVE_STRERROR_R || defined strerror_r
Packit 6c4009
#  define __strerror_r strerror_r
Packit 6c4009
# endif	/* HAVE_STRERROR_R || defined strerror_r */
Packit 6c4009
#endif	/* not _LIBC */
Packit 6c4009
Packit 6c4009
#if !_LIBC
Packit 6c4009
/* Return non-zero if FD is open.  */
Packit 6c4009
static int
Packit 6c4009
is_open (int fd)
Packit 6c4009
{
Packit 6c4009
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 6c4009
  /* On native Windows: The initial state of unassigned standard file
Packit 6c4009
     descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
Packit 6c4009
     There is no fcntl, and the gnulib replacement fcntl does not support
Packit 6c4009
     F_GETFL.  */
Packit 6c4009
  return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
Packit 6c4009
# else
Packit 6c4009
#  ifndef F_GETFL
Packit 6c4009
#   error Please port fcntl to your platform
Packit 6c4009
#  endif
Packit 6c4009
  return 0 <= fcntl (fd, F_GETFL);
Packit 6c4009
# endif
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
flush_stdout (void)
Packit 6c4009
{
Packit 6c4009
#if !_LIBC
Packit 6c4009
  int stdout_fd;
Packit 6c4009
Packit 6c4009
# if GNULIB_FREOPEN_SAFER
Packit 6c4009
  /* Use of gnulib's freopen-safer module normally ensures that
Packit 6c4009
       fileno (stdout) == 1
Packit 6c4009
     whenever stdout is open.  */
Packit 6c4009
  stdout_fd = STDOUT_FILENO;
Packit 6c4009
# else
Packit 6c4009
  /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
Packit 6c4009
     practice it is not a problem, because stdout is statically allocated and
Packit 6c4009
     the fd of a FILE stream is stored as a field in its allocated memory.  */
Packit 6c4009
  stdout_fd = fileno (stdout);
Packit 6c4009
# endif
Packit 6c4009
  /* POSIX states that fflush (stdout) after fclose is unspecified; it
Packit 6c4009
     is safe in glibc, but not on all other platforms.  fflush (NULL)
Packit 6c4009
     is always defined, but too draconian.  */
Packit 6c4009
  if (0 <= stdout_fd && is_open (stdout_fd))
Packit 6c4009
#endif
Packit 6c4009
    fflush (stdout);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_errno_message (int errnum)
Packit 6c4009
{
Packit 6c4009
  char const *s;
Packit 6c4009
Packit 6c4009
#if defined HAVE_STRERROR_R || _LIBC
Packit 6c4009
  char errbuf[1024];
Packit 6c4009
# if _LIBC || STRERROR_R_CHAR_P
Packit 6c4009
  s = __strerror_r (errnum, errbuf, sizeof errbuf);
Packit 6c4009
# else
Packit 6c4009
  if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
Packit 6c4009
    s = errbuf;
Packit 6c4009
  else
Packit 6c4009
    s = 0;
Packit 6c4009
# endif
Packit 6c4009
#else
Packit 6c4009
  s = strerror (errnum);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if !_LIBC
Packit 6c4009
  if (! s)
Packit 6c4009
    s = _("Unknown system error");
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if _LIBC
Packit 6c4009
  __fxprintf (NULL, ": %s", s);
Packit 6c4009
#else
Packit 6c4009
  fprintf (stderr, ": %s", s);
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
Packit 6c4009
error_tail (int status, int errnum, const char *message, va_list args)
Packit 6c4009
{
Packit 6c4009
#if _LIBC
Packit 6c4009
  if (_IO_fwide (stderr, 0) > 0)
Packit 6c4009
    {
Packit 6c4009
      size_t len = strlen (message) + 1;
Packit 6c4009
      wchar_t *wmessage = NULL;
Packit 6c4009
      mbstate_t st;
Packit 6c4009
      size_t res;
Packit 6c4009
      const char *tmp;
Packit 6c4009
      bool use_malloc = false;
Packit 6c4009
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  if (__libc_use_alloca (len * sizeof (wchar_t)))
Packit 6c4009
	    wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      if (!use_malloc)
Packit 6c4009
		wmessage = NULL;
Packit 6c4009
Packit 6c4009
	      wchar_t *p = (wchar_t *) realloc (wmessage,
Packit 6c4009
						len * sizeof (wchar_t));
Packit 6c4009
	      if (p == NULL)
Packit 6c4009
		{
Packit 6c4009
		  free (wmessage);
Packit 6c4009
		  fputws_unlocked (L"out of memory\n", stderr);
Packit 6c4009
		  return;
Packit 6c4009
		}
Packit 6c4009
	      wmessage = p;
Packit 6c4009
	      use_malloc = true;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  memset (&st, '\0', sizeof (st));
Packit 6c4009
	  tmp = message;
Packit 6c4009
Packit 6c4009
	  res = mbsrtowcs (wmessage, &tmp, len, &st);
Packit 6c4009
	  if (res != len)
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
Packit 6c4009
	    {
Packit 6c4009
	      /* This really should not happen if everything is fine.  */
Packit 6c4009
	      res = (size_t) -1;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  len *= 2;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (res == (size_t) -1)
Packit 6c4009
	{
Packit 6c4009
	  /* The string cannot be converted.  */
Packit 6c4009
	  if (use_malloc)
Packit 6c4009
	    {
Packit 6c4009
	      free (wmessage);
Packit 6c4009
	      use_malloc = false;
Packit 6c4009
	    }
Packit 6c4009
	  wmessage = (wchar_t *) L"???";
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      __vfwprintf (stderr, wmessage, args);
Packit 6c4009
Packit 6c4009
      if (use_malloc)
Packit 6c4009
	free (wmessage);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
    vfprintf (stderr, message, args);
Packit 6c4009
  va_end (args);
Packit 6c4009
Packit 6c4009
  ++error_message_count;
Packit 6c4009
  if (errnum)
Packit 6c4009
    print_errno_message (errnum);
Packit 6c4009
#if _LIBC
Packit 6c4009
  __fxprintf (NULL, "\n");
Packit 6c4009
#else
Packit 6c4009
  putc ('\n', stderr);
Packit 6c4009
#endif
Packit 6c4009
  fflush (stderr);
Packit 6c4009
  if (status)
Packit 6c4009
    exit (status);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Print the program name and error message MESSAGE, which is a printf-style
Packit 6c4009
   format string with optional args.
Packit 6c4009
   If ERRNUM is nonzero, print its corresponding system error message.
Packit 6c4009
   Exit with status STATUS if it is nonzero.  */
Packit 6c4009
void
Packit 6c4009
error (int status, int errnum, const char *message, ...)
Packit 6c4009
{
Packit 6c4009
  va_list args;
Packit 6c4009
Packit 6c4009
#if defined _LIBC && defined __libc_ptf_call
Packit 6c4009
  /* We do not want this call to be cut short by a thread
Packit 6c4009
     cancellation.  Therefore disable cancellation for now.  */
Packit 6c4009
  int state = PTHREAD_CANCEL_ENABLE;
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate,
Packit 6c4009
		   (PTHREAD_CANCEL_DISABLE, &state), 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  flush_stdout ();
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
  _IO_flockfile (stderr);
Packit 6c4009
#endif
Packit 6c4009
  if (error_print_progname)
Packit 6c4009
    (*error_print_progname) ();
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
#if _LIBC
Packit 6c4009
      __fxprintf (NULL, "%s: ", program_name);
Packit 6c4009
#else
Packit 6c4009
      fprintf (stderr, "%s: ", program_name);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  va_start (args, message);
Packit 6c4009
  error_tail (status, errnum, message, args);
Packit Bot c892c2
  va_end (args);
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
  _IO_funlockfile (stderr);
Packit 6c4009
# ifdef __libc_ptf_call
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009

Packit 6c4009
/* Sometimes we want to have at most one error per line.  This
Packit 6c4009
   variable controls whether this mode is selected or not.  */
Packit 6c4009
int error_one_per_line;
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
error_at_line (int status, int errnum, const char *file_name,
Packit 6c4009
	       unsigned int line_number, const char *message, ...)
Packit 6c4009
{
Packit 6c4009
  va_list args;
Packit 6c4009
Packit 6c4009
  if (error_one_per_line)
Packit 6c4009
    {
Packit 6c4009
      static const char *old_file_name;
Packit 6c4009
      static unsigned int old_line_number;
Packit 6c4009
Packit 6c4009
      if (old_line_number == line_number
Packit 6c4009
	  && (file_name == old_file_name
Packit 6c4009
	      || (old_file_name != NULL
Packit 6c4009
		  && file_name != NULL
Packit 6c4009
		  && strcmp (old_file_name, file_name) == 0)))
Packit 6c4009
Packit 6c4009
	/* Simply return and print nothing.  */
Packit 6c4009
	return;
Packit 6c4009
Packit 6c4009
      old_file_name = file_name;
Packit 6c4009
      old_line_number = line_number;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#if defined _LIBC && defined __libc_ptf_call
Packit 6c4009
  /* We do not want this call to be cut short by a thread
Packit 6c4009
     cancellation.  Therefore disable cancellation for now.  */
Packit 6c4009
  int state = PTHREAD_CANCEL_ENABLE;
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate,
Packit 6c4009
		   (PTHREAD_CANCEL_DISABLE, &state),
Packit 6c4009
		   0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  flush_stdout ();
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
  _IO_flockfile (stderr);
Packit 6c4009
#endif
Packit 6c4009
  if (error_print_progname)
Packit 6c4009
    (*error_print_progname) ();
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
#if _LIBC
Packit 6c4009
      __fxprintf (NULL, "%s:", program_name);
Packit 6c4009
#else
Packit 6c4009
      fprintf (stderr, "%s:", program_name);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#if _LIBC
Packit 6c4009
  __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
Packit 6c4009
	      file_name, line_number);
Packit 6c4009
#else
Packit 6c4009
  fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
Packit 6c4009
	   file_name, line_number);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  va_start (args, message);
Packit 6c4009
  error_tail (status, errnum, message, args);
Packit Bot c892c2
  va_end (args);
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
  _IO_funlockfile (stderr);
Packit 6c4009
# ifdef __libc_ptf_call
Packit 6c4009
  __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
/* Make the weak alias.  */
Packit 6c4009
# undef error
Packit 6c4009
# undef error_at_line
Packit 6c4009
weak_alias (__error, error)
Packit 6c4009
weak_alias (__error_at_line, error_at_line)
Packit 6c4009
#endif