Re: perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet.

Takashi Yano <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hi Corinna,

On Wed, 27 Jun 2018 14:55:03 +0200
Corinna Vinschen wrote:
> again, please send patches related to newlib to the newlib mailing list.
> Newlib patches affect more targets than just Cygwin.  I redirected this
> mail to the newlib list and attached your original attachments.  Thank
> you.

Next time I will. Sorry for my inadequate understanding about newlib.

> I'm not sure exactly.  It may be nice to keep the writes buffered
> if the original stderr stream is buffered as well.
> 
> What about duplicating the non-_FVWRITE_IN_STREAMIO part of _fputs_r,
> just without calling ORIENT?

Thank you for your suggestion.

I tried this but it failed because __sputc_r() called from _fputs_r also
sets orientation. So I have borrowed the codes from __swbuf_r() in wbuf.c
so that the strings are pushed directly into the buffer.

Could you please have a look?

-- 
Takashi Yano <[email protected]>
0001-Fix-a-bug-of-perror-which-changes-the-orientation-of.patch (application/octet-stream, 2.3 KB)
From a0932c39db0ae2a199b5238c0f65cae5e69e016c Mon Sep 17 00:00:00 2001
From: Takashi Yano <[email protected]>
Date: Thu, 28 Jun 2018 19:46:21 +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 | 54 +++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index d98e17e19..d3a3413c7 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -56,8 +56,54 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
 #include <reent.h>
 #include <stdio.h>
 #include <string.h>
+#include "fvwrite.h"
 #include "local.h"
 
+/* Use static int _fputs_r_loc() instead of fputs()/fputc()
+   to prevent the orientation from being set. */
+static int
+_fputs_r_loc (struct _reent * ptr,
+       char const *__restrict s,
+       FILE *__restrict fp)
+{
+  register int n;
+  const char *p = s;
+
+  /* Ensure stdio has been initialized.  */
+  CHECK_INIT(ptr, fp);
+
+  _newlib_flockfile_start (fp);
+
+  if (cantwrite (ptr, fp))
+    goto error;
+
+  n = fp->_p - fp->_bf._base;
+  if (n >= fp->_bf._size)
+    {
+      if (_fflush_r (ptr, fp))
+	goto error;
+      n = 0;
+    }
+  while (*p)
+    {
+      fp->_w--;
+      *fp->_p++ = *p;
+      if (++n == fp->_bf._size || (fp->_flags & __SLBF && *p == '\n'))
+	{
+	  if (_fflush_r (ptr, fp))
+	    goto error;
+	  n = 0;
+	}
+      p++;
+    }
+  _newlib_flockfile_exit (fp);
+  return 0;
+
+error:
+  _newlib_flockfile_end (fp);
+  return EOF;
+}
+
 void
 _perror_r (struct _reent *ptr,
        const char *s)
@@ -68,14 +114,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));
+      _fputs_r_loc (ptr, s, _stderr_r (ptr));
+      _fputs_r_loc (ptr, ": ", _stderr_r (ptr));
     }
 
   if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
-    fputs (error, _stderr_r (ptr));
+    _fputs_r_loc (ptr, error, _stderr_r (ptr));
 
-  fputc ('\n', _stderr_r (ptr));
+  _fputs_r_loc (ptr, "\n", _stderr_r (ptr));
 }
 
 #ifndef _REENT_ONLY
-- 
2.17.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.