diff --git a/include/libbb.h b/include/libbb.h
index 5551031..28896ae 100644
--- include/libbb.h
+++ include/libbb.h
@@ -38,6 +38,12 @@
 #include <time.h>
 #include <unistd.h>
 #include <sys/param.h>
+#ifndef HAVE_CLEARENV
+# define clearenv() environ[0] == NULL
+#endif
+#ifndef HAVE_FDATASYNC
+# define fdatasync fsync
+#endif
 #ifdef HAVE_MNTENT_H
 # include <mntent.h>
 #endif
diff --git a/include/platform.h b/include/platform.h
index bbbc0a9..2bc88c3 100644
--- include/platform.h
+++ include/platform.h
@@ -10,9 +10,12 @@
 /* Assume all these functions exist by default.  Platforms where it is not
  * true will #undef them below.
  */
+#define HAVE_CLEARENV 1
+#define HAVE_FDATASYNC 1
 #define HAVE_FDPRINTF 1
 #define HAVE_MEMRCHR 1
 #define HAVE_MKDTEMP 1
+#define HAVE_PTSNAME_R 1
 #define HAVE_SETBIT 1
 #define HAVE_SIGHANDLER_T 1
 #define HAVE_STRCASESTR 1
@@ -253,7 +256,10 @@ typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
 #if (defined __digital__ && defined __unix__) \
  || defined __APPLE__ \
  || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
+# undef HAVE_CLEARENV
+# undef HAVE_FDATASYNC
 # undef HAVE_MNTENT_H
+# undef HAVE_PTSNAME_R
 # undef HAVE_SYS_STATFS_H
 # undef HAVE_SIGHANDLER_T
 #else
diff --git a/libbb/getpty.c b/libbb/getpty.c
index ea653b0..6a15cff 100644
--- libbb/getpty.c
+++ libbb/getpty.c
@@ -19,7 +19,7 @@ int FAST_FUNC xgetpty(char *line)
 	if (p > 0) {
 		grantpt(p); /* chmod+chown corresponding slave pty */
 		unlockpt(p); /* (what does this do?) */
-#if 0 /* if ptsname_r is not available... */
+#ifndef HAVE_PTSNAME_R
 		const char *name;
 		name = ptsname(p); /* find out the name of slave pty */
 		if (!name) {
diff --git a/include/platform.h b/include/platform.h
index 2666eeb..6328ff6 100644
--- include/platform.h
+++ include/platform.h
@@ -14,6 +14,7 @@
 #define HAVE_MEMRCHR 1
 #define HAVE_MKDTEMP 1
 #define HAVE_SETBIT 1
+#define HAVE_SIGHANDLER_T 1
 #define HAVE_STRCASESTR 1
 #define HAVE_STRCHRNUL 1
 #define HAVE_STRSEP 1
@@ -253,6 +254,7 @@ typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
  || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
 # undef HAVE_MNTENT_H
 # undef HAVE_SYS_STATFS_H
+# undef HAVE_SIGHANDLER_T
 #else
 # define HAVE_MNTENT_H 1
 # define HAVE_SYS_STATFS_H 1
@@ -396,6 +398,10 @@ extern char *mkdtemp(char *template) FAST_FUNC;
 # define clrbit(a, b)  ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
 #endif
 
+#ifndef HAVE_SIGHANDLER_T
+typedef void (*sighandler_t)(int);
+#endif
+
 #ifndef HAVE_STRCASESTR
 extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
 #endif
diff --git a/include/libbb.h b/include/libbb.h
index 5551031..31d9b74 100644
--- include/libbb.h
+++ include/libbb.h
@@ -58,6 +58,9 @@
 #ifdef DMALLOC
 # include <dmalloc.h>
 #endif
+#ifndef HAVE_XTABS
+# define XTABS OXTABS
+#endif
 #include <pwd.h>
 #include <grp.h>
 #if ENABLE_FEATURE_SHADOWPASSWDS
diff --git a/include/platform.h b/include/platform.h
index bbbc0a9..73bd59a 100644
--- include/platform.h
+++ include/platform.h
@@ -256,9 +256,12 @@ typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
 # undef HAVE_MNTENT_H
 # undef HAVE_SYS_STATFS_H
 # undef HAVE_SIGHANDLER_T
+# undef HAVE_XTABS
+# undef HAVE_FDPRINTF
 #else
 # define HAVE_MNTENT_H 1
 # define HAVE_SYS_STATFS_H 1
+# define HAVE_XTABS 1
 #endif
 
 /*----- Kernel versioning ------------------------------------*/
diff --git a/libbb/makedev.c b/libbb/makedev.c
index cf59e61..1809fda 100644
--- libbb/makedev.c
+++ libbb/makedev.c
@@ -8,8 +8,14 @@
 
 /* We do not include libbb.h - #define makedev() is there! */
 #include "platform.h"
-#include <features.h>
-#include <sys/sysmacros.h>
+#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
+        || defined(__APPLE__) \
+    )
+# include <features.h>
+# include <sys/sysmacros.h>
+#else
+# include <sys/types.h>
+#endif
 
 #ifdef __GLIBC__
 /* At least glibc has horrendously large inline for this, so wrap it */
diff --git a/libbb/match_fstype.c b/libbb/match_fstype.c
index 83d6e67..655329b 100644
--- libbb/match_fstype.c
+++ libbb/match_fstype.c
@@ -10,6 +10,8 @@
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
+#ifdef CONFIG_PLATFORM_LINUX
+
 #include "libbb.h"
 
 int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
@@ -40,3 +42,5 @@ int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
 
 	return !match;
 }
+
+#endif /* CONFIG_PLATFORM_LINUX */
diff --git a/coreutils/who.c b/coreutils/who.c
index 8384d95..4c2606d 100644
--- coreutils/who.c
+++ coreutils/who.c
@@ -19,7 +19,9 @@
 /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'.  */
 
 #include "libbb.h"
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h>
+#endif
 
 static void idle_string(char *str6, time_t t)
 {
diff --git a/libbb/messages.c b/libbb/messages.c
index 66e466f..90dd955 100644
--- libbb/messages.c
+++ libbb/messages.c
@@ -48,15 +48,17 @@ const int const_int_1 = 1;
  * and it will end up in bss */
 const int const_int_0 = 0;
 
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h>
 /* This is usually something like "/var/adm/wtmp" or "/var/log/wtmp" */
 const char bb_path_wtmp_file[] ALIGN1 =
-#if defined _PATH_WTMP
+# if defined _PATH_WTMP
 	_PATH_WTMP;
-#elif defined WTMP_FILE
+# elif defined WTMP_FILE
 	WTMP_FILE;
-#else
-#error unknown path to wtmp file
+# else
+#  error unknown path to wtmp file
+# endif
 #endif
 
 /* We use it for "global" data via *(struct global*)&bb_common_bufsiz1.
diff --git a/libbb/utmp.c b/libbb/utmp.c
index 2bf9c11..a055de7 100644
--- libbb/utmp.c
+++ libbb/utmp.c
@@ -6,8 +6,9 @@
  *
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
-#include "libbb.h"
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include "libbb.h"
+# include <utmp.h>
 
 static void touch(const char *filename)
 {
@@ -53,11 +54,11 @@ void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, con
 	pututline(&utent);
 	endutent();
 
-#if ENABLE_FEATURE_WTMP
+# if ENABLE_FEATURE_WTMP
 	/* "man utmp" says wtmp file should *not* be created automagically */
 	/*touch(bb_path_wtmp_file);*/
 	updwtmp(bb_path_wtmp_file, &utent);
-#endif
+# endif
 }
 
 /*
@@ -125,9 +126,10 @@ void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const
 	pututline(&utent);
 	endutent();
 
-#if ENABLE_FEATURE_WTMP
+# if ENABLE_FEATURE_WTMP
 	/* "man utmp" says wtmp file should *not* be created automagically */
 	/*touch(bb_path_wtmp_file);*/
 	updwtmp(bb_path_wtmp_file, &utent);
-#endif
+# endif
 }
+#endif /* ENABLE_FEATURE_UTMP */
diff --git a/miscutils/last.c b/miscutils/last.c
index fec5b70..354af42 100644
--- miscutils/last.c
+++ miscutils/last.c
@@ -8,7 +8,9 @@
  */
 
 #include "libbb.h"
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h>
+#endif
 
 /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
  * to reduce confusion */
diff --git a/miscutils/runlevel.c b/miscutils/runlevel.c
index 363e450..b31736a 100644
--- miscutils/runlevel.c
+++ miscutils/runlevel.c
@@ -12,7 +12,9 @@
  * initially busyboxified by Bernhard Reutner-Fischer
  */
 #include "libbb.h"
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h>
+#endif
 
 int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int runlevel_main(int argc UNUSED_PARAM, char **argv)
diff --git a/miscutils/wall.c b/miscutils/wall.c
index eecfc16..738726e 100644
--- miscutils/wall.c
+++ miscutils/wall.c
@@ -7,7 +7,9 @@
  */
 
 #include "libbb.h"
-#include <utmp.h>
+#if ENABLE_FEATURE_UTMP
+# include <utmp.h>
+#endif
 
 int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int wall_main(int argc UNUSED_PARAM, char **argv)
