Blame misc/err.c

Packit 6c4009
/* 4.4BSD utility functions for error messages.
Packit 6c4009
   Copyright (C) 1995-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
#include <stdarg.h>
Packit 6c4009
#include <err.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#define flockfile(s) _IO_flockfile (s)
Packit 6c4009
#define funlockfile(s) _IO_funlockfile (s)
Packit 6c4009
Packit 6c4009
extern char *__progname;
Packit 6c4009
Packit 6c4009
#define VA(call)							      \
Packit 6c4009
{									      \
Packit 6c4009
  va_list ap;								      \
Packit 6c4009
  va_start (ap, format);						      \
Packit 6c4009
  call;									      \
Packit 6c4009
  va_end (ap);								      \
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
convert_and_print (const char *format, __gnuc_va_list ap)
Packit 6c4009
{
Packit 6c4009
#define ALLOCA_LIMIT	2000
Packit 6c4009
  size_t len;
Packit 6c4009
  wchar_t *wformat = NULL;
Packit 6c4009
  mbstate_t st;
Packit 6c4009
  size_t res;
Packit 6c4009
  const char *tmp;
Packit 6c4009
Packit 6c4009
  if (format == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  len = strlen (format) + 1;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (len < ALLOCA_LIMIT)
Packit 6c4009
	wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
Packit 6c4009
	    wformat = NULL;
Packit 6c4009
Packit 6c4009
	  wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
Packit 6c4009
Packit 6c4009
	  if (wformat == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      fputws_unlocked (L"out of memory\n", stderr);
Packit 6c4009
	      return;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      memset (&st, '\0', sizeof (st));
Packit 6c4009
      tmp =format;
Packit 6c4009
    }
Packit 6c4009
  while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
Packit 6c4009
Packit 6c4009
  if (res == (size_t) -1)
Packit 6c4009
    /* The string cannot be converted.  */
Packit 6c4009
    wformat = (wchar_t *) L"???";
Packit 6c4009
Packit 6c4009
  __vfwprintf (stderr, wformat, ap);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
vwarnx (const char *format, __gnuc_va_list ap)
Packit 6c4009
{
Packit 6c4009
  flockfile (stderr);
Packit 6c4009
  if (_IO_fwide (stderr, 0) > 0)
Packit 6c4009
    {
Packit 6c4009
      __fwprintf (stderr, L"%s: ", __progname);
Packit 6c4009
      convert_and_print (format, ap);
Packit 6c4009
      putwc_unlocked (L'\n', stderr);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: ", __progname);
Packit 6c4009
      if (format)
Packit 6c4009
	vfprintf (stderr, format, ap);
Packit 6c4009
      putc_unlocked ('\n', stderr);
Packit 6c4009
    }
Packit 6c4009
  funlockfile (stderr);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (vwarnx)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
vwarn (const char *format, __gnuc_va_list ap)
Packit 6c4009
{
Packit 6c4009
  int error = errno;
Packit 6c4009
Packit 6c4009
  flockfile (stderr);
Packit 6c4009
  if (_IO_fwide (stderr, 0) > 0)
Packit 6c4009
    {
Packit 6c4009
      __fwprintf (stderr, L"%s: ", __progname);
Packit 6c4009
      if (format)
Packit 6c4009
	{
Packit 6c4009
	  convert_and_print (format, ap);
Packit 6c4009
	  fputws_unlocked (L": ", stderr);
Packit 6c4009
	}
Packit 6c4009
      __set_errno (error);
Packit 6c4009
      __fwprintf (stderr, L"%m\n");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: ", __progname);
Packit 6c4009
      if (format)
Packit 6c4009
	{
Packit 6c4009
	  vfprintf (stderr, format, ap);
Packit 6c4009
	  fputs_unlocked (": ", stderr);
Packit 6c4009
	}
Packit 6c4009
      __set_errno (error);
Packit 6c4009
      fprintf (stderr, "%m\n");
Packit 6c4009
    }
Packit 6c4009
  funlockfile (stderr);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (vwarn)
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
warn (const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  VA (vwarn (format, ap))
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (warn)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
warnx (const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  VA (vwarnx (format, ap))
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (warnx)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
verr (int status, const char *format, __gnuc_va_list ap)
Packit 6c4009
{
Packit 6c4009
  vwarn (format, ap);
Packit 6c4009
  exit (status);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (verr)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
verrx (int status, const char *format, __gnuc_va_list ap)
Packit 6c4009
{
Packit 6c4009
  vwarnx (format, ap);
Packit 6c4009
  exit (status);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (verrx)
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
err (int status, const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  VA (verr (status, format, ap))
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
errx (int status, const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  VA (verrx (status, format, ap))
Packit 6c4009
}