https://gitlab.freedesktop.org/libbsd/libbsd/-/merge_requests/3

diff --git a/configure.ac b/configure.ac
index 6e9496d5775742d68d38ebfb4bead730bb0e1fe1..60b6e4cc0eaeb86dd7132f9410a929dc59c6637b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,6 +44,8 @@ AC_SUBST([TESTU01_LIBS])
 AM_CONDITIONAL([HAVE_LIBTESTU01],
                [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
 
+AX_CHECK_VSCRIPT
+
 is_windows=no
 AS_CASE([$host_os],
   [*-gnu*], [
@@ -68,7 +70,7 @@ AS_CASE([$host_os],
 AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
 
 # Checks for header files.
-AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h grp.h])
+AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h grp.h nlist.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_INLINE
@@ -169,7 +171,8 @@ AC_LINK_IFELSE(
 
 AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
                 getauxval getentropy getexecname getline \
-                pstat_getproc sysconf])
+                pstat_getproc sysconf \
+		strlcpy strlcat strnstr strmode fpurge])
 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
 
 AC_CONFIG_FILES([
diff --git a/include/bsd/string.h b/include/bsd/string.h
index f987fee3ed3c6c2ad0e5ec9aba1fcb2897a45f9f..a1e17ed2a1c2f9340450ae6a2e7c6130b658bdb9 100644
--- a/include/bsd/string.h
+++ b/include/bsd/string.h
@@ -41,10 +41,21 @@
 #include <sys/types.h>
 
 __BEGIN_DECLS
+#if !HAVE_STRLCPY
 size_t strlcpy(char *dst, const char *src, size_t siz);
+#endif
+
+#if !HAVE_STRLCAT
 size_t strlcat(char *dst, const char *src, size_t siz);
+#endif
+
+#if !HAVE_STRNSTR
 char *strnstr(const char *str, const char *find, size_t str_len);
+#endif
+
+#if !HAVE_STRMODE
 void strmode(mode_t mode, char *str);
+#endif
 
 #if !defined(__GLIBC__) || \
     (defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 25) || !defined(_GNU_SOURCE)))
diff --git a/m4/ax_check_vscript.m4 b/m4/ax_check_vscript.m4
new file mode 100644
index 0000000000000000000000000000000000000000..9851f322551e959b98385668220056959c386ad9
--- /dev/null
+++ b/m4/ax_check_vscript.m4
@@ -0,0 +1,142 @@
+# ===========================================================================
+#     https://www.gnu.org/software/autoconf-archive/ax_check_vscript.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_CHECK_VSCRIPT
+#
+# DESCRIPTION
+#
+#   Check whether the linker supports version scripts.  Version scripts are
+#   used when building shared libraries to bind symbols to version nodes
+#   (helping to detect incompatibilities) or to limit the visibility of
+#   non-public symbols.
+#
+#   Output:
+#
+#   If version scripts are supported, VSCRIPT_LDFLAGS will contain the
+#   appropriate flag to pass to the linker.  On GNU systems this would
+#   typically be "-Wl,--version-script", and on Solaris it would typically
+#   be "-Wl,-M".
+#
+#   Two Automake conditionals are also set:
+#
+#    HAVE_VSCRIPT is true if the linker supports version scripts with
+#    entries that use simple wildcards, like "local: *".
+#
+#    HAVE_VSCRIPT_COMPLEX is true if the linker supports version scripts with
+#    pattern matching wildcards, like "global: Java_*".
+#
+#   On systems that do not support symbol versioning, such as Mac OS X, both
+#   conditionals will be false.  They will also be false if the user passes
+#   "--disable-symvers" on the configure command line.
+#
+#   Example:
+#
+#    configure.ac:
+#
+#     AX_CHECK_VSCRIPT
+#
+#    Makefile.am:
+#
+#     if HAVE_VSCRIPT
+#     libfoo_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libfoo.map
+#     endif
+#
+#     if HAVE_VSCRIPT_COMPLEX
+#     libbar_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libbar.map
+#     endif
+#
+# LICENSE
+#
+#   Copyright (c) 2014 Kevin Cernekee <cernekee@gmail.com>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved. This file is offered as-is, without any
+#   warranty.
+
+#serial 2
+
+# _AX_CHECK_VSCRIPT(flag, global-sym, action-if-link-succeeds, [junk-file=no])
+AC_DEFUN([_AX_CHECK_VSCRIPT], [
+  AC_LANG_PUSH([C])
+  ax_check_vscript_save_flags="$LDFLAGS"
+  echo "V1 { global: $2; local: *; };" > conftest.map
+  AS_IF([test x$4 = xyes], [
+    echo "{" >> conftest.map
+  ])
+  LDFLAGS="$LDFLAGS -Wl,$1,conftest.map"
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[int show, hide;]], [])], [$3])
+  LDFLAGS="$ax_check_vscript_save_flags"
+  rm -f conftest.map
+  AC_LANG_POP([C])
+]) dnl _AX_CHECK_VSCRIPT
+
+AC_DEFUN([AX_CHECK_VSCRIPT], [
+
+  AC_ARG_ENABLE([symvers],
+    AS_HELP_STRING([--disable-symvers],
+                   [disable library symbol versioning [default=auto]]),
+    [want_symvers=$enableval],
+    [want_symvers=yes]
+  )
+
+  AS_IF([test x$want_symvers = xyes], [
+
+    dnl First test --version-script and -M with a simple wildcard.
+
+    AC_CACHE_CHECK([linker version script flag], ax_cv_check_vscript_flag, [
+      ax_cv_check_vscript_flag=unsupported
+      _AX_CHECK_VSCRIPT([--version-script], [show], [
+        ax_cv_check_vscript_flag=--version-script
+      ])
+      AS_IF([test x$ax_cv_check_vscript_flag = xunsupported], [
+        _AX_CHECK_VSCRIPT([-M], [show], [ax_cv_check_vscript_flag=-M])
+      ])
+
+      dnl The linker may interpret -M (no argument) as "produce a load map."
+      dnl If "-M conftest.map" doesn't fail when conftest.map contains
+      dnl obvious syntax errors, assume this is the case.
+
+      AS_IF([test x$ax_cv_check_vscript_flag != xunsupported], [
+        _AX_CHECK_VSCRIPT([$ax_cv_check_vscript_flag], [show],
+	                  [ax_cv_check_vscript_flag=unsupported], [yes])
+      ])
+    ])
+
+    dnl If the simple wildcard worked, retest with a complex wildcard.
+
+    AS_IF([test x$ax_cv_check_vscript_flag != xunsupported], [
+      ax_check_vscript_flag=$ax_cv_check_vscript_flag
+      AC_CACHE_CHECK([if version scripts can use complex wildcards],
+                     ax_cv_check_vscript_complex_wildcards, [
+        ax_cv_check_vscript_complex_wildcards=no
+        _AX_CHECK_VSCRIPT([$ax_cv_check_vscript_flag], [sh*], [
+        ax_cv_check_vscript_complex_wildcards=yes])
+      ])
+      ax_check_vscript_complex_wildcards="$ax_cv_check_vscript_complex_wildcards"
+    ], [
+      ax_check_vscript_flag=
+      ax_check_vscript_complex_wildcards=no
+    ])
+  ], [
+    AC_MSG_CHECKING([linker version script flag])
+    AC_MSG_RESULT([disabled])
+
+    ax_check_vscript_flag=
+    ax_check_vscript_complex_wildcards=no
+  ])
+
+  AS_IF([test x$ax_check_vscript_flag != x], [
+    VSCRIPT_LDFLAGS="-Wl,$ax_check_vscript_flag"
+    AC_SUBST([VSCRIPT_LDFLAGS])
+  ])
+
+  AM_CONDITIONAL([HAVE_VSCRIPT],
+    [test x$ax_check_vscript_flag != x])
+  AM_CONDITIONAL([HAVE_VSCRIPT_COMPLEX],
+    [test x$ax_check_vscript_complex_wildcards = xyes])
+
+]) dnl AX_CHECK_VSCRIPT
diff --git a/src/Makefile.am b/src/Makefile.am
index 8384b92ce57a206a9f59e44a3cd748d8ee93c004..e2c8e15095d9bdf46e134f9553e2f03a00afe0ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -65,8 +65,11 @@ libbsd_la_LIBADD = \
 	$(ARC4RANDOM_ATFORK_LIBS) \
 	$(nil)
 libbsd_la_LDFLAGS = \
-	-Wl,--version-script=$(srcdir)/libbsd.map \
 	-version-number $(LIBBSD_ABI)
+if HAVE_VSCRIPT
+libbsd_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libbsd.map
+endif
+
 libbsd_la_SOURCES = \
 	arc4random.c \
 	arc4random.h \
diff --git a/src/fpurge.c b/src/fpurge.c
index 462535ab506d6f72785986ee053e8606421c0ecc..a8941dbc3214fbe8eea3fd32c0889c2c9dfa1746 100644
--- a/src/fpurge.c
+++ b/src/fpurge.c
@@ -26,9 +26,10 @@
 
 #include <errno.h>
 #include <stdio.h>
-#include <stdio_ext.h>
 
 #ifdef HAVE___FPURGE
+#include <stdio_ext.h>
+
 int
 fpurge(FILE *fp)
 {
@@ -41,6 +42,36 @@ fpurge(FILE *fp)
 
 	return 0;
 }
+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
+#elif HAVE_FPURGE
+int
+fpurge(FILE *fp)
+{
+	if (fp == NULL || fileno(fp) < 0) {
+		errno = EBADF;
+		return EOF;
+	}
+
+  /* Call the system's fpurge function.  */
+#undef fpurge
+#if !HAVE_DECL_FPURGE
+  extern int fpurge (FILE *);
+#endif
+  int result = fpurge (fp);
+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
+#if defined(__sferror) || defined(__DragonFly__)
+  if (result == 0)
+    /* Correct the invariants that fpurge broke.
+       <stdio.h> on BSD systems says:
+         "The following always hold: if _flags & __SRD, _w is 0."
+       If this invariant is not fulfilled and the stream is read-write but
+       currently reading, subsequent putc or fputc calls will write directly
+       into the buffer, although they shouldn't be allowed to.  */
+    if ((fp->_flags & __SRD) != 0)
+      fp->_w = 0;
+#endif
+  return result;
+}
 #else
 #error "Function fpurge() needs to be ported."
 #endif
diff --git a/src/funopen.c b/src/funopen.c
index 1e05c7e75188b14974d30ce8f6b74d67d44541b7..65ba5a8d1b60cb8d95200212ccec44b123f4a6c2 100644
--- a/src/funopen.c
+++ b/src/funopen.c
@@ -143,6 +143,7 @@ funopen(const void *cookie,
  * they will not add the needed support to implement it. Just ignore this
  * interface there, as it has never been provided anyway.
  */
+#elif defined(__MACH__)
 #else
 #error "Function funopen() needs to be ported or disabled."
 #endif
diff --git a/src/hash/sha512.h b/src/hash/sha512.h
index 4f368a14bb68790662f7587d1c269001978f31ea..7632e25ad2cf8c40e9338d9babfbcdf3ba0822d3 100644
--- a/src/hash/sha512.h
+++ b/src/hash/sha512.h
@@ -29,7 +29,11 @@
 #ifndef _SHA512_H_
 #define _SHA512_H_
 
+#ifdef __MACH__
+#include <stdint.h>
+#else
 #include <sys/types.h>
+#endif
 
 #define SHA512_DIGEST_LENGTH		64
 
diff --git a/src/hash/sha512c.c b/src/hash/sha512c.c
index b3c8d5e75ed95cca24be2ed999bd70c76bf2d5a9..602f54e339870ad6215075949de9b517164bc4af 100644
--- a/src/hash/sha512c.c
+++ b/src/hash/sha512c.c
@@ -25,7 +25,12 @@
  */
 
 #include <sys/cdefs.h>
+
+#ifdef __MACH__
+#include <machine/endian.h>
+#else
 #include <sys/endian.h>
+#endif
 #include <sys/types.h>
 
 #include <string.h>
diff --git a/src/local-link.h b/src/local-link.h
index 0d4351a3cb6ff7d8bd5a76b2222b37470b21b70b..fc520af61fbfa888f45d168666d5b33e1c8a13d2 100644
--- a/src/local-link.h
+++ b/src/local-link.h
@@ -27,6 +27,11 @@
 #ifndef LIBBSD_LOCAL_LINK_H
 #define LIBBSD_LOCAL_LINK_H
 
+#ifdef __MACH__
+#define libbsd_link_warning(symbol, msg)
+#define libbsd_symver_default(alias, symbol, version)
+#define libbsd_symver_variant(alias, symbol, version)
+#else
 #define libbsd_link_warning(symbol, msg) \
 	static const char libbsd_emit_link_warning_##symbol[] \
 		__attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg;
@@ -45,3 +50,4 @@
 #endif
 
 #endif
+#endif
diff --git a/src/nlist.c b/src/nlist.c
index 228c220973bdc9bde20ae2e5827d868e56a9a70e..46c2d779cf7f028739b80ca8eccac37c02a522c9 100644
--- a/src/nlist.c
+++ b/src/nlist.c
@@ -41,6 +41,7 @@
 #include <unistd.h>
 #include <nlist.h>
 
+#if !HAVE_NLIST_H
 #include "local-elf.h"
 
 #define SIZE_T_MAX 0xffffffffU
@@ -280,3 +281,4 @@ nlist(const char *name, struct nlist *list)
 	(void)close(fd);
 	return (n);
 }
+#endif
diff --git a/src/readpassphrase.c b/src/readpassphrase.c
index f9f61950d17436f5550c2426701e5d22557eb642..2bc5fb4bca42dc650c13c1f2a8e98373349222aa 100644
--- a/src/readpassphrase.c
+++ b/src/readpassphrase.c
@@ -36,6 +36,14 @@
 #define TCSASOFT 0
 #endif
 
+#ifndef _SIGMAX
+#define	_SIGMAX	64
+#endif
+
+#ifndef _NSIG
+#define _NSIG (_SIGMAX + 1)
+#endif
+
 static volatile sig_atomic_t signo[_NSIG];
 
 static void handler(int);
diff --git a/src/setproctitle.c b/src/setproctitle.c
index ff32aa3a08a31563198cb7651cd5e665bafeee34..51ed833b68ba550614ccea4711c8bb3c292b5531 100644
--- a/src/setproctitle.c
+++ b/src/setproctitle.c
@@ -33,6 +33,10 @@
 #include <string.h>
 #include "local-link.h"
 
+#ifdef __MACH__
+extern char **environ;
+#endif
+
 static struct {
 	/* Original value. */
 	const char *arg0;
@@ -287,7 +291,8 @@ libbsd_symver_default(setproctitle, setproctitle_impl, LIBBSD_0.5);
  * in 0.5, make the implementation available in the old version as an alias
  * for code linking against that version, and change the default to use the
  * new version, so that new code depends on the implemented version. */
-#ifdef HAVE_TYPEOF
+#ifdef __MACH__
+#elif defined(HAVE_TYPEOF)
 extern __typeof__(setproctitle_impl)
 setproctitle_stub
 	__attribute__((__alias__("setproctitle_impl")));
diff --git a/src/strlcat.c b/src/strlcat.c
index 14c53a1f69c2fd43b66f504b6682383f5542a86d..5961c17ace0d1de875de5d3751e6d7dcc724d8ca 100644
--- a/src/strlcat.c
+++ b/src/strlcat.c
@@ -26,6 +26,7 @@
  * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
  * If retval >= dsize, truncation occurred.
  */
+#if !HAVE_STRLCAT
 size_t
 strlcat(char *dst, const char *src, size_t dsize)
 {
@@ -53,3 +54,4 @@ strlcat(char *dst, const char *src, size_t dsize)
 
 	return(dlen + (src - osrc));	/* count does not include NUL */
 }
+#endif
diff --git a/src/strlcpy.c b/src/strlcpy.c
index e9a7fe4be7359d0d4226952e174ff64eb4d52b18..5137acb632044e26dfbbec7b33f59773ec474763 100644
--- a/src/strlcpy.c
+++ b/src/strlcpy.c
@@ -24,6 +24,7 @@
  * chars will be copied.  Always NUL terminates (unless dsize == 0).
  * Returns strlen(src); if retval >= dsize, truncation occurred.
  */
+#if !HAVE_STRLCPY
 size_t
 strlcpy(char *dst, const char *src, size_t dsize)
 {
@@ -48,3 +49,4 @@ strlcpy(char *dst, const char *src, size_t dsize)
 
 	return(src - osrc - 1);	/* count does not include NUL */
 }
+#endif
diff --git a/src/strmode.c b/src/strmode.c
index e6afde5a6ca0ff1ee8145d8b7451dcfc9b858451..da680c917c110f365159f2f4fc068203b6fee4e8 100644
--- a/src/strmode.c
+++ b/src/strmode.c
@@ -32,6 +32,7 @@
 #include <sys/stat.h>
 #include <string.h>
 
+#if !HAVE_STRMODE
 void
 strmode(mode_t mode, char *p)
 {
@@ -141,3 +142,4 @@ strmode(mode_t mode, char *p)
 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
 	*p = '\0';
 }
+#endif
