diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index a6cf6fde3ad8ad6fd3158b03427be77e7871b471..12cf5f14d802ff6bbca8b1e349ac4d15e211ddef 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,14 @@
+2012-05-23  Tobias Burnus  <burnus@net-b.de>
+
+	PR libfortran/53444
+	* acinclude.m4 (LIBGFOR_CHECK_STRERROR_R): Add configure checks for
+	two- and three-argument versions of strerror_r.
+	* configure.ac (LIBGFOR_CHECK_STRERROR_R): Use it.
+	* runtime/error.c (gf_strerror): Handle two-argument version
+	of strerror_r.
+	* config.h.in: Regenerate.
+	* configure: Regenerate.
+
 2012-05-16  H.J. Lu  <hongjiu.lu@intel.com>
 
 	* configure: Regenerated.
diff --git a/libgfortran/acinclude.m4 b/libgfortran/acinclude.m4
index 1b11e6a072a5a9162e37f22b44f20f7bb388f3e0..d10126150eab6418fb6ffbc6e2eb0219bacab742 100644
--- a/libgfortran/acinclude.m4
+++ b/libgfortran/acinclude.m4
@@ -362,3 +362,29 @@ AC_DEFUN([LIBGFOR_CHECK_FLOAT128], [
   dnl We need a conditional for the Makefile
   AM_CONDITIONAL(LIBGFOR_BUILD_QUAD, [test "x$libgfor_cv_have_float128" = xyes])
 ])
+
+
+dnl Check whether we have strerror_r
+AC_DEFUN([LIBGFOR_CHECK_STRERROR_R], [
+  dnl Check for three-argument POSIX version of strerror_r
+  ac_save_CFLAGS="$CFLAGS"
+  CFLAGS="-Wimplicit-function-declaration -Werror"
+  AC_TRY_COMPILE([#define _GNU_SOURCE 1
+	     	  #include <string.h>
+		  #include <locale.h>],
+		  [char s[128]; strerror_r(5, s, 128);],
+		  AC_DEFINE(HAVE_STRERROR_R, 1,
+		  [Define if strerror_r is available in <string.h>.]),)
+  CFLAGS="$ac_save_CFLAGS"
+
+  dnl Check for two-argument version of strerror_r (e.g. for VxWorks)
+  ac_save_CFLAGS="$CFLAGS"
+  CFLAGS="-Wimplicit-function-declaration -Werror"
+  AC_TRY_COMPILE([#define _GNU_SOURCE 1
+	     	  #include <string.h>
+		  #include <locale.h>],
+		  [char s[128]; strerror_r(5, s);],
+		  AC_DEFINE(HAVE_STRERROR_R_2ARGS, 1,
+		  [Define if strerror_r takes two arguments and is available in <string.h>.]),)
+  CFLAGS="$ac_save_CFLAGS"
+])
diff --git a/libgfortran/config.h.in b/libgfortran/config.h.in
index b75fa968023a5f2271d34318ed7c989038aeb923..43ba0259862f70f34d5aeafebc1c841a81a9e4af 100644
--- a/libgfortran/config.h.in
+++ b/libgfortran/config.h.in
@@ -696,9 +696,12 @@
 /* Define to 1 if you have the `strcasestr' function. */
 #undef HAVE_STRCASESTR
 
-/* Define to 1 if you have the `strerror_r' function. */
+/* Define if strerror_r is available in <string.h>. */
 #undef HAVE_STRERROR_R
 
+/* Define if strerror_r takes two arguments and is available in <string.h>. */
+#undef HAVE_STRERROR_R_2ARGS
+
 /* Define to 1 if you have the <strings.h> header file. */
 #undef HAVE_STRINGS_H
 
diff --git a/libgfortran/configure b/libgfortran/configure
index 80bfe84bb89e6ae77cf05ebc9f53d467fcf04f0e..ca33870e9f630f3435b6c281df999258c439b859 100755
--- a/libgfortran/configure
+++ b/libgfortran/configure
@@ -2581,7 +2581,6 @@ as_fn_append ac_func_list " dup"
 as_fn_append ac_func_list " getcwd"
 as_fn_append ac_func_list " localtime_r"
 as_fn_append ac_func_list " gmtime_r"
-as_fn_append ac_func_list " strerror_r"
 as_fn_append ac_func_list " getpwuid_r"
 as_fn_append ac_func_list " ttyname_r"
 as_fn_append ac_func_list " clock_gettime"
@@ -12328,7 +12327,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 12331 "configure"
+#line 12330 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -12434,7 +12433,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 12437 "configure"
+#line 12436 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -16547,6 +16546,53 @@ done
 
 
 
+# Check strerror_r, cannot be above as versions with two and three arguments exist
+
+    ac_save_CFLAGS="$CFLAGS"
+  CFLAGS="-Wimplicit-function-declaration -Werror"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#define _GNU_SOURCE 1
+	     	  #include <string.h>
+		  #include <locale.h>
+int
+main ()
+{
+char s[128]; strerror_r(5, s, 128);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+$as_echo "#define HAVE_STRERROR_R 1" >>confdefs.h
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  CFLAGS="$ac_save_CFLAGS"
+
+    ac_save_CFLAGS="$CFLAGS"
+  CFLAGS="-Wimplicit-function-declaration -Werror"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#define _GNU_SOURCE 1
+	     	  #include <string.h>
+		  #include <locale.h>
+int
+main ()
+{
+char s[128]; strerror_r(5, s);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+$as_echo "#define HAVE_STRERROR_R_2ARGS 1" >>confdefs.h
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  CFLAGS="$ac_save_CFLAGS"
 
 
 # Check for C99 (and other IEEE) math functions
diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac
index 41bef720234297aa855274400c4536a6bb7863bb..fc58a5c24b95232825fe8561d8031264900ab793 100644
--- a/libgfortran/configure.ac
+++ b/libgfortran/configure.ac
@@ -265,9 +265,12 @@ AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \
 ftruncate chsize chdir getlogin gethostname kill link symlink sleep ttyname \
 alarm access fork execl wait setmode execve pipe dup2 close \
 strcasestr getrlimit gettimeofday stat fstat lstat getpwuid vsnprintf dup \
-getcwd localtime_r gmtime_r strerror_r getpwuid_r ttyname_r clock_gettime \
+getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \
 readlink getgid getpid getppid getuid geteuid umask getegid __secure_getenv)
 
+# Check strerror_r, cannot be above as versions with two and three arguments exist
+LIBGFOR_CHECK_STRERROR_R
+
 # Check for C99 (and other IEEE) math functions
 GCC_CHECK_MATH_FUNC([acosf])
 GCC_CHECK_MATH_FUNC([acos])
diff --git a/libgfortran/runtime/error.c b/libgfortran/runtime/error.c
index 7e523bc9a573110dd80ab67983e6cd982b1a49bb..3955e44cea068d8fd0c22d8087f14a0f5e3c8447 100644
--- a/libgfortran/runtime/error.c
+++ b/libgfortran/runtime/error.c
@@ -212,6 +212,7 @@ gf_strerror (int errnum,
 	     size_t buflen __attribute__((unused)))
 {
 #ifdef HAVE_STRERROR_R
+  /* POSIX returns an "int", GNU a "char*".  */
   return
     __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf, 0))
 			   == 5,
@@ -219,6 +220,9 @@ gf_strerror (int errnum,
 			   strerror_r (errnum, buf, buflen),
 			   /* POSIX strerror_r ()  */
 			   (strerror_r (errnum, buf, buflen), buf));
+#elif defined(HAVE_STRERROR_R_2ARGS)
+  strerror_r (errnum, buf);
+  return buf;
 #else
   /* strerror () is not necessarily thread-safe, but should at least
      be available everywhere.  */