From f46356fd8e23c540335d215dd676516df63a6f27 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 29 Apr 2023 03:24:43 +0200
Subject: [PATCH] stdio: Avoid different configure results in different
 testdirs.

* m4/stdio_h.m4 (gl_STDIO_H_EARLY): New macro, extracted from
gl_STDIO_H.
(gl_STDIO_H): Move the code that sets __USE_MINGW_ANSI_STDIO to
gl_STDIO_H_EARLY.
* modules/stdio (configure.ac-early): New section.
---
 ChangeLog     |  9 +++++++++
 m4/stdio_h.m4 | 21 ++++++++++++++++++---
 modules/stdio |  3 +++
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 03b13220e3..4f31482276 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-04-28  Bruno Haible  <bruno@clisp.org>
+
+	stdio: Avoid different configure results in different testdirs.
+	* m4/stdio_h.m4 (gl_STDIO_H_EARLY): New macro, extracted from
+	gl_STDIO_H.
+	(gl_STDIO_H): Move the code that sets __USE_MINGW_ANSI_STDIO to
+	gl_STDIO_H_EARLY.
+	* modules/stdio (configure.ac-early): New section.
+
 2023-04-28  Bruno Haible  <bruno@clisp.org>
 
 	stdbool tests: Avoid compilation error with Sun C on Solaris 10.
diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4
index 94271e11e7..40b262ea09 100644
--- a/m4/stdio_h.m4
+++ b/m4/stdio_h.m4
@@ -1,12 +1,22 @@
-# stdio_h.m4 serial 59
+# stdio_h.m4 serial 59.1
 dnl Copyright (C) 2007-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-AC_DEFUN_ONCE([gl_STDIO_H],
+AC_DEFUN([gl_STDIO_H_EARLY],
 [
-  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+  dnl Defining __USE_MINGW_ANSI_STDIO to 1 must be done early, because
+  dnl the results of several configure tests depend on it: The tests
+  dnl   - checking whether snprintf returns a byte count as in C99...
+  dnl   - checking whether snprintf truncates the result as in C99...
+  dnl   - checking whether printf supports the 'F' directive...
+  dnl   - checking whether printf supports the grouping flag...
+  dnl   - checking whether printf supports the zero flag correctly...
+  dnl   - checking whether printf supports infinite 'double' arguments...
+  dnl   - checking whether printf supports large precisions...
+  dnl report 'yes' if __USE_MINGW_ANSI_STDIO is 1 but 'no' if
+  dnl __USE_MINGW_ANSI_STDIO is not set.
   AH_VERBATIM([MINGW_ANSI_STDIO],
 [/* Use GNU style printf and scanf.  */
 #ifndef __USE_MINGW_ANSI_STDIO
@@ -14,6 +24,11 @@ AC_DEFUN_ONCE([gl_STDIO_H],
 #endif
 ])
   AC_DEFINE([__USE_MINGW_ANSI_STDIO])
+])
+
+AC_DEFUN_ONCE([gl_STDIO_H],
+[
+  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
   gl_NEXT_HEADERS([stdio.h])
 
   dnl Determine whether __USE_MINGW_ANSI_STDIO makes printf and
diff --git a/modules/stdio b/modules/stdio
index ff3a8d55af..d22b7e52f5 100644
--- a/modules/stdio
+++ b/modules/stdio
@@ -18,6 +18,9 @@ ssize_t
 stddef
 sys_types
 
+configure.ac-early:
+gl_STDIO_H_EARLY
+
 configure.ac:
 gl_STDIO_H
 gl_STDIO_H_REQUIRE_DEFAULTS
-- 
2.39.5