(root)/
tar-1.35/
gnu/
error.c
       1  /* Error handler for noninteractive utilities
       2     Copyright (C) 1990-1998, 2000-2007, 2009-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     This file is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation; either version 2.1 of the
       8     License, or (at your option) any later version.
       9  
      10     This file 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 Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
      19  
      20  #if !_LIBC
      21  # include <config.h>
      22  # define _GL_NO_INLINE_ERROR
      23  #endif
      24  
      25  #include "error.h"
      26  
      27  #include <stdarg.h>
      28  #include <stdio.h>
      29  #include <stdlib.h>
      30  #include <string.h>
      31  
      32  #if !_LIBC && ENABLE_NLS
      33  # include "gettext.h"
      34  # define _(msgid) gettext (msgid)
      35  #endif
      36  
      37  #ifdef _LIBC
      38  # include <libintl.h>
      39  # include <stdbool.h>
      40  # include <stdint.h>
      41  # include <wchar.h>
      42  # define mbsrtowcs __mbsrtowcs
      43  # define USE_UNLOCKED_IO 0
      44  # define _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD(a, b)
      45  # define _GL_ARG_NONNULL(a)
      46  #endif
      47  
      48  #if USE_UNLOCKED_IO
      49  # include "unlocked-io.h"
      50  #endif
      51  
      52  #ifndef _
      53  # define _(String) String
      54  #endif
      55  
      56  /* If NULL, error will flush stdout, then print on stderr the program
      57     name, a colon and a space.  Otherwise, error will call this
      58     function without parameters instead.  */
      59  void (*error_print_progname) (void);
      60  
      61  /* This variable is incremented each time 'error' is called.  */
      62  unsigned int error_message_count;
      63  
      64  #ifdef _LIBC
      65  /* In the GNU C library, there is a predefined variable for this.  */
      66  
      67  # define program_name program_invocation_name
      68  # include <errno.h>
      69  # include <limits.h>
      70  # include <libio/libioP.h>
      71  
      72  /* In GNU libc we want do not want to use the common name 'error' directly.
      73     Instead make it a weak alias.  */
      74  extern void __error (int status, int errnum, const char *message, ...)
      75       __attribute__ ((__format__ (__printf__, 3, 4)));
      76  extern void __error_at_line (int status, int errnum, const char *file_name,
      77                               unsigned int line_number, const char *message,
      78                               ...)
      79       __attribute__ ((__format__ (__printf__, 5, 6)));
      80  # define error __error
      81  # define error_at_line __error_at_line
      82  
      83  # include <libio/iolibio.h>
      84  # define fflush(s) _IO_fflush (s)
      85  # undef putc
      86  # define putc(c, fp) _IO_putc (c, fp)
      87  
      88  # include <bits/libc-lock.h>
      89  
      90  #else /* not _LIBC */
      91  
      92  # include <fcntl.h>
      93  # include <unistd.h>
      94  
      95  # if defined _WIN32 && ! defined __CYGWIN__
      96  /* Get declarations of the native Windows API functions.  */
      97  #  define WIN32_LEAN_AND_MEAN
      98  #  include <windows.h>
      99  /* Get _get_osfhandle.  */
     100  #  if GNULIB_MSVC_NOTHROW
     101  #   include "msvc-nothrow.h"
     102  #  else
     103  #   include <io.h>
     104  #  endif
     105  # endif
     106  
     107  /* The gnulib override of fcntl is not needed in this file.  */
     108  # undef fcntl
     109  
     110  # if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
     111  #  ifndef HAVE_DECL_STRERROR_R
     112  "this configure-time declaration test was not run"
     113  #  endif
     114  #  if STRERROR_R_CHAR_P
     115  char *strerror_r (int errnum, char *buf, size_t buflen);
     116  #  else
     117  int strerror_r (int errnum, char *buf, size_t buflen);
     118  #  endif
     119  # endif
     120  
     121  # define program_name getprogname ()
     122  
     123  # if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
     124  #  define __strerror_r strerror_r
     125  # endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
     126  #endif  /* not _LIBC */
     127  
     128  #if !_LIBC
     129  /* Return non-zero if FD is open.  */
     130  static int
     131  is_open (int fd)
     132  {
     133  # if defined _WIN32 && ! defined __CYGWIN__
     134    /* On native Windows: The initial state of unassigned standard file
     135       descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
     136       There is no fcntl, and the gnulib replacement fcntl does not support
     137       F_GETFL.  */
     138    return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
     139  # else
     140  #  ifndef F_GETFL
     141  #   error Please port fcntl to your platform
     142  #  endif
     143    return 0 <= fcntl (fd, F_GETFL);
     144  # endif
     145  }
     146  #endif
     147  
     148  static void
     149  flush_stdout (void)
     150  {
     151  #if !_LIBC
     152    int stdout_fd;
     153  
     154  # if GNULIB_FREOPEN_SAFER
     155    /* Use of gnulib's freopen-safer module normally ensures that
     156         fileno (stdout) == 1
     157       whenever stdout is open.  */
     158    stdout_fd = STDOUT_FILENO;
     159  # else
     160    /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
     161       practice it is not a problem, because stdout is statically allocated and
     162       the fd of a FILE stream is stored as a field in its allocated memory.  */
     163    stdout_fd = fileno (stdout);
     164  # endif
     165    /* POSIX states that fflush (stdout) after fclose is unspecified; it
     166       is safe in glibc, but not on all other platforms.  fflush (NULL)
     167       is always defined, but too draconian.  */
     168    if (0 <= stdout_fd && is_open (stdout_fd))
     169  #endif
     170      fflush (stdout);
     171  }
     172  
     173  static void
     174  print_errno_message (int errnum)
     175  {
     176    char const *s;
     177  
     178  #if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
     179    char errbuf[1024];
     180  # if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
     181    s = __strerror_r (errnum, errbuf, sizeof errbuf);
     182  # else
     183    if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
     184      s = errbuf;
     185    else
     186      s = 0;
     187  # endif
     188  #else
     189    s = strerror (errnum);
     190  #endif
     191  
     192  #if !_LIBC
     193    if (! s)
     194      s = _("Unknown system error");
     195  #endif
     196  
     197  #if _LIBC
     198    __fxprintf (NULL, ": %s", s);
     199  #else
     200    fprintf (stderr, ": %s", s);
     201  #endif
     202  }
     203  
     204  static void _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 0) _GL_ARG_NONNULL ((3))
     205  error_tail (int status, int errnum, const char *message, va_list args)
     206  {
     207  #if _LIBC
     208    if (_IO_fwide (stderr, 0) > 0)
     209      {
     210        size_t len = strlen (message) + 1;
     211        wchar_t *wmessage = NULL;
     212        mbstate_t st;
     213        size_t res;
     214        const char *tmp;
     215        bool use_malloc = false;
     216  
     217        while (1)
     218          {
     219            if (__libc_use_alloca (len * sizeof (wchar_t)))
     220              wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
     221            else
     222              {
     223                if (!use_malloc)
     224                  wmessage = NULL;
     225  
     226                wchar_t *p = (wchar_t *) realloc (wmessage,
     227                                                  len * sizeof (wchar_t));
     228                if (p == NULL)
     229                  {
     230                    free (wmessage);
     231                    fputws_unlocked (L"out of memory\n", stderr);
     232                    return;
     233                  }
     234                wmessage = p;
     235                use_malloc = true;
     236              }
     237  
     238            memset (&st, '\0', sizeof (st));
     239            tmp = message;
     240  
     241            res = mbsrtowcs (wmessage, &tmp, len, &st);
     242            if (res != len)
     243              break;
     244  
     245            if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
     246              {
     247                /* This really should not happen if everything is fine.  */
     248                res = (size_t) -1;
     249                break;
     250              }
     251  
     252            len *= 2;
     253          }
     254  
     255        if (res == (size_t) -1)
     256          {
     257            /* The string cannot be converted.  */
     258            if (use_malloc)
     259              {
     260                free (wmessage);
     261                use_malloc = false;
     262              }
     263            wmessage = (wchar_t *) L"???";
     264          }
     265  
     266        __vfwprintf (stderr, wmessage, args);
     267  
     268        if (use_malloc)
     269          free (wmessage);
     270      }
     271    else
     272  #endif
     273      vfprintf (stderr, message, args);
     274  
     275    ++error_message_count;
     276    if (errnum)
     277      print_errno_message (errnum);
     278  #if _LIBC
     279    __fxprintf (NULL, "\n");
     280  #else
     281    putc ('\n', stderr);
     282  #endif
     283    fflush (stderr);
     284    if (status)
     285      exit (status);
     286  }
     287  
     288  
     289  /* Print the program name and error message MESSAGE, which is a printf-style
     290     format string with optional args.
     291     If ERRNUM is nonzero, print its corresponding system error message.
     292     Exit with status STATUS if it is nonzero.  */
     293  void
     294  error (int status, int errnum, const char *message, ...)
     295  {
     296    va_list args;
     297  
     298  #if defined _LIBC && defined __libc_ptf_call
     299    /* We do not want this call to be cut short by a thread
     300       cancellation.  Therefore disable cancellation for now.  */
     301    int state = PTHREAD_CANCEL_ENABLE;
     302    __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
     303                     0);
     304  #endif
     305  
     306    flush_stdout ();
     307  #ifdef _LIBC
     308    _IO_flockfile (stderr);
     309  #endif
     310    if (error_print_progname)
     311      (*error_print_progname) ();
     312    else
     313      {
     314  #if _LIBC
     315        __fxprintf (NULL, "%s: ", program_name);
     316  #else
     317        fprintf (stderr, "%s: ", program_name);
     318  #endif
     319      }
     320  
     321    va_start (args, message);
     322    error_tail (status, errnum, message, args);
     323    va_end (args);
     324  
     325  #ifdef _LIBC
     326    _IO_funlockfile (stderr);
     327  # ifdef __libc_ptf_call
     328    __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
     329  # endif
     330  #endif
     331  }
     332  
     333  /* Sometimes we want to have at most one error per line.  This
     334     variable controls whether this mode is selected or not.  */
     335  int error_one_per_line;
     336  
     337  void
     338  error_at_line (int status, int errnum, const char *file_name,
     339                 unsigned int line_number, const char *message, ...)
     340  {
     341    va_list args;
     342  
     343    if (error_one_per_line)
     344      {
     345        static const char *old_file_name;
     346        static unsigned int old_line_number;
     347  
     348        if (old_line_number == line_number
     349            && (file_name == old_file_name
     350                || (old_file_name != NULL
     351                    && file_name != NULL
     352                    && strcmp (old_file_name, file_name) == 0)))
     353  
     354          /* Simply return and print nothing.  */
     355          return;
     356  
     357        old_file_name = file_name;
     358        old_line_number = line_number;
     359      }
     360  
     361  #if defined _LIBC && defined __libc_ptf_call
     362    /* We do not want this call to be cut short by a thread
     363       cancellation.  Therefore disable cancellation for now.  */
     364    int state = PTHREAD_CANCEL_ENABLE;
     365    __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
     366                     0);
     367  #endif
     368  
     369    flush_stdout ();
     370  #ifdef _LIBC
     371    _IO_flockfile (stderr);
     372  #endif
     373    if (error_print_progname)
     374      (*error_print_progname) ();
     375    else
     376      {
     377  #if _LIBC
     378        __fxprintf (NULL, "%s:", program_name);
     379  #else
     380        fprintf (stderr, "%s:", program_name);
     381  #endif
     382      }
     383  
     384  #if _LIBC
     385    __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
     386                file_name, line_number);
     387  #else
     388    fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
     389             file_name, line_number);
     390  #endif
     391  
     392    va_start (args, message);
     393    error_tail (status, errnum, message, args);
     394    va_end (args);
     395  
     396  #ifdef _LIBC
     397    _IO_funlockfile (stderr);
     398  # ifdef __libc_ptf_call
     399    __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
     400  # endif
     401  #endif
     402  }
     403  
     404  #ifdef _LIBC
     405  /* Make the weak alias.  */
     406  # undef error
     407  # undef error_at_line
     408  weak_alias (__error, error)
     409  weak_alias (__error_at_line, error_at_line)
     410  #endif