public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet.
@ 2018-06-27 16:45 Takashi Yano
  2018-06-27 18:01 ` Takashi Yano
  0 siblings, 1 reply; 2+ messages in thread
From: Takashi Yano @ 2018-06-27 16:45 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 748 bytes --]

POSIX states:
The perror() function shall not change the orientation of the standard
error stream.

However, cygwin perror() function changes the orientation of stderr to
byte-oriented mode if stderr is not oriented yet.

Simple test case (chkperror.c) attached checks this behavior.

Expected result:
0
äöü: Invalid argument
0
äöü
1
äöü: Invalid argument

However, cygwin result:
0
äöü: Invalid argument
-1
â–’â–’â–’
-1
äöü: Invalid argument

I have made a patch to solve this problem, attached. However, I am not
sure that calling _write_r() here is correct manner. I will appreciate
if anyone familiar with libc code comment or make suggestions.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: 0001-Fix-a-bug-of-perror-which-changes-the-orientation-of.patch --]
[-- Type: application/octet-stream, Size: 1690 bytes --]

From cf56cf0eef8d48ca543aac96d57fdfebe3e8767a Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Wed, 27 Jun 2018 19:26:15 +0900
Subject: [PATCH] Fix a bug of perror() which changes the orientation of
 stderr.

* perror.c: Fix the problem that perror() changes the orientation
  of stderr to byte-oriented mode if stderr is not oriented yet.
---
 newlib/libc/stdio/perror.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index d98e17e19..3328425da 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -58,6 +58,23 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
 #include <string.h>
 #include "local.h"
 
+static int
+write_err (struct _reent *ptr, const char *s)
+{
+  size_t len = strlen (s);
+  size_t len_out = 0;
+  int fd_err = fileno (_stderr_r (ptr));
+  while (len_out != len)
+    {
+      _ssize_t len1 = _write_r (ptr, fd_err, s+len_out, len-len_out);
+      if (len1 > 0)
+	len_out += len1;
+      else
+	return -1;
+    }
+  return 0;
+}
+
 void
 _perror_r (struct _reent *ptr,
        const char *s)
@@ -68,14 +85,14 @@ _perror_r (struct _reent *ptr,
   _REENT_SMALL_CHECK_INIT (ptr);
   if (s != NULL && *s != '\0')
     {
-      fputs (s, _stderr_r (ptr));
-      fputs (": ", _stderr_r (ptr));
+      write_err (ptr, s);
+      write_err (ptr, ": ");
     }
 
   if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
-    fputs (error, _stderr_r (ptr));
+    write_err (ptr, error);
 
-  fputc ('\n', _stderr_r (ptr));
+  write_err (ptr, "\n");
 }
 
 #ifndef _REENT_ONLY
-- 
2.17.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: chkperror.c --]
[-- Type: text/x-csrc; name="chkperror.c", Size: 352 bytes --]

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <errno.h>

int main()
{
	setlocale(LC_CTYPE, "de_DE.UTF-8");
	errno = EINVAL;
	printf("%d\n", fwide(stderr, 0));
	perror("äöü");
	printf("%d\n", fwide(stderr, 0));
	fwprintf(stderr, L"äöü\n");
	printf("%d\n", fwide(stderr, 0));
	perror("äöü");
	return 0;
}


[-- Attachment #4: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet.
  2018-06-27 16:45 perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet Takashi Yano
@ 2018-06-27 18:01 ` Takashi Yano
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Yano @ 2018-06-27 18:01 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 238 bytes --]

On Wed, 27 Jun 2018 20:01:16 +0900
Takashi Yano wrote:
> Simple test case (chkperror.c) attached checks this behavior.

The test case seems to be garbled, so I re-submit it compressed by gzip.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: chkperror.c.gz --]
[-- Type: application/octet-stream, Size: 212 bytes --]

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-06-27 11:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-27 16:45 perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet Takashi Yano
2018-06-27 18:01 ` Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).