Bjarni Ingi Gislason wrote:
>     HAVE_DIRFD=1
>     dnl Replace only if the system declares dirfd already.
>     if test $ac_cv_have_decl_dirfd = yes; then
> ->      REPLACE_DIRFD=0 /* FIX ME, was =1 */
>     fi

Oh, indeed, there's a mistake here. I thought that it was OK
to compile lib/dirfd.c because <dirent.h> defines DIR, and
DIR is expected to have a d_fd or dd_fd member. But glibc
defines DIR in an opaque way. The d_fd or dd_fd member is
meant only as a fallback for systems that don't have the
dirfd function or macro.

It was my mistake on 2023-04-26. I would have noticed the
mistake if there had been a unit test for the 'dirfd' module.
So, let me add a unit test now.


2023-05-05  Bruno Haible  <br...@clisp.org>

        dirfd: Add tests.
        * tests/test-dirfd.c: New file.
        * modules/dirfd-tests: New file.

2023-05-05  Bruno Haible  <br...@clisp.org>

        dirfd: Fix bogus override (regression 2023-04-26).
        Reported by Bjarni Ingi Gislason <bjarn...@simnet.is> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2023-05/msg00040.html>.
        * m4/dirfd.m4 (gl_FUNC_DIRFD): Fix mistake in last change.

>From d4d8abb39eb02c555f062b1f83ffcfac999c582f Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Fri, 5 May 2023 12:02:49 +0200
Subject: [PATCH 1/2] dirfd: Fix bogus override (regression 2023-04-26).

Reported by Bjarni Ingi Gislason <bjarn...@simnet.is> in
<https://lists.gnu.org/archive/html/bug-gnulib/2023-05/msg00040.html>.

* m4/dirfd.m4 (gl_FUNC_DIRFD): Fix mistake in last change.
---
 ChangeLog   | 7 +++++++
 m4/dirfd.m4 | 6 +-----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aaffe12fc1..5f01a52535 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-05-05  Bruno Haible  <br...@clisp.org>
+
+	dirfd: Fix bogus override (regression 2023-04-26).
+	Reported by Bjarni Ingi Gislason <bjarn...@simnet.is> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2023-05/msg00040.html>.
+	* m4/dirfd.m4 (gl_FUNC_DIRFD): Fix mistake in last change.
+
 2023-05-04  Bruno Haible  <br...@clisp.org>
 
 	c32swidth: Add tests.
diff --git a/m4/dirfd.m4 b/m4/dirfd.m4
index d1ee2c7f61..7968b1287c 100644
--- a/m4/dirfd.m4
+++ b/m4/dirfd.m4
@@ -1,4 +1,4 @@
-# serial 27   -*- Autoconf -*-
+# serial 28   -*- Autoconf -*-
 
 dnl Find out how to get the file descriptor associated with an open DIR*.
 
@@ -40,10 +40,6 @@ AC_DEFUN([gl_FUNC_DIRFD]
     HAVE_DIRFD=0
   else
     HAVE_DIRFD=1
-    dnl Replace only if the system declares dirfd already.
-    if test $ac_cv_have_decl_dirfd = yes; then
-      REPLACE_DIRFD=1
-    fi
     dnl Replace dirfd() on native Windows, to support fdopendir().
     AC_REQUIRE([gl_DIRENT_DIR])
     if test $DIR_HAS_FD_MEMBER = 0; then
-- 
2.34.1

>From c86821aa49312abab5c6f9b7941c038797b6c9a5 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Fri, 5 May 2023 12:07:22 +0200
Subject: [PATCH 2/2] dirfd: Add tests.

* tests/test-dirfd.c: New file.
* modules/dirfd-tests: New file.
---
 ChangeLog           |  6 ++++++
 modules/dirfd-tests | 11 +++++++++++
 tests/test-dirfd.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 modules/dirfd-tests
 create mode 100644 tests/test-dirfd.c

diff --git a/ChangeLog b/ChangeLog
index 5f01a52535..06ff163e0e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-05-05  Bruno Haible  <br...@clisp.org>
+
+	dirfd: Add tests.
+	* tests/test-dirfd.c: New file.
+	* modules/dirfd-tests: New file.
+
 2023-05-05  Bruno Haible  <br...@clisp.org>
 
 	dirfd: Fix bogus override (regression 2023-04-26).
diff --git a/modules/dirfd-tests b/modules/dirfd-tests
new file mode 100644
index 0000000000..0738351042
--- /dev/null
+++ b/modules/dirfd-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-dirfd.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-dirfd
+check_PROGRAMS += test-dirfd
diff --git a/tests/test-dirfd.c b/tests/test-dirfd.c
new file mode 100644
index 0000000000..f7fd395a9c
--- /dev/null
+++ b/tests/test-dirfd.c
@@ -0,0 +1,47 @@
+/* Test of dirfd() function.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <br...@clisp.org>, 2023.  */
+
+#include <config.h>
+
+#include <dirent.h>
+
+#include <stdio.h>
+
+#include "macros.h"
+
+int
+main ()
+{
+#if defined _WIN32 && !defined __CYGWIN__
+  fprintf (stderr, "Skipping test: The DIR type does not contain a file descriptor.\n");
+  return 77;
+#else
+  /* On all other platforms, we expect to have either
+       - a dirfd() function, or
+       - a dirfd macro, or
+       - a DIR struct with a d_fd member, or
+       - a DIR struct with a dd_fd member.
+     If we don't have this, dirfd.c produces a function that always returns -1.
+     Check here that this does not happen.  */
+  DIR *d = opendir (".");
+  int fd = dirfd (d);
+  ASSERT (fd >= 0);
+
+  return 0;
+#endif
+}
-- 
2.34.1

Reply via email to