diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 2fabcb28a3f2cc4e9ba9b858484f48bdf394a5ab..e9832a0e24e5e807c79d3c0f39632dc60e371c80 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,13 @@
+2007-08-23  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
+
+	PR libfortran/23138
+	* acinclude.m4 (LIBGFOR_CHECK_MINGW_SNPRINTF): New check.
+	* configure.ac: Use LIBGFOR_CHECK_MINGW_SNPRINTF.
+	* libgfortran.h: If HAVE_MINGW_SNPRINTF is true, use __mingw_snprintf
+	instead of snprintf.
+	* config.h.in: Regenerate.
+	* configure: Regenerate.
+
 2007-08-22  Bernhard Fischer  <rep.dot.nop@gmail.com>
 
 	* libgfortran/Makefile.am (AM_CPPFLAGS): Commentary typo fix.
diff --git a/libgfortran/acinclude.m4 b/libgfortran/acinclude.m4
index 1270a98c04ee8da7ef76a03f099ab939c44e50f5..40ce53560b326357b8069f4d9c611a0b79288f31 100644
--- a/libgfortran/acinclude.m4
+++ b/libgfortran/acinclude.m4
@@ -386,3 +386,19 @@ AC_DEFUN([LIBGFOR_CHECK_FPSETMASK], [
     AC_DEFINE(HAVE_FPSETMASK, 1, [Define if you have fpsetmask.])
   fi
 ])
+
+dnl Check whether we have a mingw that provides a __mingw_snprintf function
+AC_DEFUN([LIBGFOR_CHECK_MINGW_SNPRINTF], [
+  AC_CACHE_CHECK([whether __mingw_snprintf is present], have_mingw_snprintf, [
+    AC_TRY_LINK([
+#include <stdio.h>
+extern int __mingw_snprintf (char *, size_t, const char *, ...);
+],[
+__mingw_snprintf (NULL, 0, "%d\n", 1);
+],
+    eval "have_mingw_snprintf=yes", eval "have_mingw_snprintf=no")
+  ])
+  if test x"$have_mingw_snprintf" = xyes; then
+    AC_DEFINE(HAVE_MINGW_SNPRINTF, 1, [Define if you have __mingw_snprintf.])
+  fi
+])
diff --git a/libgfortran/config.h.in b/libgfortran/config.h.in
index 4099c39894ffdc65aa0782bb1954881a368d2403..877fc5a6e0174e89f72440ee7554180a7074ee53 100644
--- a/libgfortran/config.h.in
+++ b/libgfortran/config.h.in
@@ -522,6 +522,9 @@
 /* Define to 1 if you have the <memory.h> header file. */
 #undef HAVE_MEMORY_H
 
+/* Define if you have __mingw_snprintf. */
+#undef HAVE_MINGW_SNPRINTF
+
 /* Define to 1 if you have the `mkstemp' function. */
 #undef HAVE_MKSTEMP
 
diff --git a/libgfortran/configure b/libgfortran/configure
index c68cb7436b7a9b390a2c5913825e6ea1baddfdd3..f71c5900f2e677a17ce0de5a8d01412b1b993324 100755
--- a/libgfortran/configure
+++ b/libgfortran/configure
@@ -31797,6 +31797,83 @@ _ACEOF
 
 fi
 
+# Check whether __mingw_snprintf() is present
+
+  echo "$as_me:$LINENO: checking whether __mingw_snprintf is present" >&5
+echo $ECHO_N "checking whether __mingw_snprintf is present... $ECHO_C" >&6
+if test "${have_mingw_snprintf+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+    if test x$gcc_no_link = xyes; then
+  { { echo "$as_me:$LINENO: error: Link tests are not allowed after GCC_NO_EXECUTABLES." >&5
+echo "$as_me: error: Link tests are not allowed after GCC_NO_EXECUTABLES." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+#include <stdio.h>
+extern int __mingw_snprintf (char *, size_t, const char *, ...);
+
+int
+main ()
+{
+
+__mingw_snprintf (NULL, 0, "%d\n", 1);
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  eval "have_mingw_snprintf=yes"
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+eval "have_mingw_snprintf=no"
+fi
+rm -f conftest.err conftest.$ac_objext \
+      conftest$ac_exeext conftest.$ac_ext
+
+fi
+echo "$as_me:$LINENO: result: $have_mingw_snprintf" >&5
+echo "${ECHO_T}$have_mingw_snprintf" >&6
+  if test x"$have_mingw_snprintf" = xyes; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_MINGW_SNPRINTF 1
+_ACEOF
+
+  fi
+
+
 # Check for GNU libc feenableexcept
 echo "$as_me:$LINENO: checking for feenableexcept in -lm" >&5
 echo $ECHO_N "checking for feenableexcept in -lm... $ECHO_C" >&6
diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac
index 8709e16472b8f2b228a3ae6d33921da64662dfd9..8aa3b637785e3a56d8791d630940a4b7e4a0236d 100644
--- a/libgfortran/configure.ac
+++ b/libgfortran/configure.ac
@@ -383,6 +383,9 @@ LIBGFOR_CHECK_FOR_BROKEN_FPCLASSIFY
 # Check whether the system has a working stat()
 LIBGFOR_CHECK_WORKING_STAT
 
+# Check whether __mingw_snprintf() is present
+LIBGFOR_CHECK_MINGW_SNPRINTF
+
 # Check for GNU libc feenableexcept
 AC_CHECK_LIB([m],[feenableexcept],[have_feenableexcept=yes AC_DEFINE([HAVE_FEENABLEEXCEPT],[1],[libm includes feenableexcept])])
 
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index ce6d28e9f95f8425dcbddf39ce8f337773231d29..6c687f7b12b7886db7e5a4c881923722aa8bb924 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -70,6 +70,16 @@ typedef off_t gfc_offset;
 #endif
 
 
+/* On mingw, work around the buggy Windows snprintf() by using the one
+   mingw provides, __mingw_snprintf().  We also provide a prototype for
+   __mingw_snprintf(), because the mingw headers currently don't have one.  */
+#if HAVE_MINGW_SNPRINTF
+extern int __mingw_snprintf (char *, size_t, const char *, ...);
+#undef snprintf
+#define snprintf __mingw_snprintf
+#endif
+
+
 /* For a library, a standard prefix is a requirement in order to partition
    the namespace.  IPREFIX is for symbols intended to be internal to the
    library.  */