Fix a bug of perror()/psignal() that 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.
* psignal.c: Ditto.
This commit is contained in:
Takashi Yano 2018-07-03 18:04:31 +09:00 committed by Corinna Vinschen
parent 1c1cec9cdf
commit d4f4e7ae1b
2 changed files with 54 additions and 8 deletions

View File

@ -32,13 +32,37 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
#include <_ansi.h> #include <_ansi.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/uio.h>
#define ADD(str) \
{ \
v->iov_base = (void *)(str); \
v->iov_len = strlen (v->iov_base); \
v ++; \
iov_cnt ++; \
}
void void
psignal (int sig, psignal (int sig,
const char *s) const char *s)
{ {
struct iovec iov[4];
struct iovec *v = iov;
int iov_cnt = 0;
if (s != NULL && *s != '\0') if (s != NULL && *s != '\0')
fprintf (stderr, "%s: %s\n", s, strsignal (sig)); {
else ADD (s);
fprintf (stderr, "%s\n", strsignal (sig)); ADD (": ");
}
ADD (strsignal (sig));
#ifdef __SCLE
ADD ((stderr->_flags & __SCLE) ? "\r\n" : "\n");
#else
ADD ("\n");
#endif
fflush (stderr);
writev (fileno (stderr), iov, iov_cnt);
} }

View File

@ -56,26 +56,48 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
#include <reent.h> #include <reent.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/uio.h>
#include "local.h" #include "local.h"
#define ADD(str) \
{ \
v->iov_base = (void *)(str); \
v->iov_len = strlen (v->iov_base); \
v ++; \
iov_cnt ++; \
}
void void
_perror_r (struct _reent *ptr, _perror_r (struct _reent *ptr,
const char *s) const char *s)
{ {
char *error; char *error;
int dummy; int dummy;
struct iovec iov[4];
struct iovec *v = iov;
int iov_cnt = 0;
FILE *fp = _stderr_r (ptr);
_REENT_SMALL_CHECK_INIT (ptr); CHECK_INIT (ptr, fp);
if (s != NULL && *s != '\0') if (s != NULL && *s != '\0')
{ {
fputs (s, _stderr_r (ptr)); ADD (s);
fputs (": ", _stderr_r (ptr)); ADD (": ");
} }
if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL) if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
fputs (error, _stderr_r (ptr)); ADD (error);
fputc ('\n', _stderr_r (ptr)); #ifdef __SCLE
ADD ((fp->_flags & __SCLE) ? "\r\n" : "\n");
#else
ADD ("\n");
#endif
_newlib_flockfile_start (fp);
fflush (fp);
writev (fileno (fp), iov, iov_cnt);
_newlib_flockfile_end (fp);
} }
#ifndef _REENT_ONLY #ifndef _REENT_ONLY