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