This shouldn’t affect performance on glibc, where fileno and
fileno_unlocked are aliases under the theory that no locking is needed.
However, it might help a bit on platforms like FreeBSD where
fileno_unlocked is inline and does not lock.
* lib/unlocked-io.h (fileno, fileno_unlocked):
Define as needed, similarly to fgets etc.
* m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO):
Also check fileno_unlocked.
---
 ChangeLog         | 10 ++++++++++
 lib/unlocked-io.h |  7 +++++++
 m4/unlocked-io.m4 |  3 ++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index c8f5c0da3a..d5fde9e134 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2025-09-05  Paul Eggert  <[email protected]>
 
+       unlocked-io: also deal with fileno
+       This shouldn’t affect performance on glibc, where fileno and
+       fileno_unlocked are aliases under the theory that no locking is needed.
+       However, it might help a bit on platforms like FreeBSD where
+       fileno_unlocked is inline and does not lock.
+       * lib/unlocked-io.h (fileno, fileno_unlocked):
+       Define as needed, similarly to fgets etc.
+       * m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO):
+       Also check fileno_unlocked.
+
        progname: static check instead of dynamic
        * lib/progname.c: Don’t include stdio.h or stdlib.h.
        (set_program_name): Omit dynamic check that (with current glibc
diff --git a/lib/unlocked-io.h b/lib/unlocked-io.h
index 69ea6641a3..8a7719c487 100644
--- a/lib/unlocked-io.h
+++ b/lib/unlocked-io.h
@@ -73,6 +73,13 @@
 #  define fgets_unlocked(x,y,z) fgets (x,y,z)
 # endif
 
+# if HAVE_DECL_FILENO_UNLOCKED || defined fileno_unlocked
+#  undef fileno
+#  define fileno(x) fileno_unlocked (x)
+# else
+#  define fileno_unlocked(x) fileno (x)
+# endif
+
 # if HAVE_DECL_FPUTC_UNLOCKED || defined fputc_unlocked
 #  undef fputc
 #  define fputc(x,y) fputc_unlocked (x,y)
diff --git a/m4/unlocked-io.m4 b/m4/unlocked-io.m4
index 97f43f4b6c..a5eacecc61 100644
--- a/m4/unlocked-io.m4
+++ b/m4/unlocked-io.m4
@@ -1,5 +1,5 @@
 # unlocked-io.m4
-# serial 16
+# serial 17
 dnl Copyright (C) 1998-2006, 2009-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -25,6 +25,7 @@ AC_DEFUN([gl_FUNC_GLIBC_UNLOCKED_IO],
   AC_CHECK_DECLS_ONCE([ferror_unlocked])
   AC_CHECK_DECLS_ONCE([fflush_unlocked])
   AC_CHECK_DECLS_ONCE([fgets_unlocked])
+  AC_CHECK_DECLS_ONCE([fileno_unlocked])
   AC_CHECK_DECLS_ONCE([fputc_unlocked])
   AC_CHECK_DECLS_ONCE([fputs_unlocked])
   AC_CHECK_DECLS_ONCE([fread_unlocked])
-- 
2.48.1


Reply via email to