From 8d5b72ef2687f61d8f2e5c11b50ab9ca57c35a0b Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Thu, 24 Apr 2025 20:18:59 +0200
Subject: [PATCH] fbufmode: Don't require peeking into the FILE structure on
 recent Haiku.

Suggested by Augustin Cavalier <waddlesplash@gmail.com>
in <https://dev.haiku-os.org/ticket/19479#comment:15>.

* lib/fbufmode.c (fbufmode): On Haiku >= hrev58760, use the same
implementation as on musl libc.
---
 ChangeLog      | 8 ++++++++
 lib/fbufmode.c | 8 ++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b06b89be71..fe7a3bf792 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-04-24  Bruno Haible  <bruno@clisp.org>
+
+	fbufmode: Don't require peeking into the FILE structure on recent Haiku.
+	Suggested by Augustin Cavalier <waddlesplash@gmail.com>
+	in <https://dev.haiku-os.org/ticket/19479#comment:15>.
+	* lib/fbufmode.c (fbufmode): On Haiku >= hrev58760, use the same
+	implementation as on musl libc.
+
 2025-04-23  Collin Funk  <collin.funk1@gmail.com>
 
 	tsearch tests: Disable tfind signature check on Cygwin.
diff --git a/lib/fbufmode.c b/lib/fbufmode.c
index 440f019ed1..416a7a4cd0 100644
--- a/lib/fbufmode.c
+++ b/lib/fbufmode.c
@@ -33,12 +33,12 @@ fbufmode (FILE *fp)
   /* Most systems provide FILE as a struct and the necessary bitmask in
      <stdio.h>, because they need it for implementing getc() and putc() as
      fast macros.  */
-#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
+#if (defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1) && !(defined __HAIKU__ && HAVE___FLBF && HAVE___FBUFSIZE)
+  /* GNU libc, BeOS, Haiku < hrev58760, Linux libc5 */
 # if !defined __HAIKU__
 #  define fp_ fp
 # endif
-  /* GNU libc, BeOS, Haiku, Linux libc5 */
-# if HAVE___FLBF                    /* glibc >= 2.2, Haiku >= hrev58760 */
+# if HAVE___FLBF                    /* glibc >= 2.2 */
   if (__flbf (fp))
     return _IOLBF;
 # else
@@ -93,7 +93,7 @@ fbufmode (FILE *fp)
   if (fp->__linebuf)
     return _IOLBF;
   return (fp->__bufsize > 0 ? _IOFBF : _IONBF);
-#elif HAVE___FLBF && HAVE___FBUFSIZE /* musl libc */
+#elif HAVE___FLBF && HAVE___FBUFSIZE /* musl libc, Haiku >= hrev58760 */
   if (__flbf (fp))
     return _IOLBF;
   return (__fbufsize (fp) > 0 ? _IOFBF : _IONBF);
-- 
2.39.5