This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

perror doesn't work if stderr is wide



In glibc-2.2, the perror function produces no output if stderr is wide
oriented. But the SUSV2 spec says:

   "The perror() function maps the error number accessed through the
    symbol errno to a language-dependent error message, which is written
    to the standard error stream as follows: ...
    The perror() function does not change the orientation of the
    standard error stream."

Here is a test program:

=====================================================================
#define _ISOC99_SOURCE 1
#include <stdio.h>
#include <wchar.h>
#include <errno.h>

int main ()
{
  fwprintf (stderr, L"Wide output\n");
  errno = EINVAL;
  perror ("test");
  return 0;
}
=====================================================================

It produces only one line of output.

Here is a fix.

2000-12-02  Bruno Haible  <haible@clisp.cons.org>

	* stdio-common/perror.c (perror): If stderr is wide-oriented, use
	fwprintf instead of fprintf.

*** glibc-2.2/stdio-common/perror.c.bak	Wed Apr 15 17:39:15 1998
--- glibc-2.2/stdio-common/perror.c	Sat Dec  2 21:45:40 2000
***************
*** 1,4 ****
! /* Copyright (C) 1991, 1992, 1993, 1997, 1998 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
--- 1,4 ----
! /* Copyright (C) 1991, 1992, 1993, 1997, 1998, 2000 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
***************
*** 17,22 ****
--- 17,23 ----
     Boston, MA 02111-1307, USA.  */
  
  #include <stdio.h>
+ #include <wchar.h>
  #include <string.h>
  #include <errno.h>
  
***************
*** 29,40 ****
    char buf[1024];
    int errnum = errno;
    const char *colon;
  
    if (s == NULL || *s == '\0')
      s = colon = "";
    else
      colon = ": ";
  
!   (void) fprintf (stderr, "%s%s%s\n",
! 		  s, colon, __strerror_r (errnum, buf, sizeof buf));
  }
--- 30,46 ----
    char buf[1024];
    int errnum = errno;
    const char *colon;
+   const char *errstring;
  
    if (s == NULL || *s == '\0')
      s = colon = "";
    else
      colon = ": ";
  
!   errstring = __strerror_r (errnum, buf, sizeof buf);
! 
!   if (fwide (stderr, 0) > 0)
!     (void) fwprintf (stderr, L"%s%s%s\n", s, colon, errstring);
!   else
!     (void) fprintf (stderr, "%s%s%s\n", s, colon, errstring);
  }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]