1diff --git a/configure.ac b/configure.ac
2index 6e9496d..60b6e4c 100644
3--- a/configure.ac
4+++ b/configure.ac
5@@ -44,6 +44,8 @@ AC_SUBST([TESTU01_LIBS])
6 AM_CONDITIONAL([HAVE_LIBTESTU01],
7 [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
8
9+AX_CHECK_VSCRIPT
10+
11 is_windows=no
12 AS_CASE([$host_os],
13 [*-gnu*], [
14@@ -68,7 +70,7 @@ AS_CASE([$host_os],
15 AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
16
17 # Checks for header files.
18-AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h grp.h])
19+AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h grp.h nlist.h])
20
21 # Checks for typedefs, structures, and compiler characteristics.
22 AC_C_INLINE
23@@ -169,7 +171,8 @@ AC_LINK_IFELSE(
24
25 AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
26 getauxval getentropy getexecname getline \
27- pstat_getproc sysconf])
28+ pstat_getproc sysconf \
29+ strlcpy strlcat strnstr strmode fpurge])
30 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
31
32 AC_CONFIG_FILES([
33diff --git a/include/bsd/string.h b/include/bsd/string.h
34index f987fee..a1e17ed 100644
35--- a/include/bsd/string.h
36+++ b/include/bsd/string.h
37@@ -41,10 +41,21 @@
38 #include <sys/types.h>
39
40 __BEGIN_DECLS
41+#if !HAVE_STRLCPY
42 size_t strlcpy(char *dst, const char *src, size_t siz);
43+#endif
44+
45+#if !HAVE_STRLCAT
46 size_t strlcat(char *dst, const char *src, size_t siz);
47+#endif
48+
49+#if !HAVE_STRNSTR
50 char *strnstr(const char *str, const char *find, size_t str_len);
51+#endif
52+
53+#if !HAVE_STRMODE
54 void strmode(mode_t mode, char *str);
55+#endif
56
57 #if !defined(__GLIBC__) || \
58 (defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 25) || !defined(_GNU_SOURCE)))
59diff --git a/m4/ax_check_vscript.m4 b/m4/ax_check_vscript.m4
60new file mode 100644
61index 0000000..9851f32
62--- /dev/null
63+++ b/m4/ax_check_vscript.m4
64@@ -0,0 +1,142 @@
65+# ===========================================================================
66+# https://www.gnu.org/software/autoconf-archive/ax_check_vscript.html
67+# ===========================================================================
68+#
69+# SYNOPSIS
70+#
71+# AX_CHECK_VSCRIPT
72+#
73+# DESCRIPTION
74+#
75+# Check whether the linker supports version scripts. Version scripts are
76+# used when building shared libraries to bind symbols to version nodes
77+# (helping to detect incompatibilities) or to limit the visibility of
78+# non-public symbols.
79+#
80+# Output:
81+#
82+# If version scripts are supported, VSCRIPT_LDFLAGS will contain the
83+# appropriate flag to pass to the linker. On GNU systems this would
84+# typically be "-Wl,--version-script", and on Solaris it would typically
85+# be "-Wl,-M".
86+#
87+# Two Automake conditionals are also set:
88+#
89+# HAVE_VSCRIPT is true if the linker supports version scripts with
90+# entries that use simple wildcards, like "local: *".
91+#
92+# HAVE_VSCRIPT_COMPLEX is true if the linker supports version scripts with
93+# pattern matching wildcards, like "global: Java_*".
94+#
95+# On systems that do not support symbol versioning, such as Mac OS X, both
96+# conditionals will be false. They will also be false if the user passes
97+# "--disable-symvers" on the configure command line.
98+#
99+# Example:
100+#
101+# configure.ac:
102+#
103+# AX_CHECK_VSCRIPT
104+#
105+# Makefile.am:
106+#
107+# if HAVE_VSCRIPT
108+# libfoo_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libfoo.map
109+# endif
110+#
111+# if HAVE_VSCRIPT_COMPLEX
112+# libbar_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libbar.map
113+# endif
114+#
115+# LICENSE
116+#
117+# Copyright (c) 2014 Kevin Cernekee <cernekee@gmail.com>
118+#
119+# Copying and distribution of this file, with or without modification, are
120+# permitted in any medium without royalty provided the copyright notice
121+# and this notice are preserved. This file is offered as-is, without any
122+# warranty.
123+
124+#serial 2
125+
126+# _AX_CHECK_VSCRIPT(flag, global-sym, action-if-link-succeeds, [junk-file=no])
127+AC_DEFUN([_AX_CHECK_VSCRIPT], [
128+ AC_LANG_PUSH([C])
129+ ax_check_vscript_save_flags="$LDFLAGS"
130+ echo "V1 { global: $2; local: *; };" > conftest.map
131+ AS_IF([test x$4 = xyes], [
132+ echo "{" >> conftest.map
133+ ])
134+ LDFLAGS="$LDFLAGS -Wl,$1,conftest.map"
135+ AC_LINK_IFELSE([AC_LANG_PROGRAM([[int show, hide;]], [])], [$3])
136+ LDFLAGS="$ax_check_vscript_save_flags"
137+ rm -f conftest.map
138+ AC_LANG_POP([C])
139+]) dnl _AX_CHECK_VSCRIPT
140+
141+AC_DEFUN([AX_CHECK_VSCRIPT], [
142+
143+ AC_ARG_ENABLE([symvers],
144+ AS_HELP_STRING([--disable-symvers],
145+ [disable library symbol versioning [default=auto]]),
146+ [want_symvers=$enableval],
147+ [want_symvers=yes]
148+ )
149+
150+ AS_IF([test x$want_symvers = xyes], [
151+
152+ dnl First test --version-script and -M with a simple wildcard.
153+
154+ AC_CACHE_CHECK([linker version script flag], ax_cv_check_vscript_flag, [
155+ ax_cv_check_vscript_flag=unsupported
156+ _AX_CHECK_VSCRIPT([--version-script], [show], [
157+ ax_cv_check_vscript_flag=--version-script
158+ ])
159+ AS_IF([test x$ax_cv_check_vscript_flag = xunsupported], [
160+ _AX_CHECK_VSCRIPT([-M], [show], [ax_cv_check_vscript_flag=-M])
161+ ])
162+
163+ dnl The linker may interpret -M (no argument) as "produce a load map."
164+ dnl If "-M conftest.map" doesn't fail when conftest.map contains
165+ dnl obvious syntax errors, assume this is the case.
166+
167+ AS_IF([test x$ax_cv_check_vscript_flag != xunsupported], [
168+ _AX_CHECK_VSCRIPT([$ax_cv_check_vscript_flag], [show],
169+ [ax_cv_check_vscript_flag=unsupported], [yes])
170+ ])
171+ ])
172+
173+ dnl If the simple wildcard worked, retest with a complex wildcard.
174+
175+ AS_IF([test x$ax_cv_check_vscript_flag != xunsupported], [
176+ ax_check_vscript_flag=$ax_cv_check_vscript_flag
177+ AC_CACHE_CHECK([if version scripts can use complex wildcards],
178+ ax_cv_check_vscript_complex_wildcards, [
179+ ax_cv_check_vscript_complex_wildcards=no
180+ _AX_CHECK_VSCRIPT([$ax_cv_check_vscript_flag], [sh*], [
181+ ax_cv_check_vscript_complex_wildcards=yes])
182+ ])
183+ ax_check_vscript_complex_wildcards="$ax_cv_check_vscript_complex_wildcards"
184+ ], [
185+ ax_check_vscript_flag=
186+ ax_check_vscript_complex_wildcards=no
187+ ])
188+ ], [
189+ AC_MSG_CHECKING([linker version script flag])
190+ AC_MSG_RESULT([disabled])
191+
192+ ax_check_vscript_flag=
193+ ax_check_vscript_complex_wildcards=no
194+ ])
195+
196+ AS_IF([test x$ax_check_vscript_flag != x], [
197+ VSCRIPT_LDFLAGS="-Wl,$ax_check_vscript_flag"
198+ AC_SUBST([VSCRIPT_LDFLAGS])
199+ ])
200+
201+ AM_CONDITIONAL([HAVE_VSCRIPT],
202+ [test x$ax_check_vscript_flag != x])
203+ AM_CONDITIONAL([HAVE_VSCRIPT_COMPLEX],
204+ [test x$ax_check_vscript_complex_wildcards = xyes])
205+
206+]) dnl AX_CHECK_VSCRIPT
207diff --git a/src/Makefile.am b/src/Makefile.am
208index 8384b92..e2c8e15 100644
209--- a/src/Makefile.am
210+++ b/src/Makefile.am
211@@ -65,8 +65,11 @@ libbsd_la_LIBADD = \
212 $(ARC4RANDOM_ATFORK_LIBS) \
213 $(nil)
214 libbsd_la_LDFLAGS = \
215- -Wl,--version-script=$(srcdir)/libbsd.map \
216 -version-number $(LIBBSD_ABI)
217+if HAVE_VSCRIPT
218+libbsd_la_LDFLAGS += $(VSCRIPT_LDFLAGS),@srcdir@/libbsd.map
219+endif
220+
221 libbsd_la_SOURCES = \
222 arc4random.c \
223 arc4random.h \
224diff --git a/src/fpurge.c b/src/fpurge.c
225index 462535a..a8941db 100644
226--- a/src/fpurge.c
227+++ b/src/fpurge.c
228@@ -26,9 +26,10 @@
229
230 #include <errno.h>
231 #include <stdio.h>
232-#include <stdio_ext.h>
233
234 #ifdef HAVE___FPURGE
235+#include <stdio_ext.h>
236+
237 int
238 fpurge(FILE *fp)
239 {
240@@ -41,6 +42,36 @@ fpurge(FILE *fp)
241
242 return 0;
243 }
244+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
245+#elif HAVE_FPURGE
246+int
247+fpurge(FILE *fp)
248+{
249+ if (fp == NULL || fileno(fp) < 0) {
250+ errno = EBADF;
251+ return EOF;
252+ }
253+
254+ /* Call the system's fpurge function. */
255+#undef fpurge
256+#if !HAVE_DECL_FPURGE
257+ extern int fpurge (FILE *);
258+#endif
259+ int result = fpurge (fp);
260+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
261+#if defined(__sferror) || defined(__DragonFly__)
262+ if (result == 0)
263+ /* Correct the invariants that fpurge broke.
264+ <stdio.h> on BSD systems says:
265+ "The following always hold: if _flags & __SRD, _w is 0."
266+ If this invariant is not fulfilled and the stream is read-write but
267+ currently reading, subsequent putc or fputc calls will write directly
268+ into the buffer, although they shouldn't be allowed to. */
269+ if ((fp->_flags & __SRD) != 0)
270+ fp->_w = 0;
271+#endif
272+ return result;
273+}
274 #else
275 #error "Function fpurge() needs to be ported."
276 #endif
277diff --git a/src/funopen.c b/src/funopen.c
278index 1e05c7e..65ba5a8 100644
279--- a/src/funopen.c
280+++ b/src/funopen.c
281@@ -143,6 +143,7 @@ funopen(const void *cookie,
282 * they will not add the needed support to implement it. Just ignore this
283 * interface there, as it has never been provided anyway.
284 */
285+#elif defined(__MACH__)
286 #else
287 #error "Function funopen() needs to be ported or disabled."
288 #endif
289diff --git a/src/hash/sha512.h b/src/hash/sha512.h
290index 4f368a1..7632e25 100644
291--- a/src/hash/sha512.h
292+++ b/src/hash/sha512.h
293@@ -29,7 +29,11 @@
294 #ifndef _SHA512_H_
295 #define _SHA512_H_
296
297+#ifdef __MACH__
298+#include <stdint.h>
299+#else
300 #include <sys/types.h>
301+#endif
302
303 #define SHA512_DIGEST_LENGTH 64
304
305diff --git a/src/hash/sha512c.c b/src/hash/sha512c.c
306index b3c8d5e..602f54e 100644
307--- a/src/hash/sha512c.c
308+++ b/src/hash/sha512c.c
309@@ -25,7 +25,12 @@
310 */
311
312 #include <sys/cdefs.h>
313+
314+#ifdef __MACH__
315+#include <machine/endian.h>
316+#else
317 #include <sys/endian.h>
318+#endif
319 #include <sys/types.h>
320
321 #include <string.h>
322diff --git a/src/local-link.h b/src/local-link.h
323index 0d4351a..fc520af 100644
324--- a/src/local-link.h
325+++ b/src/local-link.h
326@@ -27,6 +27,11 @@
327 #ifndef LIBBSD_LOCAL_LINK_H
328 #define LIBBSD_LOCAL_LINK_H
329
330+#ifdef __MACH__
331+#define libbsd_link_warning(symbol, msg)
332+#define libbsd_symver_default(alias, symbol, version)
333+#define libbsd_symver_variant(alias, symbol, version)
334+#else
335 #define libbsd_link_warning(symbol, msg) \
336 static const char libbsd_emit_link_warning_##symbol[] \
337 __attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg;
338@@ -45,3 +50,4 @@
339 #endif
340
341 #endif
342+#endif
343diff --git a/src/nlist.c b/src/nlist.c
344index 228c220..46c2d77 100644
345--- a/src/nlist.c
346+++ b/src/nlist.c
347@@ -41,6 +41,7 @@
348 #include <unistd.h>
349 #include <nlist.h>
350
351+#if !HAVE_NLIST_H
352 #include "local-elf.h"
353
354 #define SIZE_T_MAX 0xffffffffU
355@@ -280,3 +281,4 @@ nlist(const char *name, struct nlist *list)
356 (void)close(fd);
357 return (n);
358 }
359+#endif
360diff --git a/src/readpassphrase.c b/src/readpassphrase.c
361index f9f6195..2bc5fb4 100644
362--- a/src/readpassphrase.c
363+++ b/src/readpassphrase.c
364@@ -36,6 +36,14 @@
365 #define TCSASOFT 0
366 #endif
367
368+#ifndef _SIGMAX
369+#define _SIGMAX 64
370+#endif
371+
372+#ifndef _NSIG
373+#define _NSIG (_SIGMAX + 1)
374+#endif
375+
376 static volatile sig_atomic_t signo[_NSIG];
377
378 static void handler(int);
379diff --git a/src/setproctitle.c b/src/setproctitle.c
380index ff32aa3..51ed833 100644
381--- a/src/setproctitle.c
382+++ b/src/setproctitle.c
383@@ -33,6 +33,10 @@
384 #include <string.h>
385 #include "local-link.h"
386
387+#ifdef __MACH__
388+extern char **environ;
389+#endif
390+
391 static struct {
392 /* Original value. */
393 const char *arg0;
394@@ -287,7 +291,8 @@ libbsd_symver_default(setproctitle, setproctitle_impl, LIBBSD_0.5);
395 * in 0.5, make the implementation available in the old version as an alias
396 * for code linking against that version, and change the default to use the
397 * new version, so that new code depends on the implemented version. */
398-#ifdef HAVE_TYPEOF
399+#ifdef __MACH__
400+#elif defined(HAVE_TYPEOF)
401 extern __typeof__(setproctitle_impl)
402 setproctitle_stub
403 __attribute__((__alias__("setproctitle_impl")));
404diff --git a/src/strlcat.c b/src/strlcat.c
405index 14c53a1..5961c17 100644
406--- a/src/strlcat.c
407+++ b/src/strlcat.c
408@@ -26,6 +26,7 @@
409 * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
410 * If retval >= dsize, truncation occurred.
411 */
412+#if !HAVE_STRLCAT
413 size_t
414 strlcat(char *dst, const char *src, size_t dsize)
415 {
416@@ -53,3 +54,4 @@ strlcat(char *dst, const char *src, size_t dsize)
417
418 return(dlen + (src - osrc)); /* count does not include NUL */
419 }
420+#endif
421diff --git a/src/strlcpy.c b/src/strlcpy.c
422index e9a7fe4..5137acb 100644
423--- a/src/strlcpy.c
424+++ b/src/strlcpy.c
425@@ -24,6 +24,7 @@
426 * chars will be copied. Always NUL terminates (unless dsize == 0).
427 * Returns strlen(src); if retval >= dsize, truncation occurred.
428 */
429+#if !HAVE_STRLCPY
430 size_t
431 strlcpy(char *dst, const char *src, size_t dsize)
432 {
433@@ -48,3 +49,4 @@ strlcpy(char *dst, const char *src, size_t dsize)
434
435 return(src - osrc - 1); /* count does not include NUL */
436 }
437+#endif
438diff --git a/src/strmode.c b/src/strmode.c
439index e6afde5..da680c9 100644
440--- a/src/strmode.c
441+++ b/src/strmode.c
442@@ -32,6 +32,7 @@
443 #include <sys/stat.h>
444 #include <string.h>
445
446+#if !HAVE_STRMODE
447 void
448 strmode(mode_t mode, char *p)
449 {
450@@ -141,3 +142,4 @@ strmode(mode_t mode, char *p)
451 *p++ = ' '; /* will be a '+' if ACL's implemented */
452 *p = '\0';
453 }
454+#endif