public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Add _REENT_ERRNO(ptr)
@ 2022-07-13  8:19 Sebastian Huber
  0 siblings, 0 replies; only message in thread
From: Sebastian Huber @ 2022-07-13  8:19 UTC (permalink / raw)
  To: cygwin-cvs, newlib-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f3b8138239d3ba34c4ecaa4305b0fbd7eb4e28a5

commit f3b8138239d3ba34c4ecaa4305b0fbd7eb4e28a5
Author: Matt Joyce <matthew.joyce@embedded-brains.de>
Date:   Tue Jan 18 10:13:04 2022 +0100

    Add _REENT_ERRNO(ptr)
    
    Add a _REENT_ERRNO() macro to encapsulate the access to the
    _errno member of struct reent. This will help to replace the
    structure member with a thread-local storage object in a follow
    up patch.
    
    Replace uses of __errno_r() with _REENT_ERRNO().  Keep __errno_r() macro for
    potential users outside of Newlib.

Diff:
---
 newlib/libc/ctype/wctrans.c               |  2 +-
 newlib/libc/ctype/wctype.c                |  2 +-
 newlib/libc/errno/errno.c                 |  2 +-
 newlib/libc/iconv/lib/iconv.c             | 10 ++++-----
 newlib/libc/iconv/lib/iconvnls.c          |  4 ++--
 newlib/libc/iconv/lib/nullconv.c          |  2 +-
 newlib/libc/iconv/lib/ucsconv.c           | 12 +++++------
 newlib/libc/include/sys/errno.h           |  2 +-
 newlib/libc/include/sys/reent.h           |  1 +
 newlib/libc/machine/cris/sys/errno.h      |  2 +-
 newlib/libc/machine/powerpc/strtosfix16.c | 10 ++++-----
 newlib/libc/machine/powerpc/strtosfix32.c | 10 ++++-----
 newlib/libc/machine/powerpc/strtosfix64.c | 10 ++++-----
 newlib/libc/machine/powerpc/strtoufix16.c | 10 ++++-----
 newlib/libc/machine/powerpc/strtoufix32.c | 10 ++++-----
 newlib/libc/machine/powerpc/strtoufix64.c | 10 ++++-----
 newlib/libc/machine/spu/stdio.c           |  2 +-
 newlib/libc/machine/spu/sys/errno.h       |  2 +-
 newlib/libc/reent/closer.c                |  2 +-
 newlib/libc/reent/execr.c                 |  6 +++---
 newlib/libc/reent/fcntlr.c                |  2 +-
 newlib/libc/reent/fstat64r.c              |  2 +-
 newlib/libc/reent/fstatr.c                |  2 +-
 newlib/libc/reent/gettimeofdayr.c         |  2 +-
 newlib/libc/reent/isattyr.c               |  2 +-
 newlib/libc/reent/linkr.c                 |  2 +-
 newlib/libc/reent/lseek64r.c              |  2 +-
 newlib/libc/reent/lseekr.c                |  2 +-
 newlib/libc/reent/mkdirr.c                |  2 +-
 newlib/libc/reent/open64r.c               |  2 +-
 newlib/libc/reent/openr.c                 |  2 +-
 newlib/libc/reent/readr.c                 |  2 +-
 newlib/libc/reent/renamer.c               |  2 +-
 newlib/libc/reent/sbrkr.c                 |  2 +-
 newlib/libc/reent/signalr.c               |  2 +-
 newlib/libc/reent/stat64r.c               |  2 +-
 newlib/libc/reent/statr.c                 |  2 +-
 newlib/libc/reent/unlinkr.c               |  2 +-
 newlib/libc/reent/writer.c                |  2 +-
 newlib/libc/signal/signal.c               |  6 +++---
 newlib/libc/stdio/asniprintf.c            |  4 ++--
 newlib/libc/stdio/asnprintf.c             |  4 ++--
 newlib/libc/stdio/fdopen.c                |  2 +-
 newlib/libc/stdio/fflush.c                | 18 ++++++++--------
 newlib/libc/stdio/fileno.c                |  2 +-
 newlib/libc/stdio/fileno_u.c              |  2 +-
 newlib/libc/stdio/findfp.c                |  2 +-
 newlib/libc/stdio/flags.c                 |  2 +-
 newlib/libc/stdio/fmemopen.c              | 14 ++++++-------
 newlib/libc/stdio/fopencookie.c           | 14 ++++++-------
 newlib/libc/stdio/fpurge.c                |  2 +-
 newlib/libc/stdio/freopen.c               |  4 ++--
 newlib/libc/stdio/fseeko.c                |  4 ++--
 newlib/libc/stdio/ftell.c                 |  2 +-
 newlib/libc/stdio/ftello.c                |  2 +-
 newlib/libc/stdio/funopen.c               | 16 +++++++--------
 newlib/libc/stdio/fvwrite.c               |  4 ++--
 newlib/libc/stdio/mktemp.c                | 14 ++++++-------
 newlib/libc/stdio/nano-vfprintf.c         | 10 ++++-----
 newlib/libc/stdio/open_memstream.c        | 14 ++++++-------
 newlib/libc/stdio/perror.c                |  2 +-
 newlib/libc/stdio/refill.c                |  2 +-
 newlib/libc/stdio/sniprintf.c             |  8 ++++----
 newlib/libc/stdio/snprintf.c              |  8 ++++----
 newlib/libc/stdio/swprintf.c              |  8 ++++----
 newlib/libc/stdio/tmpfile.c               |  6 +++---
 newlib/libc/stdio/tmpnam.c                |  2 +-
 newlib/libc/stdio/vasniprintf.c           |  2 +-
 newlib/libc/stdio/vasnprintf.c            |  2 +-
 newlib/libc/stdio/vfprintf.c              | 12 +++++------
 newlib/libc/stdio/vfscanf.c               |  2 +-
 newlib/libc/stdio/vfwprintf.c             |  4 ++--
 newlib/libc/stdio/vfwscanf.c              |  2 +-
 newlib/libc/stdio/vsniprintf.c            |  4 ++--
 newlib/libc/stdio/vsnprintf.c             |  4 ++--
 newlib/libc/stdio/vswprintf.c             |  4 ++--
 newlib/libc/stdio/wsetup.c                |  2 +-
 newlib/libc/stdio64/fdopen64.c            |  2 +-
 newlib/libc/stdio64/freopen64.c           |  4 ++--
 newlib/libc/stdio64/fseeko64.c            |  6 +++---
 newlib/libc/stdio64/ftello64.c            |  2 +-
 newlib/libc/stdio64/tmpfile64.c           |  6 +++---
 newlib/libc/stdlib/__adjust.c             |  4 ++--
 newlib/libc/stdlib/mbrtowc.c              |  4 ++--
 newlib/libc/stdlib/mbsnrtowcs.c           |  2 +-
 newlib/libc/stdlib/mbtowc_r.c             | 34 +++++++++++++++----------------
 newlib/libc/stdlib/strtod.c               | 10 ++++-----
 newlib/libc/stdlib/strtoimax.c            |  4 ++--
 newlib/libc/stdlib/strtol.c               |  2 +-
 newlib/libc/stdlib/strtoll.c              |  2 +-
 newlib/libc/stdlib/strtoul.c              |  2 +-
 newlib/libc/stdlib/strtoull.c             |  2 +-
 newlib/libc/stdlib/strtoumax.c            |  4 ++--
 newlib/libc/stdlib/wcrtomb.c              |  4 ++--
 newlib/libc/stdlib/wcsnrtombs.c           |  2 +-
 newlib/libc/stdlib/wcstod.c               |  4 ++--
 newlib/libc/stdlib/wcstoimax.c            |  4 ++--
 newlib/libc/stdlib/wcstol.c               |  2 +-
 newlib/libc/stdlib/wcstoll.c              |  2 +-
 newlib/libc/stdlib/wcstoul.c              |  2 +-
 newlib/libc/stdlib/wcstoull.c             |  4 ++--
 newlib/libc/stdlib/wcstoumax.c            |  4 ++--
 newlib/libc/stdlib/wctomb_r.c             | 18 ++++++++--------
 newlib/libc/string/strerror.c             |  2 +-
 winsup/cygwin/cygerrno.h                  |  4 ++--
 winsup/cygwin/errno.cc                    |  4 ++--
 winsup/cygwin/strfuncs.cc                 | 12 +++++------
 winsup/cygwin/syscalls.cc                 |  4 ++--
 108 files changed, 264 insertions(+), 263 deletions(-)

diff --git a/newlib/libc/ctype/wctrans.c b/newlib/libc/ctype/wctrans.c
index 4a58df152..dbb103d38 100644
--- a/newlib/libc/ctype/wctrans.c
+++ b/newlib/libc/ctype/wctrans.c
@@ -84,7 +84,7 @@ _wctrans_r (struct _reent *r,
     return WCT_TOUPPER;
   else
     {
-      r->_errno = EINVAL;
+      _REENT_ERRNO(r) = EINVAL;
       return 0;
     }
 }
diff --git a/newlib/libc/ctype/wctype.c b/newlib/libc/ctype/wctype.c
index 5f2ae7a25..8c7afc731 100644
--- a/newlib/libc/ctype/wctype.c
+++ b/newlib/libc/ctype/wctype.c
@@ -128,7 +128,7 @@ _wctype_r (struct _reent *r,
     }
 
   /* otherwise invalid */
-  r->_errno = EINVAL;
+  _REENT_ERRNO(r) = EINVAL;
   return 0;
 }
 
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index fd1743d73..70402924b 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -10,7 +10,7 @@
 int *
 __errno ()
 {
-  return &_REENT->_errno;
+  return &_REENT_ERRNO(_REENT);
 }
 
 #endif
diff --git a/newlib/libc/iconv/lib/iconv.c b/newlib/libc/iconv/lib/iconv.c
index 02c749d7f..458541cb9 100644
--- a/newlib/libc/iconv/lib/iconv.c
+++ b/newlib/libc/iconv/lib/iconv.c
@@ -211,7 +211,7 @@ _iconv_r (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return (size_t)-1;
     }
 
@@ -257,19 +257,19 @@ _iconv_r (struct _reent *rptr,
            ic->handlers->set_state (ic->data, &state_save, 1);
         }
        
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
   
   if (*inbytesleft == 0)
     {
-      __errno_r (rptr) = EINVAL;
+      _REENT_ERRNO (rptr) = EINVAL;
       return (size_t)-1;
     }
    
   if (*outbytesleft == 0 || *outbuf == NULL)
     {
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
 
@@ -294,7 +294,7 @@ _iconv_close_r (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return -1;
     }
 
diff --git a/newlib/libc/iconv/lib/iconvnls.c b/newlib/libc/iconv/lib/iconvnls.c
index ed8f3a7ba..5d4b0a273 100644
--- a/newlib/libc/iconv/lib/iconvnls.c
+++ b/newlib/libc/iconv/lib/iconvnls.c
@@ -177,7 +177,7 @@ _iconv_nls_conv (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return (size_t)-1;
     }
   
@@ -189,7 +189,7 @@ _iconv_nls_conv (struct _reent *rptr,
   
   if (outbytesleft == NULL || *outbytesleft == 0)
     {
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
 
diff --git a/newlib/libc/iconv/lib/nullconv.c b/newlib/libc/iconv/lib/nullconv.c
index e75d83376..cc4229181 100644
--- a/newlib/libc/iconv/lib/nullconv.c
+++ b/newlib/libc/iconv/lib/nullconv.c
@@ -73,7 +73,7 @@ null_conversion_convert (struct _reent *rptr,
     {
       result = (size_t)-1;
       len = *outbytesleft;
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
     }
   
   if ((flags & 1) == 0)
diff --git a/newlib/libc/iconv/lib/ucsconv.c b/newlib/libc/iconv/lib/ucsconv.c
index 59f4f993d..502aa440a 100644
--- a/newlib/libc/iconv/lib/ucsconv.c
+++ b/newlib/libc/iconv/lib/ucsconv.c
@@ -164,7 +164,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
 
       if (*outbytesleft == 0)
         {
-          __errno_r (rptr) = E2BIG;
+          _REENT_ERRNO (rptr) = E2BIG;
           return (size_t)-1;
         }
 
@@ -173,13 +173,13 @@ ucs_based_conversion_convert (struct _reent *rptr,
 
       if (ch == (ucs4_t)ICONV_CES_BAD_SEQUENCE)
         {
-          __errno_r (rptr) = EINVAL;
+          _REENT_ERRNO (rptr) = EINVAL;
           return (size_t)-1;
         }
 
       if (ch == (ucs4_t)ICONV_CES_INVALID_CHARACTER)
         {
-          __errno_r (rptr) = EILSEQ;
+          _REENT_ERRNO (rptr) = EILSEQ;
           return (size_t)-1;
         }
 
@@ -196,7 +196,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
         {
           *inbuf = inbuf_save;
           *inbytesleft = inbyteslef_save;
-          __errno_r (rptr) = E2BIG;
+          _REENT_ERRNO (rptr) = E2BIG;
           return (size_t)-1;
         }
       else if (bytes == (size_t)ICONV_CES_INVALID_CHARACTER)
@@ -204,7 +204,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
           if (flags & ICONV_FAIL_BIT)
             {
               /* Generate error */
-              __errno_r (rptr) = EILSEQ;
+              _REENT_ERRNO (rptr) = EILSEQ;
               return (size_t)-1;
             }
           /*
@@ -221,7 +221,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
                                          outbytesleft);
           if ((__int32_t)bytes < 0)
             {
-              __errno_r (rptr) = E2BIG;
+              _REENT_ERRNO (rptr) = E2BIG;
               return (size_t)-1;
             }
       
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 025b461d4..995f30e05 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -26,7 +26,7 @@ extern __IMPORT char *program_invocation_name;
 extern __IMPORT char *program_invocation_short_name;
 #endif
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 #define	EPERM 1		/* Not owner */
 #define	ENOENT 2	/* No such file or directory */
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2b0ff46b7..70a987100 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -719,6 +719,7 @@ struct _reent
 #endif /* !_REENT_SMALL */
 
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
+#define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index 3d573015d..c8113ed73 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -24,7 +24,7 @@ extern int *__errno (void);
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/machine/powerpc/strtosfix16.c b/newlib/libc/machine/powerpc/strtosfix16.c
index 3fba45409..5475de88e 100644
--- a/newlib/libc/machine/powerpc/strtosfix16.c
+++ b/newlib/libc/machine/powerpc/strtosfix16.c
@@ -108,10 +108,10 @@ _strtosfix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return SHRT_MIN;
       return SHRT_MAX;
@@ -120,12 +120,12 @@ _strtosfix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MIN;
     }
 
@@ -152,7 +152,7 @@ _strtosfix16_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return SHRT_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix32.c b/newlib/libc/machine/powerpc/strtosfix32.c
index b21de9ea1..6941be5d5 100644
--- a/newlib/libc/machine/powerpc/strtosfix32.c
+++ b/newlib/libc/machine/powerpc/strtosfix32.c
@@ -29,10 +29,10 @@ _strtosfix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return LONG_MIN;
       return LONG_MAX;
@@ -41,12 +41,12 @@ _strtosfix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MIN;
     }
 
@@ -75,7 +75,7 @@ _strtosfix32_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix64.c b/newlib/libc/machine/powerpc/strtosfix64.c
index 16032128f..9da3ffaee 100644
--- a/newlib/libc/machine/powerpc/strtosfix64.c
+++ b/newlib/libc/machine/powerpc/strtosfix64.c
@@ -32,10 +32,10 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return LONG_LONG_MIN;
       return LONG_LONG_MAX;
@@ -63,7 +63,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp > 0 || (exp == 0 && tmp != 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MIN;
 	}
     }
@@ -71,7 +71,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp >= 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
@@ -88,7 +88,7 @@ _strtosfix64_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix16.c b/newlib/libc/machine/powerpc/strtoufix16.c
index 9b16268bb..415652dd2 100644
--- a/newlib/libc/machine/powerpc/strtoufix16.c
+++ b/newlib/libc/machine/powerpc/strtoufix16.c
@@ -106,10 +106,10 @@ _strtoufix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return USHRT_MAX;
@@ -118,12 +118,12 @@ _strtoufix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return USHRT_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -147,7 +147,7 @@ _strtoufix16_r (struct _reent *rptr,
       if (negexp == 0)
 	{
 	  /* we have overflow which means saturation */
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return USHRT_MAX;
 	}
       result |= (1 << (16 - negexp));
diff --git a/newlib/libc/machine/powerpc/strtoufix32.c b/newlib/libc/machine/powerpc/strtoufix32.c
index 7d03f8c9b..4340c711f 100644
--- a/newlib/libc/machine/powerpc/strtoufix32.c
+++ b/newlib/libc/machine/powerpc/strtoufix32.c
@@ -28,10 +28,10 @@ _strtoufix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return ULONG_MAX;
@@ -40,12 +40,12 @@ _strtoufix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return ULONG_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -77,7 +77,7 @@ _strtoufix32_r (struct _reent *rptr,
       /* if rounding causes carry, then saturation has occurred */
       if (result < tmp)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix64.c b/newlib/libc/machine/powerpc/strtoufix64.c
index a2f0484b3..205706856 100644
--- a/newlib/libc/machine/powerpc/strtoufix64.c
+++ b/newlib/libc/machine/powerpc/strtoufix64.c
@@ -31,10 +31,10 @@ _strtoufix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return 0;
       return ULONG_LONG_MAX;
@@ -60,14 +60,14 @@ _strtoufix64_r (struct _reent *rptr,
   /* check for saturation */
   if (sign)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
   else
     {
       if (exp > 0 || (exp == 0 && tmp >= 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_LONG_MAX;
 	}
     }
@@ -89,7 +89,7 @@ _strtoufix64_r (struct _reent *rptr,
 	  /* if rounding causes carry, then saturation has occurred */
 	  if (result < tmp)
 	    {
-	      rptr->_errno = ERANGE;
+	      _REENT_ERRNO(rptr) = ERANGE;
 	      return ULONG_LONG_MAX;
 	    }
 	}
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 87e4c406d..1dcc88b60 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -47,7 +47,7 @@ __sfp (struct _reent *d)
       return &__fp[i];
     }
   }
-  d->_errno = EMFILE;
+  _REENT_ERRNO(d) = EMFILE;
   return NULL;
 }
 
diff --git a/newlib/libc/machine/spu/sys/errno.h b/newlib/libc/machine/spu/sys/errno.h
index b008ff6f4..8758283eb 100644
--- a/newlib/libc/machine/spu/sys/errno.h
+++ b/newlib/libc/machine/spu/sys/errno.h
@@ -34,7 +34,7 @@ extern "C" {
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/reent/closer.c b/newlib/libc/reent/closer.c
index deb34b002..2d72b2ab5 100644
--- a/newlib/libc/reent/closer.c
+++ b/newlib/libc/reent/closer.c
@@ -45,7 +45,7 @@ _close_r (ptr, fd)
 
   errno = 0;
   if ((ret = _close (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/execr.c b/newlib/libc/reent/execr.c
index 59b61223e..541fb8624 100644
--- a/newlib/libc/reent/execr.c
+++ b/newlib/libc/reent/execr.c
@@ -54,7 +54,7 @@ _execve_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _execve (name, argv, env)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -86,7 +86,7 @@ _fork_r (struct _reent *ptr)
 
   errno = 0;
   if ((ret = _fork ()) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -118,7 +118,7 @@ _wait_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _wait (status)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fcntlr.c b/newlib/libc/reent/fcntlr.c
index cd19d226f..f60aa091c 100644
--- a/newlib/libc/reent/fcntlr.c
+++ b/newlib/libc/reent/fcntlr.c
@@ -49,7 +49,7 @@ _fcntl_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fcntl (fd, cmd, arg)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstat64r.c b/newlib/libc/reent/fstat64r.c
index c546f5c1d..a049206e2 100644
--- a/newlib/libc/reent/fstat64r.c
+++ b/newlib/libc/reent/fstat64r.c
@@ -55,7 +55,7 @@ _fstat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fstat64 (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstatr.c b/newlib/libc/reent/fstatr.c
index ec906c98d..9a02e9a68 100644
--- a/newlib/libc/reent/fstatr.c
+++ b/newlib/libc/reent/fstatr.c
@@ -53,7 +53,7 @@ _fstat_r (ptr, fd, pstat)
 
   errno = 0;
   if ((ret = _fstat (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/gettimeofdayr.c b/newlib/libc/reent/gettimeofdayr.c
index 9b982a993..aa60e5e3a 100644
--- a/newlib/libc/reent/gettimeofdayr.c
+++ b/newlib/libc/reent/gettimeofdayr.c
@@ -60,7 +60,7 @@ _gettimeofday_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _gettimeofday (ptimeval, ptimezone)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/isattyr.c b/newlib/libc/reent/isattyr.c
index f21bf25b2..f76945519 100644
--- a/newlib/libc/reent/isattyr.c
+++ b/newlib/libc/reent/isattyr.c
@@ -50,7 +50,7 @@ _isatty_r (ptr, fd)
 
   errno = 0;
   if ((ret = _isatty (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/linkr.c b/newlib/libc/reent/linkr.c
index b22da5f94..169b6eeec 100644
--- a/newlib/libc/reent/linkr.c
+++ b/newlib/libc/reent/linkr.c
@@ -51,7 +51,7 @@ _link_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _link (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseek64r.c b/newlib/libc/reent/lseek64r.c
index 40769fb6d..c9afd01df 100644
--- a/newlib/libc/reent/lseek64r.c
+++ b/newlib/libc/reent/lseek64r.c
@@ -50,7 +50,7 @@ _lseek64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek64 (fd, pos, whence)) == (_off64_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseekr.c b/newlib/libc/reent/lseekr.c
index ac2daaab9..77e66b8e1 100644
--- a/newlib/libc/reent/lseekr.c
+++ b/newlib/libc/reent/lseekr.c
@@ -47,7 +47,7 @@ _lseek_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek (fd, pos, whence)) == (_off_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/mkdirr.c b/newlib/libc/reent/mkdirr.c
index fd72df64c..cf66a24d3 100644
--- a/newlib/libc/reent/mkdirr.c
+++ b/newlib/libc/reent/mkdirr.c
@@ -48,7 +48,7 @@ _mkdir_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _mkdir (path, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/open64r.c b/newlib/libc/reent/open64r.c
index 84bd67e34..fe288d083 100644
--- a/newlib/libc/reent/open64r.c
+++ b/newlib/libc/reent/open64r.c
@@ -52,7 +52,7 @@ _open64_r (ptr, file, flags, mode)
 
   errno = 0;
   if ((ret = _open64 (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/openr.c b/newlib/libc/reent/openr.c
index c6a7db5de..824315438 100644
--- a/newlib/libc/reent/openr.c
+++ b/newlib/libc/reent/openr.c
@@ -48,7 +48,7 @@ _open_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/readr.c b/newlib/libc/reent/readr.c
index 7fccefd32..b2ae300d3 100644
--- a/newlib/libc/reent/readr.c
+++ b/newlib/libc/reent/readr.c
@@ -47,7 +47,7 @@ _read_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/renamer.c b/newlib/libc/reent/renamer.c
index 5420dc4a0..7e1e111a8 100644
--- a/newlib/libc/reent/renamer.c
+++ b/newlib/libc/reent/renamer.c
@@ -49,7 +49,7 @@ _rename_r (struct _reent *ptr,
 #ifdef HAVE_RENAME
   errno = 0;
   if ((ret = _rename (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else
   if (_link_r (ptr, old, new) == -1)
     return -1;
diff --git a/newlib/libc/reent/sbrkr.c b/newlib/libc/reent/sbrkr.c
index 21c4bd913..ec948fe6b 100644
--- a/newlib/libc/reent/sbrkr.c
+++ b/newlib/libc/reent/sbrkr.c
@@ -49,7 +49,7 @@ _sbrk_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (char *)(_sbrk (incr))) == (void *) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/signalr.c b/newlib/libc/reent/signalr.c
index 345910e4b..863ae7400 100644
--- a/newlib/libc/reent/signalr.c
+++ b/newlib/libc/reent/signalr.c
@@ -51,7 +51,7 @@ _kill_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _kill (pid, sig)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/stat64r.c b/newlib/libc/reent/stat64r.c
index b64736ef2..160d08e33 100644
--- a/newlib/libc/reent/stat64r.c
+++ b/newlib/libc/reent/stat64r.c
@@ -53,7 +53,7 @@ _stat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat64 (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/statr.c b/newlib/libc/reent/statr.c
index 9388e0246..99453e789 100644
--- a/newlib/libc/reent/statr.c
+++ b/newlib/libc/reent/statr.c
@@ -53,7 +53,7 @@ _stat_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/unlinkr.c b/newlib/libc/reent/unlinkr.c
index 41bac0194..495e65b02 100644
--- a/newlib/libc/reent/unlinkr.c
+++ b/newlib/libc/reent/unlinkr.c
@@ -45,7 +45,7 @@ _unlink_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _unlink (file)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/writer.c b/newlib/libc/reent/writer.c
index 704aba18b..ac2217c57 100644
--- a/newlib/libc/reent/writer.c
+++ b/newlib/libc/reent/writer.c
@@ -47,7 +47,7 @@ _write_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_write (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 0110287d8..a249e9f00 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -115,7 +115,7 @@ _signal_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return SIG_ERR;
     }
 
@@ -136,7 +136,7 @@ _raise_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return -1;
     }
 
@@ -151,7 +151,7 @@ _raise_r (struct _reent *ptr,
     return 0;
   else if (func == SIG_ERR)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 1;
     }
   else
diff --git a/newlib/libc/stdio/asniprintf.c b/newlib/libc/stdio/asniprintf.c
index 0bfe00d9b..97e77748e 100644
--- a/newlib/libc/stdio/asniprintf.c
+++ b/newlib/libc/stdio/asniprintf.c
@@ -42,7 +42,7 @@ _asniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -88,7 +88,7 @@ asniprintf (char *buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/asnprintf.c b/newlib/libc/stdio/asnprintf.c
index f657f9ec9..f82556143 100644
--- a/newlib/libc/stdio/asnprintf.c
+++ b/newlib/libc/stdio/asnprintf.c
@@ -42,7 +42,7 @@ _asnprintf_r (struct _reent *__restrict ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -94,7 +94,7 @@ asnprintf (char *__restrict buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/fdopen.c b/newlib/libc/stdio/fdopen.c
index ef942c284..3481154d1 100644
--- a/newlib/libc/stdio/fdopen.c
+++ b/newlib/libc/stdio/fdopen.c
@@ -73,7 +73,7 @@ _fdopen_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio/fflush.c b/newlib/libc/stdio/fflush.c
index bbec4a19b..d51e90153 100644
--- a/newlib/libc/stdio/fflush.c
+++ b/newlib/libc/stdio/fflush.c
@@ -137,8 +137,8 @@ __sflush_r (struct _reent *ptr,
 	  /* Save last errno and set errno to 0, so we can check if a device
 	     returns with a valid position -1.  We restore the last errno if
 	     no other error condition has been encountered. */
-	  tmp_errno = ptr->_errno;
-	  ptr->_errno = 0;
+	  tmp_errno = _REENT_ERRNO(ptr);
+	  _REENT_ERRNO(ptr) = 0;
 	  /* Get the physical position we are at in the file.  */
 	  if (fp->_flags & __SOFF)
 	    curoff = fp->_offset;
@@ -152,13 +152,13 @@ __sflush_r (struct _reent *ptr,
 	      else
 #endif
 		curoff = fp->_seek (ptr, fp->_cookie, 0, SEEK_CUR);
-	      if (curoff == -1L && ptr->_errno != 0)
+	      if (curoff == -1L && _REENT_ERRNO(ptr) != 0)
 		{
 		  int result = EOF;
-		  if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+		  if (_REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 		    {
 		      result = 0;
-		      ptr->_errno = tmp_errno;
+		      _REENT_ERRNO(ptr) = tmp_errno;
 		    }
 		  else
 		    fp->_flags |= __SERR;
@@ -180,8 +180,8 @@ __sflush_r (struct _reent *ptr,
 	  else
 #endif
 	    curoff = fp->_seek (ptr, fp->_cookie, curoff, SEEK_SET);
-	  if (curoff != -1 || ptr->_errno == 0
-	      || ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+	  if (curoff != -1 || _REENT_ERRNO(ptr) == 0
+	      || _REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 	    {
 	      /* Seek successful or ignorable error condition.
 		 We can clear read buffer now.  */
@@ -190,9 +190,9 @@ __sflush_r (struct _reent *ptr,
 #endif
 	      fp->_r = 0;
 	      fp->_p = fp->_bf._base;
-	      if ((fp->_flags & __SOFF) && (curoff != -1 || ptr->_errno == 0))
+	      if ((fp->_flags & __SOFF) && (curoff != -1 || _REENT_ERRNO(ptr) == 0))
 		fp->_offset = curoff;
-	      ptr->_errno = tmp_errno;
+	      _REENT_ERRNO(ptr) = tmp_errno;
 	      if (HASUB (fp))
 		FREEUB (ptr, fp);
 	    }
diff --git a/newlib/libc/stdio/fileno.c b/newlib/libc/stdio/fileno.c
index 10fc6e283..4faa6a843 100644
--- a/newlib/libc/stdio/fileno.c
+++ b/newlib/libc/stdio/fileno.c
@@ -73,7 +73,7 @@ fileno (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   _newlib_flockfile_end (f);
   return result;
diff --git a/newlib/libc/stdio/fileno_u.c b/newlib/libc/stdio/fileno_u.c
index 830a07d2b..f3940e885 100644
--- a/newlib/libc/stdio/fileno_u.c
+++ b/newlib/libc/stdio/fileno_u.c
@@ -39,7 +39,7 @@ fileno_unlocked (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   return result;
 }
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index ee991ed24..c7a4a941e 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -179,7 +179,7 @@ __sfp (struct _reent *d)
 	break;
     }
   _newlib_sfp_lock_exit ();
-  d->_errno = ENOMEM;
+  _REENT_ERRNO(d) = ENOMEM;
   return NULL;
 
 found:
diff --git a/newlib/libc/stdio/flags.c b/newlib/libc/stdio/flags.c
index 7bbd50181..d686fa046 100644
--- a/newlib/libc/stdio/flags.c
+++ b/newlib/libc/stdio/flags.c
@@ -56,7 +56,7 @@ __sflags (struct _reent *ptr,
       o = O_CREAT | O_APPEND;
       break;
     default:			/* illegal mode */
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return (0);
     }
   while (*++mode)
diff --git a/newlib/libc/stdio/fmemopen.c b/newlib/libc/stdio/fmemopen.c
index 0d043520e..886ba7efe 100644
--- a/newlib/libc/stdio/fmemopen.c
+++ b/newlib/libc/stdio/fmemopen.c
@@ -148,7 +148,7 @@ fmemwriter (struct _reent *ptr,
     memcpy (c->buf + c->pos - n, buf, n - adjust);
   else
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       return EOF;
     }
   return n;
@@ -175,18 +175,18 @@ fmemseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -224,12 +224,12 @@ fmemseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -277,7 +277,7 @@ _fmemopen_r (struct _reent *ptr,
     return NULL;
   if (!size || !(buf || flags & __SRW))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fopencookie.c b/newlib/libc/stdio/fopencookie.c
index 0861528e0..ab406a866 100644
--- a/newlib/libc/stdio/fopencookie.c
+++ b/newlib/libc/stdio/fopencookie.c
@@ -107,7 +107,7 @@ fcreader (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -129,7 +129,7 @@ fcwriter (struct _reent *ptr,
     }
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -148,11 +148,11 @@ fcseeker (struct _reent *ptr,
 
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #ifdef __LARGE64_FILES
   else if ((_fpos_t)offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -170,7 +170,7 @@ fcseeker64 (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return (_fpos64_t) offset;
 }
 #endif /* __LARGE64_FILES */
@@ -185,7 +185,7 @@ fccloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -207,7 +207,7 @@ _fopencookie_r (struct _reent *ptr,
   if (((flags & (__SRD | __SRW)) && !functions.read)
       || ((flags & (__SWR | __SRW)) && !functions.write))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fpurge.c b/newlib/libc/stdio/fpurge.c
index 2e4f61ec9..93ac0e2a9 100644
--- a/newlib/libc/stdio/fpurge.c
+++ b/newlib/libc/stdio/fpurge.c
@@ -72,7 +72,7 @@ _fpurge_r (struct _reent *ptr,
   t = fp->_flags;
   if (!t)
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       _newlib_flockfile_exit (fp);
       return EOF;
     }
diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c
index 29ba5b7ff..f0da9a159 100644
--- a/newlib/libc/stdio/freopen.c
+++ b/newlib/libc/stdio/freopen.c
@@ -137,7 +137,7 @@ _freopen_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -205,7 +205,7 @@ _freopen_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;	/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index 13df28bfc..6fcc8ef06 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -128,7 +128,7 @@ _fseeko_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit (fp);
       return EOF;
     }
@@ -178,7 +178,7 @@ _fseeko_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit (fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio/ftell.c b/newlib/libc/stdio/ftell.c
index c540c430f..f68b3afcc 100644
--- a/newlib/libc/stdio/ftell.c
+++ b/newlib/libc/stdio/ftell.c
@@ -92,7 +92,7 @@ _ftell_r (struct _reent *ptr,
   if ((long)pos != pos)
     {
       pos = -1;
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
     }
   return (long)pos;
 }
diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 3df200cea..d62d48a89 100644
--- a/newlib/libc/stdio/ftello.c
+++ b/newlib/libc/stdio/ftello.c
@@ -96,7 +96,7 @@ _ftello_r (struct _reent * ptr,
 
   if (fp->_seek == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit (fp);
       return (_off_t) -1;
     }
diff --git a/newlib/libc/stdio/funopen.c b/newlib/libc/stdio/funopen.c
index a1c0d767d..4ed50196e 100644
--- a/newlib/libc/stdio/funopen.c
+++ b/newlib/libc/stdio/funopen.c
@@ -113,7 +113,7 @@ funreader (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -127,7 +127,7 @@ funwriter (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -142,15 +142,15 @@ funseeker (struct _reent *ptr,
   fpos_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (fpos_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else /* __LARGE64_FILES */
   _fpos64_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (_fpos64_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   else if ((_fpos_t)result != result)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       result = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -168,7 +168,7 @@ funseeker64 (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 #endif /* __LARGE64_FILES */
@@ -183,7 +183,7 @@ funcloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -202,7 +202,7 @@ _funopen_r (struct _reent *ptr,
 
   if (!readfn && !writefn)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fvwrite.c b/newlib/libc/stdio/fvwrite.c
index 587ffacd7..34361cb8d 100644
--- a/newlib/libc/stdio/fvwrite.c
+++ b/newlib/libc/stdio/fvwrite.c
@@ -145,7 +145,7 @@ __sfvwrite_r (struct _reent *ptr,
 		      str = (unsigned char *)_malloc_r (ptr, newsize);
 		      if (!str)
 			{
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		      memcpy (str, fp->_bf._base, curpos);
@@ -162,7 +162,7 @@ __sfvwrite_r (struct _reent *ptr,
 			  _free_r (ptr, fp->_bf._base);
 			  fp->_flags &=  ~__SMBF;
 			  /* Ensure correct errno, even if free changed it.  */
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		    }
diff --git a/newlib/libc/stdio/mktemp.c b/newlib/libc/stdio/mktemp.c
index 3514818a6..265968198 100644
--- a/newlib/libc/stdio/mktemp.c
+++ b/newlib/libc/stdio/mktemp.c
@@ -159,7 +159,7 @@ _gettemp (struct _reent *ptr,
     continue;
   if (trv - path < suffixlen)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
   trv -= suffixlen;
@@ -171,7 +171,7 @@ _gettemp (struct _reent *ptr,
     }
   if (end - trv < 6)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
 
@@ -195,7 +195,7 @@ _gettemp (struct _reent *ptr,
 	    return (0);
 	  if (!(sbuf.st_mode & S_IFDIR))
 	    {
-	      ptr->_errno = ENOTDIR;
+	      _REENT_ERRNO(ptr) = ENOTDIR;
 	      return (0);
 	    }
 	  *trv = '/';
@@ -211,10 +211,10 @@ _gettemp (struct _reent *ptr,
 #ifdef HAVE_MKDIR
 	  if (_mkdir_r (ptr, path, 0700) == 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 #else /* !HAVE_MKDIR */
-	  ptr->_errno = ENOSYS;
+	  _REENT_ERRNO(ptr) = ENOSYS;
 	  return 0;
 #endif /* !HAVE_MKDIR */
 	}
@@ -225,7 +225,7 @@ _gettemp (struct _reent *ptr,
 	  if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR | flags,
 				  0600)) >= 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 	}
 #ifdef __USE_INTERNAL_STAT64
@@ -233,7 +233,7 @@ _gettemp (struct _reent *ptr,
 #else
       else if (_stat_r (ptr, path, &sbuf))
 #endif
-	return (ptr->_errno == ENOENT ? 1 : 0);
+	return (_REENT_ERRNO(ptr) == ENOENT ? 1 : 0);
 
       /* tricky little algorithm for backward compatibility */
       for (trv = start;;)
diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index 838804eb9..0d42a940f 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -201,7 +201,7 @@ __ssputs_r (struct _reent *ptr,
 	  str = (unsigned char *)_malloc_r (ptr, newsize);
 	  if (!str)
 	    {
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	  memcpy (str, fp->_bf._base, curpos);
@@ -215,7 +215,7 @@ __ssputs_r (struct _reent *ptr,
 	      /* Free unneeded buffer.  */
 	      _free_r (ptr, fp->_bf._base);
 	      /* Ensure correct errno, even if free changed it.  */
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	}
@@ -291,7 +291,7 @@ __ssprint_r (struct _reent *ptr,
 	      str = (unsigned char *)_malloc_r (ptr, newsize);
 	      if (!str)
 		{
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	      memcpy (str, fp->_bf._base, curpos);
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 		  /* Free unneeded buffer.  */
 		  _free_r (ptr, fp->_bf._base);
 		  /* Ensure correct errno, even if free changed it.  */
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	    }
@@ -501,7 +501,7 @@ _VFPRINTF_R (struct _reent *data,
       fp->_bf._base = fp->_p = _malloc_r (data, 64);
       if (!fp->_p)
 	{
-	  data->_errno = ENOMEM;
+	  _REENT_ERRNO(data) = ENOMEM;
 	  return EOF;
 	}
       fp->_bf._size = 64;
diff --git a/newlib/libc/stdio/open_memstream.c b/newlib/libc/stdio/open_memstream.c
index ed71b1cdc..e3a52ed33 100644
--- a/newlib/libc/stdio/open_memstream.c
+++ b/newlib/libc/stdio/open_memstream.c
@@ -105,7 +105,7 @@ memwriter (struct _reent *ptr,
      big that user cannot do ftello.  */
   if (sizeof (OFF_T) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0)
     {
-      ptr->_errno = EFBIG;
+      _REENT_ERRNO(ptr) = EFBIG;
       return EOF;
     }
   /* Grow the buffer, if necessary.  Choose a geometric growth factor
@@ -160,18 +160,18 @@ memseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -227,12 +227,12 @@ memseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -301,7 +301,7 @@ internal_open_memstream_r (struct _reent *ptr,
 
   if (!buf || !size)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index eda7e993b..2033a6f2f 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -90,7 +90,7 @@ _perror_r (struct _reent *ptr,
       WRITE_STR (": ");
     }
 
-  if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
+  if ((error = _strerror_r (ptr, _REENT_ERRNO(ptr), 1, &dummy)) != NULL)
     WRITE_STR (error);
 
 #ifdef __SCLE
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index 91e79e832..7bd38806c 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -56,7 +56,7 @@ __srefill_r (struct _reent * ptr,
     {
       if ((fp->_flags & __SRW) == 0)
 	{
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
 	}
diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c
index 375a3971f..b05ffcb12 100644
--- a/newlib/libc/stdio/sniprintf.c
+++ b/newlib/libc/stdio/sniprintf.c
@@ -38,7 +38,7 @@ _sniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -49,7 +49,7 @@ _sniprintf_r (struct _reent *ptr,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -69,7 +69,7 @@ sniprintf (char *str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -80,7 +80,7 @@ sniprintf (char *str,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/snprintf.c b/newlib/libc/stdio/snprintf.c
index 50d7ddd11..4a3084bea 100644
--- a/newlib/libc/stdio/snprintf.c
+++ b/newlib/libc/stdio/snprintf.c
@@ -37,7 +37,7 @@ _snprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -48,7 +48,7 @@ _snprintf_r (struct _reent *ptr,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -74,7 +74,7 @@ snprintf (char *__restrict str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -85,7 +85,7 @@ snprintf (char *__restrict str,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/swprintf.c b/newlib/libc/stdio/swprintf.c
index ec7e089ca..634b22beb 100644
--- a/newlib/libc/stdio/swprintf.c
+++ b/newlib/libc/stdio/swprintf.c
@@ -564,7 +564,7 @@ _swprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -584,7 +584,7 @@ _swprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
@@ -604,7 +604,7 @@ swprintf (wchar_t *__restrict str,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -624,7 +624,7 @@ swprintf (wchar_t *__restrict str,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
diff --git a/newlib/libc/stdio/tmpfile.c b/newlib/libc/stdio/tmpfile.c
index f209a3edf..5fe6e3bf1 100644
--- a/newlib/libc/stdio/tmpfile.c
+++ b/newlib/libc/stdio/tmpfile.c
@@ -65,15 +65,15 @@ _tmpfile_r (struct _reent *ptr)
       fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		    S_IRUSR | S_IWUSR);
     }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 3bd5b6a58..7379a7640 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -104,7 +104,7 @@ worker (struct _reent *ptr,
       t = _open_r (ptr, result, O_RDONLY, 0);
       if (t == -1)
 	{
-	  if (ptr->_errno == ENOSYS)
+	  if (_REENT_ERRNO(ptr) == ENOSYS)
 	    {
 	      result[0] = '\0';
 	      return 0;
diff --git a/newlib/libc/stdio/vasniprintf.c b/newlib/libc/stdio/vasniprintf.c
index edfbd2242..b24cd96fb 100644
--- a/newlib/libc/stdio/vasniprintf.c
+++ b/newlib/libc/stdio/vasniprintf.c
@@ -42,7 +42,7 @@ _vasniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vasnprintf.c b/newlib/libc/stdio/vasnprintf.c
index 1fbf5a171..b3787ec7a 100644
--- a/newlib/libc/stdio/vasnprintf.c
+++ b/newlib/libc/stdio/vasnprintf.c
@@ -42,7 +42,7 @@ _vasnprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vfprintf.c b/newlib/libc/stdio/vfprintf.c
index 32ebb1468..6a198e2c6 100644
--- a/newlib/libc/stdio/vfprintf.c
+++ b/newlib/libc/stdio/vfprintf.c
@@ -222,7 +222,7 @@ __ssputs_r (struct _reent *ptr,
 			str = (unsigned char *)_malloc_r (ptr, newsize);
 			if (!str)
 			{
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 			memcpy (str, fp->_bf._base, curpos);
@@ -237,7 +237,7 @@ __ssputs_r (struct _reent *ptr,
 				_free_r (ptr, fp->_bf._base);
 				/* Ensure correct errno, even if free
 				 * changed it.  */
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 		}
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 				str = (unsigned char *)_malloc_r (ptr, newsize);
 				if (!str)
 				{
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 				memcpy (str, fp->_bf._base, curpos);
@@ -321,7 +321,7 @@ __ssprint_r (struct _reent *ptr,
 					_free_r (ptr, fp->_bf._base);
 					/* Ensure correct errno, even if free
 					 * changed it.  */
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 			}
@@ -868,7 +868,7 @@ _VFPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1374,7 +1374,7 @@ reswitch:	switch (ch) {
 		case 'm':  /* extension */
 			{
 				int dummy;
-				cp = _strerror_r (data, data->_errno, 1, &dummy);
+				cp = _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfscanf.c b/newlib/libc/stdio/vfscanf.c
index 037692531..cfeea9876 100644
--- a/newlib/libc/stdio/vfscanf.c
+++ b/newlib/libc/stdio/vfscanf.c
@@ -774,7 +774,7 @@ __SVFSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vfwprintf.c b/newlib/libc/stdio/vfwprintf.c
index 7384b37d3..7807a1229 100644
--- a/newlib/libc/stdio/vfwprintf.c
+++ b/newlib/libc/stdio/vfwprintf.c
@@ -611,7 +611,7 @@ _VFWPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1119,7 +1119,7 @@ reswitch:	switch (ch) {
 		case L'm':  /* GNU extension */
 			{
 				int dummy;
-				cp = (wchar_t *) _strerror_r (data, data->_errno, 1, &dummy);
+				cp = (wchar_t *) _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfwscanf.c b/newlib/libc/stdio/vfwscanf.c
index e9e00dfec..df966f929 100644
--- a/newlib/libc/stdio/vfwscanf.c
+++ b/newlib/libc/stdio/vfwscanf.c
@@ -682,7 +682,7 @@ __SVFWSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vsniprintf.c b/newlib/libc/stdio/vsniprintf.c
index a8b272e2f..e7625ffd5 100644
--- a/newlib/libc/stdio/vsniprintf.c
+++ b/newlib/libc/stdio/vsniprintf.c
@@ -54,7 +54,7 @@ _vsniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -63,7 +63,7 @@ _vsniprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vsnprintf.c b/newlib/libc/stdio/vsnprintf.c
index 5c617a8a3..ecf82eaef 100644
--- a/newlib/libc/stdio/vsnprintf.c
+++ b/newlib/libc/stdio/vsnprintf.c
@@ -60,7 +60,7 @@ _vsnprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -69,7 +69,7 @@ _vsnprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vswprintf.c b/newlib/libc/stdio/vswprintf.c
index 89795ed8d..08a8545ba 100644
--- a/newlib/libc/stdio/vswprintf.c
+++ b/newlib/libc/stdio/vswprintf.c
@@ -42,7 +42,7 @@ _vswprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -60,7 +60,7 @@ _vswprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return ret;
diff --git a/newlib/libc/stdio/wsetup.c b/newlib/libc/stdio/wsetup.c
index 6a820f1c0..0fd9690aa 100644
--- a/newlib/libc/stdio/wsetup.c
+++ b/newlib/libc/stdio/wsetup.c
@@ -45,7 +45,7 @@ __swsetup_r (struct _reent *ptr,
     {
       if ((fp->_flags & __SRW) == 0)
         {
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
         }
diff --git a/newlib/libc/stdio64/fdopen64.c b/newlib/libc/stdio64/fdopen64.c
index 9d9645b36..d93b3d4d8 100644
--- a/newlib/libc/stdio64/fdopen64.c
+++ b/newlib/libc/stdio64/fdopen64.c
@@ -55,7 +55,7 @@ _fdopen64_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c
index e6ba64f7d..4d1e9b76c 100644
--- a/newlib/libc/stdio64/freopen64.c
+++ b/newlib/libc/stdio64/freopen64.c
@@ -138,7 +138,7 @@ _freopen64_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open64_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -206,7 +206,7 @@ _freopen64_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;	/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index 3087bef9e..c5b30aed0 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -102,7 +102,7 @@ _fseeko64_r (struct _reent *ptr,
     {
       if ((_off_t) offset != offset)
 	{
-	  ptr->_errno = EOVERFLOW;
+	  _REENT_ERRNO(ptr) = EOVERFLOW;
 	  return EOF;
 	}
       return (_off64_t) _fseeko_r (ptr, fp, offset, whence);
@@ -129,7 +129,7 @@ _fseeko64_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek64) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit(fp);
       return EOF;
     }
@@ -179,7 +179,7 @@ _fseeko64_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit(fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 6fb76c3ef..cd00def46 100644
--- a/newlib/libc/stdio64/ftello64.c
+++ b/newlib/libc/stdio64/ftello64.c
@@ -93,7 +93,7 @@ _ftello64_r (struct _reent *ptr,
 
   if (fp->_seek64 == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit(fp);
       return (_off64_t) -1;
     }
diff --git a/newlib/libc/stdio64/tmpfile64.c b/newlib/libc/stdio64/tmpfile64.c
index 18a38d65c..35b035c91 100644
--- a/newlib/libc/stdio64/tmpfile64.c
+++ b/newlib/libc/stdio64/tmpfile64.c
@@ -68,15 +68,15 @@ _tmpfile64_r (struct _reent *ptr)
       fd = _open64_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		      S_IRUSR | S_IWUSR);
   }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen64_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdlib/__adjust.c b/newlib/libc/stdlib/__adjust.c
index 4c478f30b..ab6f125b9 100644
--- a/newlib/libc/stdlib/__adjust.c
+++ b/newlib/libc/stdlib/__adjust.c
@@ -21,12 +21,12 @@ __adjust (struct _reent *ptr,
 
   if (dexp > MAXE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return (sign) ? -HUGE_VAL : HUGE_VAL;
     }
   else if (dexp < MINE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return 0.0;
     }
 
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 65284a0eb..521b7a5f4 100644
--- a/newlib/libc/stdlib/mbrtowc.c
+++ b/newlib/libc/stdlib/mbrtowc.c
@@ -32,7 +32,7 @@ _mbrtowc_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -68,7 +68,7 @@ mbrtowc (wchar_t *__restrict pwc,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index d3ce25084..8f94b1da5 100644
--- a/newlib/libc/stdlib/mbsnrtowcs.c
+++ b/newlib/libc/stdlib/mbsnrtowcs.c
@@ -126,7 +126,7 @@ _mbsnrtowcs_r (struct _reent *r,
       else
 	{
 	  ps->__count = 0;
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return (size_t)-1;
 	}
     }
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 920a7ea3c..ca876f9a0 100644
--- a/newlib/libc/stdlib/mbtowc_r.c
+++ b/newlib/libc/stdlib/mbtowc_r.c
@@ -39,7 +39,7 @@ __ascii_mbtowc (struct _reent *r,
 #ifdef __CYGWIN__
   if ((wchar_t)*t >= 0x80)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 #endif
@@ -117,7 +117,7 @@ ___iso_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __iso_8859_conv[iso_idx][*t - 0xa0];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -290,7 +290,7 @@ ___cp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __cp_conv[cp_idx][*t - 0x80];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -578,13 +578,13 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (state->__value.__wchb[0] < 0xc2)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -607,12 +607,12 @@ __utf8_mbtowc (struct _reent *r,
       if (state->__value.__wchb[0] == 0xe0 && ch < 0xa0)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -625,7 +625,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -651,12 +651,12 @@ __utf8_mbtowc (struct _reent *r,
 	  || (state->__value.__wchb[0] == 0xf4 && ch >= 0x90))
 	{
 	  /* overlong UTF-8 sequence or result is > 0x10ffff */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -669,7 +669,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = (state->__count == 2) ? t[i++] : state->__value.__wchb[2];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[2] = ch;
@@ -702,7 +702,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       tmp = (wint_t)((state->__value.__wchb[0] & 0x07) << 18)
@@ -719,7 +719,7 @@ __utf8_mbtowc (struct _reent *r,
       return i;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -769,7 +769,7 @@ __sjis_mbtowc (struct _reent *r,
 	}
       else  
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -836,7 +836,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -851,7 +851,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -955,7 +955,7 @@ __jis_mbtowc (struct _reent *r,
 	  break;
 	case ERROR:
 	default:
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
 
diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c
index cd0222481..9156ed7d6 100644
--- a/newlib/libc/stdlib/strtod.c
+++ b/newlib/libc/stdlib/strtod.c
@@ -595,7 +595,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 			if (e1 > DBL_MAX_10_EXP) {
  ovfl:
 #ifndef NO_ERRNO
-				ptr->_errno = ERANGE;
+				_REENT_ERRNO(ptr) = ERANGE;
 #endif
 				/* Can't trust HUGE_VAL */
 #ifdef IEEE_Arith
@@ -702,7 +702,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
  undfl:
 					dval(rv) = 0.;
 #ifndef NO_ERRNO
-					ptr->_errno = ERANGE;
+					_REENT_ERRNO(ptr) = ERANGE;
 #endif
 					if (bd0)
 						goto retfree;
@@ -1249,7 +1249,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 #ifndef NO_ERRNO
 		/* try to avoid the bug of testing an 8087 register value */
 		if ((dword0(rv) & Exp_mask) == 0)
-			ptr->_errno = ERANGE;
+			_REENT_ERRNO(ptr) = ERANGE;
 #endif
 		}
 #endif /* Avoid_Underflow */
@@ -1303,7 +1303,7 @@ strtof_l (const char *__restrict s00, char **__restrict se, locale_t loc)
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -1340,7 +1340,7 @@ strtof (const char *__restrict s00,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if ((isinf (retval) && !isinf (val)) || (isdenormf(retval) && !isdenorm(val)))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
diff --git a/newlib/libc/stdlib/strtoimax.c b/newlib/libc/stdlib/strtoimax.c
index c3f27df2e..a64b7da66 100644
--- a/newlib/libc/stdlib/strtoimax.c
+++ b/newlib/libc/stdlib/strtoimax.c
@@ -136,10 +136,10 @@ _strtoimax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index 6383c27e8..09d4333ed 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -204,7 +204,7 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoll.c b/newlib/libc/stdlib/strtoll.c
index 9aa2c747b..0f2fa5315 100644
--- a/newlib/libc/stdlib/strtoll.c
+++ b/newlib/libc/stdlib/strtoll.c
@@ -201,7 +201,7 @@ _strtoll_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoul.c b/newlib/libc/stdlib/strtoul.c
index 4191e43ec..d31bde5d2 100644
--- a/newlib/libc/stdlib/strtoul.c
+++ b/newlib/libc/stdlib/strtoul.c
@@ -178,7 +178,7 @@ _strtoul_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoull.c b/newlib/libc/stdlib/strtoull.c
index 10018ca0d..943de6b83 100644
--- a/newlib/libc/stdlib/strtoull.c
+++ b/newlib/libc/stdlib/strtoull.c
@@ -176,7 +176,7 @@ _strtoull_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoumax.c b/newlib/libc/stdlib/strtoumax.c
index cf1a427fe..adcf2b338 100644
--- a/newlib/libc/stdlib/strtoumax.c
+++ b/newlib/libc/stdlib/strtoumax.c
@@ -115,10 +115,10 @@ _strtoumax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
index 97436cb74..6d670e23a 100644
--- a/newlib/libc/stdlib/wcrtomb.c
+++ b/newlib/libc/stdlib/wcrtomb.c
@@ -31,7 +31,7 @@ _wcrtomb_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -67,7 +67,7 @@ wcrtomb (char *__restrict s,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index 43dd2f3e6..dfd974f24 100644
--- a/newlib/libc/stdlib/wcsnrtombs.c
+++ b/newlib/libc/stdlib/wcsnrtombs.c
@@ -104,7 +104,7 @@ _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
       int bytes = loc->wctomb (r, buff, *pwcs, ps);
       if (bytes == -1)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  ps->__count = 0;
 	  return (size_t)-1;
 	}
diff --git a/newlib/libc/stdlib/wcstod.c b/newlib/libc/stdlib/wcstod.c
index 375ffe288..7b0983470 100644
--- a/newlib/libc/stdlib/wcstod.c
+++ b/newlib/libc/stdlib/wcstod.c
@@ -257,7 +257,7 @@ wcstof_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -272,7 +272,7 @@ wcstof (const wchar_t *__restrict nptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
 
   return retval;
diff --git a/newlib/libc/stdlib/wcstoimax.c b/newlib/libc/stdlib/wcstoimax.c
index 02ab1c1f4..0c939c978 100644
--- a/newlib/libc/stdlib/wcstoimax.c
+++ b/newlib/libc/stdlib/wcstoimax.c
@@ -122,10 +122,10 @@ _wcstoimax_l(struct _reent *rptr, const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcstol.c b/newlib/libc/stdlib/wcstol.c
index 8b6de3873..2a8c28f97 100644
--- a/newlib/libc/stdlib/wcstol.c
+++ b/newlib/libc/stdlib/wcstol.c
@@ -200,7 +200,7 @@ _wcstol_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoll.c b/newlib/libc/stdlib/wcstoll.c
index c0e5dc747..34a7e376d 100644
--- a/newlib/libc/stdlib/wcstoll.c
+++ b/newlib/libc/stdlib/wcstoll.c
@@ -200,7 +200,7 @@ _wcstoll_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoul.c b/newlib/libc/stdlib/wcstoul.c
index fe3c87867..9c78fc3b4 100644
--- a/newlib/libc/stdlib/wcstoul.c
+++ b/newlib/libc/stdlib/wcstoul.c
@@ -179,7 +179,7 @@ _wcstoul_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoull.c b/newlib/libc/stdlib/wcstoull.c
index 5ac325790..09422fa97 100644
--- a/newlib/libc/stdlib/wcstoull.c
+++ b/newlib/libc/stdlib/wcstoull.c
@@ -150,7 +150,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	register int neg = 0, any, cutlim;
 
 	if(base < 0  ||  base == 1  ||  base > 36)  {
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 		return(0ULL);
 	}
 	/*
@@ -195,7 +195,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULLONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoumax.c b/newlib/libc/stdlib/wcstoumax.c
index 17b5275fa..623f5b947 100644
--- a/newlib/libc/stdlib/wcstoumax.c
+++ b/newlib/libc/stdlib/wcstoumax.c
@@ -121,10 +121,10 @@ _wcstoumax_l(struct _reent *rptr,const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
index b4799341e..a7f87cd9e 100644
--- a/newlib/libc/stdlib/wctomb_r.c
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -35,7 +35,7 @@ __ascii_wctomb (struct _reent *r,
   if ((size_t)wchar >= 0x100)
 #endif
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -133,7 +133,7 @@ __utf8_wctomb (struct _reent *r,
       return 4;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -165,7 +165,7 @@ __sjis_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -204,7 +204,7 @@ __eucjp_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -244,7 +244,7 @@ __jis_wctomb (struct _reent *r,
 	  *s = (char)char2;
 	  return cnt + 2;
 	}
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   if (state->__state != 0)
@@ -284,14 +284,14 @@ ___iso_wctomb (struct _reent *r, char *s, wchar_t _wchar, int iso_idx,
 		*s = (char) (mb + 0xa0);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
  
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -440,14 +440,14 @@ ___cp_wctomb (struct _reent *r, char *s, wchar_t _wchar, int cp_idx,
 		*s = (char) (mb + 0x80);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
 
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c
index c1b934696..8f961d3b5 100644
--- a/newlib/libc/string/strerror.c
+++ b/newlib/libc/string/strerror.c
@@ -882,7 +882,7 @@ _strerror_r (struct _reent *ptr,
 #endif
     default:
       if (!errptr)
-        errptr = &ptr->_errno;
+        errptr = &_REENT_ERRNO(ptr);
       if ((error = _user_strerror (errnum, internal, errptr)) == 0)
         error = "";
       break;
diff --git a/winsup/cygwin/cygerrno.h b/winsup/cygwin/cygerrno.h
index 940bfa5e0..e1c3f04f7 100644
--- a/winsup/cygwin/cygerrno.h
+++ b/winsup/cygwin/cygerrno.h
@@ -36,7 +36,7 @@ extern inline int
 __set_errno (const char *fn, int ln, int val)
 {
   debug_printf ("%s:%d setting errno %d", fn, ln, val);
-  return errno = _impure_ptr->_errno = val;
+  return errno = _REENT_ERRNO(_impure_ptr) = val;
 }
 #define set_errno(val) __set_errno (__PRETTY_FUNCTION__, __LINE__, (val))
 
@@ -55,7 +55,7 @@ class save_errno
     save_errno (int what) {saved = get_errno (); set_errno (what); }
     void set (int what) {set_errno (what); saved = what;}
     void reset () {saved = get_errno ();}
-    ~save_errno () {errno = _impure_ptr->_errno = saved;}
+    ~save_errno () {errno = _REENT_ERRNO(_impure_ptr) = saved;}
   };
 
 extern const char *__sp_fn;
diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index f200e5b73..2f2c26984 100644
--- a/winsup/cygwin/errno.cc
+++ b/winsup/cygwin/errno.cc
@@ -335,7 +335,7 @@ void
 seterrno_from_win_error (const char *file, int line, DWORD code)
 {
   syscall_printf ("%s:%d windows error %u", file, line, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 int
@@ -353,7 +353,7 @@ seterrno_from_nt_status (const char *file, int line, NTSTATUS status)
   SetLastError (code);
   syscall_printf ("%s:%d status %y -> windows error %u",
 		  file, line, status, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 static char *
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 9a97b3a24..22a79b614 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -144,7 +144,7 @@ __db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
   if (ret > 0 && !def_used)
     return ret;
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -194,7 +194,7 @@ __eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
       return ret;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -255,7 +255,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 	 here is to check if the first byte returns a valid value... */
       else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count] = *s;
@@ -263,7 +263,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 			     (const char *) state->__value.__wchb, 2, pwc, 1);
   if (!ret)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
@@ -324,7 +324,7 @@ __eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	}
       else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count++] = *s;
@@ -347,7 +347,7 @@ jis_x_0212:
   if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
 			    (const char *) state->__value.__wchb, 2, pwc, 1))
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 7d53c2af7..b8260b4d1 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1900,7 +1900,7 @@ _fstat_r (struct _reent *ptr, int fd, struct stat *buf)
   int ret;
 
   if ((ret = fstat (fd, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }
 
@@ -2051,7 +2051,7 @@ _stat_r (struct _reent *__restrict ptr, const char *__restrict name,
   int ret;
 
   if ((ret = stat (name, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-13  8:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-13  8:19 [newlib-cygwin] Add _REENT_ERRNO(ptr) Sebastian Huber

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).