Home | History | Annotate | Download | only in lib
      1 /* Error handler for noninteractive utilities
      2    Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc.
      3    This file is part of the GNU C Library.
      4 
      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 2, or (at your option)
      8    any later version.
      9 
     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.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with this program; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     18 
     19 /* Written by David MacKenzie <djm (at) gnu.ai.mit.edu>.  */
     20 
     21 #ifdef HAVE_CONFIG_H
     22 # include <config.h>
     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 #endif
     35 
     36 #ifdef _LIBC
     37 # include <wchar.h>
     38 # define mbsrtowcs __mbsrtowcs
     39 #endif
     40 
     41 #if USE_UNLOCKED_IO
     42 # include "unlocked-io.h"
     43 #endif
     44 
     45 #ifndef _
     46 # define _(String) String
     47 #endif
     48 
     49 /* If NULL, error will flush stdout, then print on stderr the program
     50    name, a colon and a space.  Otherwise, error will call this
     51    function without parameters instead.  */
     52 void (*error_print_progname) (void);
     53 
     54 /* This variable is incremented each time `error' is called.  */
     55 unsigned int error_message_count;
     56 
     57 #ifdef _LIBC
     58 /* In the GNU C library, there is a predefined variable for this.  */
     59 
     60 # define program_name program_invocation_name
     61 # include <errno.h>
     62 # include <libio/libioP.h>
     63 
     64 /* In GNU libc we want do not want to use the common name `error' directly.
     65    Instead make it a weak alias.  */
     66 extern void __error (int status, int errnum, const char *message, ...)
     67      __attribute__ ((__format__ (__printf__, 3, 4)));
     68 extern void __error_at_line (int status, int errnum, const char *file_name,
     69 			     unsigned int line_number, const char *message,
     70 			     ...)
     71      __attribute__ ((__format__ (__printf__, 5, 6)));;
     72 # define error __error
     73 # define error_at_line __error_at_line
     74 
     75 # include <libio/iolibio.h>
     76 # define fflush(s) INTUSE(_IO_fflush) (s)
     77 # undef putc
     78 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
     79 
     80 # include <bits/libc-lock.h>
     81 
     82 #else /* not _LIBC */
     83 
     84 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
     85 #  ifndef HAVE_DECL_STRERROR_R
     86 "this configure-time declaration test was not run"
     87 #  endif
     88 char *strerror_r ();
     89 # endif
     90 
     91 # ifndef SIZE_MAX
     92 #  define SIZE_MAX ((size_t) -1)
     93 # endif
     94 
     95 /* The calling program should define program_name and set it to the
     96    name of the executing program.  */
     97 extern char *program_name;
     98 
     99 # if HAVE_STRERROR_R || defined strerror_r
    100 #  define __strerror_r strerror_r
    101 # endif
    102 #endif	/* not _LIBC */
    103 
    104 static void
    105 print_errno_message (int errnum)
    106 {
    107   char const *s = NULL;
    108 
    109 #if defined HAVE_STRERROR_R || _LIBC
    110   char errbuf[1024];
    111 # if STRERROR_R_CHAR_P || _LIBC
    112   s = __strerror_r (errnum, errbuf, sizeof errbuf);
    113 # else
    114   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
    115     s = errbuf;
    116 # endif
    117 #endif
    118 
    119 #if !_LIBC
    120   if (! s && ! (s = strerror (errnum)))
    121     s = _("Unknown system error");
    122 #endif
    123 
    124 #if _LIBC
    125   if (_IO_fwide (stderr, 0) > 0)
    126     {
    127       __fwprintf (stderr, L": %s", s);
    128       return;
    129     }
    130 #endif
    131 
    132   fprintf (stderr, ": %s", s);
    133 }
    134 
    135 static void
    136 error_tail (int status, int errnum, const char *message, va_list args)
    137 {
    138 #if _LIBC
    139   if (_IO_fwide (stderr, 0) > 0)
    140     {
    141 # define ALLOCA_LIMIT 2000
    142       size_t len = strlen (message) + 1;
    143       const wchar_t *wmessage = L"out of memory";
    144       wchar_t *wbuf = (len < ALLOCA_LIMIT
    145 		       ? alloca (len * sizeof *wbuf)
    146 		       : len <= SIZE_MAX / sizeof *wbuf
    147 		       ? malloc (len * sizeof *wbuf)
    148 		       : NULL);
    149 
    150       if (wbuf)
    151 	{
    152 	  size_t res;
    153 	  mbstate_t st;
    154 	  const char *tmp = message;
    155 	  memset (&st, '\0', sizeof (st));
    156 	  res = mbsrtowcs (wbuf, &tmp, len, &st);
    157 	  wmessage = res == (size_t) -1 ? L"???" : wbuf;
    158 	}
    159 
    160       __vfwprintf (stderr, wmessage, args);
    161       if (! (len < ALLOCA_LIMIT))
    162 	free (wbuf);
    163     }
    164   else
    165 #endif
    166     vfprintf (stderr, message, args);
    167   va_end (args);
    168 
    169   ++error_message_count;
    170   if (errnum)
    171     print_errno_message (errnum);
    172 #if _LIBC
    173   if (_IO_fwide (stderr, 0) > 0)
    174     putwc (L'\n', stderr);
    175   else
    176 #endif
    177     putc ('\n', stderr);
    178   fflush (stderr);
    179   if (status)
    180     exit (status);
    181 }
    182 
    183 
    184 /* Print the program name and error message MESSAGE, which is a printf-style
    185    format string with optional args.
    186    If ERRNUM is nonzero, print its corresponding system error message.
    187    Exit with status STATUS if it is nonzero.  */
    188 void
    189 error (int status, int errnum, const char *message, ...)
    190 {
    191   va_list args;
    192 
    193 #if defined _LIBC && defined __libc_ptf_call
    194   /* We do not want this call to be cut short by a thread
    195      cancellation.  Therefore disable cancellation for now.  */
    196   int state = PTHREAD_CANCEL_ENABLE;
    197   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
    198 		   0);
    199 #endif
    200 
    201   fflush (stdout);
    202 #ifdef _LIBC
    203   _IO_flockfile (stderr);
    204 #endif
    205   if (error_print_progname)
    206     (*error_print_progname) ();
    207   else
    208     {
    209 #if _LIBC
    210       if (_IO_fwide (stderr, 0) > 0)
    211 	__fwprintf (stderr, L"%s: ", program_name);
    212       else
    213 #endif
    214 	fprintf (stderr, "%s: ", program_name);
    215     }
    216 
    217   va_start (args, message);
    218   error_tail (status, errnum, message, args);
    219 
    220 #ifdef _LIBC
    221   _IO_funlockfile (stderr);
    222 # ifdef __libc_ptf_call
    223   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
    224 # endif
    225 #endif
    226 }
    227 
    228 /* Sometimes we want to have at most one error per line.  This
    230    variable controls whether this mode is selected or not.  */
    231 int error_one_per_line;
    232 
    233 void
    234 error_at_line (int status, int errnum, const char *file_name,
    235 	       unsigned int line_number, const char *message, ...)
    236 {
    237   va_list args;
    238 
    239   if (error_one_per_line)
    240     {
    241       static const char *old_file_name;
    242       static unsigned int old_line_number;
    243 
    244       if (old_line_number == line_number
    245 	  && (file_name == old_file_name
    246 	      || strcmp (old_file_name, file_name) == 0))
    247 	/* Simply return and print nothing.  */
    248 	return;
    249 
    250       old_file_name = file_name;
    251       old_line_number = line_number;
    252     }
    253 
    254 #if defined _LIBC && defined __libc_ptf_call
    255   /* We do not want this call to be cut short by a thread
    256      cancellation.  Therefore disable cancellation for now.  */
    257   int state = PTHREAD_CANCEL_ENABLE;
    258   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
    259 		   0);
    260 #endif
    261 
    262   fflush (stdout);
    263 #ifdef _LIBC
    264   _IO_flockfile (stderr);
    265 #endif
    266   if (error_print_progname)
    267     (*error_print_progname) ();
    268   else
    269     {
    270 #if _LIBC
    271       if (_IO_fwide (stderr, 0) > 0)
    272 	__fwprintf (stderr, L"%s: ", program_name);
    273       else
    274 #endif
    275 	fprintf (stderr, "%s:", program_name);
    276     }
    277 
    278   if (file_name != NULL)
    279     {
    280 #if _LIBC
    281       if (_IO_fwide (stderr, 0) > 0)
    282 	__fwprintf (stderr, L"%s:%d: ", file_name, line_number);
    283       else
    284 #endif
    285 	fprintf (stderr, "%s:%d: ", file_name, line_number);
    286     }
    287 
    288   va_start (args, message);
    289   error_tail (status, errnum, message, args);
    290 
    291 #ifdef _LIBC
    292   _IO_funlockfile (stderr);
    293 # ifdef __libc_ptf_call
    294   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
    295 # endif
    296 #endif
    297 }
    298 
    299 #ifdef _LIBC
    300 /* Make the weak alias.  */
    301 # undef error
    302 # undef error_at_line
    303 weak_alias (__error, error)
    304 weak_alias (__error_at_line, error_at_line)
    305 #endif
    306