1diff --git a/configure.ac b/configure.ac
2index 88ccd91..0857782 100644
3--- a/configure.ac
4+++ b/configure.ac
5@@ -59,7 +59,7 @@ AS_CASE([$host_os],
6 )
7
8 # Checks for header files.
9-AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h])
10+AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h stdio_ext.h])
11
12 # Checks for typedefs, structures, and compiler characteristics.
13 AC_C_INLINE
14@@ -146,6 +146,31 @@ AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
15 pstat_getproc sysconf])
16 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
17
18+HostOS=`echo "$host" | sed 's/.*-//'`
19+os_is_macosx=false
20+nonLinuxOS=false
21+AC_SUBST(HostOS)
22+case ${HostOS} in
23+ darwin* | powerpc*-*-darwin* | freebsd* | netbsd* | openbsd*)
24+ os_is_macosx=true
25+ nonLinuxOS=true
26+ echo HostOS="$HostOS"
27+ ;;
28+ *)
29+ echo host="$host"
30+ echo HostOS="$HostOS"
31+ os_is_macosx=false
32+ nonLinuxOS=false
33+ ;;
34+esac
35+AM_CONDITIONAL([IS_DARWIN], [test x$os_is_macosx = xtrue])
36+AM_COND_IF([IS_DARWIN],
37+ [AC_DEFINE([IS_DARWIN], [1], [Get HostOS Type is Darwin])])
38+
39+AM_CONDITIONAL([NON_LINUX], [test x$userdefine_gethostbyname_r = xtrue])
40+AM_COND_IF([NON_LINUX],
41+ [AC_DEFINE([NON_LINUX], [1], [Get HostOS Type])])
42+
43 AC_CONFIG_FILES([
44 Makefile
45 include/Makefile
46diff --git a/include/bsd/libutil.h b/include/bsd/libutil.h
47index 45b3b15..d0d4043 100644
48--- a/include/bsd/libutil.h
49+++ b/include/bsd/libutil.h
50@@ -39,7 +39,9 @@
51 #ifndef LIBBSD_LIBUTIL_H
52 #define LIBBSD_LIBUTIL_H
53
54+#ifdef HAVE_FEATURES_H
55 #include <features.h>
56+#endif
57 #include <sys/cdefs.h>
58 #include <sys/types.h>
59 #include <stdint.h>
60diff --git a/include/bsd/stdio.h b/include/bsd/stdio.h
61index 4b69983..c75151b 100644
62--- a/include/bsd/stdio.h
63+++ b/include/bsd/stdio.h
64@@ -48,12 +48,16 @@
65 __BEGIN_DECLS
66 const char *fmtcheck(const char *, const char *);
67
68+#if !defined(darwin) && !defined(__APPLE__) && !defined(MACOSX)
69 /* XXX: The function requires cooperation from the system libc to store the
70 * line buffer in the FILE struct itself. */
71 char *fgetln(FILE *fp, size_t *lenp)
72- LIBBSD_DEPRECATED("This functions cannot be safely ported, "
73- "use getline(3) instead, as it is supported "
74- "by GNU and POSIX.1-2008.");
75+ __attribute__((deprecated("This functions cannot be safely ported, "
76+ "use getline(3) instead, as it is supported "
77+ "by GNU and POSIX.1-2008.")));
78+#else
79+char *fgetln(FILE *fp, size_t *lenp);
80+#endif
81
82 /*
83 * Note: We diverge from the FreeBSD, OpenBSD and DragonFlyBSD declarations,
84diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
85index ebc9638..6cd7943 100644
86--- a/include/bsd/stdlib.h
87+++ b/include/bsd/stdlib.h
88@@ -67,9 +67,11 @@ int sradixsort(const unsigned char **base, int nmemb,
89 const unsigned char *table, unsigned endbyte);
90
91 void *reallocf(void *ptr, size_t size);
92-#if defined(_GNU_SOURCE) && defined(__GLIBC__) && !__GLIBC_PREREQ(2, 26)
93+#if defined(_GNU_SOURCE) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
94+#if !__GLIBC_PREREQ(2, 26)
95 void *reallocarray(void *ptr, size_t nmemb, size_t size);
96 #endif
97+#endif
98
99 long long strtonum(const char *nptr, long long minval, long long maxval,
100 const char **errstr);
101diff --git a/include/bsd/string.h b/include/bsd/string.h
102index 6798bf6..6baaa14 100644
103--- a/include/bsd/string.h
104+++ b/include/bsd/string.h
105@@ -37,6 +37,12 @@
106 #include <sys/types.h>
107
108 __BEGIN_DECLS
109+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
110+size_t bsd_strlcpy(char *dst, const char *src, size_t siz);
111+size_t bsd_strlcat(char *dst, const char *src, size_t siz);
112+char *bsd_strnstr(const char *str, const char *find, size_t str_len);
113+void bsd_strmode(mode_t mode, char *str);
114+#else
115 size_t strlcpy(char *dst, const char *src, size_t siz);
116 size_t strlcat(char *dst, const char *src, size_t siz);
117 char *strnstr(const char *str, const char *find, size_t str_len);
118@@ -45,6 +51,7 @@ void strmode(mode_t mode, char *str);
119 #if defined(_GNU_SOURCE) && defined(__GLIBC__) && !__GLIBC_PREREQ(2, 25)
120 void explicit_bzero(void *buf, size_t len);
121 #endif
122+#endif
123 __END_DECLS
124
125 #endif
126diff --git a/src/Makefile.am b/src/Makefile.am
127index ad83dbf..0f2a7ee 100644
128--- a/src/Makefile.am
129+++ b/src/Makefile.am
130@@ -54,17 +54,21 @@ libbsd_la_DEPENDENCIES = \
131 libbsd.map
132 libbsd_la_LIBADD = \
133 $(CLOCK_GETTIME_LIBS)
134+
135+if IS_DARWIN
136+libbsd_la_LDFLAGS = \
137+ -Wl \
138+ -version-number $(LIBBSD_ABI)
139+else
140 libbsd_la_LDFLAGS = \
141 -Wl,--version-script=$(srcdir)/libbsd.map \
142 -version-number $(LIBBSD_ABI)
143+endif
144+
145 libbsd_la_SOURCES = \
146 arc4random.c \
147- arc4random.h \
148- arc4random_unix.h \
149- arc4random_openbsd.h \
150 arc4random_uniform.c \
151 bsd_getopt.c \
152- chacha_private.h \
153 closefrom.c \
154 dehumanize_number.c \
155 err.c \
156@@ -117,6 +121,15 @@ libbsd_la_SOURCES += \
157 $(nil)
158 endif
159
160+noinst_HEADERS = \
161+ arc4random.h \
162+ arc4random_bsd.h \
163+ arc4random_linux.h \
164+ arc4random_unix.h \
165+ arc4random_osx.h \
166+ arc4random_openbsd.h \
167+ chacha_private.h
168+
169 libbsd_ctor_a_SOURCES = \
170 setproctitle_ctor.c \
171 $(nil)
172diff --git a/src/arc4random_bsd.h b/src/arc4random_bsd.h
173new file mode 100644
174index 0000000..ece2f85
175--- /dev/null
176+++ b/src/arc4random_bsd.h
177@@ -0,0 +1,86 @@
178+/* $OpenBSD: arc4random_freebsd.h,v 1.2 2015/01/15 06:57:18 deraadt Exp $ */
179+
180+/*
181+ * Copyright (c) 1996, David Mazieres <dm@uun.org>
182+ * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
183+ * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
184+ * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
185+ *
186+ * Permission to use, copy, modify, and distribute this software for any
187+ * purpose with or without fee is hereby granted, provided that the above
188+ * copyright notice and this permission notice appear in all copies.
189+ *
190+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
191+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
192+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
193+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
194+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
195+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
196+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
197+ */
198+
199+/*
200+ * Stub functions for portability.
201+ */
202+
203+#include <sys/mman.h>
204+
205+#include <pthread.h>
206+#include <signal.h>
207+
208+static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
209+#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
210+#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
211+
212+/*
213+ * Unfortunately, pthread_atfork() is broken on FreeBSD (at least 9 and 10) if
214+ * a program does not link to -lthr. Callbacks registered with pthread_atfork()
215+ * appear to fail silently. So, it is not always possible to detect a PID
216+ * wraparound.
217+ */
218+#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
219+
220+static inline void
221+_getentropy_fail(void)
222+{
223+ raise(SIGKILL);
224+}
225+
226+static volatile sig_atomic_t _rs_forked;
227+
228+static inline void
229+_rs_forkhandler(void)
230+{
231+ _rs_forked = 1;
232+}
233+
234+static inline void
235+_rs_forkdetect(void)
236+{
237+ static pid_t _rs_pid = 0;
238+ pid_t pid = getpid();
239+
240+ if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
241+ _rs_pid = pid;
242+ _rs_forked = 0;
243+ if (rs)
244+ memset(rs, 0, sizeof(*rs));
245+ }
246+}
247+
248+static inline int
249+_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
250+{
251+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
252+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
253+ return (-1);
254+
255+ if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
256+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
257+ munmap(*rsp, sizeof(**rsp));
258+ return (-1);
259+ }
260+
261+ _ARC4_ATFORK(_rs_forkhandler);
262+ return (0);
263+}
264diff --git a/src/arc4random_linux.h b/src/arc4random_linux.h
265new file mode 100644
266index 0000000..d61a8db
267--- /dev/null
268+++ b/src/arc4random_linux.h
269@@ -0,0 +1,86 @@
270+/* $OpenBSD: arc4random_linux.h,v 1.8 2014/08/13 06:04:10 deraadt Exp $ */
271+
272+/*
273+ * Copyright (c) 1996, David Mazieres <dm@uun.org>
274+ * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
275+ * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
276+ * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
277+ *
278+ * Permission to use, copy, modify, and distribute this software for any
279+ * purpose with or without fee is hereby granted, provided that the above
280+ * copyright notice and this permission notice appear in all copies.
281+ *
282+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
283+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
284+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
285+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
286+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
287+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
288+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
289+ */
290+
291+/*
292+ * Stub functions for portability.
293+ */
294+
295+#include <sys/mman.h>
296+
297+#include <pthread.h>
298+#include <signal.h>
299+
300+static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
301+#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
302+#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
303+
304+#ifdef __GLIBC__
305+extern void *__dso_handle;
306+extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
307+#define _ARC4_ATFORK(f) __register_atfork(NULL, NULL, (f), __dso_handle)
308+#else
309+#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
310+#endif
311+
312+static inline void
313+_getentropy_fail(void)
314+{
315+ raise(SIGKILL);
316+}
317+
318+static volatile sig_atomic_t _rs_forked;
319+
320+static inline void
321+_rs_forkhandler(void)
322+{
323+ _rs_forked = 1;
324+}
325+
326+static inline void
327+_rs_forkdetect(void)
328+{
329+ static pid_t _rs_pid = 0;
330+ pid_t pid = getpid();
331+
332+ if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
333+ _rs_pid = pid;
334+ _rs_forked = 0;
335+ if (rs)
336+ memset(rs, 0, sizeof(*rs));
337+ }
338+}
339+
340+static inline int
341+_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
342+{
343+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
344+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
345+ return (-1);
346+
347+ if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
348+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
349+ munmap(*rsp, sizeof(**rsp));
350+ return (-1);
351+ }
352+
353+ _ARC4_ATFORK(_rs_forkhandler);
354+ return (0);
355+}
356diff --git a/src/arc4random_osx.h b/src/arc4random_osx.h
357new file mode 100644
358index 0000000..14771a6
359--- /dev/null
360+++ b/src/arc4random_osx.h
361@@ -0,0 +1,82 @@
362+/* $OpenBSD: arc4random_osx.h,v 1.10 2015/09/11 11:52:55 deraadt Exp $ */
363+
364+/*
365+ * Copyright (c) 1996, David Mazieres <dm@uun.org>
366+ * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
367+ * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
368+ * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
369+ *
370+ * Permission to use, copy, modify, and distribute this software for any
371+ * purpose with or without fee is hereby granted, provided that the above
372+ * copyright notice and this permission notice appear in all copies.
373+ *
374+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
375+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
376+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
377+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
378+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
379+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
380+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
381+ */
382+
383+/*
384+ * Stub functions for portability.
385+ */
386+
387+#include <sys/mman.h>
388+
389+#include <unistd.h>
390+#include <pthread.h>
391+#include <signal.h>
392+
393+static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
394+#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
395+#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
396+
397+#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
398+
399+static inline void
400+_getentropy_fail(void)
401+{
402+ raise(SIGKILL);
403+}
404+
405+static volatile sig_atomic_t _rs_forked;
406+
407+static inline void
408+_rs_forkhandler(void)
409+{
410+ _rs_forked = 1;
411+}
412+
413+static inline void
414+_rs_forkdetect(void)
415+{
416+ static pid_t _rs_pid = 0;
417+ pid_t pid = getpid();
418+
419+ if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
420+ _rs_pid = pid;
421+ _rs_forked = 0;
422+ if (rs)
423+ memset(rs, 0, sizeof(*rs));
424+ }
425+}
426+
427+static inline int
428+_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
429+{
430+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
431+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
432+ return (-1);
433+
434+ if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
435+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
436+ munmap(*rsp, sizeof(**rsp));
437+ *rsp = NULL;
438+ return (-1);
439+ }
440+
441+ _ARC4_ATFORK(_rs_forkhandler);
442+ return (0);
443+}
444diff --git a/src/fgetln.c b/src/fgetln.c
445index 4d1726e..9c73788 100644
446--- a/src/fgetln.c
447+++ b/src/fgetln.c
448@@ -30,7 +30,9 @@
449 #include <sys/types.h>
450 #include <string.h>
451
452+#if !defined(darwin) && !defined(__APPLE__) && !defined(MACOSX)
453 #include "local-link.h"
454+#endif
455
456 #ifdef HAVE_GETLINE
457 struct filebuf {
458@@ -75,9 +77,11 @@ fgetln(FILE *stream, size_t *len)
459 return fb->buf;
460 }
461 }
462+#if !defined(darwin) && !defined(__APPLE__) && !defined(MACOSX)
463 libbsd_link_warning(fgetln,
464 "This functions cannot be safely ported, use getline(3) "
465 "instead, as it is supported by GNU and POSIX.1-2008.")
466+#endif
467 #else
468 #error "Function fgetln() needs to be ported."
469 #endif
470diff --git a/src/fpurge.c b/src/fpurge.c
471index 462535a..e7eb46f 100644
472--- a/src/fpurge.c
473+++ b/src/fpurge.c
474@@ -26,9 +26,11 @@
475
476 #include <errno.h>
477 #include <stdio.h>
478+#if HAVE___FPURGE
479 #include <stdio_ext.h>
480+#endif
481
482-#ifdef HAVE___FPURGE
483+#ifdef HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7 */
484 int
485 fpurge(FILE *fp)
486 {
487@@ -42,5 +44,55 @@ fpurge(FILE *fp)
488 return 0;
489 }
490 #else
491-#error "Function fpurge() needs to be ported."
492+#define fp_ fp
493+//#error "Function fpurge() needs to be ported."
494+//#elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
495+int
496+fpurge(FILE *fp)
497+{
498+ if (fp == NULL || fileno(fp) < 0) {
499+ errno = EBADF;
500+ return EOF;
501+ }
502+
503+ /* Call the system's fpurge function. */
504+# undef fpurge
505+# if !HAVE_DECL_FPURGE
506+ extern int fpurge (FILE *);
507+# endif
508+ int result = fpurge (fp);
509+# if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
510+ if (result == 0)
511+ /* Correct the invariants that fpurge broke.
512+ <stdio.h> on BSD systems says:
513+ "The following always hold: if _flags & __SRD, _w is 0."
514+ If this invariant is not fulfilled and the stream is read-write but
515+ currently reading, subsequent putc or fputc calls will write directly
516+ into the buffer, although they shouldn't be allowed to. */
517+ if ((fp_->_flags & __SRD) != 0)
518+ fp_->_w = 0;
519+#endif
520+ return result;
521+}
522+//#endif
523+#endif
524+
525+#ifdef TEST
526+int
527+main()
528+{
529+ static FILE fp_bad;
530+ FILE *fp;
531+
532+ if (fpurge(&fp_bad) == 0)
533+ return 1;
534+
535+ fp = fopen("/dev/zero", "r");
536+ if (fpurge(fp) < 0)
537+ return 1;
538+
539+ fclose(fp);
540+
541+ return 0;
542+}
543 #endif
544diff --git a/src/funopen.c b/src/funopen.c
545index 1e05c7e..75e61ea 100644
546--- a/src/funopen.c
547+++ b/src/funopen.c
548@@ -143,6 +143,7 @@ funopen(const void *cookie,
549 * they will not add the needed support to implement it. Just ignore this
550 * interface there, as it has never been provided anyway.
551 */
552+#elif defined(darwin) || defined(__APPLE__) || defined(MACOSX)
553 #else
554 #error "Function funopen() needs to be ported or disabled."
555 #endif
556diff --git a/src/getentropy.c b/src/getentropy.c
557index 3f11a1e..8a23a07 100644
558--- a/src/getentropy.c
559+++ b/src/getentropy.c
560@@ -28,9 +28,7 @@
561 #include "getentropy_linux.c"
562 #elif defined(__GNU__)
563 #include "getentropy_hurd.c"
564-#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
565-#include "getentropy_bsd.c"
566-#elif defined(__NetBSD__)
567+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
568 #include "getentropy_bsd.c"
569 #elif defined(__sun)
570 #include "getentropy_solaris.c"
571diff --git a/src/hash/sha512.h b/src/hash/sha512.h
572index 4f368a1..ab22fc1 100644
573--- a/src/hash/sha512.h
574+++ b/src/hash/sha512.h
575@@ -29,7 +29,11 @@
576 #ifndef _SHA512_H_
577 #define _SHA512_H_
578
579+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
580+#include <stdint.h>
581+#else
582 #include <sys/types.h>
583+#endif
584
585 #define SHA512_DIGEST_LENGTH 64
586
587diff --git a/src/hash/sha512c.c b/src/hash/sha512c.c
588index b3c8d5e..f69013d 100644
589--- a/src/hash/sha512c.c
590+++ b/src/hash/sha512c.c
591@@ -25,7 +25,13 @@
592 */
593
594 #include <sys/cdefs.h>
595+__FBSDID("$FreeBSD$");
596+
597+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
598+#include <machine/endian.h>
599+#else
600 #include <sys/endian.h>
601+#endif
602 #include <sys/types.h>
603
604 #include <string.h>
605diff --git a/src/nlist.c b/src/nlist.c
606index 0932f59..598a329 100644
607--- a/src/nlist.c
608+++ b/src/nlist.c
609@@ -27,6 +27,10 @@
610 * SUCH DAMAGE.
611 */
612
613+#if !defined(darwin) && !defined(__APPLE__) && !defined(MACOSX)
614+#if defined(LIBC_SCCS) && !defined(lint)
615+static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
616+#endif /* LIBC_SCCS and not lint */
617 #include <sys/cdefs.h>
618
619 #include <sys/param.h>
620@@ -265,3 +269,4 @@ nlist(const char *name, struct nlist *list)
621 (void)close(fd);
622 return (n);
623 }
624+#endif /* _NLIST_DO_ELF */
625diff --git a/src/setproctitle.c b/src/setproctitle.c
626index 038ac7d..d0ef01b 100644
627--- a/src/setproctitle.c
628+++ b/src/setproctitle.c
629@@ -32,6 +32,11 @@
630 #include <unistd.h>
631 #include <string.h>
632
633+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
634+#define __asm__(x)
635+extern char **environ;
636+#endif
637+
638 static struct {
639 /* Original value. */
640 const char *arg0;
641@@ -287,7 +292,14 @@ __asm__(".symver setproctitle_impl,setproctitle@@LIBBSD_0.5");
642 * for code linking against that version, and change the default to use the
643 * new version, so that new code depends on the implemented version. */
644 #ifdef HAVE_TYPEOF
645+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
646+//
647+// HACK: even weak aliasing breaks in clang so just comment this out for now
648+//
649+// extern typeof(setproctitle_impl) setproctitle_stub __attribute__((weak, alias("setproctitle_impl")));
650+#else
651 extern typeof(setproctitle_impl) setproctitle_stub __attribute__((alias("setproctitle_impl")));
652+#endif
653 #else
654 void setproctitle_stub(const char *fmt, ...)
655 __attribute__((alias("setproctitle_impl")));
656diff --git a/src/strlcat.c b/src/strlcat.c
657index 14c53a1..e01cb60 100644
658--- a/src/strlcat.c
659+++ b/src/strlcat.c
660@@ -27,7 +27,11 @@
661 * If retval >= dsize, truncation occurred.
662 */
663 size_t
664+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
665+bsd_strlcat(char *dst, const char *src, size_t dsize)
666+#else
667 strlcat(char *dst, const char *src, size_t dsize)
668+#endif
669 {
670 const char *odst = dst;
671 const char *osrc = src;
672diff --git a/src/strlcpy.c b/src/strlcpy.c
673index e9a7fe4..10a855f 100644
674--- a/src/strlcpy.c
675+++ b/src/strlcpy.c
676@@ -25,7 +25,11 @@
677 * Returns strlen(src); if retval >= dsize, truncation occurred.
678 */
679 size_t
680+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
681+bsd_strlcpy(char *dst, const char *src, size_t dsize)
682+#else
683 strlcpy(char *dst, const char *src, size_t dsize)
684+#endif
685 {
686 const char *osrc = src;
687 size_t nleft = dsize;
688diff --git a/src/strmode.c b/src/strmode.c
689index e6afde5..c463243 100644
690--- a/src/strmode.c
691+++ b/src/strmode.c
692@@ -33,7 +33,11 @@
693 #include <string.h>
694
695 void
696+#if defined(darwin) || defined(__APPLE__) || defined(MACOSX)
697+bsd_strmode(mode_t mode, char *p)
698+#else
699 strmode(mode_t mode, char *p)
700+#endif
701 {
702 /* print type */
703 switch (mode & S_IFMT) {