1diff --git a/configure.ac b/configure.ac
2index 5b6d22b..98c449b 100644
3--- a/configure.ac
4+++ b/configure.ac
5@@ -145,7 +145,7 @@ AS_CASE([$host_os],
6 AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
7
8 # Checks for header files.
9-AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h pwd.h grp.h])
10+AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h pwd.h grp.h nlist.h])
11
12 # Checks for typedefs, structures, and compiler characteristics.
13 AC_C_INLINE
14@@ -245,7 +245,9 @@ AC_LINK_IFELSE(
15
16 AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
17 getauxval getentropy getexecname getline \
18- pstat_getproc sysconf])
19+ pstat_getproc sysconf \
20+ strlcpy strlcat strnstr strmode fpurge \
21+ user_from_uid group_from_gid])
22 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
23
24 AC_SUBST([MD5_LIBS])
25diff --git a/include/bsd/grp.h b/include/bsd/grp.h
26index b2705e5..c9423a2 100644
27--- a/include/bsd/grp.h
28+++ b/include/bsd/grp.h
29@@ -44,8 +44,10 @@
30 __BEGIN_DECLS
31 int
32 gid_from_group(const char *, gid_t *);
33+#if !HAVE_GROUP_FROM_GID
34 const char *
35 group_from_gid(gid_t, int);
36+#endif
37 __END_DECLS
38
39 #endif
40diff --git a/include/bsd/pwd.h b/include/bsd/pwd.h
41index 798af4b..6ae5244 100644
42--- a/include/bsd/pwd.h
43+++ b/include/bsd/pwd.h
44@@ -44,8 +44,10 @@
45 __BEGIN_DECLS
46 int
47 uid_from_user(const char *, uid_t *);
48+#if !HAVE_USER_FROM_UID
49 const char *
50 user_from_uid(uid_t, int);
51+#endif
52 __END_DECLS
53
54 #endif
55diff --git a/include/bsd/string.h b/include/bsd/string.h
56index f987fee..a1e17ed 100644
57--- a/include/bsd/string.h
58+++ b/include/bsd/string.h
59@@ -41,10 +41,21 @@
60 #include <sys/types.h>
61
62 __BEGIN_DECLS
63+#if !HAVE_STRLCPY
64 size_t strlcpy(char *dst, const char *src, size_t siz);
65+#endif
66+
67+#if !HAVE_STRLCAT
68 size_t strlcat(char *dst, const char *src, size_t siz);
69+#endif
70+
71+#if !HAVE_STRNSTR
72 char *strnstr(const char *str, const char *find, size_t str_len);
73+#endif
74+
75+#if !HAVE_STRMODE
76 void strmode(mode_t mode, char *str);
77+#endif
78
79 #if !defined(__GLIBC__) || \
80 (defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 25) || !defined(_GNU_SOURCE)))
81diff --git a/src/fpurge.c b/src/fpurge.c
82index 350f364..ff7f01e 100644
83--- a/src/fpurge.c
84+++ b/src/fpurge.c
85@@ -26,9 +26,10 @@
86
87 #include <errno.h>
88 #include <stdio.h>
89-#include <stdio_ext.h>
90
91 #ifdef HAVE___FPURGE
92+#include <stdio_ext.h>
93+
94 int
95 fpurge(FILE *fp)
96 {
97@@ -41,6 +42,36 @@ fpurge(FILE *fp)
98
99 return 0;
100 }
101+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
102+#elif HAVE_FPURGE
103+int
104+fpurge(FILE *fp)
105+{
106+ if (fp == NULL || fileno(fp) < 0) {
107+ errno = EBADF;
108+ return EOF;
109+ }
110+
111+ /* Call the system's fpurge function. */
112+#undef fpurge
113+#if !HAVE_DECL_FPURGE
114+ extern int fpurge (FILE *);
115+#endif
116+ int result = fpurge (fp);
117+/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
118+#if defined(__sferror) || defined(__DragonFly__)
119+ if (result == 0)
120+ /* Correct the invariants that fpurge broke.
121+ <stdio.h> on BSD systems says:
122+ "The following always hold: if _flags & __SRD, _w is 0."
123+ If this invariant is not fulfilled and the stream is read-write but
124+ currently reading, subsequent putc or fputc calls will write directly
125+ into the buffer, although they shouldn't be allowed to. */
126+ if ((fp->_flags & __SRD) != 0)
127+ fp->_w = 0;
128+#endif
129+ return result;
130+}
131 #else
132 #error "Function fpurge() needs to be ported."
133 #endif
134diff --git a/src/funopen.c b/src/funopen.c
135index 1e6f43a..3a3af6a 100644
136--- a/src/funopen.c
137+++ b/src/funopen.c
138@@ -143,6 +143,7 @@ funopen(const void *cookie,
139 * they will not add the needed support to implement it. Just ignore this
140 * interface there, as it has never been provided anyway.
141 */
142+#elif defined(__MACH__)
143 #else
144 #error "Function funopen() needs to be ported or disabled."
145 #endif
146diff --git a/src/local-link.h b/src/local-link.h
147index 6782d9a..fb76098 100644
148--- a/src/local-link.h
149+++ b/src/local-link.h
150@@ -29,6 +29,12 @@
151
152 #include <sys/cdefs.h>
153
154+#ifdef __MACH__
155+#define libbsd_link_warning(symbol, msg)
156+#define libbsd_symver_default(alias, symbol, version)
157+#define libbsd_symver_variant(alias, symbol, version)
158+#define libbsd_symver_weak(alias, symbol, version)
159+#else
160 #define libbsd_link_warning(symbol, msg) \
161 static const char libbsd_emit_link_warning_##symbol[] \
162 __attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg
163@@ -68,3 +74,4 @@
164 #endif
165
166 #endif
167+#endif
168diff --git a/src/nlist.c b/src/nlist.c
169index 1cb9d18..b476f1e 100644
170--- a/src/nlist.c
171+++ b/src/nlist.c
172@@ -41,6 +41,7 @@
173 #include <unistd.h>
174 #include <nlist.h>
175
176+#if !HAVE_NLIST_H
177 #include "local-elf.h"
178
179 /* Note: This function is used by libkvm0, so we need to export it.
180@@ -277,3 +278,4 @@ nlist(const char *name, struct nlist *list)
181 (void)close(fd);
182 return (n);
183 }
184+#endif
185diff --git a/src/pwcache.c b/src/pwcache.c
186index d54daa0..74fde9f 100644
187--- a/src/pwcache.c
188+++ b/src/pwcache.c
189@@ -191,6 +191,7 @@ grptb_start(void)
190 return 0;
191 }
192
193+#if !HAVE_USER_FROM_UID
194 /*
195 * user_from_uid()
196 * caches the name (if any) for the uid. If noname clear, we always
197@@ -251,7 +252,9 @@ user_from_uid(uid_t uid, int noname)
198 }
199 return ptr->name;
200 }
201+#endif
202
203+#if !HAVE_USER_FROM_UID
204 /*
205 * group_from_gid()
206 * caches the name (if any) for the gid. If noname clear, we always
207@@ -312,6 +315,7 @@ group_from_gid(gid_t gid, int noname)
208 }
209 return ptr->name;
210 }
211+#endif
212
213 /*
214 * uid_from_user()
215diff --git a/src/readpassphrase.c b/src/readpassphrase.c
216index f9f6195..2bc5fb4 100644
217--- a/src/readpassphrase.c
218+++ b/src/readpassphrase.c
219@@ -36,6 +36,14 @@
220 #define TCSASOFT 0
221 #endif
222
223+#ifndef _SIGMAX
224+#define _SIGMAX 64
225+#endif
226+
227+#ifndef _NSIG
228+#define _NSIG (_SIGMAX + 1)
229+#endif
230+
231 static volatile sig_atomic_t signo[_NSIG];
232
233 static void handler(int);
234diff --git a/src/setproctitle.c b/src/setproctitle.c
235index d3e1087..0e5f64c 100644
236--- a/src/setproctitle.c
237+++ b/src/setproctitle.c
238@@ -33,6 +33,10 @@
239 #include <string.h>
240 #include "local-link.h"
241
242+#ifdef __MACH__
243+extern char **environ;
244+#endif
245+
246 static struct {
247 /* Original value. */
248 const char *arg0;
249@@ -291,7 +295,8 @@ libbsd_symver_default(setproctitle, setproctitle_impl, LIBBSD_0.5);
250 * in 0.5, make the implementation available in the old version as an alias
251 * for code linking against that version, and change the default to use the
252 * new version, so that new code depends on the implemented version. */
253-#ifdef HAVE_TYPEOF
254+#ifdef __MACH__
255+#elif defined(HAVE_TYPEOF)
256 extern __typeof__(setproctitle_impl)
257 setproctitle_stub
258 __attribute__((__alias__("setproctitle_impl")));
259diff --git a/src/strlcat.c b/src/strlcat.c
260index 14c53a1..5961c17 100644
261--- a/src/strlcat.c
262+++ b/src/strlcat.c
263@@ -26,6 +26,7 @@
264 * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
265 * If retval >= dsize, truncation occurred.
266 */
267+#if !HAVE_STRLCAT
268 size_t
269 strlcat(char *dst, const char *src, size_t dsize)
270 {
271@@ -53,3 +54,4 @@ strlcat(char *dst, const char *src, size_t dsize)
272
273 return(dlen + (src - osrc)); /* count does not include NUL */
274 }
275+#endif
276diff --git a/src/strlcpy.c b/src/strlcpy.c
277index e9a7fe4..5137acb 100644
278--- a/src/strlcpy.c
279+++ b/src/strlcpy.c
280@@ -24,6 +24,7 @@
281 * chars will be copied. Always NUL terminates (unless dsize == 0).
282 * Returns strlen(src); if retval >= dsize, truncation occurred.
283 */
284+#if !HAVE_STRLCPY
285 size_t
286 strlcpy(char *dst, const char *src, size_t dsize)
287 {
288@@ -48,3 +49,4 @@ strlcpy(char *dst, const char *src, size_t dsize)
289
290 return(src - osrc - 1); /* count does not include NUL */
291 }
292+#endif
293diff --git a/src/strmode.c b/src/strmode.c
294index e6afde5..da680c9 100644
295--- a/src/strmode.c
296+++ b/src/strmode.c
297@@ -32,6 +32,7 @@
298 #include <sys/stat.h>
299 #include <string.h>
300
301+#if !HAVE_STRMODE
302 void
303 strmode(mode_t mode, char *p)
304 {
305@@ -141,3 +142,4 @@ strmode(mode_t mode, char *p)
306 *p++ = ' '; /* will be a '+' if ACL's implemented */
307 *p = '\0';
308 }
309+#endif