Correct comments.
[gnulib.git] / lib / error.c
index 31d3aff..45698be 100644 (file)
@@ -1,5 +1,7 @@
 /* Error handler for noninteractive utilities
-   Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
@@ -12,7 +14,7 @@
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
 
@@ -27,9 +29,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef _LIBC
-# include <libintl.h>
-#else
+#if !_LIBC && ENABLE_NLS
 # include "gettext.h"
 #endif
 
@@ -38,7 +38,7 @@
 # define mbsrtowcs __mbsrtowcs
 #endif
 
-#if !_LIBC
+#if USE_UNLOCKED_IO
 # include "unlocked-io.h"
 #endif
 
@@ -58,6 +58,7 @@ unsigned int error_message_count;
 /* In the GNU C library, there is a predefined variable for this.  */
 
 # define program_name program_invocation_name
+# include <errno.h>
 # include <libio/libioP.h>
 
 /* In GNU libc we want do not want to use the common name `error' directly.
@@ -76,6 +77,8 @@ extern void __error_at_line (int status, int errnum, const char *file_name,
 # undef putc
 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
 
+# include <bits/libc-lock.h>
+
 #else /* not _LIBC */
 
 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
@@ -85,6 +88,10 @@ extern void __error_at_line (int status, int errnum, const char *file_name,
 char *strerror_r ();
 # endif
 
+# ifndef SIZE_MAX
+#  define SIZE_MAX ((size_t) -1)
+# endif
+
 /* The calling program should define program_name and set it to the
    name of the executing program.  */
 extern char *program_name;
@@ -97,7 +104,7 @@ extern char *program_name;
 static void
 print_errno_message (int errnum)
 {
-  char const *s;
+  char const *s = NULL;
 
 #if defined HAVE_STRERROR_R || _LIBC
   char errbuf[1024];
@@ -106,15 +113,11 @@ print_errno_message (int errnum)
 # else
   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
     s = errbuf;
-  else
-    s = 0;
 # endif
-#else
-  s = strerror (errnum);
 #endif
 
 #if !_LIBC
-  if (! s)
+  if (! s && ! (s = strerror (errnum)))
     s = _("Unknown system error");
 #endif
 
@@ -132,63 +135,45 @@ print_errno_message (int errnum)
 static void
 error_tail (int status, int errnum, const char *message, va_list args)
 {
-# if HAVE_VPRINTF || _LIBC
-#  if _LIBC
+#if _LIBC
   if (_IO_fwide (stderr, 0) > 0)
     {
-#   define ALLOCA_LIMIT        2000
+# define ALLOCA_LIMIT 2000
       size_t len = strlen (message) + 1;
-      wchar_t *wmessage = NULL;
-      mbstate_t st;
-      size_t res;
-      const char *tmp;
-
-      do
+      const wchar_t *wmessage = L"out of memory";
+      wchar_t *wbuf = (len < ALLOCA_LIMIT
+                      ? alloca (len * sizeof *wbuf)
+                      : len <= SIZE_MAX / sizeof *wbuf
+                      ? malloc (len * sizeof *wbuf)
+                      : NULL);
+
+      if (wbuf)
        {
-         if (len < ALLOCA_LIMIT)
-           wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
-         else
-           {
-             if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
-               wmessage = NULL;
-
-             wmessage = (wchar_t *) realloc (wmessage,
-                                             len * sizeof (wchar_t));
-
-             if (wmessage == NULL)
-               {
-                 fputws_unlocked (L"out of memory\n", stderr);
-                 return;
-               }
-           }
-
+         size_t res;
+         mbstate_t st;
+         const char *tmp = message;
          memset (&st, '\0', sizeof (st));
-         tmp =message;
+         res = mbsrtowcs (wbuf, &tmp, len, &st);
+         wmessage = res == (size_t) -1 ? L"???" : wbuf;
        }
-      while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
-
-      if (res == (size_t) -1)
-       /* The string cannot be converted.  */
-       wmessage = (wchar_t *) L"???";
 
       __vfwprintf (stderr, wmessage, args);
+      if (! (len < ALLOCA_LIMIT))
+       free (wbuf);
     }
   else
-#  endif
+#endif
     vfprintf (stderr, message, args);
-# else
-  _doprnt (message, args, stderr);
-# endif
   va_end (args);
 
   ++error_message_count;
   if (errnum)
     print_errno_message (errnum);
-# if _LIBC
+#if _LIBC
   if (_IO_fwide (stderr, 0) > 0)
     putwc (L'\n', stderr);
   else
-# endif
+#endif
     putc ('\n', stderr);
   fflush (stderr);
   if (status)
@@ -200,12 +185,19 @@ error_tail (int status, int errnum, const char *message, va_list args)
    format string with optional args.
    If ERRNUM is nonzero, print its corresponding system error message.
    Exit with status STATUS if it is nonzero.  */
-/* VARARGS */
 void
 error (int status, int errnum, const char *message, ...)
 {
   va_list args;
 
+#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.  */
+  int state = PTHREAD_CANCEL_ENABLE;
+  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
+                  0);
+#endif
+
   fflush (stdout);
 #ifdef _LIBC
   _IO_flockfile (stderr);
@@ -227,6 +219,9 @@ error (int status, int errnum, const char *message, ...)
 
 #ifdef _LIBC
   _IO_funlockfile (stderr);
+# ifdef __libc_ptf_call
+  __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
+# endif
 #endif
 }
 \f
@@ -255,6 +250,14 @@ error_at_line (int status, int errnum, const char *file_name,
       old_line_number = line_number;
     }
 
+#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.  */
+  int state = PTHREAD_CANCEL_ENABLE;
+  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
+                  0);
+#endif
+
   fflush (stdout);
 #ifdef _LIBC
   _IO_flockfile (stderr);
@@ -286,6 +289,9 @@ error_at_line (int status, int errnum, const char *file_name,
 
 #ifdef _LIBC
   _IO_funlockfile (stderr);
+# ifdef __libc_ptf_call
+  __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
+# endif
 #endif
 }