public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
To: C Howland <cc1964t@gmail.com>, newlib@sourceware.org
Subject: Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
Date: Mon, 11 Jul 2022 09:41:00 +0200	[thread overview]
Message-ID: <3ac4531e-9b39-2389-fdf5-72506ce075c9@embedded-brains.de> (raw)
In-Reply-To: <c19c5b78-8ffc-171c-e080-c10313dd368c@embedded-brains.de>

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

On 23/06/2022 12:55, Sebastian Huber wrote:
> On 21/06/2022 16:41, C Howland wrote:
>>>
>>> ------------------------------
>>> *From:* Newlib<newlib-bounces+craig.howland=caci.com@sourceware.org>  on
>>> behalf of Sebastian Huber<sebastian.huber@embedded-brains.de>
>>> *Sent:* Tuesday, June 21, 2022 8:49 AM
>>> *To:*newlib@sourceware.org  <newlib@sourceware.org>
>>> *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
>>>
>>>
>>>
>>> From: Matt Joyce<matthew.joyce@embedded-brains.de>
>>>
>>> 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.
>>> ---
>>>
>> There already exists an __errno_r() macro that does the very same 
>> function
>> (defined in sys/errno.h).  (Its use, however, is limited, only being used
>> in files under iconv/lib.)  Having the same thing done both ways probably
>> doesn't make sense.  The new name is more consistent with the rest of the
>> things being done, while the old name is established and errno is a more
>> specialized case.  It probably would be a good idea to either
>> 1)  use __errno_r() instead of creating _REENT_ERRNO() or
>> 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
> 
> I would not remove an existing macro, so option 1) would be preferred by 
> me.

Attached is a patch which implements option 1).

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

[-- Attachment #2: 0001-Use-__errno_r-ptr.patch --]
[-- Type: text/x-patch, Size: 78808 bytes --]

From 2a2c654eb37081932a8e50a993b98298deadd433 Mon Sep 17 00:00:00 2001
From: Matt Joyce <matthew.joyce@embedded-brains.de>
Date: Tue, 18 Jan 2022 10:13:04 +0100
Subject: [PATCH] Use __errno_r(ptr)

Use the __errno_r() 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.

Move definition of __errno_r() to <sys/reent.h>.
---
 newlib/libc/ctype/wctrans.c               |  2 +-
 newlib/libc/ctype/wctype.c                |  2 +-
 newlib/libc/errno/errno.c                 |  2 +-
 newlib/libc/include/sys/errno.h           |  2 --
 newlib/libc/include/sys/reent.h           |  2 ++
 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 +--
 104 files changed, 249 insertions(+), 251 deletions(-)

diff --git a/newlib/libc/ctype/wctrans.c b/newlib/libc/ctype/wctrans.c
index 4a58df152..1aa4e26bf 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;
+      __errno_r(r) = EINVAL;
       return 0;
     }
 }
diff --git a/newlib/libc/ctype/wctype.c b/newlib/libc/ctype/wctype.c
index 5f2ae7a25..38a86ff5a 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;
+  __errno_r(r) = EINVAL;
   return 0;
 }
 
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index fd1743d73..da24f883d 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -10,7 +10,7 @@
 int *
 __errno ()
 {
-  return &_REENT->_errno;
+  return &__errno_r(_REENT);
 }
 
 #endif
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 025b461d4..8c93139b3 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -26,8 +26,6 @@ extern __IMPORT char *program_invocation_name;
 extern __IMPORT char *program_invocation_short_name;
 #endif
 
-#define __errno_r(ptr) ((ptr)->_errno)
-
 #define	EPERM 1		/* Not owner */
 #define	ENOENT 2	/* No such file or directory */
 #define	ESRCH 3		/* No such process */
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2b0ff46b7..ac5933842 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -718,6 +718,8 @@ struct _reent
 
 #endif /* !_REENT_SMALL */
 
+#define __errno_r(_ptr)		((_ptr)->_errno)
+
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 
 #define _REENT_INIT_PTR(var) \
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index 3d573015d..f259df82d 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -24,8 +24,6 @@ extern int *__errno (void);
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
-
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
 #define	ENOENT		 2	/* No such file or directory */
diff --git a/newlib/libc/machine/powerpc/strtosfix16.c b/newlib/libc/machine/powerpc/strtosfix16.c
index 3fba45409..e15e8d558 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+      __errno_r(rptr) = ERANGE;
       return SHRT_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+	  __errno_r(rptr) = ERANGE;
 	  return SHRT_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix32.c b/newlib/libc/machine/powerpc/strtosfix32.c
index b21de9ea1..beb160860 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+      __errno_r(rptr) = ERANGE;
       return LONG_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix64.c b/newlib/libc/machine/powerpc/strtosfix64.c
index 16032128f..d2f1f7ba9 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_LONG_MIN;
 	}
     }
@@ -71,7 +71,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp >= 0)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(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;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix16.c b/newlib/libc/machine/powerpc/strtoufix16.c
index 9b16268bb..4c66a3fb4 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+      __errno_r(rptr) = ERANGE;
       return USHRT_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+	  __errno_r(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..02bad29f5 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+      __errno_r(rptr) = ERANGE;
       return ULONG_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+	  __errno_r(rptr) = ERANGE;
 	  return ULONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix64.c b/newlib/libc/machine/powerpc/strtoufix64.c
index a2f0484b3..8adfefa99 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;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(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;
+      __errno_r(rptr) = ERANGE;
       return 0;
     }
   else
     {
       if (exp > 0 || (exp == 0 && tmp >= 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(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;
+	      __errno_r(rptr) = ERANGE;
 	      return ULONG_LONG_MAX;
 	    }
 	}
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 87e4c406d..825fa2d54 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;
+  __errno_r(d) = EMFILE;
   return NULL;
 }
 
diff --git a/newlib/libc/machine/spu/sys/errno.h b/newlib/libc/machine/spu/sys/errno.h
index b008ff6f4..754cf8198 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) __errno_r(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..1ac955844 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/execr.c b/newlib/libc/reent/execr.c
index 59b61223e..8c3c123fa 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
@@ -86,7 +86,7 @@ _fork_r (struct _reent *ptr)
 
   errno = 0;
   if ((ret = _fork ()) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fcntlr.c b/newlib/libc/reent/fcntlr.c
index cd19d226f..e4a1bb67f 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstat64r.c b/newlib/libc/reent/fstat64r.c
index c546f5c1d..e14814fde 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstatr.c b/newlib/libc/reent/fstatr.c
index ec906c98d..6026a6377 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/gettimeofdayr.c b/newlib/libc/reent/gettimeofdayr.c
index 9b982a993..91c2a37c6 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/isattyr.c b/newlib/libc/reent/isattyr.c
index f21bf25b2..01df8de85 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/linkr.c b/newlib/libc/reent/linkr.c
index b22da5f94..39eb17ba0 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseek64r.c b/newlib/libc/reent/lseek64r.c
index 40769fb6d..d3fe02bbc 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseekr.c b/newlib/libc/reent/lseekr.c
index ac2daaab9..142f028e7 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/mkdirr.c b/newlib/libc/reent/mkdirr.c
index fd72df64c..054fa8797 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/open64r.c b/newlib/libc/reent/open64r.c
index 84bd67e34..0c02af187 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/openr.c b/newlib/libc/reent/openr.c
index c6a7db5de..d22894cd3 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/readr.c b/newlib/libc/reent/readr.c
index 7fccefd32..a06d0537a 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/renamer.c b/newlib/libc/reent/renamer.c
index 5420dc4a0..14cc2eb5b 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;
+    __errno_r(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..7b512e2e2 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/signalr.c b/newlib/libc/reent/signalr.c
index 345910e4b..0e57b5d31 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/stat64r.c b/newlib/libc/reent/stat64r.c
index b64736ef2..af7390db0 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/statr.c b/newlib/libc/reent/statr.c
index 9388e0246..efb1169c1 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/unlinkr.c b/newlib/libc/reent/unlinkr.c
index 41bac0194..d0f68e58b 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/writer.c b/newlib/libc/reent/writer.c
index 704aba18b..f08521ee1 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;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 0110287d8..fa4779d5a 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;
+      __errno_r(ptr) = EINVAL;
       return SIG_ERR;
     }
 
@@ -136,7 +136,7 @@ _raise_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return -1;
     }
 
@@ -151,7 +151,7 @@ _raise_r (struct _reent *ptr,
     return 0;
   else if (func == SIG_ERR)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return 1;
     }
   else
diff --git a/newlib/libc/stdio/asniprintf.c b/newlib/libc/stdio/asniprintf.c
index 0bfe00d9b..17e232d10 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;
+      __errno_r(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;
+      __errno_r(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..2397ce40e 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;
+      __errno_r(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;
+      __errno_r(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..52c0cc2bc 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;
+      __errno_r(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio/fflush.c b/newlib/libc/stdio/fflush.c
index bbec4a19b..170ad4cd8 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 = __errno_r(ptr);
+	  __errno_r(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 && __errno_r(ptr) != 0)
 		{
 		  int result = EOF;
-		  if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+		  if (__errno_r(ptr) == ESPIPE || __errno_r(ptr) == EINVAL)
 		    {
 		      result = 0;
-		      ptr->_errno = tmp_errno;
+		      __errno_r(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 || __errno_r(ptr) == 0
+	      || __errno_r(ptr) == ESPIPE || __errno_r(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 || __errno_r(ptr) == 0))
 		fp->_offset = curoff;
-	      ptr->_errno = tmp_errno;
+	      __errno_r(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..bdb3a050c 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;
+      __errno_r(_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..d3837ddef 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;
+      __errno_r(_REENT) = EBADF;
     }
   return result;
 }
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index ee991ed24..7dce3ba89 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;
+  __errno_r(d) = ENOMEM;
   return NULL;
 
 found:
diff --git a/newlib/libc/stdio/flags.c b/newlib/libc/stdio/flags.c
index 7bbd50181..6c2fcf401 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;
+      __errno_r(ptr) = EINVAL;
       return (0);
     }
   while (*++mode)
diff --git a/newlib/libc/stdio/fmemopen.c b/newlib/libc/stdio/fmemopen.c
index 0d043520e..a8e441b00 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;
+      __errno_r(ptr) = ENOSPC;
       return EOF;
     }
   return n;
@@ -175,18 +175,18 @@ fmemseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -224,12 +224,12 @@ fmemseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -277,7 +277,7 @@ _fmemopen_r (struct _reent *ptr,
     return NULL;
   if (!size || !(buf || flags & __SRW))
     {
-      ptr->_errno = EINVAL;
+      __errno_r(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..576430b7f 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;
+    __errno_r(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;
+    __errno_r(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;
+    __errno_r(ptr) = errno;
 #ifdef __LARGE64_FILES
   else if ((_fpos_t)offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(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;
+    __errno_r(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;
+	__errno_r(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;
+      __errno_r(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..f94f76770 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;
+      __errno_r(ptr) = EBADF;
       _newlib_flockfile_exit (fp);
       return EOF;
     }
diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c
index 29ba5b7ff..bc4996343 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 = __errno_r(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 */
+      __errno_r(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..e26fccb89 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;	/* ??? */
+      __errno_r(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit (fp);
       return EOF;
     }
@@ -178,7 +178,7 @@ _fseeko_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       _newlib_flockfile_exit (fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio/ftell.c b/newlib/libc/stdio/ftell.c
index c540c430f..e4a38674d 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;
+      __errno_r(ptr) = EOVERFLOW;
     }
   return (long)pos;
 }
diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 3df200cea..ee5ea19a3 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;
+      __errno_r(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..c9084ca1d 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;
+    __errno_r(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;
+    __errno_r(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;
+    __errno_r(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;
+    __errno_r(ptr) = errno;
   else if ((_fpos_t)result != result)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(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;
+    __errno_r(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;
+	__errno_r(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -202,7 +202,7 @@ _funopen_r (struct _reent *ptr,
 
   if (!readfn && !writefn)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(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..90fa491b7 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;
+			  __errno_r(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;
+			  __errno_r(ptr) = ENOMEM;
 			  goto err;
 			}
 		    }
diff --git a/newlib/libc/stdio/mktemp.c b/newlib/libc/stdio/mktemp.c
index 3514818a6..da30f0be7 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;
+      __errno_r(ptr) = EINVAL;
       return 0;
     }
   trv -= suffixlen;
@@ -171,7 +171,7 @@ _gettemp (struct _reent *ptr,
     }
   if (end - trv < 6)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return 0;
     }
 
@@ -195,7 +195,7 @@ _gettemp (struct _reent *ptr,
 	    return (0);
 	  if (!(sbuf.st_mode & S_IFDIR))
 	    {
-	      ptr->_errno = ENOTDIR;
+	      __errno_r(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 (__errno_r(ptr) != EEXIST)
 	    return 0;
 #else /* !HAVE_MKDIR */
-	  ptr->_errno = ENOSYS;
+	  __errno_r(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 (__errno_r(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 (__errno_r(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..60beda610 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;
+	      __errno_r(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;
+	      __errno_r(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;
+		  __errno_r(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;
+		  __errno_r(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;
+	  __errno_r(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..a4dac6713 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;
+      __errno_r(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;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -227,12 +227,12 @@ memseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -301,7 +301,7 @@ internal_open_memstream_r (struct _reent *ptr,
 
   if (!buf || !size)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(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..fbbb27de4 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, __errno_r(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..7abbbbf23 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;
+	  __errno_r(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
 	}
diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c
index 375a3971f..c25d189fd 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;
+      __errno_r(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;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -69,7 +69,7 @@ sniprintf (char *str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(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;
+    __errno_r(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..f32806069 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;
+      __errno_r(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;
+    __errno_r(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;
+      __errno_r(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;
+    __errno_r(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..c831cfbe1 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 */
+      __errno_r(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 */
+    __errno_r(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 */
+      __errno_r(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 */
+    __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
diff --git a/newlib/libc/stdio/tmpfile.c b/newlib/libc/stdio/tmpfile.c
index f209a3edf..79fe69f6d 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 && __errno_r(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = __errno_r(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  __errno_r(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 3bd5b6a58..607e784b8 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 (__errno_r(ptr) == ENOSYS)
 	    {
 	      result[0] = '\0';
 	      return 0;
diff --git a/newlib/libc/stdio/vasniprintf.c b/newlib/libc/stdio/vasniprintf.c
index edfbd2242..69ce67239 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;
+      __errno_r(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..f21ab6c2a 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;
+      __errno_r(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..113c0fd11 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;
+				__errno_r(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;
+				__errno_r(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;
+					__errno_r(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;
+					__errno_r(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;
+			__errno_r(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, __errno_r(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfscanf.c b/newlib/libc/stdio/vfscanf.c
index 037692531..a171b2188 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;
+	  __errno_r(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..7d65da626 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;
+			__errno_r(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, __errno_r(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfwscanf.c b/newlib/libc/stdio/vfwscanf.c
index e9e00dfec..78d19b2f2 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;
+	  __errno_r(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..a12576539 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;
+      __errno_r(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;
+    __errno_r(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..d1a28ad81 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;
+      __errno_r(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;
+    __errno_r(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..914b365a2 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 */
+      __errno_r(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 */
+    __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return ret;
diff --git a/newlib/libc/stdio/wsetup.c b/newlib/libc/stdio/wsetup.c
index 6a820f1c0..6e6519767 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;
+	  __errno_r(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
         }
diff --git a/newlib/libc/stdio64/fdopen64.c b/newlib/libc/stdio64/fdopen64.c
index 9d9645b36..3774c913f 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;
+      __errno_r(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c
index e6ba64f7d..5b4899f67 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 = __errno_r(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 */
+      __errno_r(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..0510e3582 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;
+	  __errno_r(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;	/* ??? */
+      __errno_r(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit(fp);
       return EOF;
     }
@@ -179,7 +179,7 @@ _fseeko64_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       _newlib_flockfile_exit(fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 6fb76c3ef..97c7e96b8 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;
+      __errno_r(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..d8fe55605 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 && __errno_r(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen64_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = __errno_r(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  __errno_r(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdlib/__adjust.c b/newlib/libc/stdlib/__adjust.c
index 4c478f30b..9b38e8d3e 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;
+      __errno_r(ptr) = ERANGE;
       return (sign) ? -HUGE_VAL : HUGE_VAL;
     }
   else if (dexp < MINE)
     {
-      ptr->_errno = ERANGE;
+      __errno_r(ptr) = ERANGE;
       return 0.0;
     }
 
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 65284a0eb..b43d8ea6a 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;
+      __errno_r(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;
+      __errno_r(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index d3ce25084..e4221d75e 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;
+	  __errno_r(r) = EILSEQ;
 	  return (size_t)-1;
 	}
     }
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 920a7ea3c..eb50fa6da 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;
+      __errno_r(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;
+	      __errno_r(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;
+	      __errno_r(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;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (state->__value.__wchb[0] < 0xc2)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  __errno_r(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;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(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;
+	  __errno_r(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;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(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;
+	  __errno_r(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;
+	  __errno_r(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;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -769,7 +769,7 @@ __sjis_mbtowc (struct _reent *r,
 	}
       else  
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -836,7 +836,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -851,7 +851,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -955,7 +955,7 @@ __jis_mbtowc (struct _reent *r,
 	  break;
 	case ERROR:
 	default:
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
 
diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c
index cd0222481..91fc756d1 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;
+				__errno_r(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;
+					__errno_r(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;
+			__errno_r(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;
+    __errno_r(_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;
+    __errno_r(_REENT) = ERANGE;
 #endif
   return retval;
 }
diff --git a/newlib/libc/stdlib/strtoimax.c b/newlib/libc/stdlib/strtoimax.c
index c3f27df2e..5aed095af 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;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(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..8af07c4da 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;
+		__errno_r(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..5d87e6593 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;
+		__errno_r(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..accd2852d 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;
+		__errno_r(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..2c8000a65 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;
+		__errno_r(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..57629eb03 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;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(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..73a7404be 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;
+      __errno_r(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -67,7 +67,7 @@ wcrtomb (char *__restrict s,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      __errno_r(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index 43dd2f3e6..e58a1c3b2 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;
+	  __errno_r(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..17c5c4e8a 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;
+    __errno_r(_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;
+    __errno_r(_REENT) = ERANGE;
 #endif
 
   return retval;
diff --git a/newlib/libc/stdlib/wcstoimax.c b/newlib/libc/stdlib/wcstoimax.c
index 02ab1c1f4..2542048ca 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;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(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..cafc609c8 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;
+		__errno_r(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..79f4d8f94 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;
+		__errno_r(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..27f6f52b6 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;
+		__errno_r(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..4f02c7482 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;
+		__errno_r(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;
+		__errno_r(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..d0a4f108b 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;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(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..059b7703f 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;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 
@@ -133,7 +133,7 @@ __utf8_wctomb (struct _reent *r,
       return 4;
     }
 
-  r->_errno = EILSEQ;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -165,7 +165,7 @@ __sjis_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -204,7 +204,7 @@ __eucjp_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -244,7 +244,7 @@ __jis_wctomb (struct _reent *r,
 	  *s = (char)char2;
 	  return cnt + 2;
 	}
-      r->_errno = EILSEQ;
+      __errno_r(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;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
  
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      __errno_r(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;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
 
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c
index c1b934696..5bed5ffbe 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 = &__errno_r(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..04436d28a 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 = __errno_r(_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 = __errno_r(_impure_ptr) = saved;}
   };
 
 extern const char *__sp_fn;
diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index f200e5b73..6ad0e6d78 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 = __errno_r(_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 = __errno_r(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 static char *
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 9a97b3a24..3521c0764 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;
+  __errno_r(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;
+  __errno_r(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;
+      __errno_r(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;
+      __errno_r(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;
+      __errno_r(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;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 7d53c2af7..a4525f2e0 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 ();
+    __errno_r(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 ();
+    __errno_r(ptr) = get_errno ();
   return ret;
 }
 
-- 
2.35.3


  reply	other threads:[~2022-07-11  7:41 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
2022-06-21 12:49 ` [PATCH 01/14] Move content in <sys/reent.h> Sebastian Huber
2022-06-21 12:49 ` [PATCH 02/14] Define _REENT_EMERGENCY(ptr) only once Sebastian Huber
2022-06-21 12:49 ` [PATCH 03/14] Add _REENT_ERRNO(ptr) Sebastian Huber
     [not found]   ` <BN2P110MB15447CD8C6DD815779AA7E4C9AB39@BN2P110MB1544.NAMP110.PROD.OUTLOOK.COM>
2022-06-21 14:41     ` Fw: " C Howland
2022-06-23 10:55       ` Sebastian Huber
2022-07-11  7:41         ` Sebastian Huber [this message]
2022-07-12 16:11         ` Corinna Vinschen
2022-07-12 16:38           ` Sebastian Huber
2022-07-12 18:25             ` Corinna Vinschen
2022-07-13  7:17               ` Sebastian Huber
2022-07-13  7:50                 ` Corinna Vinschen
2022-07-13  8:19                   ` Sebastian Huber
2022-07-13 11:13                     ` Corinna Vinschen
2022-06-21 12:49 ` [PATCH 04/14] Add _REENT_STDIN(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 05/14] Add _REENT_STDOUT(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 06/14] Add _REENT_STDERR(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 07/14] Add _REENT_INC(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 08/14] Add _REENT_LOCALE(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 09/14] Add _REENT_CLEANUP(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 10/14] Add _REENT_CVTLEN(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 11/14] Add _REENT_CVTBUF(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 12/14] Add _REENT_SIG_FUNC(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 13/14] Add _REENT_IS_NULL() Sebastian Huber
2022-06-21 12:49 ` [PATCH 14/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
2022-06-21 13:59   ` Torbjorn SVENSSON
2022-06-21 15:10     ` Sebastian Huber
2022-07-12 11:18 ` [PATCH 00/14] " Sebastian Huber
2022-07-12 14:45   ` Corinna Vinschen
2022-07-12 15:53     ` Sebastian Huber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ac4531e-9b39-2389-fdf5-72506ce075c9@embedded-brains.de \
    --to=sebastian.huber@embedded-brains.de \
    --cc=cc1964t@gmail.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).