Here is the sys_un module as promised + typical C/C++ header checks.

I've decided to make <sys/un.h> only generate if using a Windows version
with <afunix.h>. Therefore it cannot be included unless HAVE_UNIXSOCKET
is set to 1.

Feel free to change it if you think another way would work better.

Collin

>From 861f4ceeece282892fc58e9278cd2f4dbdbe7d29 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 24 Jul 2024 20:14:49 -0700
Subject: [PATCH 1/3] sys_un: New module.

* doc/posix-headers/sys_un.texi (sys/un.h): Mention it.
* lib/sys_un.in.h: New file.
* m4/sys_un_h.m4: New file.
* modules/sys_un: New file.
---
 ChangeLog                     |  8 +++++++
 doc/posix-headers/sys_un.texi | 11 +++++----
 lib/sys_un.in.h               | 34 +++++++++++++++++++++++++++
 m4/sys_un_h.m4                | 32 +++++++++++++++++++++++++
 modules/sys_un                | 44 +++++++++++++++++++++++++++++++++++
 5 files changed, 125 insertions(+), 4 deletions(-)
 create mode 100644 lib/sys_un.in.h
 create mode 100644 m4/sys_un_h.m4
 create mode 100644 modules/sys_un

diff --git a/ChangeLog b/ChangeLog
index f7fcead6a4..6342a6ce7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-07-24  Collin Funk  <collin.fu...@gmail.com>
+
+	sys_un: New module.
+	* doc/posix-headers/sys_un.texi (sys/un.h): Mention it.
+	* lib/sys_un.in.h: New file.
+	* m4/sys_un_h.m4: New file.
+	* modules/sys_un: New file.
+
 2024-07-24  Bruno Haible  <br...@clisp.org>
 
 	Avoid false select_used_without_requesting_gnulib_module_select.
diff --git a/doc/posix-headers/sys_un.texi b/doc/posix-headers/sys_un.texi
index 48ecf3753f..61affda2c6 100644
--- a/doc/posix-headers/sys_un.texi
+++ b/doc/posix-headers/sys_un.texi
@@ -3,18 +3,21 @@ @node sys/un.h
 
 POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_un.h.html}
 
-Gnulib module: ---
+Gnulib module: sys_un
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+@c Added in Windows 10 Insider Build 17063:
+@c <https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/>
+@c <https://blogs.windows.com/windows-insider/2017/12/19/announcing-windows-10-insider-preview-build-17063-pc/>
+This header file is missing on some platforms:
+mingw, MSVC 14.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This header file is missing on some platforms:
-mingw, MSVC 14.
-@item
 This header requires <code>sys/socket.h</code> to be included first on
 some platforms:
 Cygwin 1.7.18.
diff --git a/lib/sys_un.in.h b/lib/sys_un.in.h
new file mode 100644
index 0000000000..52993d65f5
--- /dev/null
+++ b/lib/sys_un.in.h
@@ -0,0 +1,34 @@
+/* sys/un.h - definitions for UNIX domain sockets
+
+   Copyright 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file 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 Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Collin Funk.  */
+
+#ifndef _@GUARD_PREFIX@_SYS_UN_H
+#define _@GUARD_PREFIX@_SYS_UN_H 1
+
+/* Windows requires <winsock2.h> to be included before <afunix.h>.  */
+#if @HAVE_WINSOCK2_H@
+# include <winsock2.h>
+#endif
+#if @HAVE_AFUNIX_H@
+# include <afunix.h>
+#endif
+
+/* If a platform does not support AF_UNIX sockets 'struct sockaddr_un' will
+   not be defined.  You may use HAVE_UNIXSOCKET after including <config.h>.  */
+
+#endif
diff --git a/m4/sys_un_h.m4 b/m4/sys_un_h.m4
new file mode 100644
index 0000000000..ed19b70cb5
--- /dev/null
+++ b/m4/sys_un_h.m4
@@ -0,0 +1,32 @@
+# sys_un_h.m4
+# serial 1
+dnl Copyright 2024 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_SYS_UN_H],
+[
+  dnl Check if UNIX domain sockets are supported.
+  AC_REQUIRE([gl_SOCKET_FAMILY_UNIX])
+
+  GL_GENERATE_SYS_UN_H=false
+
+  if test $gl_cv_socket_unix = yes; then
+
+    dnl Check if using a Windows version that supports AF_UNIX.
+    dnl See <https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/>.
+    if test "$ac_cv_header_winsock2_h" = yes \
+       && test "$ac_cv_header_afunix_h" = yes; then
+      GL_GENERATE_SYS_UN_H=true
+    fi
+  fi
+
+  dnl Checked in gl_SOCKET_FAMILY_UNIX.
+  if test "$ac_cv_header_afunix_h" = yes; then
+    HAVE_AFUNIX_H=1
+  else
+    HAVE_AFUNIX_H=0
+  fi
+  AC_SUBST([HAVE_AFUNIX_H])
+])
diff --git a/modules/sys_un b/modules/sys_un
new file mode 100644
index 0000000000..af72a80aab
--- /dev/null
+++ b/modules/sys_un
@@ -0,0 +1,44 @@
+Description:
+A POSIX-like <sys/un.h>.
+
+Files:
+lib/sys_un.in.h
+m4/sys_un_h.m4
+
+Depends-on:
+gen-header
+sys_socket
+
+configure.ac:
+gl_SYS_UN_H
+gl_CONDITIONAL_HEADER([sys/un.h])
+AC_PROG_MKDIR_P
+
+Makefile.am:
+BUILT_SOURCES += $(SYS_UN_H)
+
+# We need the following in order to create <sys/un.h> when the system
+# doesn't have one.
+if GL_GENERATE_SYS_UN_H
+sys/un.h: sys_un.in.h $(top_builddir)/config.status
+@NMD@	$(AM_V_GEN)$(MKDIR_P) '%reldir%'
+	$(gl_V_at)$(SED_HEADER_STDOUT) \
+	      -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
+	      -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
+	      -e 's|@''HAVE_AFUNIX_H''@|$(HAVE_AFUNIX_H)|g' \
+	$(srcdir)/sys_un.in.h > $@-t
+	$(AM_V_at)mv $@-t $@
+else
+sys/un.h: $(top_builddir)/config.status
+	rm -f $@
+endif
+MOSTLYCLEANFILES += sys/un.h sys/un.h-t
+
+Include:
+<sys/un.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
-- 
2.45.2

>From c2d32810156e4d2473e534feca6ba141c29deeed Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 24 Jul 2024 20:16:26 -0700
Subject: [PATCH 2/3] sys_un: Add tests.

* modules/sys_un-tests: New file.
* tests/test-sys_un.c: New file.
---
 ChangeLog            |  4 ++++
 modules/sys_un-tests | 11 ++++++++++
 tests/test-sys_un.c  | 50 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 modules/sys_un-tests
 create mode 100644 tests/test-sys_un.c

diff --git a/ChangeLog b/ChangeLog
index 6342a6ce7f..8c93d18bdc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-07-24  Collin Funk  <collin.fu...@gmail.com>
 
+	sys_un: Add tests.
+	* modules/sys_un-tests: New file.
+	* tests/test-sys_un.c: New file.
+
 	sys_un: New module.
 	* doc/posix-headers/sys_un.texi (sys/un.h): Mention it.
 	* lib/sys_un.in.h: New file.
diff --git a/modules/sys_un-tests b/modules/sys_un-tests
new file mode 100644
index 0000000000..a7d912be0c
--- /dev/null
+++ b/modules/sys_un-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-sys_un.c
+
+Depends-on:
+assert-h
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sys_un
+check_PROGRAMS += test-sys_un
diff --git a/tests/test-sys_un.c b/tests/test-sys_un.c
new file mode 100644
index 0000000000..c6b293179c
--- /dev/null
+++ b/tests/test-sys_un.c
@@ -0,0 +1,50 @@
+/* Test of the <sys/un.h> substitute.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This file 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 file 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 Collin Funk <collin.fu...@gmail.com>, 2024.  */
+
+#include <config.h>
+
+#if HAVE_UNIXSOCKET
+
+/* Specification.  */
+# include <sys/un.h>
+
+int
+main (void)
+{
+  struct sockaddr_un sa;
+
+  /* POSIX requires sun_path is 92 bytes or greater.  */
+  static_assert (92 <= sizeof sa.sun_path);
+
+  return 0;
+}
+
+#else
+
+/* UNIX domain sockets unsupported.  */
+
+# include <stdio.h>
+
+int
+main (void)
+{
+  fputs ("Skipping test: UNIX domain sockets not supported\n", stderr);
+  return 77;
+}
+
+#endif
-- 
2.45.2

>From f9d6d12c37745c2ebd7a8d02d24faced3a5669be Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 24 Jul 2024 20:27:46 -0700
Subject: [PATCH 3/3] sys_un: Add C++ tests.

* modules/sys_un-c++-tests: New file.
* tests/test-sys_un-c++.cc: New file.
* modules/sys_un-tests (Depends-on): Add sys_un-c++-tests.
---
 ChangeLog                |  5 +++++
 modules/sys_un-c++-tests | 17 ++++++++++++++++
 modules/sys_un-tests     |  1 +
 tests/test-sys_un-c++.cc | 44 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 67 insertions(+)
 create mode 100644 modules/sys_un-c++-tests
 create mode 100644 tests/test-sys_un-c++.cc

diff --git a/ChangeLog b/ChangeLog
index 8c93d18bdc..ad0e632c03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-07-24  Collin Funk  <collin.fu...@gmail.com>
 
+	sys_un: Add C++ tests.
+	* modules/sys_un-c++-tests: New file.
+	* tests/test-sys_un-c++.cc: New file.
+	* modules/sys_un-tests (Depends-on): Add sys_un-c++-tests.
+
 	sys_un: Add tests.
 	* modules/sys_un-tests: New file.
 	* tests/test-sys_un.c: New file.
diff --git a/modules/sys_un-c++-tests b/modules/sys_un-c++-tests
new file mode 100644
index 0000000000..d5aac5183a
--- /dev/null
+++ b/modules/sys_un-c++-tests
@@ -0,0 +1,17 @@
+Files:
+tests/test-sys_un-c++.cc
+
+Status:
+c++-test
+
+Depends-on:
+ansi-c++-opt
+
+configure.ac:
+
+Makefile.am:
+if ANSICXX
+TESTS += test-sys_un-c++
+check_PROGRAMS += test-sys_un-c++
+test_sys_un_c___SOURCES = test-sys_un-c++.cc
+endif
diff --git a/modules/sys_un-tests b/modules/sys_un-tests
index a7d912be0c..ef4e81bb82 100644
--- a/modules/sys_un-tests
+++ b/modules/sys_un-tests
@@ -3,6 +3,7 @@ tests/test-sys_un.c
 
 Depends-on:
 assert-h
+sys_un-c++-tests
 
 configure.ac:
 
diff --git a/tests/test-sys_un-c++.cc b/tests/test-sys_un-c++.cc
new file mode 100644
index 0000000000..5b2e39177a
--- /dev/null
+++ b/tests/test-sys_un-c++.cc
@@ -0,0 +1,44 @@
+/* Test of the <sys/un.h> substitute in C++ mode.
+   Copyright (C) 2024 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 Collin Funk <collin.fu...@gmail.com>, 2024.  */
+
+#define GNULIB_NAMESPACE gnulib
+#include <config.h>
+
+#if HAVE_UNIXSOCKET
+
+# include <sys/un.h>
+
+int
+main ()
+{
+}
+
+#else
+
+/* UNIX domain sockets unsupported.  */
+
+# include <iostream>
+
+int
+main ()
+{
+  std::cout << "Skipping test: UNIX domain sockets not supported" << std::endl;
+  return 77;
+}
+
+#endif
-- 
2.45.2

Reply via email to